From 27dbeb3ecb5c23bba22981a3d0f789c49a0cafa9 Mon Sep 17 00:00:00 2001 From: Hamza Remmal <56235032+hamzaremmal@users.noreply.github.com> Date: Wed, 8 Nov 2023 11:23:23 +0100 Subject: [PATCH] Add tests to check for wrong owner --- .../dotc/transform/MacroAnnotations.scala | 4 ++-- tests/neg-macros/wrong-owner.check | 23 +++++++++++++++++++ tests/neg-macros/wrong-owner/Macro_1.scala | 19 +++++++++++++++ tests/neg-macros/wrong-owner/Test_2.scala | 5 ++++ 4 files changed, 49 insertions(+), 2 deletions(-) create mode 100644 tests/neg-macros/wrong-owner.check create mode 100644 tests/neg-macros/wrong-owner/Macro_1.scala create mode 100644 tests/neg-macros/wrong-owner/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala b/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala index 5e0c0bc0a1c0..dbc1639f4b55 100644 --- a/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala +++ b/compiler/src/dotty/tools/dotc/transform/MacroAnnotations.scala @@ -143,10 +143,10 @@ class MacroAnnotations(phase: IdentityDenotTransformer): val isSymbolInDecls = tdef.symbol.asClass.info.decls.toList.toSet for tree <- template.body do if tree.symbol.owner != tdef.symbol then - report.error(em"Macro added a definition with the wrong owner - ${tree.symbol.owner} - ${tdef.symbol} in ${tree.source}") + report.error(em"Macro added a definition with the wrong owner - ${tree.symbol.owner} - ${tdef.symbol} in ${tree.source}", tree.srcPos) 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..24c08c283430 --- /dev/null +++ b/tests/neg-macros/wrong-owner.check @@ -0,0 +1,23 @@ +-- Error: tests/neg-macros/wrong-owner/Test_2.scala:7:6 ------------------------ +5 |@experimental +6 |@wrongOwner +7 |class Foo // error + |^ + |Malformed tree was found while expanding macro with -Xcheck-macros. + | |The tree does not conform to the compiler's tree invariants. + | | + | |Macro was: + | |@scala.annotation.internal.SourceFile("tests/neg-macros/wrong-owner/Test_2.scala") @wrongOwner @scala.annotation.experimental class Foo() + | | + | |The macro returned: + | |@scala.annotation.internal.SourceFile("tests/neg-macros/wrong-owner/Test_2.scala") @wrongOwner @scala.annotation.experimental class Foo() { + | override def toString(): java.lang.String = "Hello from macro" + |} + | | + | |Error: + | |assertion failed: bad owner; method toString has owner class String, expected was class Foo + |owner chain = method toString, class String, package java.lang, package java, package , ctxOwners = class Foo, class Foo, package , package , package , package , package , package , package , package , package , , , , + | | + |stacktrace available when compiling with `-Ydebug` + | | +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..a1e397aed60d --- /dev/null +++ b/tests/neg-macros/wrong-owner/Test_2.scala @@ -0,0 +1,5 @@ +import scala.annotation.experimental + +@experimental +@wrongOwner +class Foo // error \ No newline at end of file