Skip to content

Fix interpretation of Typed trees #3852

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
Jan 18, 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
5 changes: 4 additions & 1 deletion compiler/src/dotty/tools/dotc/interpreter/Interpreter.scala
Original file line number Diff line number Diff line change
Expand Up @@ -111,10 +111,13 @@ class Interpreter(implicit ctx: Context) {
val env2 = bindings.foldLeft(env)((acc, x) => interpretStat(x, acc))
interpretTreeImpl(expansion, env2)

case Typed(expr, _) =>
interpretTreeImpl(expr, env)
Copy link
Contributor

Choose a reason for hiding this comment

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

Do we need a check here? Maybe not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

What kind of check? This is a fully typed tree, we evaluate it's erased version.

Copy link
Contributor

Choose a reason for hiding this comment

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

I was thinking to check the type, but it's not the semantics of the language. So ignore the comment.


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}\n${tree}", tree.pos)
throw new StopInterpretation(s"Could not interpret ${tree.show}. Consider extracting logic into a helper def.", tree.pos)
}
}

Expand Down
2 changes: 2 additions & 0 deletions tests/run/quote-splice-interpret-1.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
true
false
Copy link
Contributor

Choose a reason for hiding this comment

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

How can I be sure the output is from compile-time, not run-time?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Because the inline def has a top level ~ it can always be inlined. Then the inlined tree is interpreted to evaluate the Expr[T] which is then spliced in (see #3853 for a bit of info on that regard).
In short a macro call is guaranteed to be inlined or the compilation fails.

14 changes: 14 additions & 0 deletions tests/run/quote-splice-interpret-1/Macro_1.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import dotty.tools.dotc.ast.Trees.Import

import scala.quoted._
object Macros {
sealed trait Nat
case object Z extends Nat
case class S[N <: Nat]() extends Nat

inline def isZero(inline n: Int): Boolean = ~{
if (n == 0) (true: Expr[Boolean])
else (false: Expr[Boolean])
}

}
7 changes: 7 additions & 0 deletions tests/run/quote-splice-interpret-1/Test_2.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import Macros._
object Test {
def main(args: Array[String]): Unit = {
println(isZero(0))
println(isZero(1))
}
}