Skip to content

Allow interpretation of inline methods in value classes and varargs #3913

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
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
50 changes: 39 additions & 11 deletions compiler/src/dotty/tools/dotc/interpreter/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import dotty.tools.dotc.ast.Trees._
import dotty.tools.dotc.core.Constants._
import dotty.tools.dotc.core.Contexts._
import dotty.tools.dotc.core.Decorators._
import dotty.tools.dotc.core.Flags._
import dotty.tools.dotc.core.Names._
import dotty.tools.dotc.core.Symbols._
import dotty.tools.dotc.core.quoted.Quoted
Expand Down Expand Up @@ -82,20 +83,20 @@ class Interpreter(implicit ctx: Context) {
val constructor = getConstructor(clazz, paramClasses)
interpreted(constructor.newInstance(interpretedArgs: _*))

case _: RefTree | _: Apply if tree.symbol.isStatic =>
case _: RefTree if tree.symbol.isStatic =>
val clazz = loadClass(tree.symbol.owner.companionModule.fullName)
val method = getMethod(clazz, tree.symbol.name, Nil)
interpreted(method.invoke(null))

case tree: Apply =>
val evaluatedPrefix = if (tree.symbol.isStatic) null else interpretPrefix(tree, env)
val clazz =
if (tree.symbol.isStatic) loadClass(tree.symbol.owner.companionModule.fullName)
else evaluatedPrefix.getClass
val paramClasses = paramsSig(tree.symbol)
val interpretedArgs = Array.newBuilder[Object]
def interpretArgs(tree: Tree): Unit = tree match {
case Apply(fn, args) =>
interpretArgs(fn)
args.foreach(arg => interpretedArgs += interpretTreeImpl(arg, env))
case _ =>
}
interpretArgs(tree)

val interpretedArgs = interpretArgs(tree, env)
val method = getMethod(clazz, tree.symbol.name, paramClasses)
interpreted(method.invoke(null, interpretedArgs.result(): _*))
interpreted(method.invoke(evaluatedPrefix, interpretedArgs: _*))

case tree: Ident if env.contains(tree.symbol) =>
env(tree.symbol)
Expand All @@ -114,13 +115,40 @@ class Interpreter(implicit ctx: Context) {
case Typed(expr, _) =>
interpretTreeImpl(expr, env)

// Getting the underlying value of a value class. The value class is evaluated as its boxed representation
// as values in the interpreter are `Object`s. Therefore we just get it from the enviroment as is.
case Select(qualifier, _)
if tree.symbol.owner.isValueClass && tree.symbol.is(ParamAccessor) && env.contains(qualifier.symbol) =>
env(qualifier.symbol)

case SeqLiteral(elems, _) =>
elems.map(elem => interpretTreeImpl(elem, env))

case _ =>
// TODO Add more precise descriptions of why it could not be interpreted.
// This should be done after the full interpreter is implemented.
throw new StopInterpretation(s"Could not interpret ${tree.show}. Consider extracting logic into a helper def.", tree.pos)
}
}

private def interpretArgs(tree: Tree, env: Env): Seq[Object] = {
val b = Seq.newBuilder[Object]
def interpretArgs(tree: Tree): Unit = tree match {
case Apply(fn, args) =>
interpretArgs(fn)
args.foreach(arg => b += interpretTreeImpl(arg, env))
case _ =>
}
interpretArgs(tree)
b.result()
}

private def interpretPrefix(tree: Tree, env: Env): Object = tree match {
case Apply(qual, _) => interpretPrefix(qual, env)
case TypeApply(qual, _) => interpretPrefix(qual, env)
case Select(qual, _) => interpretTreeImpl(qual, env)
}

/** Interprets the statement and returns the updated environment */
private def interpretStat(stat: Tree, env: Env): Env = stat match {
case tree: ValDef =>
Expand Down
24 changes: 24 additions & 0 deletions tests/pos/quote-interpolator-core/quoted_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import scala.quoted._

// This test checks the correct interpretation of the inlined value class

object FInterpolation {

implicit class FInterpolatorHelper(val sc: StringContext) extends AnyVal {
inline def ff(arg1: Any): String = ~fInterpolation(sc, Seq('(arg1)))
inline def ff(arg1: Any, arg2: Any): String = ~fInterpolation(sc, Seq('(arg1), '(arg2)))
inline def ff(arg1: Any, arg2: Any, arg3: Any): String = ~fInterpolation(sc, Seq('(arg1), '(arg2), '(arg3)))
// ...
}

private def liftSeq(args: Seq[Expr[Any]]): Expr[Seq[Any]] = args match {
case x :: xs => '{ (~x) +: ~(liftSeq(xs)) }
case Nil => '(Seq(): Seq[Any])
}

def fInterpolation(sc: StringContext, args: Seq[Expr[Any]]): Expr[String] = {
val str: Expr[String] = sc.parts.mkString("")
val args1: Expr[Seq[Any]] = liftSeq(args)
'{ (~str).format(~args1: _*) }
}
}
8 changes: 8 additions & 0 deletions tests/pos/quote-interpolator-core/quoted_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@

import FInterpolation._

object Test {
println(ff"integer: ${5}%d")
println(ff"string: ${"l"}%s")
println(ff"${5}%s, ${6}%d, ${"hello"}%s")
}