Skip to content

Commit

Permalink
Add tests for Reflect hasErasedParams/erasedParams and creating a…
Browse files Browse the repository at this point in the history
…n erased method
  • Loading branch information
natsukagami committed Feb 20, 2023
1 parent 6d639ec commit e1cc96c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 0 deletions.
1 change: 1 addition & 0 deletions tests/run-custom-args/erased/quotes-add-erased.check
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
1
26 changes: 26 additions & 0 deletions tests/run-custom-args/erased/quotes-add-erased/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -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)
12 changes: 12 additions & 0 deletions tests/run-custom-args/erased/quotes-add-erased/Test_2.scala
Original file line number Diff line number Diff line change
@@ -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)

0 comments on commit e1cc96c

Please sign in to comment.