-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Fix #8250: Allow Expr.betaReduce to drop type ascriptions. #8251
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
|
||
-- [E007] Type Mismatch Error: tests/neg-macros/beta-reduce-inline-result/Test_2.scala:11:41 --------------------------- | ||
11 | val x2: 4 = Macros.betaReduce(dummy1)(3) // error | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
| Found: Int | ||
| Required: (4 : Int) |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import scala.quoted._ | ||
|
||
object Macros { | ||
inline def betaReduce[Arg,Result](inline fn: Arg=>Result)(inline arg: Arg): Result = | ||
${ betaReduceImpl('{ fn })('{ arg }) } | ||
|
||
def betaReduceImpl[Arg,Result](fn: Expr[Arg=>Result])(arg: Expr[Arg])(using qctx: QuoteContext): Expr[Result] = | ||
Expr.betaReduce(fn)(arg) | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
|
||
object Test { | ||
|
||
inline def dummy1: Int => Int = | ||
(i: Int) => i + 1 | ||
|
||
inline def dummy2: Int => Int = | ||
(i: Int) => ??? | ||
|
||
val x1: Int = Macros.betaReduce(dummy1)(3) | ||
val x2: 4 = Macros.betaReduce(dummy1)(3) // error | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
compile-time: 4 | ||
run-time: 4 | ||
compile-time: 1 | ||
run-time: 1 | ||
run-time: 5 |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
import scala.quoted._ | ||
|
||
object Macros { | ||
inline def betaReduce[Arg,Result](inline fn : Arg=>Result)(inline arg: Arg): Result = | ||
${ betaReduceImpl('{ fn })('{ arg }) } | ||
|
||
def betaReduceImpl[Arg,Result](fn: Expr[Arg=>Result])(arg: Expr[Arg])(using qctx : QuoteContext): Expr[Result] = | ||
Expr.betaReduce(fn)(arg) | ||
|
||
inline def betaReduceAdd1[Arg](inline fn: Arg=>Int)(inline arg: Arg): Int = | ||
${ betaReduceAdd1Impl('{ fn })('{ arg }) } | ||
|
||
def betaReduceAdd1Impl[Arg](fn: Expr[Arg=>Int])(arg: Expr[Arg])(using qctx: QuoteContext): Expr[Int] = | ||
'{ ${ Expr.betaReduce(fn)(arg) } + 1 } | ||
} | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
import scala.compiletime._ | ||
|
||
object Test { | ||
|
||
inline def dummy1: Int => Int = | ||
(i: Int) => i + 1 | ||
|
||
inline def dummy2: (i: Int) => i.type = | ||
(i: Int) => i | ||
|
||
inline def dummy3: Int => Int = | ||
(i: Int) => ??? | ||
|
||
inline def dummy4: Int => Int = | ||
??? | ||
|
||
def main(argv : Array[String]) : Unit = { | ||
println(code"compile-time: ${Macros.betaReduce(dummy1)(3)}") | ||
println(s"run-time: ${Macros.betaReduce(dummy1)(3)}") | ||
println(code"compile-time: ${Macros.betaReduce(dummy2)(1)}") | ||
// paramrefs have to be properly substituted in this case | ||
println(s"run-time: ${Macros.betaReduce(dummy2)(1)}") | ||
|
||
// ensure the inlined ??? is ascribed type Int so this compiles | ||
def throwsNotImplemented1 = Macros.betaReduceAdd1(dummy3)(4) | ||
// ensure we handle cases where the (non-inlineable) function itself needs ascribing | ||
def throwsNotImplemented2 = Macros.betaReduce(dummy4)(6) | ||
|
||
// make sure paramref types work when inlining is not possible | ||
println(s"run-time: ${Macros.betaReduce(Predef.identity)(5)}") | ||
} | ||
} | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. We could have a simple neg test import scala.compiletime._
object Test {
inline def dummy: Int => Int =
(i: Int) => i + 1
val x1: Int = Macros.betaReduce(dummy)(3)
val x2: 4 = Macros.betaReduce(dummy)(3) // error
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Agreed, I will add this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Funny detail: this test actually passes already because the inline expansion adds its own type ascription. I'll look for a test that properly exercises the case where betaReduce's own type ascription is needed... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@fhackett won't this crash if the ascribed type has a
val apply
member that's not a method? I think this could happen if the term passed asfn
is not really a function, or if theapply
is overloaded. Something like:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This case is only reached if the prefix is an explicit closure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Right, I had missed the
closureDef
pattern. But it seems even aClosure
can have a type that contains an overloadedapply
member: indeed it can be any SAM type, no?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe, I will have to check if we can get one of those here. @LPTK did you have a specific example in mind?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Something like
betaReduce('{ (x => x) : Test }, ...)
where we have:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried it, and we get a class cast exception.
I can figure out a fix - should I push a second commit here or open a second issue+PR?
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, I can do one "better". See this fun edge case:
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is getting more involved than just a quick fix, so I'll open a fresh issue.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Continued on #8290