Skip to content

Commit 61d6b1b

Browse files
committed
Fix scala#6419: Support @compileTimeOnly
1 parent 2d07ae8 commit 61d6b1b

File tree

6 files changed

+40
-6
lines changed

6 files changed

+40
-6
lines changed

compiler/src/dotty/tools/dotc/core/Definitions.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -858,7 +858,7 @@ class Definitions {
858858
lazy val TransientParamAnnotType: TypeRef = ctx.requiredClassRef("scala.annotation.constructorOnly")
859859
def TransientParamAnnot(implicit ctx: Context): ClassSymbol = TransientParamAnnotType.symbol.asClass
860860
lazy val CompileTimeOnlyAnnotType: TypeRef = ctx.requiredClassRef("scala.annotation.compileTimeOnly")
861-
def CompileTimeOnlyParamAnnot(implicit ctx: Context): ClassSymbol = CompileTimeOnlyAnnotType.symbol.asClass
861+
def CompileTimeOnlyAnnot(implicit ctx: Context): ClassSymbol = CompileTimeOnlyAnnotType.symbol.asClass
862862
lazy val SwitchAnnotType: TypeRef = ctx.requiredClassRef("scala.annotation.switch")
863863
def SwitchAnnot(implicit ctx: Context): ClassSymbol = SwitchAnnotType.symbol.asClass
864864
lazy val ThrowsAnnotType: TypeRef = ctx.requiredClassRef("scala.throws")

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

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -326,8 +326,18 @@ object Erasure {
326326
}
327327

328328
private def checkNotErased(tree: Tree)(implicit ctx: Context): tree.type = {
329-
if (isErased(tree) && !ctx.mode.is(Mode.Type))
330-
ctx.error(em"${tree.symbol} is declared as erased, but is in fact used", tree.sourcePos)
329+
if (!ctx.mode.is(Mode.Type)) {
330+
if (isErased(tree))
331+
ctx.error(em"${tree.symbol} is declared as erased, but is in fact used", tree.sourcePos)
332+
tree.symbol.getAnnotation(defn.CompileTimeOnlyAnnot) match {
333+
case Some(annot) =>
334+
annot.tree match {
335+
case Apply(_, Literal(msg) :: Nil) => ctx.error(msg.stringValue, tree.sourcePos)
336+
case _ => ctx.error("Reference to member with @compileTimeOnly", tree.sourcePos)
337+
}
338+
case _ => // OK
339+
}
340+
}
331341
tree
332342
}
333343

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -401,8 +401,8 @@ object Checking {
401401
if (!sym.is(Deferred))
402402
fail(NativeMembersMayNotHaveImplementation(sym))
403403
}
404-
if (sym.hasAnnotation(defn.CompileTimeOnlyParamAnnot))
405-
ctx.migrationWarning("`@compileTimeOnly(msg)` will be replaced by `scala.compiletime.error(msg)`", sym.sourcePos)
404+
if (sym.hasAnnotation(defn.CompileTimeOnlyAnnot))
405+
ctx.migrationWarning("`@compileTimeOnly(msg)` should be replaced with `scala.compiletime.error(msg)`", sym.sourcePos)
406406
else if (sym.is(Deferred, butNot = Param) && !sym.isType && !sym.isSelfSym) {
407407
if (!sym.owner.isClass || sym.owner.is(Module) || sym.owner.isAnonymousClass)
408408
fail(OnlyClassesCanHaveDeclaredButUndefinedMembers(sym))

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ class CompilationTests extends ParallelTesting {
172172
"tests/neg-custom-args/toplevel-samesource/S.scala",
173173
"tests/neg-custom-args/toplevel-samesource/nested/S.scala"),
174174
defaultOptions) +
175-
compileFile("tests/neg-custom-args/i6300.scala", allowDeepSubtypes)
175+
compileFile("tests/neg-custom-args/i6300.scala", allowDeepSubtypes) +
176176
compileFile("tests/neg-custom-args/i6312.scala", defaultOptions and "-Xfatal-warnings" and "-migration")
177177
}.checkExpectedErrors()
178178

tests/neg/i6419.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait A {
2+
@scala.annotation.compileTimeOnly("oops") def f: Int
3+
}
4+
5+
class B extends A {
6+
def f = 0
7+
}
8+
9+
object App {
10+
(new B).f
11+
(new B: A).f // error
12+
}

tests/neg/i6419b.scala

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
trait A {
2+
inline def f: Int = scala.compiletime.error("oops")
3+
}
4+
5+
class B extends A {
6+
override def f = 0
7+
}
8+
9+
object App {
10+
(new B).f
11+
(new B: A).f // error
12+
}

0 commit comments

Comments
 (0)