File tree 3 files changed +60
-21
lines changed
compiler/src/dotty/tools/dotc/typer 3 files changed +60
-21
lines changed Original file line number Diff line number Diff line change @@ -34,7 +34,7 @@ object VarianceChecker {
34
34
val paramVarianceStr = if (v == 0 ) " contra" else " co"
35
35
val occursStr = variance match {
36
36
case - 1 => " contra"
37
- case 0 => " non "
37
+ case 0 => " in "
38
38
case 1 => " co"
39
39
}
40
40
val pos = tree.tparams
@@ -123,18 +123,19 @@ class VarianceChecker()(implicit ctx: Context) {
123
123
def apply (status : Option [VarianceError ], tp : Type ): Option [VarianceError ] = trace(s " variance checking $tp of $base at $variance" , variances) {
124
124
try
125
125
if (status.isDefined) status
126
- else tp match {
126
+ else tp.normalized match {
127
127
case tp : TypeRef =>
128
128
val sym = tp.symbol
129
129
if (sym.variance != 0 && base.isContainedIn(sym.owner)) checkVarianceOfSymbol(sym)
130
- else if (sym.isAliasType) this (status, sym.info.bounds.hi)
131
- else foldOver(status, tp)
130
+ else sym.info match {
131
+ case MatchAlias (_) => foldOver(status, tp)
132
+ case TypeAlias (alias) => this (status, alias)
133
+ case _ => foldOver(status, tp)
134
+ }
132
135
case tp : MethodOrPoly =>
133
136
this (status, tp.resultType) // params will be checked in their TypeDef or ValDef nodes.
134
137
case AnnotatedType (_, annot) if annot.symbol == defn.UncheckedVarianceAnnot =>
135
138
status
136
- case tp : MatchType =>
137
- apply(status, tp.bound)
138
139
case tp : ClassInfo =>
139
140
foldOver(status, tp.classParents)
140
141
case _ =>
Original file line number Diff line number Diff line change 1
- type Id [+ X ] = X match { // error: covariant type X appears in nonvariant position
1
+ type Id [+ X ] = X match { // error: covariant type X appears in invariant position
2
2
case Int => Int
3
3
case String => String
4
4
}
5
- type T1 [+ X , Y ] = Y match { // error: covariant type X appears in nonvariant position
5
+ type T1 [+ X , Y ] = Y match { // error: covariant type X appears in invariant position
6
6
case List [X ] => Int
7
7
case _ => String
8
8
}
9
- type T2 [+ X , Y ] = Y match { // error: covariant type X appears in nonvariant position
9
+ type T2 [+ X , Y ] = Y match { // error: covariant type X appears in invariant position
10
10
case List [_ >: X ] => Int
11
11
case _ => String
12
12
}
13
- type T3 [+ X , Y ] = Y match { // error: covariant type X appears in nonvariant position
13
+ type T3 [+ X , Y ] = Y match { // error: covariant type X appears in invariant position
14
14
case List [_ <: X ] => Int
15
15
case _ => String
16
16
}
17
- type Id2 [- X ] = X match { // error: contravariant type X appears in nonvariant position
17
+ type Id2 [- X ] = X match { // error: contravariant type X appears in invariant position
18
18
case Int => Int
19
19
case String => String
20
20
}
21
- type T4 [+ X , Y ] = Y match { // error: contravariant type X appears in nonvariant position
21
+ type T4 [- X , Y ] = Y match { // error: contravariant type X appears in invariant position
22
22
case List [X ] => Int
23
23
case _ => String
24
24
}
25
- type T5 [+ X , Y ] = Y match { // error: contravariant type X appears in nonvariant position
25
+ type T5 [- X , Y ] = Y match { // error: contravariant type X appears in invariant position
26
26
case List [_ >: X ] => Int
27
27
case _ => String
28
28
}
29
- type T6 [+ X , Y ] = Y match { // error: contravariant type X appears in nonvariant position
29
+ type T6 [- X , Y ] = Y match { // error: contravariant type X appears in invariant position
30
30
case List [_ <: X ] => Int
31
31
case _ => String
32
32
}
33
33
type T7 [- X , Y ] = Y match { // error: contravariant type X appears in covariant position
34
34
case List [_] => X
35
35
case _ => String
36
36
}
37
-
38
- class C [+ X ] {
39
- type Id = X match {
40
- case Int => Int
41
- case String => String
42
- }
43
- }
Original file line number Diff line number Diff line change
1
+ class C [+ X ] {
2
+ type Id = X match { // error: covariant type X appears in invariant position
3
+ case Int => Int
4
+ case String => String
5
+ }
6
+
7
+ type T1 [Y ] = Y match { // error: covariant type X appears in invariant position
8
+ case List [X ] => Int
9
+ case _ => String
10
+ }
11
+ type T2 [Y ] = Y match { // error: covariant type X appears in invariant position
12
+ case List [_ >: X ] => Int
13
+ case _ => String
14
+ }
15
+ type T3 [Y ] = Y match { // error: covariant type X appears in invariant position
16
+ case List [_ <: X ] => Int
17
+ case _ => String
18
+ }
19
+
20
+ def foo [Y <: X ](): Unit = ??? // error: covariant type X appears in contravariant position
21
+ type Foo [Y <: X ] <: Y // error: covariant type X appears in contravariant position
22
+ }
23
+ class D [- X ] {
24
+
25
+ type Id2 = X match { // error: contravariant type X appears in invariant position
26
+ case Int => Int
27
+ case String => String
28
+ }
29
+ type T4 [Y ] = Y match { // error: contravariant type X appears in invariant position
30
+ case List [X ] => Int
31
+ case _ => String
32
+ }
33
+ type T5 [Y ] = Y match { // error: contravariant type X appears in invariant position
34
+ case List [_ >: X ] => Int
35
+ case _ => String
36
+ }
37
+ type T6 [Y ] = Y match { // error: contravariant type X appears in invariant position
38
+ case List [_ <: X ] => Int
39
+ case _ => String
40
+ }
41
+ type T7 [Y ] = Y match { // error: contravariant type X appears in covariant position
42
+ case List [_] => X
43
+ case _ => String
44
+ }
45
+ }
You can’t perform that action at this time.
0 commit comments