Skip to content

Commit

Permalink
Add test for overriding a definition with MacroAnnotation without dum…
Browse files Browse the repository at this point in the history
…my definition (scala#19742)
  • Loading branch information
hamzaremmal authored Feb 22, 2024
2 parents 51ea65a + 4df6b10 commit 119bc33
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/run-macros/annot-concrete-class.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hello, I was added by a MacroAnnotation and without being defined in the class.
16 changes: 16 additions & 0 deletions tests/run-macros/annot-concrete-class/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import scala.annotation.MacroAnnotation
import scala.quoted.*

class implementAFoo extends MacroAnnotation:

def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
import quotes.reflect.*
tree match
case ClassDef(name, cstr, parents, self, body) =>
val owner = tree.symbol
val sym = Symbol.newMethod(tree.symbol, "foo", ByNameType.apply(TypeRepr.of[String]))
val mtd = DefDef.apply(sym, _ => Some(Literal(StringConstant("Hello, I was added by a MacroAnnotation and without being defined in the class."))))
List(ClassDef.copy(tree)(name, cstr, parents, self, mtd :: body))
case _ => report.errorAndAbort(s"@implementAFoo can only be applied to classes that extend AFoo")

end implementAFoo
11 changes: 11 additions & 0 deletions tests/run-macros/annot-concrete-class/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

trait AFoo:
def foo: String

@implementAFoo
class Foo extends AFoo

@main def Test =
val foo = new Foo
println(foo.foo)

0 comments on commit 119bc33

Please sign in to comment.