Skip to content

Commit b7d691f

Browse files
Merge pull request #7053 from dotty-staging/fix-#7052
Fix #7052: Transform annotations in ReifyQuotes
2 parents 38e5259 + 866d6ab commit b7d691f

File tree

6 files changed

+39
-3
lines changed

6 files changed

+39
-3
lines changed

compiler/src/dotty/tools/dotc/ast/TreeMapWithImplicits.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ class TreeMapWithImplicits extends tpd.TreeMap {
6262
private def nestedScopeCtx(defs: List[Tree])(implicit ctx: Context): Context = {
6363
val nestedCtx = ctx.fresh.setNewScope
6464
defs foreach {
65-
case d: DefTree => nestedCtx.enter(d.symbol)
65+
case d: DefTree if d.symbol.isOneOf(GivenOrImplicit) => nestedCtx.enter(d.symbol)
6666
case _ =>
6767
}
6868
nestedCtx
@@ -73,7 +73,7 @@ class TreeMapWithImplicits extends tpd.TreeMap {
7373
new TreeTraverser {
7474
def traverse(tree: Tree)(implicit ctx: Context): Unit = {
7575
tree match {
76-
case d: DefTree => nestedCtx.enter(d.symbol)
76+
case d: DefTree if d.symbol.isOneOf(GivenOrImplicit) => nestedCtx.enter(d.symbol)
7777
case _ =>
7878
}
7979
traverseChildren(tree)

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

+4
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,10 @@ class PCPCheckAndHeal(@constructorOnly ictx: Context) extends TreeMapWithStages(
3838
transform(tree)(ctx.withSource(tree.source))
3939
else tree match {
4040
case tree: DefDef if tree.symbol.is(Inline) && level > 0 => EmptyTree
41+
case tree: DefTree =>
42+
for (annot <- tree.symbol.annotations)
43+
transform(annot.tree) given ctx.withOwner(tree.symbol)
44+
checkLevel(super.transform(tree))
4145
case _ => checkLevel(super.transform(tree))
4246
}
4347
}

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

+9-1
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ import dotty.tools.dotc.ast.tpd
1919
import typer.Implicits.SearchFailureType
2020

2121
import scala.collection.mutable
22-
import dotty.tools.dotc.core.Annotations.Annotation
22+
import dotty.tools.dotc.core.Annotations._
2323
import dotty.tools.dotc.core.Names._
2424
import dotty.tools.dotc.core.StdNames._
2525
import dotty.tools.dotc.core.quoted._
@@ -407,6 +407,14 @@ class ReifyQuotes extends MacroTransform {
407407
// TODO move to FirstTransform to trigger even without quotes
408408
cpy.DefDef(tree)(rhs = defaultValue(tree.rhs.tpe))
409409

410+
case tree: DefTree if level >= 1 =>
411+
val newAnnotations = tree.symbol.annotations.mapconserve { annot =>
412+
val newAnnotTree = transform(annot.tree) given ctx.withOwner(tree.symbol)
413+
if (annot.tree == newAnnotTree) annot
414+
else ConcreteAnnotation(newAnnotTree)
415+
}
416+
tree.symbol.annotations = newAnnotations
417+
super.transform(tree)
410418
case _ =>
411419
super.transform(tree)
412420
}

tests/neg/i7052.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted._
2+
class Test {
3+
def foo(str: String) given QuoteContext = '{
4+
@deprecated(str, "") // error
5+
def bar = ???
6+
}
7+
}

tests/neg/i7052b.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted._
2+
class Test {
3+
def foo(str: String) given QuoteContext = '{
4+
delegate for QuoteContext = ???
5+
'{
6+
@deprecated(str, "") // error
7+
def bar = ???
8+
}
9+
}
10+
}

tests/pos/i7052.scala

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import scala.quoted._
2+
class Test {
3+
def foo(str: Expr[String]) given QuoteContext = '{
4+
@deprecated($str, "")
5+
def bar = ???
6+
}
7+
}

0 commit comments

Comments
 (0)