From 92aabde4c2da5a2717bd7d7d443415a5c1bbccaf Mon Sep 17 00:00:00 2001 From: Yichen Xu Date: Sat, 30 Apr 2022 12:27:06 +0800 Subject: [PATCH] try LHS and type before tryBaseType when comparing applied types in GADT mode --- .../dotty/tools/dotc/core/TypeComparer.scala | 9 ++++++++ tests/neg/i11545.scala | 21 +++++++++++++++++++ 2 files changed, 30 insertions(+) create mode 100644 tests/neg/i11545.scala diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index e1605c326a05..ed610a7cedb6 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -1198,6 +1198,14 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling case _ => false } && { GADTused = true; true } + def tryGadtAnd1: Boolean = + ctx.mode.is(Mode.GadtConstraintInference) && { + tp1 match + case AndType(tp11, tp12) => + either(recur(tp11, tp2), recur(tp12, tp2)) + case _ => false + } + tycon2 match { case param2: TypeParamRef => isMatchingApply(tp1) || @@ -1213,6 +1221,7 @@ class TypeComparer(@constructorOnly initctx: Context) extends ConstraintHandling case info2: ClassInfo => tycon2.name.startsWith("Tuple") && defn.isTupleNType(tp2) && recur(tp1, tp2.toNestedPairs) || + tryGadtAnd1 || tryBaseType(info2.cls) case _ => fourthTry diff --git a/tests/neg/i11545.scala b/tests/neg/i11545.scala new file mode 100644 index 000000000000..27f4b4f4b559 --- /dev/null +++ b/tests/neg/i11545.scala @@ -0,0 +1,21 @@ +// Taken from +// https://github.com/lampepfl/dotty/issues/11545#issuecomment-787609144 +class test0 { + type AA + trait S[A] + trait Inv[A] + + class P[X] extends S[Inv[X] & AA] + +} + +@main def test: Unit = + new test0: + type AA = Inv[String] + + def patmat[A, Y](s: S[Inv[A] & Y]): A = s match { + case p: P[x] => + "Hello" // error + } + + val got: Int = patmat[Int, AA](new P)