Skip to content

Commit 4878939

Browse files
committed
Accommodate quasi-constant annotation value
1 parent 22b2259 commit 4878939

File tree

4 files changed

+14
-4
lines changed

4 files changed

+14
-4
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package dotty.tools
22
package dotc
33
package core
44

5-
import Symbols._, Types._, Contexts._, Constants._, Phases.*
5+
import StdNames.*, Symbols.*, Types.*, Contexts.*, Constants.*, Phases.*
66
import ast.tpd, tpd.*
77
import util.Spans.Span
88
import printing.{Showable, Printer}
@@ -43,6 +43,12 @@ object Annotations {
4343
def argumentConstantString(i: Int)(using Context): Option[String] =
4444
for (case Constant(s: String) <- argumentConstant(i)) yield s
4545

46+
def argumentAdaptedConstantString(i: Int)(using Context): Option[String] =
47+
argument(i) match
48+
case Some(Literal(Constant(s: String))) => Some(s)
49+
case Some(TypeApply(Select(Literal(Constant(s: String)), n), _)) if n == nme.asInstanceOf_ => Some(s)
50+
case _ => None
51+
4652
/** The tree evaluaton is in progress. */
4753
def isEvaluating: Boolean = false
4854

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

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -562,9 +562,8 @@ object Erasure {
562562
report.error(msg, tree.srcPos)
563563
tree.symbol.getAnnotation(defn.CompileTimeOnlyAnnot) match
564564
case Some(annot) =>
565-
val message = annot.argumentConstant(0) match
566-
case Some(c) =>
567-
c.stringValue.toMessage
565+
val message = annot.argumentConstantString(0).orElse(annot.argumentAdaptedConstantString(0)) match
566+
case Some(c) => c.toMessage
568567
case _ =>
569568
em"""Reference to ${tree.symbol.showLocated} should not have survived,
570569
|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)