Skip to content

Fix #8866: Inline calls inserted by macros #8870

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
Sep 11, 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
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1239,7 +1239,8 @@ class Inliner(call: tpd.Tree, rhsToInline: tpd.Tree)(using Context) {
case res: Apply if res.symbol == defn.InternalQuoted_exprSplice
&& level == 0
&& !suppressInline =>
expandMacro(res.args.head, tree.span)
val expanded = expandMacro(res.args.head, tree.span)
typedExpr(expanded) // Inline calls and constant fold code generated by the macro
case res => res
}

Expand Down
27 changes: 27 additions & 0 deletions tests/pos-macros/i8866/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import scala.quoted._

object OtherMacro {

def impl(using qctx: QuoteContext): Expr[Int] =
'{ 42 }

inline def apply = ${ OtherMacro.impl }

}

object Macro {

def impl(using qctx: QuoteContext): Expr[Int] = {
import qctx.tasty._

let(
Select.unique(
'{ OtherMacro }.unseal,
"apply"
)
)(identity).seal.cast[Int]
}

inline def apply = ${ Macro.impl }

}
1 change: 1 addition & 0 deletions tests/pos-macros/i8866/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val a = Macro.apply
23 changes: 23 additions & 0 deletions tests/pos-macros/i8866b/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import scala.quoted._

object Other {
inline def apply = 5
}

object Macro {

def impl(using qctx: QuoteContext): Expr[Int] = {
import qctx.tasty._

let(
Select.unique(
'{ Other }.unseal,
"apply"
)
)(identity).seal.cast[Int]

}

inline def apply = ${ Macro.impl }

}
1 change: 1 addition & 0 deletions tests/pos-macros/i8866b/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val a = Macro.apply
12 changes: 12 additions & 0 deletions tests/pos-macros/i8866c/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
import scala.quoted._

def f(xs: Boolean*): Unit = ???

def mcrImpl(using QuoteContext): Expr[Unit] =
val func: Expr[Seq[Boolean] => Unit] =
'{(esx: Seq[Boolean]) => f(esx: _*)}
val trees: Expr[Seq[Boolean]] = '{Seq(true)}
Expr.betaReduce('{ $func($trees) })
end mcrImpl

inline def mcr: Unit = ${ mcrImpl }
1 change: 1 addition & 0 deletions tests/pos-macros/i8866c/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
val x = mcr
44 changes: 44 additions & 0 deletions tests/pos-macros/i9687/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package x

import scala.quoted._

object FastPath {

inline def sum(x:Int):Int =
x + 1

}

object SlowPath {

def sum(x:Int):Int =
x + 1

}

object X {

inline def transform[A](inline x:A):A = ${
transformImpl[A]('x)
}

def transformImpl[A:Type](x:Expr[A])(using qctx: QuoteContext):Expr[A] = {
import qctx.tasty._
val slowPath = '{ SlowPath }.unseal
val fastPath = '{ FastPath }.unseal
val transformer = new TreeMap() {
override def transformTerm(term:Term)(using ctx:Context):Term = {
term match
case Apply(sel@Select(o,m),args) =>
if ( o.tpe =:= slowPath.tpe && m=="sum" )
Apply(Select.unique(fastPath,"sum"), args)
else
super.transformTerm(term)
case _ => super.transformTerm(term)
}
}
val r = transformer.transformTerm(x.unseal).seal.cast[A]
s"result: ${r.show}"
r
}
}
12 changes: 12 additions & 0 deletions tests/pos-macros/i9687/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package x


object Main {


def main(args:Array[String]):Unit =
val r = X.transform{
SlowPath.sum(1)
}

}