From 9d729c32693b5b7ea673a57f05b20005caf71df9 Mon Sep 17 00:00:00 2001 From: Martin Odersky Date: Mon, 6 Dec 2021 19:48:33 +0100 Subject: [PATCH] Check variance of RHS of opaque type Fixes #13997 --- .../src/dotty/tools/dotc/transform/PostTyper.scala | 2 ++ tests/neg/i13997.scala | 10 ++++++++++ 2 files changed, 12 insertions(+) create mode 100644 tests/neg/i13997.scala 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