Skip to content

Commit 23eee17

Browse files
committed
Fix scala#7121: Disallow quotes in annotations
1 parent 15cece6 commit 23eee17

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

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

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import dotty.tools.dotc.typer.Inliner
2323

2424
import scala.collection.mutable
2525
import dotty.tools.dotc.util.SourcePosition
26+
import dotty.tools.dotc.util.Property
2627

2728
import scala.annotation.constructorOnly
2829

@@ -33,20 +34,25 @@ import scala.annotation.constructorOnly
3334
class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(ictx) {
3435
import tpd._
3536

37+
private val InAnnotation = Property.Key[Unit]()
38+
3639
override def transform(tree: Tree)(implicit ctx: Context): Tree =
3740
if (tree.source != ctx.source && tree.source.exists)
3841
transform(tree)(ctx.withSource(tree.source))
3942
else tree match {
4043
case tree: DefDef if tree.symbol.is(Inline) && level > 0 => EmptyTree
4144
case tree: DefTree =>
45+
lazy val annotCtx = ctx.fresh.setProperty(InAnnotation, ()).withOwner(tree.symbol)
4246
for (annot <- tree.symbol.annotations)
43-
transform(annot.tree)(given ctx.withOwner(tree.symbol))
47+
transform(annot.tree)(given annotCtx)
4448
checkLevel(super.transform(tree))
4549
case _ => checkLevel(super.transform(tree))
4650
}
4751

4852
/** Transform quoted trees while maintaining phase correctness */
4953
override protected def transformQuotation(body: Tree, quote: Tree)(implicit ctx: Context): Tree = {
54+
if (ctx.property(InAnnotation).isDefined)
55+
ctx.error("Cannot have a quote in an annotation", quote.sourcePos)
5056
val body1 = transform(body)(quoteContext)
5157
super.transformQuotation(body1, quote)
5258
}

tests/neg/i7121.scala

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.quoted._
2+
3+
class annot1[T](x: Expr[T]) extends scala.annotation.Annotation
4+
class annot2[T: Type](x: T) extends scala.annotation.Annotation
5+
6+
class Test()(implicit qtx: QuoteContext) {
7+
@annot1('{4}) // error
8+
def foo(str: String) = ()
9+
10+
@annot2(4)(given '[Int]) // error
11+
def foo2(str: String) = ()
12+
13+
}

0 commit comments

Comments
 (0)