Skip to content

Commit 8203c20

Browse files
committed
Accommodate adapted Scala 2 annotation value
1 parent a115c8d commit 8203c20

File tree

3 files changed

+14
-4
lines changed

3 files changed

+14
-4
lines changed

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

Lines changed: 9 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import core.Scopes.newScopeWith
1919
import core.Decorators.*
2020
import core.Constants.*
2121
import core.Definitions.*
22-
import core.Annotations.BodyAnnotation
22+
import core.Annotations.{Annotation, BodyAnnotation}
2323
import typer.NoChecking
2424
import inlines.Inlines
2525
import typer.ProtoTypes.*
@@ -550,6 +550,11 @@ object Erasure {
550550
* - Java statics and packages can only be used in selections.
551551
*/
552552
private def checkNotErased(tree: Tree)(using Context): tree.type =
553+
extension (annot: Annotation) def argumentAdaptedConstantString(i: Int)(using Context): Option[String] =
554+
annot.argument(i) match
555+
case Some(Literal(Constant(s: String))) => Some(s)
556+
case Some(TypeApply(Select(Literal(Constant(s: String)), nme.asInstanceOf_), _)) => Some(s)
557+
case _ => None
553558
if !ctx.mode.is(Mode.Type) then
554559
if isErased(tree) then
555560
val msg =
@@ -562,15 +567,15 @@ object Erasure {
562567
report.error(msg, tree.srcPos)
563568
tree.symbol.getAnnotation(defn.CompileTimeOnlyAnnot) match
564569
case Some(annot) =>
565-
val message = annot.argumentConstant(0) match
566-
case Some(c) =>
570+
val message = annot.argumentConstantString(0).orElse(annot.argumentAdaptedConstantString(0)) match
571+
case Some(msg) =>
567572
val addendum = tree match
568573
case tree: RefTree
569574
if tree.symbol == defn.Compiletime_deferred && tree.name != nme.deferred =>
570575
i".\nNote that `deferred` can only be used under its own name when implementing a given in a trait; `${tree.name}` is not accepted."
571576
case _ =>
572577
""
573-
(c.stringValue ++ addendum).toMessage
578+
(msg + addendum).toMessage
574579
case _ =>
575580
em"""Reference to ${tree.symbol.showLocated} should not have survived,
576581
|it should have been processed and eliminated during expansion of an enclosing macro or term erasure."""

tests/neg/no-unit.check

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-- Error: tests/neg/no-unit.scala:1:8 ----------------------------------------------------------------------------------
2+
1 |val u = Unit // error
3+
| ^^^^
4+
| `Unit` companion object is not allowed in source; instead, use `()` for the unit value

tests/neg/no-unit.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
val u = Unit // error

0 commit comments

Comments
 (0)