Skip to content

Don't strip @uncheckedVariance when inferring the type of default get… #12275

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

Merged
merged 1 commit into from
Apr 29, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/core/TypeOps.scala
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ object TypeOps:
// corrective steps, so no widening is wanted.
simplify(l, theMap) | simplify(r, theMap)
case AnnotatedType(parent, annot)
if !ctx.mode.is(Mode.Type) && annot.symbol == defn.UncheckedVarianceAnnot =>
if annot.symbol == defn.UncheckedVarianceAnnot && !ctx.mode.is(Mode.Type) && !theMap.isInstanceOf[SimplifyKeepUnchecked] =>
simplify(parent, theMap)
case _: MatchType =>
val normed = tp.tryNormalize
Expand All @@ -180,6 +180,8 @@ object TypeOps:
def apply(tp: Type): Type = simplify(tp, this)
}

class SimplifyKeepUnchecked(using Context) extends SimplifyMap

/** Approximate union type by intersection of its dominators.
* That is, replace a union type Tn | ... | Tn
* by the smallest intersection type of base-class instances of T1,...,Tn.
Expand Down
10 changes: 6 additions & 4 deletions compiler/src/dotty/tools/dotc/typer/Namer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1550,10 +1550,12 @@ class Namer { typer: Typer =>
// For justification on the use of `@uncheckedVariance`, see
// `default-getter-variance.scala`.
AnnotatedType(defaultTp, Annotation(defn.UncheckedVarianceAnnot))
else tp.widenTermRefExpr.simplified match
case ctp: ConstantType if isInlineVal => ctp
case tp =>
TypeComparer.widenInferred(tp, pt)
else
// don't strip @uncheckedVariance annot for default getters
TypeOps.simplify(tp.widenTermRefExpr,
if defaultTp.exists then TypeOps.SimplifyKeepUnchecked() else null) match
case ctp: ConstantType if isInlineVal => ctp
case tp => TypeComparer.widenInferred(tp, pt)

// Replace aliases to Unit by Unit itself. If we leave the alias in
// it would be erased to BoxedUnit.
Expand Down
4 changes: 4 additions & 0 deletions tests/pos/i12273.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import scala.annotation.unchecked.uncheckedVariance

final case class Outlet[T](out: T)
final case class SourceShape[+T](val out: Outlet[T @uncheckedVariance])