Skip to content

Commit 4d8749d

Browse files
committed
Warn when ??? is used to implement erased definition
1 parent 02cee1f commit 4d8749d

File tree

2 files changed

+14
-4
lines changed

2 files changed

+14
-4
lines changed

compiler/src/dotty/tools/dotc/transform/PruneErasedDefs.scala

Lines changed: 13 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,9 +16,13 @@ import ast.tpd
1616
/** This phase makes all erased term members of classes private so that they cannot
1717
* conflict with non-erased members. This is needed so that subsequent phases like
1818
* ResolveSuper that inspect class members work correctly.
19-
* The phase also replaces all expressions that appear in an erased context by
20-
* default values. This is necessary so that subsequent checking phases such
21-
* as IsInstanceOfChecker don't give false negatives.
19+
* The phase also checks that `erased` definitions contain a call to `erasedValue`
20+
* on their RHS. Then replaces all expressions that appear in an effectively
21+
* erased context by default values. This is necessary so that subsequent checking
22+
* phases such as IsInstanceOfChecker don't give false negatives.
23+
*
24+
* TODO: This does not belong in this phase as it has nothing to do with `erased`
25+
* definitions. Move it out to keep logic clean.
2226
* Finally, the phase replaces `compiletime.uninitialized` on the right hand side
2327
* of a mutable field definition by `_`. This avoids a "is declared erased, but is
2428
* in fact used" error in Erasure and communicates to Constructors that the
@@ -69,6 +73,12 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
6973
else tree
7074

7175
private def trivialErasedTree(tree: Tree)(using Context): Tree =
76+
tree match
77+
case tree: ValOrDefDef if !tree.symbol.is(Inline) && !tree.symbol.is(Synthetic) =>
78+
if tree.rhs.symbol == defn.Predef_undefined then
79+
report.warning("Implementation of erased definition should be `erasedValue`. May require an import of `scala.comiletime.erasedValue`.", tree.rhs)
80+
case _ =>
81+
7282
tree.tpe.widenTermRefExpr.dealias.normalized match
7383
case ConstantType(c) => Literal(c)
7484
case _ => ref(defn.Predef_undefined)

library/src/scala/compiletime/package.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package object compiletime {
1616
* the branches.
1717
* @syntax markdown
1818
*/
19-
erased def erasedValue[T]: T = ???
19+
erased def erasedValue[T]: T = erasedValue[T]
2020

2121
/** Used as the initializer of a mutable class or object field, like this:
2222
*

0 commit comments

Comments
 (0)