Skip to content

Add a val in Scala 2 macro bundles #9098

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 5 commits into from
Jun 6, 2020
Merged
Changes from 4 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
20 changes: 13 additions & 7 deletions compiler/src/dotty/tools/dotc/typer/Typer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3565,28 +3565,34 @@ 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
def typedPrefix(tree: untpd.RefTree): Tree = {
def typedPrefix(tree: untpd.RefTree)(splice: Context ?=> Tree => Tree)(using Context): Tree = {
tryAlternatively {
typedExpr(tree, defn.AnyType)
splice(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)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This .withSpan(call.span) should probably be around the tpd.Block

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

perhaps I overdid it?,

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed some of them. Could you check it works with your example?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure whats happening because the CI is passing but testCompilation tests/pos/i8945.scala fails for me

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

wait CI is still running

Copy link
Member

@bishabosha bishabosha Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.withSpan still needs to be added to bundleVal - I'll do it

Copy link
Member

@bishabosha bishabosha Jun 5, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@nicolasstucki the commit I just added also works with my scala 2 testing bd066e1

typedExpr(bundle, defn.AnyType)
val bundle1 = typedExpr(bundle, defn.AnyType)
val bundleVal = SyntheticValDef(NameKinds.UniqueName.fresh("bundle".toTermName), bundle1)
tpd.Block(List(bundleVal), splice(tpd.ref(bundleVal.symbol))).withSpan(call.span)
}
}
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)
typedPrefix(qual) { qual =>
val call2 = untpd.Select(untpd.TypedSplice(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)
typedPrefix(qual) { qual =>
val call2= untpd.TypeApply(untpd.Select(untpd.TypedSplice(qual), name), targs).withSpan(call.span)
typedTypeApply(call2, defn.AnyType)
}
case _ =>
ctx.error("Invalid Scala 2 macro " + call.show, call.sourcePos)
EmptyTree
Expand Down