Skip to content

Commit e1cc96c

Browse files
committed
Add tests for Reflect hasErasedParams/erasedParams and creating an erased method
1 parent 6d639ec commit e1cc96c

File tree

3 files changed

+39
-0
lines changed

3 files changed

+39
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import scala.annotation.MacroAnnotation
2+
import scala.annotation.internal.ErasedParam
3+
import scala.quoted._
4+
5+
class NewAnnotation extends scala.annotation.Annotation
6+
7+
class erasedParamsMethod extends MacroAnnotation:
8+
def transform(using Quotes)(tree: quotes.reflect.Definition): List[quotes.reflect.Definition] =
9+
import quotes.reflect._
10+
tree match
11+
case ClassDef(name, ctr, parents, self, body) =>
12+
val erasedInt = AnnotatedType(TypeRepr.of[Int], '{ new ErasedParam }.asTerm)
13+
val methType = MethodType(List("x", "y"))(_ => List(erasedInt, TypeRepr.of[Int]), _ => TypeRepr.of[Int])
14+
15+
assert(methType.hasErasedParams)
16+
assert(methType.erasedParams == List(true, false))
17+
18+
val methSym = Symbol.newMethod(tree.symbol, "takesErased", methType, Flags.EmptyFlags, Symbol.noSymbol)
19+
val methDef = DefDef(methSym, _ => Some(Literal(IntConstant(1))))
20+
21+
val clsDef = ClassDef.copy(tree)(name, ctr, parents, self, methDef :: body)
22+
23+
List(clsDef)
24+
case _ =>
25+
report.error("Annotation only supports `class`")
26+
List(tree)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.language.experimental.erasedDefinitions
2+
3+
class TakesErased {
4+
def takesErased(erased x: Int, y: Int): Int = ???
5+
}
6+
7+
@erasedParamsMethod class Foo extends TakesErased
8+
9+
@main def Test() =
10+
val foo = Foo()
11+
val v = foo.takesErased(1, 2)
12+
println(v)

0 commit comments

Comments
 (0)