Skip to content

Commit fda646c

Browse files
committed
Add tests to check for wrong owner
1 parent 0e2041a commit fda646c

File tree

4 files changed

+29
-1
lines changed

4 files changed

+29
-1
lines changed

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -146,7 +146,7 @@ class MacroAnnotations(phase: IdentityDenotTransformer):
146146
report.error(em"Macro added a definition with the wrong owner - ${tree.symbol.owner} - ${tdef.symbol} in ${tree.source}")
147147
else if !isSymbolInDecls(tree.symbol) then
148148
tree.symbol.enteredAfter(phase)
149-
traverseChildren(tree) // Taverse before or after dealing with this class?
149+
traverseChildren(tree)
150150
case _ => traverseChildren(tree)
151151
}.traverse(tree)
152152

tests/neg-macros/wrong-owner.check

+2
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
Macro added a definition with the wrong owner - class String - class Foo in tests/neg-macros/wrong-owner/Test_2.scala
2+
1 error found
+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import scala.annotation.experimental
2+
import scala.annotation.MacroAnnotation
3+
import scala.quoted.*
4+
5+
@experimental
6+
class wrongOwner extends MacroAnnotation :
7+
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
8+
import quotes.reflect.*
9+
tree match
10+
case ClassDef(name, ctr, parents, self, body) =>
11+
val cls = tree.symbol
12+
val toStringSym = Symbol.requiredMethod("java.lang.Object.toString")
13+
val toStringOverrideSym = Symbol.newMethod(Symbol.classSymbol("java.lang.String"), "toString", toStringSym.info, Flags.Override, Symbol.noSymbol)
14+
val toStringDef = DefDef(toStringOverrideSym, _ => Some(Literal(StringConstant("Hello from macro"))))
15+
val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, toStringDef :: body)
16+
List(newClassDef)
17+
case _ =>
18+
report.error("@toString can only be annotated on class definitions")
19+
tree :: Nil
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// nopos-error
2+
3+
import scala.annotation.experimental
4+
5+
@experimental
6+
@wrongOwner
7+
class Foo

0 commit comments

Comments
 (0)