From e1cc96cdd422d3627a65bbc96a89d4d4b855146f Mon Sep 17 00:00:00 2001 From: Natsu Kagami Date: Mon, 20 Feb 2023 13:52:34 +0100 Subject: [PATCH] Add tests for Reflect `hasErasedParams`/`erasedParams` and creating an erased method --- .../erased/quotes-add-erased.check | 1 + .../erased/quotes-add-erased/Macro_1.scala | 26 +++++++++++++++++++ .../erased/quotes-add-erased/Test_2.scala | 12 +++++++++ 3 files changed, 39 insertions(+) create mode 100644 tests/run-custom-args/erased/quotes-add-erased.check create mode 100644 tests/run-custom-args/erased/quotes-add-erased/Macro_1.scala create mode 100644 tests/run-custom-args/erased/quotes-add-erased/Test_2.scala diff --git a/tests/run-custom-args/erased/quotes-add-erased.check b/tests/run-custom-args/erased/quotes-add-erased.check new file mode 100644 index 000000000000..d00491fd7e5b --- /dev/null +++ b/tests/run-custom-args/erased/quotes-add-erased.check @@ -0,0 +1 @@ +1 diff --git a/tests/run-custom-args/erased/quotes-add-erased/Macro_1.scala b/tests/run-custom-args/erased/quotes-add-erased/Macro_1.scala new file mode 100644 index 000000000000..66f8475da96d --- /dev/null +++ b/tests/run-custom-args/erased/quotes-add-erased/Macro_1.scala @@ -0,0 +1,26 @@ +import scala.annotation.MacroAnnotation +import scala.annotation.internal.ErasedParam +import scala.quoted._ + +class NewAnnotation extends scala.annotation.Annotation + +class erasedParamsMethod 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 erasedInt = AnnotatedType(TypeRepr.of[Int], '{ new ErasedParam }.asTerm) + val methType = MethodType(List("x", "y"))(_ => List(erasedInt, TypeRepr.of[Int]), _ => TypeRepr.of[Int]) + + assert(methType.hasErasedParams) + assert(methType.erasedParams == List(true, false)) + + val methSym = Symbol.newMethod(tree.symbol, "takesErased", methType, Flags.EmptyFlags, Symbol.noSymbol) + val methDef = DefDef(methSym, _ => Some(Literal(IntConstant(1)))) + + val clsDef = ClassDef.copy(tree)(name, ctr, parents, self, methDef :: body) + + List(clsDef) + case _ => + report.error("Annotation only supports `class`") + List(tree) diff --git a/tests/run-custom-args/erased/quotes-add-erased/Test_2.scala b/tests/run-custom-args/erased/quotes-add-erased/Test_2.scala new file mode 100644 index 000000000000..107fa0833e95 --- /dev/null +++ b/tests/run-custom-args/erased/quotes-add-erased/Test_2.scala @@ -0,0 +1,12 @@ +import scala.language.experimental.erasedDefinitions + +class TakesErased { + def takesErased(erased x: Int, y: Int): Int = ??? +} + +@erasedParamsMethod class Foo extends TakesErased + +@main def Test() = + val foo = Foo() + val v = foo.takesErased(1, 2) + println(v)