Skip to content

Fix #7488 and fix #7487: Get String form ConstantType #7490

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
Nov 4, 2019
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
17 changes: 8 additions & 9 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -197,16 +197,14 @@ object Inliner {
/** Expand call to scala.compiletime.testing.typeChecks */
def typeChecks(tree: Tree)(implicit ctx: Context): Tree = {
assert(tree.symbol == defn.CompiletimeTesting_typeChecks)
def getCodeArgValue(t: Tree): Option[String] = t match {
case Literal(Constant(code: String)) => Some(code)
case Typed(t2, _) => getCodeArgValue(t2)
case Inlined(_, Nil, t2) => getCodeArgValue(t2)
case Block(Nil, t2) => getCodeArgValue(t2)
case _ => None
def stripTyped(t: Tree): Tree = t match {
case Typed(t2, _) => stripTyped(t2)
case _ => t
}

val Apply(_, codeArg :: Nil) = tree
getCodeArgValue(codeArg.underlyingArgument) match {
case Some(code) =>
ConstFold(stripTyped(codeArg.underlyingArgument)).tpe.widenTermRefExpr match {
case ConstantType(Constant(code: String)) =>
val ctx2 = ctx.fresh.setNewTyperState().setTyper(new Typer)
val tree2 = new Parser(SourceFile.virtual("tasty-reflect", code))(ctx2).block()
val res =
Expand All @@ -216,7 +214,8 @@ object Inliner {
!ctx2.reporter.hasErrors
}
Literal(Constant(res))
case _ =>
case t =>
assert(ctx.reporter.hasErrors) // at least: argument to inline parameter must be a known value
EmptyTree
}
}
Expand Down
11 changes: 11 additions & 0 deletions tests/run/i7487.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.compiletime.testing.typeChecks

object Test extends App {
inline val code = "1 + 1"
val result1: Boolean = typeChecks(code) // true
val result2: Boolean = typeChecks("1 + 1") // true
val result3: Boolean = typeChecks("1" + "1") // true
assert(result1)
assert(result2)
assert(result3)
}
11 changes: 11 additions & 0 deletions tests/run/i7488.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import scala.compiletime.testing.typeChecks

object Test extends App {

inline final def assertCompiles(inline code: String): Boolean =
if (typeChecks(code)) true else false

inline val code = "1 + 1"
val result = assertCompiles(code)
assert(result)
}