diff --git a/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala b/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala index 5e0c0bc0a1c0..d72c21e7ca43 100644 --- a/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala +++ b/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala @@ -146,7 +146,7 @@ class MacroAnnotations(phase: IdentityDenotTransformer): report.error(em"Macro added a definition with the wrong owner - ${tree.symbol.owner} - ${tdef.symbol} in ${tree.source}") else if !isSymbolInDecls(tree.symbol) then tree.symbol.enteredAfter(phase) - traverseChildren(tree) // Taverse before or after dealing with this class? + traverseChildren(tree) case _ => traverseChildren(tree) }.traverse(tree) diff --git a/tests/neg-macros/wrong-owner.check b/tests/neg-macros/wrong-owner.check new file mode 100644 index 000000000000..4b748320f6d0 --- /dev/null +++ b/tests/neg-macros/wrong-owner.check @@ -0,0 +1,2 @@ +Macro added a definition with the wrong owner - class String - class Foo in tests/neg-macros/wrong-owner/Test_2.scala +1 error found \ No newline at end of file diff --git a/tests/neg-macros/wrong-owner/Macro_1.scala b/tests/neg-macros/wrong-owner/Macro_1.scala new file mode 100644 index 000000000000..12f4c2a95a60 --- /dev/null +++ b/tests/neg-macros/wrong-owner/Macro_1.scala @@ -0,0 +1,19 @@ +import scala.annotation.experimental +import scala.annotation.MacroAnnotation +import scala.quoted.* + +@experimental +class wrongOwner extends MacroAnnotation : + def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] = + import quotes.reflect.* + tree match + case ClassDef(name, ctr, parents, self, body) => + val cls = tree.symbol + val toStringSym = Symbol.requiredMethod("java.lang.Object.toString") + val toStringOverrideSym = Symbol.newMethod(Symbol.classSymbol("java.lang.String"), "toString", toStringSym.info, Flags.Override, Symbol.noSymbol) + val toStringDef = DefDef(toStringOverrideSym, _ => Some(Literal(StringConstant("Hello from macro")))) + val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, toStringDef :: body) + List(newClassDef) + case _ => + report.error("@toString can only be annotated on class definitions") + tree :: Nil \ No newline at end of file diff --git a/tests/neg-macros/wrong-owner/Test_2.scala b/tests/neg-macros/wrong-owner/Test_2.scala new file mode 100644 index 000000000000..032224a67acc --- /dev/null +++ b/tests/neg-macros/wrong-owner/Test_2.scala @@ -0,0 +1,7 @@ +// nopos-error + +import scala.annotation.experimental + +@experimental +@wrongOwner +class Foo \ No newline at end of file