Skip to content

Commit 1fc0059

Browse files
Special case intersecting for Singleton
Singleton is not part of the normal type hierarchy, special treatment in requiered in intersecting to avoid making incorrect assumptions.
1 parent be29f14 commit 1fc0059

File tree

2 files changed

+39
-3
lines changed

2 files changed

+39
-3
lines changed

compiler/src/dotty/tools/dotc/core/TypeComparer.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1920,6 +1920,8 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] {
19201920
sym.children.map(x => ctx.refineUsingParent(tp, x)).filter(_.exists)
19211921

19221922
(tp1.dealias, tp2.dealias) match {
1923+
case (tp1: TypeRef, tp2: TypeRef) if tp1.symbol == defn.SingletonClass || tp2.symbol == defn.SingletonClass =>
1924+
true
19231925
case (tp1: ConstantType, tp2: ConstantType) =>
19241926
tp1 == tp2
19251927
case (tp1: TypeRef, tp2: TypeRef) if tp1.symbol.isClass && tp2.symbol.isClass =>

tests/neg/6314.scala

Lines changed: 37 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
1-
object G {
2-
final class X
3-
final class Y
1+
final class X
2+
final class Y
43

4+
object Test1 {
55
trait Test {
66
type Type
7+
// This is testing that both permutations of the types in a &
8+
// are taken into account by the intersection test
79
val i: Bar[Y & Type] = 1 // error
810
}
911

@@ -12,3 +14,35 @@ object G {
1214
case Y => Int
1315
}
1416
}
17+
18+
object Test2 {
19+
trait Wizzle[L <: Int with Singleton] {
20+
type Bar[A] = A match {
21+
case 0 => String
22+
case L => Int
23+
}
24+
25+
// This is testing that we don't make wrong assumptions about Singleton
26+
def right(fa: Bar[L]): Int = fa // error
27+
}
28+
29+
trait Wazzlo[L <: Int with AnyVal] {
30+
type Bar[A] = A match {
31+
case 0 => String
32+
case L => Int
33+
}
34+
35+
// This is testing that we don't make wrong assumptions about AnyVal
36+
def right(fa: Bar[L]): Int = fa // error
37+
}
38+
39+
trait Wuzzlu[L <: String with AnyRef] {
40+
type Bar[A] = A match {
41+
case "" => String
42+
case L => Int
43+
}
44+
45+
// This is testing that we don't make wrong assumptions about AnyRef
46+
def right(fa: Bar[L]): Int = fa // error
47+
}
48+
}

0 commit comments

Comments
 (0)