Skip to content

Fix #8945: Support macro bundles #8964

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 18, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 26 additions & 10 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3446,15 +3446,31 @@ class Typer extends Namer
/** Types the body Scala 2 macro declaration `def f = macro <body>` */
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)

}
27 changes: 27 additions & 0 deletions tests/pos/i8945.scala
Original file line number Diff line number Diff line change
@@ -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) = '{()}
}

}