Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 983cd85

Browse files
committed
Fix variance computation for parameterized type aliases
The variance of a type alias was taken to be the variance of its right-hand side, but that doesn't make sense for a parameterized type alias which is free to appear in any position: variance checking should only kick in when it is applied to something. It was possible to work around this by using a type lambda instead of a type alias, cats just had to do that: typelevel/cats#3264
1 parent f0e9c40 commit 983cd85

3 files changed

Lines changed: 22 additions & 1 deletion

File tree

src/reflect/scala/reflect/internal/Symbols.scala

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3076,7 +3076,15 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
30763076
class AliasTypeSymbol protected[Symbols] (initOwner: Symbol, initPos: Position, initName: TypeName)
30773077
extends TypeSymbol(initOwner, initPos, initName) {
30783078
type TypeOfClonedSymbol = TypeSymbol
3079-
override def variance = if (isLocalToThis) Bivariant else info.typeSymbol.variance
3079+
override def variance =
3080+
// A non-applied parameterized type alias can appear in any variance position
3081+
if (typeParams.nonEmpty)
3082+
Invariant
3083+
else if (isLocalToThis)
3084+
Bivariant
3085+
else
3086+
info.typeSymbol.variance
3087+
30803088
override def isContravariant = variance.isContravariant
30813089
override def isCovariant = variance.isCovariant
30823090
final override def isAliasType = true
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
trait Inv[A]
2+
3+
class X[+T] {
4+
type Id[+A] = A
5+
val a: Inv[({type L[+X] = X})#L[T]] = new Inv[({type L[+X] = X})#L[T]] {} // error
6+
val b: Inv[Id[T]] = new Inv[Id[T]] {} // error
7+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
trait Tc[F[_]]
2+
object X {
3+
type Id[+A] = A
4+
val a: Tc[({type L[+X] = X})#L] = new Tc[({type L[+X] = X})#L] {} // ok, therefore the following should be to:
5+
val b: Tc[Id] = new Tc[Id] {} // ok
6+
}

0 commit comments

Comments
 (0)