Skip to content

Commit

Permalink
Add quotes test for some erased parameters APIs
Browse files Browse the repository at this point in the history
  • Loading branch information
natsukagami committed Feb 13, 2023
1 parent f22ac2f commit faea51f
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 1 deletion.
2 changes: 1 addition & 1 deletion library/src/scala/quoted/Quotes.scala
Original file line number Diff line number Diff line change
Expand Up @@ -2374,7 +2374,7 @@ trait Quotes { self: runtime.QuoteUnpickler & runtime.QuoteMatching =>
/** Is this a given parameter clause `(using X1, ..., Xn)` or `(using x1: X1, ..., xn: Xn)` */
def isGiven: Boolean
/** Is this a erased parameter clause `(erased x1: X1, ..., xn: Xn)` */
// TODO:deprecate in 3.4 and stabilize `erasedParams` and `hasErasedParams`.
// TODO:deprecate in 3.4 and stabilize `erasedArgs` and `hasErasedArgs`.
// @deprecated("Use `hasErasedArgs`","3.4")
def isErased: Boolean

Expand Down
10 changes: 10 additions & 0 deletions tests/run-custom-args/erased/quotes-reflection.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
method <init>: () isGiven=false isImplicit=false erasedArgs=List()
method m1: (i: scala.Int) isGiven=true isImplicit=false erasedArgs=List(false)
method m2: (i: scala.Int) isGiven=false isImplicit=false erasedArgs=List(true)
method m3: (i: scala.Int, j: scala.Int) isGiven=false isImplicit=false erasedArgs=List(false, true)
method m4: (i: EC) isGiven=false isImplicit=false erasedArgs=List(true)
val l1: scala.ContextFunction1[scala.Int, scala.Int]
val l2: scala.compiletime.ErasedFunction with apply: (x$0: scala.Int @scala.annotation.internal.ErasedParam) isImplicit=false erasedParams=List(true)
val l3: scala.compiletime.ErasedFunction with apply: (x$0: scala.Int @scala.annotation.internal.ErasedParam) isImplicit=true erasedParams=List(true)
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)
val l5: scala.compiletime.ErasedFunction with apply: (x$0: EC @scala.annotation.internal.ErasedParam) isImplicit=false erasedParams=List(true)
35 changes: 35 additions & 0 deletions tests/run-custom-args/erased/quotes-reflection/Macros_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
import scala.quoted.*

inline def inspect[A]: String =
${ inspect2[A] }

def inspect2[A: Type](using Quotes): Expr[String] = {
import quotes.reflect.*

val methods = TypeRepr.of[A].typeSymbol.declarations
val names = methods.map { m =>
m.tree match
case dd @ DefDef(name, params, r, body) =>
val paramStr =
params.map {
case ps: TermParamClause =>
val params = ps.params.map(p => s"${p.name}: ${p.tpt.show}").mkString("(", ", ", ")")
s"$params isGiven=${ps.isGiven} isImplicit=${ps.isImplicit} erasedArgs=${ps.erasedArgs}"
case ps: TypeParamClause => ps.params.map(_.show).mkString("[", ", ", "]")
}.mkString("")
s"method $name: $paramStr"
case vd @ ValDef(name, tpt, body) =>
tpt.tpe match
case Refinement(parent, "apply", tpe: MethodType) if parent == defn.ErasedFunctionClass.typeRef =>
assert(tpt.tpe.isErasedFunctionType)

val params = tpe.paramNames.zip(tpe.paramTypes).map((n, t) => s"$n: ${t.show}").mkString("(", ", ", ")")
s"val $name: ${parent.show} with apply: ${params} isImplicit=${tpe.isImplicit} erasedParams=${tpe.erasedParams}"
case _ =>
s"val $name: ${tpt.show}"
case td @ TypeDef(name, tpt) => s"type $name: ${tpt.show}"
case _ => s"something else: $m"
}

Expr(names.mkString("\n"))
}
20 changes: 20 additions & 0 deletions tests/run-custom-args/erased/quotes-reflection/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
import scala.language.experimental.erasedDefinitions

erased class EC

trait X {
def m1(using i: Int): Int
def m2(erased i: Int): Int
def m3(i: Int, erased j: Int): Int
def m4(i: EC): Int

val l1 = (x: Int) ?=> 5
val l2 = (erased x: Int) => 5
val l3 = (erased x: Int) ?=> 5
val l4 = (x: Int, erased y: Int) => 5
val l5 = (x: EC) => 5
}

@main def Test = {
println(inspect[X])
}

0 comments on commit faea51f

Please sign in to comment.