Skip to content

Fix macro detection #6805

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
Jul 4, 2019
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1027,7 +1027,7 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(implicit ctx: Context) {

override def typedApply(tree: untpd.Apply, pt: Type)(implicit ctx: Context): Tree = {
constToLiteral(betaReduce(super.typedApply(tree, pt))) match {
case res: Apply if res.symbol == defn.InternalQuoted_exprSplice && level == 0 =>
case res: Apply if res.symbol == defn.InternalQuoted_exprSplice && level == 0 && call.symbol.is(Macro) =>
expandMacro(res.args.head, tree.span)
case res => res
}
Expand Down
71 changes: 28 additions & 43 deletions compiler/src/dotty/tools/dotc/typer/PrepareInlineable.scala
Original file line number Diff line number Diff line change
Expand Up @@ -245,51 +245,36 @@ object PrepareInlineable {
}

def checkInlineMacro(sym: Symbol, rhs: Tree, pos: SourcePosition)(implicit ctx: Context) = {
if (!ctx.isAfterTyper) {
var isMacro = false
new TreeMapWithStages(freshStagingContext) {
override protected def transformSplice(body: tpd.Tree, splice: tpd.Tree)(implicit ctx: Context): tpd.Tree = {
isMacro = true
splice
}
override def transform(tree: tpd.Tree)(implicit ctx: Context): tpd.Tree =
if (isMacro) tree else super.transform(tree)
}.transform(rhs)

if (isMacro) {
sym.setFlag(Macro)
if (level == 0) {
def isValidMacro(tree: Tree)(implicit ctx: Context): Unit = tree match {
case Spliced(code) =>
if (code.symbol.flags.is(Inline))
ctx.error("Macro cannot be implemented with an `inline` method", code.sourcePos)
Splicer.checkValidMacroBody(code)
new PCPCheckAndHeal(freshStagingContext).transform(rhs) // Ignore output, only check PCP
if (sym.is(Macro) && !ctx.isAfterTyper) {
def isValidMacro(tree: Tree)(implicit ctx: Context): Unit = tree match {
case Spliced(code) =>
if (code.symbol.flags.is(Inline))
ctx.error("Macro cannot be implemented with an `inline` method", code.sourcePos)
Splicer.checkValidMacroBody(code)
new PCPCheckAndHeal(freshStagingContext).transform(rhs) // Ignore output, only check PCP

case Block(List(stat), Literal(Constants.Constant(()))) => isValidMacro(stat)
case Block(Nil, expr) => isValidMacro(expr)
case Typed(expr, _) => isValidMacro(expr)
case Block(DefDef(nme.ANON_FUN, _, _, _, _) :: Nil, Closure(_, fn, _)) if fn.symbol.info.isImplicitMethod =>
// TODO Suppot this pattern
ctx.error(
"""Macros using a return type of the form `foo(): given X => Y` are not yet supported.
|
|Place the implicit as an argument (`foo() given X: Y`) to overcome this limitation.
|""".stripMargin, tree.sourcePos)
case _ =>
ctx.error(
"""Malformed macro.
|
|Expected the splice ${...} to be at the top of the RHS:
| inline def foo(inline x: X, ..., y: Y): Int = ${impl(x, ... '{y}})
|
| * The contents of the splice must call a static method
| * All arguments must be quoted or inline
""".stripMargin, pos)
}
isValidMacro(rhs)
}
case Block(List(stat), Literal(Constants.Constant(()))) => isValidMacro(stat)
case Block(Nil, expr) => isValidMacro(expr)
case Typed(expr, _) => isValidMacro(expr)
case Block(DefDef(nme.ANON_FUN, _, _, _, _) :: Nil, Closure(_, fn, _)) if fn.symbol.info.isImplicitMethod =>
// TODO Suppot this pattern
ctx.error(
"""Macros using a return type of the form `foo(): given X => Y` are not yet supported.
|
|Place the implicit as an argument (`foo() given X: Y`) to overcome this limitation.
|""".stripMargin, tree.sourcePos)
case _ =>
ctx.error(
"""Malformed macro.
|
|Expected the splice ${...} to be at the top of the RHS:
| inline def foo(inline x: X, ..., y: Y): Int = ${impl(x, ... '{y}})
|
| * The contents of the splice must call a static method
| * All arguments must be quoted or inline
""".stripMargin, pos)
}
isValidMacro(rhs)
}
}

Expand Down
15 changes: 15 additions & 0 deletions tests/neg/inline-quote.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import scala.quoted._

object Test {

inline def foo(x: Expr[Int]) given QuoteContext: Expr[Int] = '{ // error
println("foo")
${
${??? : Expr[Int]}

x
}
}


}
4 changes: 4 additions & 0 deletions tests/run-with-compiler/inline-quote.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
scala.Predef.println("foo")
45
}
17 changes: 17 additions & 0 deletions tests/run-with-compiler/inline-quote.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import scala.quoted._

object Test {

inline def foo(x: Expr[Int]) given QuoteContext: Expr[Int] = '{
println("foo")
$x
}

implicit val toolbox: scala.quoted.Toolbox = scala.quoted.Toolbox.make(getClass.getClassLoader)

def main(args: Array[String]): Unit = withQuoteContext {
val y = '{45}
println(foo(y).show)
}

}