Skip to content

Commit 27dbeb3

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

File tree

4 files changed

+49
-2
lines changed

4 files changed

+49
-2
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -143,10 +143,10 @@ class MacroAnnotations(phase: IdentityDenotTransformer):
143143
val isSymbolInDecls = tdef.symbol.asClass.info.decls.toList.toSet
144144
for tree <- template.body do
145145
if tree.symbol.owner != tdef.symbol then
146-
report.error(em"Macro added a definition with the wrong owner - ${tree.symbol.owner} - ${tdef.symbol} in ${tree.source}")
146+
report.error(em"Macro added a definition with the wrong owner - ${tree.symbol.owner} - ${tdef.symbol} in ${tree.source}", tree.srcPos)
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

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
-- Error: tests/neg-macros/wrong-owner/Test_2.scala:7:6 ------------------------
2+
5 |@experimental
3+
6 |@wrongOwner
4+
7 |class Foo // error
5+
|^
6+
|Malformed tree was found while expanding macro with -Xcheck-macros.
7+
| |The tree does not conform to the compiler's tree invariants.
8+
| |
9+
| |Macro was:
10+
| |@scala.annotation.internal.SourceFile("tests/neg-macros/wrong-owner/Test_2.scala") @wrongOwner @scala.annotation.experimental class Foo()
11+
| |
12+
| |The macro returned:
13+
| |@scala.annotation.internal.SourceFile("tests/neg-macros/wrong-owner/Test_2.scala") @wrongOwner @scala.annotation.experimental class Foo() {
14+
| override def toString(): java.lang.String = "Hello from macro"
15+
|}
16+
| |
17+
| |Error:
18+
| |assertion failed: bad owner; method toString has owner class String, expected was class Foo
19+
|owner chain = method toString, class String, package java.lang, package java, package <root>, ctxOwners = class Foo, class Foo, package <empty>, package <empty>, package <empty>, package <root>, package <root>, package <root>, package <root>, package <root>, package <root>, <none>, <none>, <none>, <none>
20+
| |
21+
|stacktrace available when compiling with `-Ydebug`
22+
| |
23+
1 error found
Lines changed: 19 additions & 0 deletions
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
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import scala.annotation.experimental
2+
3+
@experimental
4+
@wrongOwner
5+
class Foo // error

0 commit comments

Comments
 (0)