Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix variance handling for parameterized type aliases #8651

Merged
merged 1 commit into from Feb 10, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
10 changes: 9 additions & 1 deletion src/reflect/scala/reflect/internal/Symbols.scala
Expand Up @@ -3076,7 +3076,15 @@ trait Symbols extends api.Symbols { self: SymbolTable =>
class AliasTypeSymbol protected[Symbols] (initOwner: Symbol, initPos: Position, initName: TypeName)
extends TypeSymbol(initOwner, initPos, initName) {
type TypeOfClonedSymbol = TypeSymbol
override def variance = if (isLocalToThis) Bivariant else info.typeSymbol.variance
override def variance =
// A non-applied parameterized type alias can appear in any variance position
if (typeParams.nonEmpty)
Invariant
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

looks sound to me :-)

else if (isLocalToThis)
Bivariant
else
info.typeSymbol.variance
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can no longer construct the soundness argument for this; but, then again, I also don't have a counter-example. (And it's been like this forever 🤷‍♂ )


override def isContravariant = variance.isContravariant
override def isCovariant = variance.isCovariant
final override def isAliasType = true
Expand Down
7 changes: 7 additions & 0 deletions test/files/neg/variance-alias.check
@@ -0,0 +1,7 @@
variance-alias.scala:5: error: covariant type T occurs in invariant position in type => Inv[T] of value a
val a: Inv[({type L[+X] = X})#L[T]] = new Inv[({type L[+X] = X})#L[T]] {} // error
^
variance-alias.scala:6: error: covariant type T occurs in invariant position in type => Inv[X.this.Id[T]] of value b
val b: Inv[Id[T]] = new Inv[Id[T]] {} // error
^
2 errors
7 changes: 7 additions & 0 deletions test/files/neg/variance-alias.scala
@@ -0,0 +1,7 @@
trait Inv[A]

class X[+T] {
type Id[+A] = A
val a: Inv[({type L[+X] = X})#L[T]] = new Inv[({type L[+X] = X})#L[T]] {} // error
val b: Inv[Id[T]] = new Inv[Id[T]] {} // error
}
6 changes: 6 additions & 0 deletions test/files/pos/variance-alias.scala
@@ -0,0 +1,6 @@
trait Tc[F[_]]
object X {
type Id[+A] = A
val a: Tc[({type L[+X] = X})#L] = new Tc[({type L[+X] = X})#L] {} // ok, therefore the following should be too:
val b: Tc[Id] = new Tc[Id] {} // ok
}