From 9220a244fbe3ef7147d3c975f5635f01e4ac9cfb Mon Sep 17 00:00:00 2001 From: Anatolii Date: Mon, 14 Oct 2019 13:49:33 +0200 Subject: [PATCH 1/2] Fix #7407: Synthesise a quote context only for macros --- compiler/src/dotty/tools/dotc/core/Flags.scala | 2 +- compiler/src/dotty/tools/dotc/core/TypeOps.scala | 3 +++ compiler/src/dotty/tools/dotc/typer/Implicits.scala | 2 +- tests/neg/i7407.scala | 2 ++ 4 files changed, 7 insertions(+), 2 deletions(-) create mode 100644 tests/neg/i7407.scala diff --git a/compiler/src/dotty/tools/dotc/core/Flags.scala b/compiler/src/dotty/tools/dotc/core/Flags.scala index ce4a40b2248a..af0db39f8304 100644 --- a/compiler/src/dotty/tools/dotc/core/Flags.scala +++ b/compiler/src/dotty/tools/dotc/core/Flags.scala @@ -442,7 +442,7 @@ object Flags { Scala2ExistentialCommon, Mutable, Opaque, Touched, JavaStatic, OuterOrCovariant, LabelOrContravariant, CaseAccessor, Extension, NonMember, Implicit, Given, Permanent, Synthetic, - SuperAccessorOrScala2x, Inline) + SuperAccessorOrScala2x, Inline, Macro) /** Flags that are not (re)set when completing the denotation, or, if symbol is * a top-level class or object, when completing the denotation once the class diff --git a/compiler/src/dotty/tools/dotc/core/TypeOps.scala b/compiler/src/dotty/tools/dotc/core/TypeOps.scala index 15c3537de67c..f0a710ea8b17 100644 --- a/compiler/src/dotty/tools/dotc/core/TypeOps.scala +++ b/compiler/src/dotty/tools/dotc/core/TypeOps.scala @@ -457,6 +457,9 @@ trait TypeOps { this: Context => // TODO: Make standalone object. /** Are we in an inline method body? */ def inInlineMethod: Boolean = owner.ownersIterator.exists(_.isInlineMethod) + /** Are we in a macro? */ + def inMacro: Boolean = owner.ownersIterator.exists(s => s.isInlineMethod && s.is(Macro)) + /** Is `feature` enabled in class `owner`? * This is the case if one of the following two alternatives holds: * diff --git a/compiler/src/dotty/tools/dotc/typer/Implicits.scala b/compiler/src/dotty/tools/dotc/typer/Implicits.scala index f740aa6fd072..5e8449e66dc8 100644 --- a/compiler/src/dotty/tools/dotc/typer/Implicits.scala +++ b/compiler/src/dotty/tools/dotc/typer/Implicits.scala @@ -729,7 +729,7 @@ trait Implicits { self: Typer => lazy val synthesizedQuoteContext: SpecialHandler = (formal, span) => implicit ctx => - if (ctx.inInlineMethod || enclosingInlineds.nonEmpty) ref(defn.QuoteContext_macroContext) + if (ctx.inMacro || enclosingInlineds.nonEmpty) ref(defn.QuoteContext_macroContext) else EmptyTree lazy val synthesizedTupleFunction: SpecialHandler = diff --git a/tests/neg/i7407.scala b/tests/neg/i7407.scala new file mode 100644 index 000000000000..03823eb22e68 --- /dev/null +++ b/tests/neg/i7407.scala @@ -0,0 +1,2 @@ +def qc(given ctx: scala.quoted.QuoteContext) = println(ctx) +inline def g = qc // error: no implicit argument From d37aa554306420a2cde6c688e818dd1719fddab6 Mon Sep 17 00:00:00 2001 From: Anatolii Date: Mon, 14 Oct 2019 16:02:21 +0200 Subject: [PATCH 2/2] Fix #6739 test There was an error in that test where you need a QuoteContext in scope to write quotes. The error did not manifest itself since a synthetic QuoteContext was generated for all inline methods from which it was required (disregarding whether they are meant as macro implementations or not). This behavior was wrong, however, (see #7407) and hence this test needs fixing. --- tests/neg/i6739.scala | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/neg/i6739.scala b/tests/neg/i6739.scala index 2852394d11d5..b961e969d7eb 100644 --- a/tests/neg/i6739.scala +++ b/tests/neg/i6739.scala @@ -3,4 +3,4 @@ import scala.quoted._ inline def assert(expr: => Boolean): Unit = ${ assertImpl('expr) } // error: Macro cannot be implemented with an `inline` method -inline def assertImpl(expr: Expr[Boolean]): Expr[Unit] = '{ println("Hello World") } +inline def assertImpl(expr: Expr[Boolean])(given QuoteContext): Expr[Unit] = '{ println("Hello World") }