diff --git a/compiler/src/dotty/tools/dotc/typer/Inliner.scala b/compiler/src/dotty/tools/dotc/typer/Inliner.scala index 1de7324a194d..632b5be4ae53 100644 --- a/compiler/src/dotty/tools/dotc/typer/Inliner.scala +++ b/compiler/src/dotty/tools/dotc/typer/Inliner.scala @@ -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 = @@ -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 } } diff --git a/tests/run/i7487.scala b/tests/run/i7487.scala new file mode 100644 index 000000000000..f029543d1918 --- /dev/null +++ b/tests/run/i7487.scala @@ -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) +} diff --git a/tests/run/i7488.scala b/tests/run/i7488.scala new file mode 100644 index 000000000000..c88113913728 --- /dev/null +++ b/tests/run/i7488.scala @@ -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) +} \ No newline at end of file