diff --git a/compiler/src/dotty/tools/dotc/core/TypeOps.scala b/compiler/src/dotty/tools/dotc/core/TypeOps.scala index 332129e72850..d538a949f9bc 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeOps.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeOps.scala @@ -861,6 +861,10 @@ object TypeOps: } def instantiate(): Type = { + // if there's a change in variance in type parameters (between subtype tp1 and supertype tp2) + // then we don't want to maximise the type variables in the wrong direction. + // For instance 15967, A[-Z] and B[Y] extends A[Y], we don't want to maximise Y to Any + maximizeType(protoTp1.baseType(tp2.classSymbol), NoSpan) maximizeType(protoTp1, NoSpan) wildApprox(protoTp1) } diff --git a/tests/pos/i15967.scala b/tests/pos/i15967.scala new file mode 100644 index 000000000000..0ef00ae0cea1 --- /dev/null +++ b/tests/pos/i15967.scala @@ -0,0 +1,10 @@ +// scalac: -Werror +sealed trait A[-Z] +final case class B[Y]() extends A[Y] + +class Test: + def t1[X](a: A[X]) = a match // was inexhaustive + case _: B[X] @unchecked => + +//def t2[X](a: A[X]) = a match // was inexhaustive +// case _: B[X] => // expected unchecked warning