Skip to content

Commit fc7e8c3

Browse files
authored
Merge pull request #14058 from dotty-staging/fix-13997
Check variance of RHS of opaque type
2 parents 80ec523 + 9d729c3 commit fc7e8c3

File tree

2 files changed

+12
-0
lines changed

2 files changed

+12
-0
lines changed

compiler/src/dotty/tools/dotc/transform/PostTyper.scala

+2
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
378378
else (tree.rhs, sym.info) match
379379
case (rhs: LambdaTypeTree, bounds: TypeBounds) =>
380380
VarianceChecker.checkLambda(rhs, bounds)
381+
if sym.isOpaqueAlias then
382+
VarianceChecker.checkLambda(rhs, TypeBounds.upper(sym.opaqueAlias))
381383
case _ =>
382384
processMemberDef(super.transform(tree))
383385
case tree: New if isCheckable(tree) =>

tests/neg/i13997.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
opaque type CovariantArray[+A] = Array[A] // error
2+
3+
object CovariantArray:
4+
def crash() =
5+
val stringArray: CovariantArray[String] = Array("foo", "bar")
6+
val anyArray: CovariantArray[Any] = stringArray
7+
anyArray(0) = 42
8+
stringArray(0).length
9+
10+
@main def Test = CovariantArray.crash()

0 commit comments

Comments
 (0)