Skip to content

Commit 6d639ec

Browse files
committed
Add quotes test for some erased parameters APIs
1 parent 2f76003 commit 6d639ec

File tree

4 files changed

+66
-1
lines changed

4 files changed

+66
-1
lines changed

library/src/scala/quoted/Quotes.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -2374,7 +2374,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
23742374
/** Is this a given parameter clause `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
23752375
def isGiven: Boolean
23762376
/** Is this a erased parameter clause `(erased x1: X1, ..., xn: Xn)` */
2377-
// TODO:deprecate in 3.4 and stabilize `erasedParams` and `hasErasedParams`.
2377+
// TODO:deprecate in 3.4 and stabilize `erasedArgs` and `hasErasedArgs`.
23782378
// @deprecated("Use `hasErasedArgs`","3.4")
23792379
def isErased: Boolean
23802380

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
method <init>: () isGiven=false isImplicit=false erasedArgs=List()
2+
method m1: (i: scala.Int) isGiven=true isImplicit=false erasedArgs=List(false)
3+
method m2: (i: scala.Int) isGiven=false isImplicit=false erasedArgs=List(true)
4+
method m3: (i: scala.Int, j: scala.Int) isGiven=false isImplicit=false erasedArgs=List(false, true)
5+
method m4: (i: EC) isGiven=false isImplicit=false erasedArgs=List(true)
6+
val l1: scala.ContextFunction1[scala.Int, scala.Int]
7+
val l2: scala.compiletime.ErasedFunction with apply: (x$0: scala.Int @scala.annotation.internal.ErasedParam) isImplicit=false erasedParams=List(true)
8+
val l3: scala.compiletime.ErasedFunction with apply: (x$0: scala.Int @scala.annotation.internal.ErasedParam) isImplicit=true erasedParams=List(true)
9+
val l4: scala.compiletime.ErasedFunction with apply: (x$0: scala.Int, x$1: scala.Int @scala.annotation.internal.ErasedParam) isImplicit=false erasedParams=List(false, true)
10+
val l5: scala.compiletime.ErasedFunction with apply: (x$0: EC @scala.annotation.internal.ErasedParam) isImplicit=false erasedParams=List(true)
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import scala.quoted.*
2+
3+
inline def inspect[A]: String =
4+
${ inspect2[A] }
5+
6+
def inspect2[A: Type](using Quotes): Expr[String] = {
7+
import quotes.reflect.*
8+
9+
val methods = TypeRepr.of[A].typeSymbol.declarations
10+
val names = methods.map { m =>
11+
m.tree match
12+
case dd @ DefDef(name, params, r, body) =>
13+
val paramStr =
14+
params.map {
15+
case ps: TermParamClause =>
16+
val params = ps.params.map(p => s"${p.name}: ${p.tpt.show}").mkString("(", ", ", ")")
17+
s"$params isGiven=${ps.isGiven} isImplicit=${ps.isImplicit} erasedArgs=${ps.erasedArgs}"
18+
case ps: TypeParamClause => ps.params.map(_.show).mkString("[", ", ", "]")
19+
}.mkString("")
20+
s"method $name: $paramStr"
21+
case vd @ ValDef(name, tpt, body) =>
22+
tpt.tpe match
23+
case Refinement(parent, "apply", tpe: MethodType) if parent == defn.ErasedFunctionClass.typeRef =>
24+
assert(tpt.tpe.isErasedFunctionType)
25+
26+
val params = tpe.paramNames.zip(tpe.paramTypes).map((n, t) => s"$n: ${t.show}").mkString("(", ", ", ")")
27+
s"val $name: ${parent.show} with apply: ${params} isImplicit=${tpe.isImplicit} erasedParams=${tpe.erasedParams}"
28+
case _ =>
29+
s"val $name: ${tpt.show}"
30+
case td @ TypeDef(name, tpt) => s"type $name: ${tpt.show}"
31+
case _ => s"something else: $m"
32+
}
33+
34+
Expr(names.mkString("\n"))
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import scala.language.experimental.erasedDefinitions
2+
3+
erased class EC
4+
5+
trait X {
6+
def m1(using i: Int): Int
7+
def m2(erased i: Int): Int
8+
def m3(i: Int, erased j: Int): Int
9+
def m4(i: EC): Int
10+
11+
val l1 = (x: Int) ?=> 5
12+
val l2 = (erased x: Int) => 5
13+
val l3 = (erased x: Int) ?=> 5
14+
val l4 = (x: Int, erased y: Int) => 5
15+
val l5 = (x: EC) => 5
16+
}
17+
18+
@main def Test = {
19+
println(inspect[X])
20+
}

0 commit comments

Comments
 (0)