Skip to content
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

Warn when ??? is used to implement erased definition #11740

Closed
wants to merge 1 commit into from
Closed
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
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
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Next step might be to do tree.rhs.symbol != defn.Compiletime_erasedValue.

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