Skip to content

Commit 7e4fe17

Browse files
committed
Expr.betaReduce support for polymorphic function
Fixes #15968
1 parent e02c9e1 commit 7e4fe17

File tree

3 files changed

+22
-1
lines changed

3 files changed

+22
-1
lines changed

compiler/src/scala/quoted/runtime/impl/QuotesImpl.scala

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -362,11 +362,14 @@ class QuotesImpl private (using val ctx: Context) extends Quotes, QuoteUnpickler
362362
object Term extends TermModule:
363363
def betaReduce(tree: Term): Option[Term] =
364364
tree match
365-
// TODO support TypeApply. Would fix #15968.
366365
case app @ tpd.Apply(tpd.Select(fn, nme.apply), args) if dotc.core.Symbols.defn.isFunctionType(fn.tpe) =>
367366
val app1 = dotc.transform.BetaReduce(app, fn, List(args))
368367
if app1 eq app then None
369368
else Some(app1.withSpan(tree.span))
369+
case app @ tpd.Apply(tpd.TypeApply(tpd.Select(fn, nme.apply), targs), args) if fn.tpe.typeSymbol eq dotc.core.Symbols.defn.PolyFunctionClass =>
370+
val app1 = dotc.transform.BetaReduce(app, fn, List(targs, app.args))
371+
if app1 eq app then None
372+
else Some(app1.withSpan(tree.span))
370373
case tpd.Block(Nil, expr) =>
371374
for e <- betaReduce(expr) yield tpd.cpy.Block(tree)(Nil, e)
372375
case tpd.Inlined(_, Nil, expr) =>

tests/run-macros/i15968/Macro_1.scala

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
import scala.quoted.*
2+
3+
inline def macroPolyFun[A](inline arg: A, inline f: [Z] => Z => String): String =
4+
${ macroPolyFunImpl[A]('arg, 'f) }
5+
6+
private def macroPolyFunImpl[A: Type](arg: Expr[A], f: Expr[[Z] => Z => String])(using Quotes): Expr[String] =
7+
Expr(Expr.betaReduce('{ $f($arg) }).show)
8+
9+
10+
inline def macroFun[A](inline arg: A, inline f: A => String): String =
11+
${ macroFunImpl[A]('arg, 'f) }
12+
13+
private def macroFunImpl[A: Type](arg: Expr[A], f: Expr[A => String])(using Quotes): Expr[String] =
14+
Expr(Expr.betaReduce('{ $f($arg) }).show)
15+

tests/run-macros/i15968/Test_2.scala

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
@main def Test: Unit =
2+
println(macroPolyFun("foo", [Z] => (arg: Z) => arg.toString))
3+
println(macroFun("foo", arg => arg.toString))

0 commit comments

Comments
 (0)