Skip to content

Commit 48f7d56

Browse files
nicolasstuckiWojciechMazur
authored andcommitted
Do not propagate macro annotations to exported methods
[Cherry-picked 29deb05]
1 parent b76f92f commit 48f7d56

File tree

3 files changed

+24
-0
lines changed

3 files changed

+24
-0
lines changed

Diff for: compiler/src/dotty/tools/dotc/typer/Namer.scala

+1
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ class Namer { typer: Typer =>
12541254
forwarder.addAnnotations(sym.annotations.filterConserve { annot =>
12551255
annot.symbol != defn.BodyAnnot
12561256
&& annot.symbol != defn.TailrecAnnot
1257+
&& !annot.symbol.derivesFrom(defn.MacroAnnotationClass)
12571258
})
12581259

12591260
if forwarder.isType then

Diff for: tests/run-macros/annot-export/Macro_1.scala

+13
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import scala.annotation.{experimental, MacroAnnotation}
2+
import scala.quoted._
3+
import scala.collection.mutable.Map
4+
5+
@experimental
6+
class returnClassName extends MacroAnnotation {
7+
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
8+
import quotes.reflect._
9+
tree match
10+
case DefDef(name, params, tpt, _) =>
11+
val rhs = Literal(StringConstant(Symbol.spliceOwner.name.stripSuffix("$")))
12+
List(DefDef.copy(tree)(name, params, tpt, Some(rhs)))
13+
}

Diff for: tests/run-macros/annot-export/Test_2.scala

+10
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
object Bar:
2+
@returnClassName
3+
def f(): String = ??? // def f(): String = "Bar"
4+
5+
object Baz:
6+
export Bar.f // def f(): String = Bar.f(n)
7+
8+
@main def Test =
9+
assert(Bar.f() == "Bar")
10+
assert(Baz.f() == "Bar")

0 commit comments

Comments
 (0)