Skip to content

Explicitly provide the given QuoteContext of the splice #8244

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
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
16 changes: 14 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,20 @@ trait QuotesAndSplices {
else if (!c.outer.owner.is(Package)) markAsMacro(c.outer)
markAsMacro(ctx)
}
typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprSplice.termRef), tree.expr), pt)(spliceContext).withSpan(tree.span)

// 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 `${ ... }`
// * TODO preserve QuoteContext.tasty path dependent type (see comment below and #8045)
val qctxParamName = NameKinds.UniqueName.fresh(s"qctx${level - 1}_".toTermName)
// TODO: Refine QuoteContext with the tasty context that the quote received
// If encoloseing quote receives `qctx` then this type should be `QuoteContext { val tasty: qxtx.tasty.type }`
val qctxParamTpt = untpd.TypedSplice(TypeTree(defn.QuoteContextClass.typeRef))
val qctxParam = untpd.makeParameter(qctxParamName, qctxParamTpt, untpd.Modifiers(Given))
val expr = untpd.Function(List(qctxParam), tree.expr).withSpan(tree.span)

typedApply(untpd.Apply(untpd.ref(defn.InternalQuoted_exprSplice.termRef), expr), pt)(spliceContext).withSpan(tree.span)
}
}

Expand Down Expand Up @@ -379,4 +392,3 @@ trait QuotesAndSplices {
proto = quoteClass.typeRef.appliedTo(replaceBindings(quoted1.tpe) & quotedPt))
}
}

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) given (qctx)
((evidence$2: scala.quoted.QuoteContext) => a) given (qctx)
((qctx1_$1: scala.quoted.QuoteContext) => a) given (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) given (qctx)
((qctx2: scala.quoted.QuoteContext) => ((evidence$3: scala.quoted.QuoteContext) => a) given (qctx2)) given (qctx)
((qctx2: scala.quoted.QuoteContext) => ((qctx1_$1: scala.quoted.QuoteContext) => a) given (qctx2)) given (qctx)
})