diff --git a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala index 048395e8dffa..5d08921f1ea6 100644 --- a/compiler/src/dotty/tools/dotc/transform/PostTyper.scala +++ b/compiler/src/dotty/tools/dotc/transform/PostTyper.scala @@ -378,6 +378,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase else (tree.rhs, sym.info) match case (rhs: LambdaTypeTree, bounds: TypeBounds) => VarianceChecker.checkLambda(rhs, bounds) + if sym.isOpaqueAlias then + VarianceChecker.checkLambda(rhs, TypeBounds.upper(sym.opaqueAlias)) case _ => processMemberDef(super.transform(tree)) case tree: New if isCheckable(tree) => diff --git a/tests/neg/i13997.scala b/tests/neg/i13997.scala new file mode 100644 index 000000000000..77cca35d93bd --- /dev/null +++ b/tests/neg/i13997.scala @@ -0,0 +1,10 @@ + opaque type CovariantArray[+A] = Array[A] // error + + object CovariantArray: + def crash() = + val stringArray: CovariantArray[String] = Array("foo", "bar") + val anyArray: CovariantArray[Any] = stringArray + anyArray(0) = 42 + stringArray(0).length + + @main def Test = CovariantArray.crash() \ No newline at end of file