From 995cfc1f757dd30c9eda9a8ee57aee5e16f4b63d Mon Sep 17 00:00:00 2001 From: Hamza Remmal <56235032+hamzaremmal@users.noreply.github.com> Date: Wed, 8 Nov 2023 14:08:20 +0100 Subject: [PATCH] Add check for #18806 --- tests/run-macros/i18806.check | 1 + tests/run-macros/i18806/Macro_1.scala | 24 ++++++++++++++++++++++++ tests/run-macros/i18806/Test_2.scala | 14 ++++++++++++++ 3 files changed, 39 insertions(+) create mode 100644 tests/run-macros/i18806.check create mode 100644 tests/run-macros/i18806/Macro_1.scala create mode 100644 tests/run-macros/i18806/Test_2.scala diff --git a/tests/run-macros/i18806.check b/tests/run-macros/i18806.check new file mode 100644 index 000000000000..32f95c0d1244 --- /dev/null +++ b/tests/run-macros/i18806.check @@ -0,0 +1 @@ +hi \ No newline at end of file diff --git a/tests/run-macros/i18806/Macro_1.scala b/tests/run-macros/i18806/Macro_1.scala new file mode 100644 index 000000000000..76c2e638d977 --- /dev/null +++ b/tests/run-macros/i18806/Macro_1.scala @@ -0,0 +1,24 @@ +import scala.annotation.{experimental, MacroAnnotation} +import scala.quoted._ + +@experimental +class gen 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 meth = cls.methodMember("foo").head + // val fooTpe = cls.typeRef.memberType(meth) + + val overrideTpe = MethodType(Nil)(_ => Nil, _ => defn.StringClass.typeRef) + + val fooOverrideSym = Symbol.newMethod(cls, "foo", overrideTpe, Flags.Override, Symbol.noSymbol) + + val fooDef = DefDef(fooOverrideSym, _ => Some(Literal(StringConstant("hi")))) + + val newClassDef = ClassDef.copy(tree)(name, ctr, parents, self, fooDef :: body) + List(newClassDef) + case _ => + report.error("Annotation only supports `class`") + List(tree) \ No newline at end of file diff --git a/tests/run-macros/i18806/Test_2.scala b/tests/run-macros/i18806/Test_2.scala new file mode 100644 index 000000000000..d8697c89e452 --- /dev/null +++ b/tests/run-macros/i18806/Test_2.scala @@ -0,0 +1,14 @@ +import scala.annotation.experimental + +class Base: + def foo(): Object = ??? + +@experimental +@gen +class Sub extends Base +// > override def foo(): String = "hi" + +@experimental +@main def Test(): Unit = + val sub = new Sub + println(sub.foo()) \ No newline at end of file