Skip to content

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
merged 2 commits into from
Apr 7, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 4 additions & 18 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why remove level == 0?

Copy link
Contributor Author

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.

Copy link
Contributor Author

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.

Copy link
Contributor

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.

Copy link
Contributor Author

@nicolasstucki nicolasstucki Apr 6, 2020

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.

typedQuotePattern(tree, pt, qctx)
else if (tree.quoted.isType)
typedTypeApply(untpd.TypeApply(untpd.ref(defn.InternalQuoted_typeQuote.termRef), tree.quoted :: Nil), pt)(using quoteContext)
Expand All @@ -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))
if (isFullyDefined(pt, ForceDegree.all)) {
def spliceOwner(ctx: Context): Symbol =
if (ctx.mode.is(Mode.QuotedPattern)) spliceOwner(ctx.outer) else ctx.owner
Expand All @@ -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)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice simplification 👍


typedApply(internalSplice, pt)(using ctx1).withSpan(tree.span)
}
Expand Down
7 changes: 7 additions & 0 deletions tests/pos/splice-with-explicit-context.scala
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 } }
2 changes: 1 addition & 1 deletion tests/run-staging/quote-nested-2.check
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)
})
2 changes: 1 addition & 1 deletion tests/run-staging/quote-nested-5.check
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)
})