-
Notifications
You must be signed in to change notification settings - Fork 1.1k
Use simpler desugaring for splices #8660
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
Merged
nicolasstucki
merged 2 commits into
scala:master
from
dotty-staging:simple-splice-desugaring
Apr 7, 2020
Merged
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
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 |
---|---|---|
|
@@ -53,7 +53,7 @@ trait QuotesAndSplices { | |
ctx.error(em"Quotes require stable QuoteContext, but found non stable $qctx", qctx.sourcePos) | ||
|
||
val tree1 = | ||
if ctx.mode.is(Mode.Pattern) && level == 0 then | ||
if ctx.mode.is(Mode.Pattern) then | ||
typedQuotePattern(tree, pt, qctx) | ||
else if (tree.quoted.isType) | ||
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuote.termRef), tree.quoted :: Nil), pt)(using quoteContext) | ||
|
@@ -72,7 +72,7 @@ trait QuotesAndSplices { | |
ctx.warning("Canceled quote directly inside a splice. ${ '{ XYZ } } is equivalent to XYZ.", tree.sourcePos) | ||
case _ => | ||
} | ||
if (ctx.mode.is(Mode.QuotedPattern) && level == 1) | ||
if (ctx.mode.is(Mode.QuotedPattern)) | ||
nicolasstucki marked this conversation as resolved.
Show resolved
Hide resolved
|
||
if (isFullyDefined(pt, ForceDegree.all)) { | ||
def spliceOwner(ctx: Context): Symbol = | ||
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner | ||
|
@@ -99,24 +99,10 @@ trait QuotesAndSplices { | |
|
||
val (outerQctx, ctx1) = popQuoteContext() | ||
|
||
// Explicitly provide the given QuoteContext of the splice. | ||
// * Avoids leaking implementation details of scala.internal.quoted.CompileTime.exprSplice, | ||
// such as exprSplice taking a ?=> function argument | ||
// * Provide meaningful names for QuoteContext synthesized by within `${ ... }` | ||
// * If within a quote, provide a QuoteContext is linked typewise with the outer QuoteContext | ||
val qctxParamName = NameKinds.UniqueName.fresh(s"qctx${if level > 0 then level - 1 else ""}_".toTermName) | ||
val qctxParamTpe = outerQctx match { | ||
case Some(qctxRef) => qctxRef.tpe.select("NestedContext".toTypeName) | ||
case _ => defn.QuoteContextClass.typeRef // splice at level 0 (or lower) | ||
} | ||
val qctxParamTpt = untpd.TypedSplice(TypeTree(qctxParamTpe)) | ||
val qctxParam = untpd.makeParameter(qctxParamName, qctxParamTpt, untpd.Modifiers(Given)) | ||
val expr = untpd.Function(List(qctxParam), tree.expr).withSpan(tree.span) | ||
|
||
val internalSplice = | ||
outerQctx match | ||
case Some(qctxRef) => untpd.Apply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprNestedSplice.termRef), qctxRef), expr) | ||
case _ => untpd.Apply(untpd.ref(defn.InternalQuoted_exprSplice.termRef), expr) | ||
case Some(qctxRef) => untpd.Apply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprNestedSplice.termRef), qctxRef), tree.expr) | ||
case _ => untpd.Apply(untpd.ref(defn.InternalQuoted_exprSplice.termRef), tree.expr) | ||
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. Nice simplification 👍 |
||
|
||
typedApply(internalSplice, pt)(using ctx1).withSpan(tree.span) | ||
} | ||
|
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,7 @@ | ||
import scala.quoted._ | ||
|
||
def f(a: Expr[Int])(using qctx: QuoteContext): Unit = | ||
|
||
'{ val x: Int = ${ (using qctx2) => a } } | ||
|
||
'{ val x: Int = ${ (using qctx2: qctx.NestedContext) => a } } |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
((qctx: scala.quoted.QuoteContext) ?=> { | ||
val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx) | ||
((qctx1_$1: qctx.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx) | ||
((evidence$2: qctx.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx) | ||
}) |
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 |
---|---|---|
@@ -1,4 +1,4 @@ | ||
((qctx: scala.quoted.QuoteContext) ?=> { | ||
val a: scala.quoted.Expr[scala.Int] = scala.internal.quoted.CompileTime.exprQuote[scala.Int](4).apply(using qctx) | ||
((qctx2: scala.quoted.QuoteContext) ?=> ((qctx1_$1: qctx2.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx2)).apply(using qctx) | ||
((qctx2: scala.quoted.QuoteContext) ?=> ((evidence$3: qctx2.NestedContext) ?=> a).asInstanceOf[scala.ContextFunction1[scala.quoted.QuoteContext, scala.quoted.Expr[scala.Int]]].apply(using qctx2)).apply(using qctx) | ||
}) |
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.
Why remove
level == 0
?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.
Because if we are in a pattern and we go in the other branch we might generate some non-pattern trees (after some error has been emitted) which leads to crashes later in typer.
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.
tests/neg/i8052.scala
had this issue. With this change, the error message is also more informative.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
level == 0
is a correct check, it seems better to keep it, and fix crash & error message elsewhere. The reason is that we don't know whether the removal introduces crashes for more ill-formed programs or not.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.
The point is that is was not a correct check. The level checks happen later in the Staging phase.