Skip to content

Commit 31879e5

Browse files
Merge pull request #9098 from dotty-staging/scala2-macro-bundle-val
Add a val in Scala 2 macro bundles
2 parents 2fbefb0 + bd066e1 commit 31879e5

File tree

2 files changed

+14
-7
lines changed

2 files changed

+14
-7
lines changed

compiler/src/dotty/tools/dotc/core/StdNames.scala

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -425,6 +425,7 @@ object StdNames {
425425
val assume_ : N = "assume"
426426
val box: N = "box"
427427
val build : N = "build"
428+
val bundle: N = "bundle"
428429
val bytes: N = "bytes"
429430
val canEqual_ : N = "canEqual"
430431
val cbnArg: N = "<cbn-arg>"

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

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3566,28 +3566,34 @@ class Typer extends Namer
35663566
/** Types the body Scala 2 macro declaration `def f = macro <body>` */
35673567
private def typedScala2MacroBody(call: untpd.Tree)(using Context): Tree =
35683568
// TODO check that call is to a method with valid signature
3569-
def typedPrefix(tree: untpd.RefTree): Tree = {
3569+
def typedPrefix(tree: untpd.RefTree)(splice: Context ?=> Tree => Tree)(using Context): Tree = {
35703570
tryAlternatively {
3571-
typedExpr(tree, defn.AnyType)
3571+
splice(typedExpr(tree, defn.AnyType))
35723572
} {
35733573
// Try to type as a macro bundle
35743574
val ref = tree match
35753575
case Ident(name) => untpd.Ident(name.toTypeName).withSpan(tree.span)
35763576
case Select(qual, name) => untpd.Select(qual, name.toTypeName).withSpan(tree.span)
35773577
val bundle = untpd.Apply(untpd.Select(untpd.New(ref), nme.CONSTRUCTOR), untpd.Literal(Constant(null))).withSpan(call.span)
3578-
typedExpr(bundle, defn.AnyType)
3578+
val bundle1 = typedExpr(bundle, defn.AnyType)
3579+
val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh(nme.bundle), bundle1).withSpan(call.span)
3580+
tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))).withSpan(call.span)
35793581
}
35803582
}
35813583
if ctx.phase.isTyper then
35823584
call match
35833585
case call: untpd.Ident =>
35843586
typedIdent(call, defn.AnyType)
35853587
case untpd.Select(qual: untpd.RefTree, name) =>
3586-
val call2 = untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name).withSpan(call.span)
3587-
typedSelect(call2, defn.AnyType)
3588+
typedPrefix(qual) { qual =>
3589+
val call2 = untpd.Select(untpd.TypedSplice(qual), name).withSpan(call.span)
3590+
typedSelect(call2, defn.AnyType)
3591+
}
35883592
case untpd.TypeApply(untpd.Select(qual: untpd.RefTree, name), targs) =>
3589-
val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(typedPrefix(qual)), name), targs).withSpan(call.span)
3590-
typedTypeApply(call2, defn.AnyType)
3593+
typedPrefix(qual) { qual =>
3594+
val call2 = untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span)
3595+
typedTypeApply(call2, defn.AnyType)
3596+
}
35913597
case _ =>
35923598
ctx.error("Invalid Scala 2 macro " + call.show, call.sourcePos)
35933599
EmptyTree

0 commit comments

Comments
 (0)