Skip to content

Commit 441c2ee

Browse files
Merge pull request #7368 from dotty-staging/fix-#7121
Fix #7121: Disallow quotes in annotations
2 parents e7bf561 + 2ed363a commit 441c2ee

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

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

+11-2
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package transform
33

44
import dotty.tools.dotc.ast.Trees._
55
import dotty.tools.dotc.ast.{TreeTypeMap, tpd, untpd}
6+
import dotty.tools.dotc.core.Annotations.BodyAnnotation
67
import dotty.tools.dotc.core.Constants._
78
import dotty.tools.dotc.core.Contexts._
89
import dotty.tools.dotc.core.Decorators._
@@ -23,6 +24,7 @@ import dotty.tools.dotc.typer.Inliner
2324

2425
import scala.collection.mutable
2526
import dotty.tools.dotc.util.SourcePosition
27+
import dotty.tools.dotc.util.Property
2628

2729
import scala.annotation.constructorOnly
2830

@@ -33,20 +35,27 @@ import scala.annotation.constructorOnly
3335
class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(ictx) {
3436
import tpd._
3537

38+
private val InAnnotation = Property.Key[Unit]()
39+
3640
override def transform(tree: Tree)(implicit ctx: Context): Tree =
3741
if (tree.source != ctx.source && tree.source.exists)
3842
transform(tree)(ctx.withSource(tree.source))
3943
else tree match {
4044
case tree: DefDef if tree.symbol.is(Inline) && level > 0 => EmptyTree
4145
case tree: DefTree =>
42-
for (annot <- tree.symbol.annotations)
43-
transform(annot.tree)(given ctx.withOwner(tree.symbol))
46+
lazy val annotCtx = ctx.fresh.setProperty(InAnnotation, true).withOwner(tree.symbol)
47+
for (annot <- tree.symbol.annotations) annot match {
48+
case annot: BodyAnnotation => annot // already checked in PrepareInlineable before the creation of the BodyAnnotation
49+
case annot => transform(annot.tree)(given annotCtx)
50+
}
4451
checkLevel(super.transform(tree))
4552
case _ => checkLevel(super.transform(tree))
4653
}
4754

4855
/** Transform quoted trees while maintaining phase correctness */
4956
override protected def transformQuotation(body: Tree, quote: Tree)(implicit ctx: Context): Tree = {
57+
if (ctx.property(InAnnotation).isDefined)
58+
ctx.error("Cannot have a quote in an annotation", quote.sourcePos)
5059
val body1 = transform(body)(quoteContext)
5160
super.transformQuotation(body1, quote)
5261
}

tests/neg/i7121.scala

+13
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)