Skip to content

Commit 3633d62

Browse files
authored
Merge pull request #12275 from dotty-staging/fix-12273
Don't strip @uncheckedVariance when inferring the type of default get…
2 parents e4151a4 + 7103791 commit 3633d62

File tree

3 files changed

+13
-5
lines changed

3 files changed

+13
-5
lines changed

compiler/src/dotty/tools/dotc/core/TypeOps.scala

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ object TypeOps:
164164
// corrective steps, so no widening is wanted.
165165
simplify(l, theMap) | simplify(r, theMap)
166166
case AnnotatedType(parent, annot)
167-
if !ctx.mode.is(Mode.Type) && annot.symbol == defn.UncheckedVarianceAnnot =>
167+
if annot.symbol == defn.UncheckedVarianceAnnot && !ctx.mode.is(Mode.Type) && !theMap.isInstanceOf[SimplifyKeepUnchecked] =>
168168
simplify(parent, theMap)
169169
case _: MatchType =>
170170
val normed = tp.tryNormalize
@@ -180,6 +180,8 @@ object TypeOps:
180180
def apply(tp: Type): Type = simplify(tp, this)
181181
}
182182

183+
class SimplifyKeepUnchecked(using Context) extends SimplifyMap
184+
183185
/** Approximate union type by intersection of its dominators.
184186
* That is, replace a union type Tn | ... | Tn
185187
* by the smallest intersection type of base-class instances of T1,...,Tn.

compiler/src/dotty/tools/dotc/typer/Namer.scala

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1550,10 +1550,12 @@ class Namer { typer: Typer =>
15501550
// For justification on the use of `@uncheckedVariance`, see
15511551
// `default-getter-variance.scala`.
15521552
AnnotatedType(defaultTp, Annotation(defn.UncheckedVarianceAnnot))
1553-
else tp.widenTermRefExpr.simplified match
1554-
case ctp: ConstantType if isInlineVal => ctp
1555-
case tp =>
1556-
TypeComparer.widenInferred(tp, pt)
1553+
else
1554+
// don't strip @uncheckedVariance annot for default getters
1555+
TypeOps.simplify(tp.widenTermRefExpr,
1556+
if defaultTp.exists then TypeOps.SimplifyKeepUnchecked() else null) match
1557+
case ctp: ConstantType if isInlineVal => ctp
1558+
case tp => TypeComparer.widenInferred(tp, pt)
15571559

15581560
// Replace aliases to Unit by Unit itself. If we leave the alias in
15591561
// it would be erased to BoxedUnit.

tests/pos/i12273.scala

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import scala.annotation.unchecked.uncheckedVariance
2+
3+
final case class Outlet[T](out: T)
4+
final case class SourceShape[+T](val out: Outlet[T @uncheckedVariance])

0 commit comments

Comments
 (0)