Skip to content

Commit d95f8f9

Browse files
committed
Fix #8250: Allow Expr.betaReduce to drop type ascriptions.
When a blackbox inline def is inlined, a type ascription is added to preserve the apparent return type of the inline def. Prior to this commit, the quoted.Expr.betaReduce function was unable to properly inline a known lambda expression coming from an inline def as it would see the type ascription and give up. This change instead makes betaReduce drop type ascriptions, allowing these two features to interact properly. Testing is done by verifying that betaReduce can correctly reduce the application of the result of an inline def to a constant value, with the reduction result being printed using the `compiletime.code` interpolator.
1 parent abaf47e commit d95f8f9

File tree

5 files changed

+26
-4
lines changed

5 files changed

+26
-4
lines changed

compiler/src/dotty/tools/dotc/tastyreflect/ReflectionCompilerInterface.scala

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2043,6 +2043,8 @@ class ReflectionCompilerInterface(val rootContext: core.Contexts.Context) extend
20432043
val argVals = argVals0.reverse
20442044
val argRefs = argRefs0.reverse
20452045
def rec(fn: Tree): Tree = fn match {
2046+
case Typed(expr, tpt) =>
2047+
rec(expr)
20462048
case Inlined(call, bindings, expansion) =>
20472049
// this case must go before closureDef to avoid dropping the inline node
20482050
cpy.Inlined(fn)(call, bindings, rec(expansion))
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
4
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import scala.quoted._
2+
3+
object Macros {
4+
inline def betaReduce[Arg,Result](inline fn : Arg=>Result)(inline arg: Arg): Result =
5+
${ betaReduceImpl('{ fn })('{ arg }) }
6+
7+
def betaReduceImpl[Arg,Result](fn: Expr[Arg=>Result])(arg: Expr[Arg])(using qctx : QuoteContext): Expr[Result] =
8+
Expr.betaReduce(fn)(arg)
9+
}
10+
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
import scala.compiletime._
2+
3+
object Test {
4+
5+
inline def dummy: Int => Int =
6+
(i: Int) => i + 1
7+
8+
def main(argv : Array[String]) : Unit = {
9+
println(code"${Macros.betaReduce(dummy)(3)}")
10+
}
11+
}
12+

tests/run-staging/i3876-c.check

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,5 @@
33
val f: scala.Function1[scala.Int, scala.Int] {
44
def apply(x: scala.Int): scala.Int
55
} = ((x: scala.Int) => x.+(x))
6-
7-
(f: scala.Function1[scala.Int, scala.Int] {
8-
def apply(x: scala.Int): scala.Int
9-
}).apply(3)
6+
f.apply(3)
107
}

0 commit comments

Comments
 (0)