Skip to content

Commit

Permalink
Add tests to check for wrong owner
Browse files Browse the repository at this point in the history
  • Loading branch information
hamzaremmal committed Nov 8, 2023
1 parent 0e2041a commit 27dbeb3
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
23 changes: 23 additions & 0 deletions tests/neg-macros/wrong-owner.check
Original file line number Diff line number Diff line change
@@ -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 <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>
| |
|stacktrace available when compiling with `-Ydebug`
| |
1 error found
19 changes: 19 additions & 0 deletions tests/neg-macros/wrong-owner/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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
5 changes: 5 additions & 0 deletions tests/neg-macros/wrong-owner/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.annotation.experimental

@experimental
@wrongOwner
class Foo // error

0 comments on commit 27dbeb3

Please sign in to comment.