Skip to content

Commit 63a5a62

Browse files
Merge pull request #8964 from dotty-staging/fix-##8945
Fix #8945: Support macro bundles
2 parents 9450bf2 + b76d01d commit 63a5a62

File tree

2 files changed

+53
-10
lines changed

2 files changed

+53
-10
lines changed

compiler/src/dotty/tools/dotc/typer/Typer.scala

Lines changed: 26 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3492,15 +3492,31 @@ class Typer extends Namer
34923492
/** Types the body Scala 2 macro declaration `def f = macro <body>` */
34933493
private def typedScala2MacroBody(call: untpd.Tree)(using Context): Tree =
34943494
// TODO check that call is to a method with valid signature
3495-
call match
3496-
case rhs0: untpd.Ident =>
3497-
typedIdent(rhs0, defn.AnyType)
3498-
case rhs0: untpd.Select =>
3499-
typedSelect(rhs0, defn.AnyType)
3500-
case rhs0: untpd.TypeApply =>
3501-
typedTypeApply(rhs0, defn.AnyType)
3502-
case _ =>
3503-
ctx.error("Invalid Scala 2 macro", call.sourcePos)
3504-
EmptyTree
3495+
def typedPrefix(tree: untpd.RefTree): Tree = {
3496+
tryAlternatively {
3497+
typedExpr(tree, defn.AnyType)
3498+
} {
3499+
// Try to type as a macro bundle
3500+
val ref = tree match
3501+
case Ident(name) => untpd.Ident(name.toTypeName).withSpan(tree.span)
3502+
case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span)
3503+
val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span)
3504+
typedExpr(bundle, defn.AnyType)
3505+
}
3506+
}
3507+
if ctx.phase.isTyper then
3508+
call match
3509+
case call: untpd.Ident =>
3510+
typedIdent(call, defn.AnyType)
3511+
case untpd.Select(qual: untpd.RefTree, name) =>
3512+
val call2 = untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name).withSpan(call.span)
3513+
typedSelect(call2, defn.AnyType)
3514+
case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) =>
3515+
val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name), targs).withSpan(call.span)
3516+
typedTypeApply(call2, defn.AnyType)
3517+
case _ =>
3518+
ctx.error("Invalid Scala 2 macro " + call.show, call.sourcePos)
3519+
EmptyTree
3520+
else typedExpr(call, defn.AnyType)
35053521

35063522
}

tests/pos/i8945.scala

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// src-2/MacroImpl.scala
2+
trait Context {
3+
object universe {
4+
type Literal
5+
}
6+
}
7+
8+
class MacroImpl(val c: Context) {
9+
import c.universe._
10+
def mono: Literal = ???
11+
}
12+
13+
// src-3/Macros.scala
14+
import scala.language.experimental.macros
15+
16+
object Macros {
17+
18+
object Bundles {
19+
def mono: Unit = macro MacroImpl.mono
20+
inline def mono: Unit = ${ Macros3.monoImpl }
21+
}
22+
23+
object Macros3 {
24+
def monoImpl(using quoted.QuoteContext) = '{()}
25+
}
26+
27+
}

0 commit comments

Comments
 (0)