diff --git a/compiler/src/dotty/tools/dotc/typer/Typer.scala b/compiler/src/dotty/tools/dotc/typer/Typer.scala index ddb7b7b5e6db..e453b796204f 100644 --- a/compiler/src/dotty/tools/dotc/typer/Typer.scala +++ b/compiler/src/dotty/tools/dotc/typer/Typer.scala @@ -3446,15 +3446,31 @@ class Typer extends Namer /** Types the body Scala 2 macro declaration `def f = macro ` */ private def typedScala2MacroBody(call: untpd.Tree)(using Context): Tree = // TODO check that call is to a method with valid signature - call match - case rhs0: untpd.Ident => - typedIdent(rhs0, defn.AnyType) - case rhs0: untpd.Select => - typedSelect(rhs0, defn.AnyType) - case rhs0: untpd.TypeApply => - typedTypeApply(rhs0, defn.AnyType) - case _ => - ctx.error("Invalid Scala 2 macro", call.sourcePos) - EmptyTree + def typedPrefix(tree: untpd.RefTree): Tree = { + tryAlternatively { + typedExpr(tree, defn.AnyType) + } { + // Try to type as a macro bundle + val ref = tree match + case Ident(name) => untpd.Ident(name.toTypeName).withSpan(tree.span) + case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span) + val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span) + typedExpr(bundle, defn.AnyType) + } + } + if ctx.phase.isTyper then + call match + case call: untpd.Ident => + typedIdent(call, defn.AnyType) + case untpd.Select(qual: untpd.RefTree, name) => + val call2 = untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name).withSpan(call.span) + typedSelect(call2, defn.AnyType) + case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) => + val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name), targs).withSpan(call.span) + typedTypeApply(call2, defn.AnyType) + case _ => + ctx.error("Invalid Scala 2 macro " + call.show, call.sourcePos) + EmptyTree + else typedExpr(call, defn.AnyType) } diff --git a/tests/pos/i8945.scala b/tests/pos/i8945.scala new file mode 100644 index 000000000000..d0873a4496b2 --- /dev/null +++ b/tests/pos/i8945.scala @@ -0,0 +1,27 @@ +// src-2/MacroImpl.scala +trait Context { + object universe { + type Literal + } +} + +class MacroImpl(val c: Context) { + import c.universe._ + def mono: Literal = ??? +} + +// src-3/Macros.scala +import scala.language.experimental.macros + +object Macros { + + object Bundles { + def mono: Unit = macro MacroImpl.mono + inline def mono: Unit = ${ Macros3.monoImpl } + } + + object Macros3 { + def monoImpl(using quoted.QuoteContext) = '{()} + } + +} \ No newline at end of file