Skip to content

Fix #9802: Interpret by-name params as () => interpret(arg) #9811

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 17, 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
30 changes: 28 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/Splicer.scala
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,7 @@ object Splicer {
interpretModuleAccess(fn.symbol)
else if (fn.symbol.is(Method) && fn.symbol.isStatic) {
val staticMethodCall = interpretedStaticMethodCall(fn.symbol.owner, fn.symbol)
staticMethodCall(args.flatten.map(interpretTree))
staticMethodCall(interpretArgs(args, fn.symbol.info))
}
else if fn.symbol.isStatic then
assert(args.isEmpty)
Expand All @@ -260,7 +260,7 @@ object Splicer {
interpretModuleAccess(fn.qualifier.symbol)
else {
val staticMethodCall = interpretedStaticMethodCall(fn.qualifier.symbol.moduleClass, fn.symbol)
staticMethodCall(args.flatten.map(interpretTree))
staticMethodCall(interpretArgs(args, fn.symbol.info))
}
else if (env.contains(fn.symbol))
env(fn.symbol)
Expand Down Expand Up @@ -289,6 +289,32 @@ object Splicer {
unexpectedTree(tree)
}

private def interpretArgs(argss: List[List[Tree]], fnType: Type)(using Env): List[Object] = {
def interpretArgsGroup(args: List[Tree], argTypes: List[Type]): List[Object] =
assert(args.size == argTypes.size)
val view =
for (arg, info) <- args.lazyZip(argTypes) yield
info match
case _: ExprType => () => interpretTree(arg) // by-name argument
case _ => interpretTree(arg) // by-value argument
view.toList

fnType.dealias match
case fnType: MethodType if fnType.isErasedMethod => interpretArgs(argss, fnType.resType)
case fnType: MethodType =>
val argTypes = fnType.paramInfos
assert(argss.head.size == argTypes.size)
interpretArgsGroup(argss.head, argTypes) ::: interpretArgs(argss.tail, fnType.resType)
case fnType: AppliedType if defn.isContextFunctionType(fnType) =>
val argTypes :+ resType = fnType.args
interpretArgsGroup(argss.head, argTypes) ::: interpretArgs(argss.tail, resType)
case fnType: PolyType => interpretArgs(argss, fnType.resType)
case fnType: ExprType => interpretArgs(argss, fnType.resType)
case _ =>
assert(argss.isEmpty)
Nil
}

private def interpretBlock(stats: List[Tree], expr: Tree)(implicit env: Env) = {
var unexpected: Option[Object] = None
val newEnv = stats.foldLeft(env)((accEnv, stat) => stat match {
Expand Down
5 changes: 5 additions & 0 deletions tests/pos-macros/i9802/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
import scala.quoted._

inline def fun(inline prog: Double): Double = ${impl('prog)}

def impl(prog: => Expr[Double])(using QuoteContext) : Expr[Double] = '{ 42.0 }
2 changes: 2 additions & 0 deletions tests/pos-macros/i9802/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

def test: Unit = fun(4)
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@

object Test {
def main(args: Array[String]): Unit = {
println(SourceFiles.getThisFile)
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import scala.quoted._

object SourceFiles {

type Macro[X] = (=> QuoteContext) ?=> Expr[X]

implicit inline def getThisFile: String =
${getThisFileImpl}

def getThisFileImpl: Macro[String] =
Expr(qctx.tasty.Source.path.getFileName.toString)

}