Skip to content

Commit

Permalink
Fix isEffectivelySingleton
Browse files Browse the repository at this point in the history
As usual, OrTypes need to be excluded. a.type | b.type is not effectively a
singleton. It seems to be an easy trap to fall into.

Follow-up to #20474
  • Loading branch information
odersky committed May 28, 2024
1 parent 9b5ab2e commit d04005c
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 2 deletions.
3 changes: 1 addition & 2 deletions compiler/src/dotty/tools/dotc/core/Types.scala
Original file line number Diff line number Diff line change
Expand Up @@ -329,13 +329,12 @@ object Types extends TypeUtils {
def isSingleton(using Context): Boolean = dealias.isInstanceOf[SingletonType]

/** Is this type a (possibly aliased) singleton type or a type proxy
* or Or/And type known to be a singleton type?
* or an AndType where one operand is effectively a singleton?
*/
def isEffectivelySingleton(using Context): Boolean = dealias match
case tp: SingletonType => true
case tp: TypeProxy => tp.superType.isEffectivelySingleton
case AndType(tpL, tpR) => tpL.isEffectivelySingleton || tpR.isEffectivelySingleton
case OrType(tpL, tpR) => tpL.isEffectivelySingleton && tpR.isEffectivelySingleton
case _ => false

/** Is this upper-bounded by a (possibly aliased) singleton type?
Expand Down
13 changes: 13 additions & 0 deletions tests/neg/i20474.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
class A
class B extends A

def f(a: A, c: A) =
val b1: a.type = a
val b2: a.type & B = a.asInstanceOf[a.type & B]
val b3: c.type & A = c
val b4: a.type | c.type = c

val d1: b1.type = a
val d2: b2.type = a // ok
val d3: b3.type = a // error
val d4: b4.type = a // error

0 comments on commit d04005c

Please # to comment.