diff --git a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala index e6fbd1019c84..f52570b9bd82 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeComparer.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeComparer.scala @@ -1239,7 +1239,11 @@ class TypeComparer(initctx: Context) extends ConstraintHandling[AbsentContext] w if (leftRoot.isStable || (ctx.isAfterTyper || ctx.mode.is(Mode.TypevarsMissContext)) && leftRoot.member(tparam.name).exists) { val captured = TypeRef(leftRoot, tparam) - isSubArg(captured, arg2) + try isSubArg(captured, arg2) + catch case ex: TypeError => + // The captured reference could be illegal and cause a + // TypeError to be thrown in argDenot + false } else if (v > 0) isSubType(paramBounds(tparam).hi, arg2) diff --git a/tests/pos/i8319.scala b/tests/pos/i8319.scala new file mode 100644 index 000000000000..d08ba85e7115 --- /dev/null +++ b/tests/pos/i8319.scala @@ -0,0 +1,14 @@ +package example + +sealed abstract class Tree[T] +case class Lam[T,U]() extends Tree[Any] +case class App[T,U]() extends Tree[Any] +case class Var() extends Tree[Any] + +object Branch: + def unapply(branch: Lam[?,?] | App[?,?]): true = true + +private def foo(s: Option[Tree[?]]) = s match // seems to only occur in a nested pattern + case Some(_: Var) => true // must come first + case Some(Branch()) => true // must be unapply and not direct type check + case _ => false \ No newline at end of file