From 51a15d66fcf4a27d4567699b1d1c3f04f94e7ba3 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Fri, 7 Feb 2020 17:10:45 +0100 Subject: [PATCH 1/2] 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 `${ ... }` --- .../dotty/tools/dotc/typer/QuotesAndSplices.scala | 15 ++++++++++++++- tests/run-staging/quote-nested-2.check | 2 +- tests/run-staging/quote-nested-5.check | 2 +- 3 files changed, 16 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index 37532d2a9c22..3dfc96457952 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -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 reacived + // If encoloseing quote recieves `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) } } diff --git a/tests/run-staging/quote-nested-2.check b/tests/run-staging/quote-nested-2.check index 8afb56fb9d78..dcffb43a90a5 100644 --- a/tests/run-staging/quote-nested-2.check +++ b/tests/run-staging/quote-nested-2.check @@ -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) }) diff --git a/tests/run-staging/quote-nested-5.check b/tests/run-staging/quote-nested-5.check index fd7b1485df13..0e26248287d1 100644 --- a/tests/run-staging/quote-nested-5.check +++ b/tests/run-staging/quote-nested-5.check @@ -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) }) From 5c5514620226d0642d1945492fd0934cb53edb73 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Sun, 9 Feb 2020 12:06:09 +0100 Subject: [PATCH 2/2] Apply suggestions from code review Co-Authored-By: Fengyun Liu --- compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala index 3dfc96457952..e91687dbe344 100644 --- a/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala +++ b/compiler/src/dotty/tools/dotc/typer/QuotesAndSplices.scala @@ -90,8 +90,8 @@ trait QuotesAndSplices { // * 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 reacived - // If encoloseing quote recieves `qctx` then this type should be `QuoteContext { val tasty: qxtx.tasty.type }` + // 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) @@ -392,4 +392,3 @@ trait QuotesAndSplices { proto = quoteClass.typeRef.appliedTo(replaceBindings(quoted1.tpe) & quotedPt)) } } -