-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Add quoted let #7388
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
Closed
Closed
Add quoted let #7388
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
22 changes: 22 additions & 0 deletions
22
compiler/src/dotty/tools/dotc/tastyreflect/QuoteContextState.scala
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package dotty.tools.dotc.tastyreflect | ||
|
||
import dotty.tools.dotc.ast.tpd | ||
import dotty.tools.dotc.core._ | ||
import dotty.tools.dotc.core.Contexts._ | ||
import dotty.tools.dotc.util.Property | ||
|
||
object QuoteContextState { | ||
|
||
val NextIndex = new Property.Key[scala.runtime.IntRef] | ||
|
||
def init(ctx: Context): Context = | ||
ctx.fresh.setProperty(NextIndex, new scala.runtime.IntRef(0)) | ||
|
||
def nextIndex()(given ctx: Context): Int = { | ||
val ref = ctx.property(NextIndex).get | ||
val ret = ref.elem | ||
ref.elem = ret + 1 | ||
ret | ||
} | ||
} | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,17 @@ | ||
package scala.quoted | ||
|
||
import scala.internal.quoted.showName | ||
|
||
package object util { | ||
|
||
/** Genrate the code `'{ val `<name>` = $expr; ${ body('<name>) } }` with the given name */ | ||
def let[T: Type, U: Type](name: String)(expr: Expr[T])(body: Expr[T] => Expr[U])(given QuoteContext): Expr[U] = '{ | ||
@showName(${Expr(name)}) val x = $expr | ||
${ body('x) } | ||
} | ||
nicolasstucki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
/** Genrate the code `'{ val x$<i> = $expr; ${ body('x$<i>) } }` for a fresh i */ | ||
def let[T: Type, U: Type](expr: Expr[T])(body: Expr[T] => Expr[U])(given qctx: QuoteContext): Expr[U] = | ||
let("x$" + qctx.nextIndex())(expr)(body) | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -117,6 +117,8 @@ trait CompilerInterface { | |
|
||
def settings: Settings | ||
|
||
def nextIndex(): Int | ||
|
||
// | ||
// QUOTE UNPICKLING | ||
// | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
Test.x.*({ | ||
val x$0: scala.Double = Test.x.*(Test.x) | ||
val x$1: scala.Double = x$0.*(x$0) | ||
x$1.*({ | ||
val x$2: scala.Double = x$1.*(x$1) | ||
x$2.*({ | ||
val x$3: scala.Double = x$2.*(x$2) | ||
val x$4: scala.Double = x$3.*(x$3) | ||
val x$5: scala.Double = x$4.*(x$4) | ||
x$5 | ||
}) | ||
}) | ||
}) | ||
Test.x.*({ | ||
val x$0: scala.Double = Test.x.*(Test.x) | ||
x$0.*({ | ||
val x$1: scala.Double = x$0.*(x$0) | ||
val x$2: scala.Double = x$1.*(x$1) | ||
val x$3: scala.Double = x$2.*(x$2) | ||
val x$4: scala.Double = x$3.*(x$3) | ||
x$4 | ||
}) | ||
}) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
import scala.quoted._ | ||
import scala.quoted.util.let | ||
|
||
inline def power(x: Double, inline n: Long): String = | ||
${ powerCodeShow('x, n) } | ||
|
||
private def powerCodeShow(x: Expr[Double], n: Long)(given QuoteContext): Expr[String] = | ||
Expr(powerCode(n, 2, x).show) | ||
|
||
private def powerCode(n: Long, idx: Int, x: Expr[Double])(given QuoteContext): Expr[Double] = | ||
if (n == 0) '{1.0} | ||
else if (n == 1) x | ||
else if (n % 2 == 0) let('{ $x * $x })(y => powerCode(n / 2, idx * 2, y)) | ||
else '{ $x * ${powerCode(n - 1, idx, x)} } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
|
||
|
||
|
||
object Test extends App { | ||
val x = 4.5 | ||
println(power(x, 77)) | ||
println(power(x, 35)) | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
((x: scala.Double) => x.*({ | ||
val x$0: scala.Double = x.*(x) | ||
val x$1: scala.Double = x$0.*(x$0) | ||
x$1.*({ | ||
val x$2: scala.Double = x$1.*(x$1) | ||
x$2.*({ | ||
val x$3: scala.Double = x$2.*(x$2) | ||
val x$4: scala.Double = x$3.*(x$3) | ||
val x$5: scala.Double = x$4.*(x$4) | ||
x$5 | ||
}) | ||
}) | ||
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import scala.quoted._ | ||
import scala.quoted.util.let | ||
import scala.quoted.staging._ | ||
|
||
object Test { | ||
given Toolbox = Toolbox.make(getClass.getClassLoader) | ||
def main(args: Array[String]): Unit = withQuoteContext { | ||
println(powerCode(77).show) | ||
} | ||
|
||
def powerCode(n: Long)(given QuoteContext): Expr[Double => Double] = | ||
'{ x => ${powerCode(n, 2, 'x)} } | ||
|
||
def powerCode(n: Long, idx: Int, x: Expr[Double])(given QuoteContext): Expr[Double] = | ||
if (n == 0) '{1.0} | ||
else if (n == 1) x | ||
else if (n % 2 == 0) let('{ $x * $x })(y => powerCode(n / 2, idx * 2, y)) | ||
else '{ $x * ${powerCode(n - 1, idx, x)} } | ||
|
||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
((x1: scala.Double) => x1.*({ | ||
val x2: scala.Double = x1.*(x1) | ||
val x4: scala.Double = x2.*(x2) | ||
x4.*({ | ||
val x8: scala.Double = x4.*(x4) | ||
x8.*({ | ||
val x16: scala.Double = x8.*(x8) | ||
val x32: scala.Double = x16.*(x16) | ||
val x64: scala.Double = x32.*(x32) | ||
x64 | ||
}) | ||
}) | ||
})) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import scala.quoted._ | ||
import scala.quoted.util.let | ||
import scala.quoted.staging._ | ||
|
||
object Test { | ||
given Toolbox = Toolbox.make(getClass.getClassLoader) | ||
def main(args: Array[String]): Unit = withQuoteContext { | ||
println(powerCode(77).show) | ||
} | ||
|
||
def powerCode(n: Long)(given QuoteContext): Expr[Double => Double] = | ||
'{ x1 => ${powerCode(n, 2, 'x1)} } | ||
|
||
def powerCode(n: Long, idx: Int, x: Expr[Double])(given QuoteContext): Expr[Double] = | ||
if (n == 0) '{1.0} | ||
else if (n == 1) x | ||
else if (n % 2 == 0) let(s"x$idx")('{ $x * $x })(y => powerCode(n / 2, idx * 2, y)) | ||
else '{ $x * ${powerCode(n - 1, idx, x)} } | ||
|
||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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 reuse FreshNameCreator from the compiler?
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.
It would have a global indexing which would make any debugging harder as it would keep undesired state from one macro expansion to the other.
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.
If a macro contains many usage of
let
, then it faces the same problem.FreshNameCreator
is better in the sense that it keeps different counters for different names, instead of using one global counter.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.
But it uses a global counter across all files in the compilation run and each macro expansion site which is what we are trying to avoid.
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.
It's possible to call
def setFreshNames(freshNames: FreshNameCreator)
onContext
before macro expansion.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 a could try to create a fresh
FreshNameCreator
for each macro expansion site.