From 717a65a8eacd7b5acee481b65275a223f67872a6 Mon Sep 17 00:00:00 2001 From: Nicolas Stucki Date: Wed, 10 Jun 2020 09:18:24 +0200 Subject: [PATCH] Fix #7716: Always take prefix into account --- compiler/src/dotty/tools/dotc/ast/tpd.scala | 2 +- .../dotty/tools/dotc/transform/Splicer.scala | 3 ++- tests/run-macros/i7716.check | 3 +++ tests/run-macros/i7716/Macro_1.scala | 17 +++++++++++++++++ tests/run-macros/i7716/Test_2.scala | 5 +++++ 5 files changed, 28 insertions(+), 2 deletions(-) create mode 100644 tests/run-macros/i7716.check create mode 100644 tests/run-macros/i7716/Macro_1.scala create mode 100644 tests/run-macros/i7716/Test_2.scala diff --git a/compiler/src/dotty/tools/dotc/ast/tpd.scala b/compiler/src/dotty/tools/dotc/ast/tpd.scala index c23f81fb465a..df96a1dc7cb1 100644 --- a/compiler/src/dotty/tools/dotc/ast/tpd.scala +++ b/compiler/src/dotty/tools/dotc/ast/tpd.scala @@ -1244,7 +1244,7 @@ object tpd extends Trees.Instance[Type] with TypedTreeInfo { def sourceFile(call: Tree)(implicit ctx: Context): SourceFile = call.symbol.source /** Desugar identifier into a select node. Return the tree itself if not possible */ - def desugarIdent(tree: Ident)(implicit ctx: Context): Tree = { + def desugarIdent(tree: Ident)(implicit ctx: Context): RefTree = { val qual = desugarIdentPrefix(tree) if (qual.isEmpty) tree else qual.select(tree.symbol) diff --git a/compiler/src/dotty/tools/dotc/transform/Splicer.scala b/compiler/src/dotty/tools/dotc/transform/Splicer.scala index d33038d2018a..d53e4be69849 100644 --- a/compiler/src/dotty/tools/dotc/transform/Splicer.scala +++ b/compiler/src/dotty/tools/dotc/transform/Splicer.scala @@ -495,7 +495,8 @@ object Splicer { def unapply(arg: Tree)(implicit ctx: Context): Option[(RefTree, List[List[Tree]])] = arg match { case Select(Call0(fn, args), nme.apply) if defn.isContextFunctionType(fn.tpe.widenDealias.finalResultType) => Some((fn, args)) - case fn: RefTree => Some((fn, Nil)) + case fn: Ident => Some((tpd.desugarIdent(fn).withSpan(fn.span), Nil)) + case fn: Select => Some((fn, Nil)) case Apply(f @ Call0(fn, args1), args2) => if (f.tpe.widenDealias.isErasedMethod) Some((fn, args1)) else Some((fn, args2 :: args1)) diff --git a/tests/run-macros/i7716.check b/tests/run-macros/i7716.check new file mode 100644 index 000000000000..20894538dafa --- /dev/null +++ b/tests/run-macros/i7716.check @@ -0,0 +1,3 @@ +Hello 1 +Hello 2 +Hello 3 diff --git a/tests/run-macros/i7716/Macro_1.scala b/tests/run-macros/i7716/Macro_1.scala new file mode 100644 index 000000000000..10f8436c8171 --- /dev/null +++ b/tests/run-macros/i7716/Macro_1.scala @@ -0,0 +1,17 @@ +import scala.quoted._ + +trait Foo: + def mcrImpl1(e: Expr[Any])(using ctx: QuoteContext): Expr[Any] = + '{println(s"Hello ${$e}")} + +object Foo extends Foo: + def mcrImpl2(e: Expr[Any])(using ctx: QuoteContext): Expr[Any] = + '{println(s"Hello ${$e}")} + +object Bar: + import Foo._ + inline def mcr1(e: => Any) = ${mcrImpl1('e)} + + inline def mcr2(e: => Any) = ${Foo.mcrImpl1('e)} + + inline def mcr3(e: => Any) = ${mcrImpl2('e)} diff --git a/tests/run-macros/i7716/Test_2.scala b/tests/run-macros/i7716/Test_2.scala new file mode 100644 index 000000000000..9681fa673045 --- /dev/null +++ b/tests/run-macros/i7716/Test_2.scala @@ -0,0 +1,5 @@ +object Test extends App { + Bar.mcr1(1) + Bar.mcr2(2) + Bar.mcr3(3) +}