Skip to content

Fix #3916: Fix interpretation of boxed value classes #4025

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 2 commits into from
Feb 26, 2018
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
20 changes: 12 additions & 8 deletions compiler/src/dotty/tools/dotc/interpreter/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,12 +83,12 @@ class Interpreter(implicit ctx: Context) {
val paramClasses = paramsSig(fn.symbol)
val interpretedArgs = args.map(arg => interpretTreeImpl(arg, env))
val constructor = getConstructor(clazz, paramClasses)
interpreted(constructor.newInstance(interpretedArgs: _*))
stopIfRuntimeException(constructor.newInstance(interpretedArgs: _*))

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))
stopIfRuntimeException(method.invoke(null))

case tree: Apply =>
val evaluatedPrefix = if (tree.symbol.isStatic) null else interpretPrefix(tree, env)
Expand All @@ -98,7 +98,7 @@ class Interpreter(implicit ctx: Context) {
val paramClasses = paramsSig(tree.symbol)
val interpretedArgs = interpretArgs(tree, env)
val method = getMethod(clazz, tree.symbol.name, paramClasses)
interpreted(method.invoke(evaluatedPrefix, interpretedArgs: _*))
stopIfRuntimeException(method.invoke(evaluatedPrefix, interpretedArgs: _*))

case tree: Ident if env.contains(tree.symbol) =>
env(tree.symbol)
Expand All @@ -117,11 +117,15 @@ 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, _)
case Select(qualifier, name)
if tree.symbol.owner.isValueClass && tree.symbol.is(ParamAccessor) && env.contains(qualifier.symbol) =>
env(qualifier.symbol)
val value = env(qualifier.symbol)
val clazz = value.getClass
if (clazz.getCanonicalName != tree.symbol.owner.showFullName) value // Already unboxed
Copy link
Contributor

Choose a reason for hiding this comment

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

If the value class is a nested inner class, I guess this check will fail.

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 will add a test case and check 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.

There is an issue with inner classes but it is not in this code. I will fix it another PR.

else {
val method = getMethod(clazz, name, Nil)
stopIfRuntimeException(method.invoke(value))
}

case SeqLiteral(elems, _) =>
elems.map(elem => interpretTreeImpl(elem, env))
Expand Down Expand Up @@ -189,7 +193,7 @@ class Interpreter(implicit ctx: Context) {
}
}

private def interpreted[T](thunk: => T)(implicit pos: Position): T = {
private def stopIfRuntimeException[T](thunk: => T)(implicit pos: Position): T = {
try thunk
catch {
case ex: RuntimeException =>
Expand Down
21 changes: 21 additions & 0 deletions tests/pos/i3916/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import scala.quoted._

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

object FInterpolation {
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: _*) }
}
}
5 changes: 5 additions & 0 deletions tests/pos/i3916/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
object Test {
def main(args: Array[String]): Unit = {
println(new FInterpolatorHelper(StringContext("hello%s")).ff(5))
}
}