Skip to content

Add test for overriding a definition with MacroAnnotation without dummy definition #19742

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Feb 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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)