@@ -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