bug icon indicating copy to clipboard operation
bug copied to clipboard

2.13.8: @uncheckedVariance is ignored in partially applied types

Open mucaho opened this issue 3 years ago • 5 comments

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] {}
}

mucaho avatar Jul 24 '22 18:07 mucaho

cc @neko-kai

mucaho avatar Jul 24 '22 18:07 mucaho

does Scala 3 do any better here?

SethTisue avatar Jul 24 '22 21:07 SethTisue

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] {}

mucaho avatar Jul 24 '22 21:07 mucaho

The @uncheckedVariance annotation is lost in the final type which looks like this: BIO[[+E, +A]UncheckedVariance.EitherT[F,E,A]]

joroKr21 avatar Jul 25 '22 06:07 joroKr21

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
  }

joroKr21 avatar Jul 25 '22 06:07 joroKr21