Skip to content

Commit 119bc33

Browse files
authored
Add test for overriding a definition with MacroAnnotation without dummy definition (#19742)
2 parents 51ea65a + 4df6b10 commit 119bc33

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello, I was added by a MacroAnnotation and without being defined in the class.
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import scala.annotation.MacroAnnotation
2+
import scala.quoted.*
3+
4+
class implementAFoo extends MacroAnnotation:
5+
6+
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
7+
import quotes.reflect.*
8+
tree match
9+
case ClassDef(name, cstr, parents, self, body) =>
10+
val owner = tree.symbol
11+
val sym = Symbol.newMethod(tree.symbol, "foo", ByNameType.apply(TypeRepr.of[String]))
12+
val mtd = DefDef.apply(sym, _ => Some(Literal(StringConstant("Hello, I was added by a MacroAnnotation and without being defined in the class."))))
13+
List(ClassDef.copy(tree)(name, cstr, parents, self, mtd :: body))
14+
case _ => report.errorAndAbort(s"@implementAFoo can only be applied to classes that extend AFoo")
15+
16+
end implementAFoo
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
2+
trait AFoo:
3+
def foo: String
4+
5+
@implementAFoo
6+
class Foo extends AFoo
7+
8+
@main def Test =
9+
val foo = new Foo
10+
println(foo.foo)
11+

0 commit comments

Comments
 (0)