-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for Reflect
hasErasedParams
/erasedParams
and creating a…
…n erased method
- Loading branch information
1 parent
6d639ec
commit e1cc96c
Showing
3 changed files
with
39 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
1 |
26 changes: 26 additions & 0 deletions
26
tests/run-custom-args/erased/quotes-add-erased/Macro_1.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
12
tests/run-custom-args/erased/quotes-add-erased/Test_2.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |