Skip to content

fix #10122: check parent types of references and types of parameters #10126

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

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
50 changes: 38 additions & 12 deletions compiler/src/dotty/tools/dotc/transform/Erasure.scala
Original file line number Diff line number Diff line change
Expand Up @@ -550,15 +550,41 @@ object Erasure {
if (!ctx.mode.is(Mode.Type)) {
if (isErased(tree))
report.error(em"${tree.symbol} is declared as erased, but is in fact used", tree.srcPos)
tree.symbol.getAnnotation(defn.CompileTimeOnlyAnnot) match {
case Some(annot) =>
def defaultMsg =
i"""Reference to ${tree.symbol.showLocated} should not have survived,
|it should have been processed and eliminated during expansion of an enclosing macro or term erasure."""
val message = annot.argumentConstant(0).fold(defaultMsg)(_.stringValue)
report.error(message, tree.srcPos)
case _ => // OK
}

def parentSyms(tpe: Type): LazyList[Symbol] =
val ref = tpe.widen.finalResultType
ref.classSymbol #:: ref.parents.to(LazyList).flatMap(parentSyms)

def checkTree(tree: Tree, pos: util.SrcPos, toCheck: LazyList[Symbol]): Boolean =
toCheck.exists { sym =>
sym.getAnnotation(defn.CompileTimeOnlyAnnot) match {
case Some(annot) =>
def defaultMsg =
i"""Reference to ${tree.symbol.showLocated} should not have survived,
|it should have been processed and eliminated during expansion of an enclosing macro or term erasure."""
val message = annot.argumentConstant(0).fold(defaultMsg)(_.stringValue)
report.error(message, pos)
true
case _ => // OK
false
}
}

tree match
case ddef: DefDef =>
for
vparams <- ddef.vparamss
Copy link
Member

Choose a reason for hiding this comment

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

RefChecks already traverses trees, can we check things at the point where we're traversing them instead of retraversing them here? This is what scala 2 does (see checkUndesiredProperties)

Copy link
Member Author

@bishabosha bishabosha Oct 30, 2020

Choose a reason for hiding this comment

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

yes thank you I will try that, the reverse order is annoying for module definitions

Copy link
Member Author

@bishabosha bishabosha Nov 3, 2020

Choose a reason for hiding this comment

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

following discussion at the dotty meeting this will need to be done at the same place we issue deprecation warnings, as scalac reports violations of compileTimeOnly for type aliases.

Possibly follow what is described in #9825 (comment)

vparam <- vparams
do
checkTree(vparam, vparam.tpt.srcPos, parentSyms(vparam.tpt.tpe))
checkTree(ddef, ddef.tpt.srcPos, parentSyms(ddef.tpt.tpe))

case vdef: ValDef => checkTree(vdef, vdef.tpt.srcPos, parentSyms(vdef.tpt.tpe))

case tree =>
// in the other branches we avoid checking the symbol itself
// in-case it is annotated for downstream members
checkTree(tree, tree.srcPos, tree.symbol #:: parentSyms(tree.tpe))
}
tree
}
Expand Down Expand Up @@ -844,8 +870,8 @@ object Erasure {
override def typedValDef(vdef: untpd.ValDef, sym: Symbol)(using Context): Tree =
if (sym.isEffectivelyErased) erasedDef(sym)
else
super.typedValDef(untpd.cpy.ValDef(vdef)(
tpt = untpd.TypedSplice(TypeTree(sym.info).withSpan(vdef.tpt.span))), sym)
checkNotErased(super.typedValDef(untpd.cpy.ValDef(vdef)(
tpt = untpd.TypedSplice(TypeTree(sym.info).withSpan(vdef.tpt.span))), sym))

/** Besides normal typing, this function also compacts anonymous functions
* with more than `MaxImplementedFunctionArity` parameters to use a single
Expand Down Expand Up @@ -889,7 +915,7 @@ object Erasure {
vparamss = vparams :: Nil,
tpt = untpd.TypedSplice(TypeTree(restpe).withSpan(ddef.tpt.span)),
rhs = rhs1)
super.typedDefDef(ddef1, sym)
checkNotErased(super.typedDefDef(ddef1, sym))
end typedDefDef

/** The outer parameter definition of a constructor if it needs one */
Expand Down
17 changes: 17 additions & 0 deletions tests/neg/i10122.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import scala.annotation.compileTimeOnly

@compileTimeOnly("FooBar can not be used as an expression") trait FooBar

object foo extends FooBar // error

val fooAnon = new FooBar {} // error // error
val fooRef1: FooBar = ??? // error
def fooRef2: FooBar = ??? // error
def useFoo(foo: FooBar): foo.type = foo // error // error // error
val bar = fooRef2 // error

@compileTimeOnly("baz can not be used as an expression") val baz = 23
val qux = baz // error

@compileTimeOnly("quux can not be used as an expression") def quux = 47
val quxx = quux // error