Skip to content

Commit

Permalink
Warn when ??? is used to implement erased definition
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolasstucki committed Mar 15, 2021
1 parent 02cee1f commit 4d8749d
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 4 deletions.
16 changes: 13 additions & 3 deletions compiler/src/dotty/tools/dotc/transform/PruneErasedDefs.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,13 @@ import ast.tpd
/** This phase makes all erased term members of classes private so that they cannot
* conflict with non-erased members. This is needed so that subsequent phases like
* ResolveSuper that inspect class members work correctly.
* The phase also replaces all expressions that appear in an erased context by
* default values. This is necessary so that subsequent checking phases such
* as IsInstanceOfChecker don't give false negatives.
* The phase also checks that `erased` definitions contain a call to `erasedValue`
* on their RHS. Then replaces all expressions that appear in an effectively
* erased context by default values. This is necessary so that subsequent checking
* phases such as IsInstanceOfChecker don't give false negatives.
*
* TODO: This does not belong in this phase as it has nothing to do with `erased`
* definitions. Move it out to keep logic clean.
* Finally, the phase replaces `compiletime.uninitialized` on the right hand side
* of a mutable field definition by `_`. This avoids a "is declared erased, but is
* in fact used" error in Erasure and communicates to Constructors that the
Expand Down Expand Up @@ -69,6 +73,12 @@ class PruneErasedDefs extends MiniPhase with SymTransformer { thisTransform =>
else tree

private def trivialErasedTree(tree: Tree)(using Context): Tree =
tree match
case tree: ValOrDefDef if !tree.symbol.is(Inline) && !tree.symbol.is(Synthetic) =>
if tree.rhs.symbol == defn.Predef_undefined then
report.warning("Implementation of erased definition should be `erasedValue`. May require an import of `scala.comiletime.erasedValue`.", tree.rhs)
case _ =>

tree.tpe.widenTermRefExpr.dealias.normalized match
case ConstantType(c) => Literal(c)
case _ => ref(defn.Predef_undefined)
Expand Down
2 changes: 1 addition & 1 deletion library/src/scala/compiletime/package.scala
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ package object compiletime {
* the branches.
* @syntax markdown
*/
erased def erasedValue[T]: T = ???
erased def erasedValue[T]: T = erasedValue[T]

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

0 comments on commit 4d8749d

Please sign in to comment.