bug
bug copied to clipboard
2.13.8: @uncheckedVariance is ignored in partially applied types
Reproduction steps
Scala version: 2.13.8, full example on Scastie: https://scastie.scala-lang.org/g1aCjox4QBylJgfllSoBAQ
trait BIO[F[+_, +_]]
trait EitherT[F[_], E, A]
def makeWithNativeTypeProjection[F[_]] = new BIO[({type L[+E, +A] = EitherT[F, E @uncheckedVariance, A @uncheckedVariance]})#L] {}
prints
covariant type A occurs in invariant position in type [+A]F0[A @scala.annotation.unchecked.uncheckedVariance] of type F
Problem
Unchecked variance annotations are ignored in partially applied types and variance errors abort compilation. Possibly related to #11968 .
Expectation
I expect @uncheckedVariance annotation to work in partially applied types.
Workaround
trait BIO[F[+_, +_]]
trait EitherT[F[_], E, A]
class BIOFactory[F[_]] {
type DependentEitherT[+E, +A] = EitherT[F, E @uncheckedVariance, A @uncheckedVariance]
def makeWithFactory = new BIO[DependentEitherT] {}
}
cc @neko-kai
does Scala 3 do any better here?
Scala 3.1.3 compiles cleanly, see this scastie: https://scastie.scala-lang.org/Ejo7SnnDRFOvQvyP582qvg
trait BIO[F[+_, +_]]
trait EitherT[F[_], E, A]
def makeWithNativeTypeProjection[F[_]] = new BIO[({type L[+E, +A] = EitherT[F, E @uncheckedVariance, A @uncheckedVariance]})#L] {}
The @uncheckedVariance annotation is lost in the final type which looks like this: BIO[[+E, +A]UncheckedVariance.EitherT[F,E,A]]
It's lost by AsSeenFromMap which extends KeepOnlyTypeConstraints:
trait KeepOnlyTypeConstraints extends AnnotationFilter {
// filter keeps only type constraint annotations
def keepAnnotation(annot: AnnotationInfo) = annot matches TypeConstraintClass
}