@@ -10,6 +10,7 @@ import Symbols._
1010import Types ._
1111import typer .RefChecks
1212import MegaPhase .MiniPhase
13+ import StdNames .nme
1314import ast .tpd
1415
1516/** This phase makes all erased term members of classes private so that they cannot
@@ -18,6 +19,10 @@ import ast.tpd
1819 * The phase also replaces all expressions that appear in an erased context by
1920 * default values. This is necessary so that subsequent checking phases such
2021 * as IsInstanceOfChecker don't give false negatives.
22+ * Finally, the phase replaces `compiletime.uninitialized` on the right hand side
23+ * of a mutable field definition by `_`. This avoids a "is declared erased, but is
24+ * in fact used" error in Erasure and communicates to Constructors that the
25+ * variable does not have an initializer.
2126 */
2227class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
2328 import tpd ._
@@ -38,10 +43,25 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
3843 cpy.Apply (tree)(tree.fun, tree.args.map(trivialErasedTree))
3944 else tree
4045
46+ private def hasUninitializedRHS (tree : ValOrDefDef )(using Context ): Boolean =
47+ def recur (rhs : Tree ): Boolean = rhs match
48+ case rhs : RefTree =>
49+ rhs.symbol == defn.Compiletime_uninitialized
50+ && tree.symbol.is(Mutable ) && tree.symbol.owner.isClass
51+ case closureDef(ddef) if defn.isContextFunctionType(tree.tpt.tpe.dealias) =>
52+ recur(ddef.rhs)
53+ case _ =>
54+ false
55+ recur(tree.rhs)
56+
4157 override def transformValDef (tree : ValDef )(using Context ): Tree =
42- if (tree.symbol.isEffectivelyErased && ! tree.rhs.isEmpty)
58+ val sym = tree.symbol
59+ if tree.symbol.isEffectivelyErased && ! tree.rhs.isEmpty then
4360 cpy.ValDef (tree)(rhs = trivialErasedTree(tree))
44- else tree
61+ else if hasUninitializedRHS(tree) then
62+ cpy.ValDef (tree)(rhs = cpy.Ident (tree.rhs)(nme.WILDCARD ).withType(tree.tpt.tpe))
63+ else
64+ tree
4565
4666 override def transformDefDef (tree : DefDef )(using Context ): Tree =
4767 if (tree.symbol.isEffectivelyErased && ! tree.rhs.isEmpty)
0 commit comments