diff --git a/tests/run-macros/annot-concrete-class.check b/tests/run-macros/annot-concrete-class.check new file mode 100644 index 000000000000..694fe5799063 --- /dev/null +++ b/tests/run-macros/annot-concrete-class.check @@ -0,0 +1 @@ +Hello, I was added by a MacroAnnotation and without being defined in the class. diff --git a/tests/run-macros/annot-concrete-class/Macro_1.scala b/tests/run-macros/annot-concrete-class/Macro_1.scala new file mode 100644 index 000000000000..54d1bc7c20ce --- /dev/null +++ b/tests/run-macros/annot-concrete-class/Macro_1.scala @@ -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 diff --git a/tests/run-macros/annot-concrete-class/Test_2.scala b/tests/run-macros/annot-concrete-class/Test_2.scala new file mode 100644 index 000000000000..713b0d814813 --- /dev/null +++ b/tests/run-macros/annot-concrete-class/Test_2.scala @@ -0,0 +1,11 @@ + +trait AFoo: + def foo: String + +@implementAFoo +class Foo extends AFoo + +@main def Test = + val foo = new Foo + println(foo.foo) + \ No newline at end of file