Skip to content

Commit 32c4280

Browse files
committed
Do not rely on subtyping to infer constraints for object patterns
Fixed soundness problems with abstract types - see test.
1 parent 54047ba commit 32c4280

File tree

2 files changed

+21
-1
lines changed

2 files changed

+21
-1
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3153,7 +3153,7 @@ class Typer extends Namer
31533153
case _: RefTree | _: Literal
31543154
if !isVarPattern(tree) &&
31553155
!(pt <:< tree.tpe) &&
3156-
!(tree.tpe <:< pt)(ctx.addMode(Mode.GADTflexible)) =>
3156+
!ctx.addMode(Mode.GADTflexible).typeComparer.constrainPatternType(tree.tpe, pt) =>
31573157
val cmp =
31583158
untpd.Apply(
31593159
untpd.Select(untpd.TypedSplice(tree), nme.EQ),
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
object Test {
2+
enum Foo[X] {
3+
case Str extends Foo[String]
4+
case Int extends Foo[Int]
5+
}
6+
7+
trait Test {
8+
type A
9+
10+
def foo[T](f: Foo[A] | Foo[T]): T =
11+
f match { case Foo.Str =>
12+
"" // error
13+
}
14+
15+
def bar[T](f: Unit | Foo[T]): T =
16+
f match { case Foo.Str =>
17+
""
18+
}
19+
}
20+
}

0 commit comments

Comments
 (0)