Skip to content

Commit 33ebb4a

Browse files
committed
Fix #7488 and fix #7487: Get String form ConstantType
1 parent 332c831 commit 33ebb4a

File tree

3 files changed

+30
-9
lines changed

3 files changed

+30
-9
lines changed

compiler/src/dotty/tools/dotc/typer/Inliner.scala

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -197,16 +197,14 @@ object Inliner {
197197
/** Expand call to scala.compiletime.testing.typeChecks */
198198
def typeChecks(tree: Tree)(implicit ctx: Context): Tree = {
199199
assert(tree.symbol == defn.CompiletimeTesting_typeChecks)
200-
def getCodeArgValue(t: Tree): Option[String] = t match {
201-
case Literal(Constant(code: String)) => Some(code)
202-
case Typed(t2, _) => getCodeArgValue(t2)
203-
case Inlined(_, Nil, t2) => getCodeArgValue(t2)
204-
case Block(Nil, t2) => getCodeArgValue(t2)
205-
case _ => None
200+
def stripTyped(t: Tree): Tree = t match {
201+
case Typed(t2, _) => stripTyped(t2)
202+
case _ => t
206203
}
204+
207205
val Apply(_, codeArg :: Nil) = tree
208-
getCodeArgValue(codeArg.underlyingArgument) match {
209-
case Some(code) =>
206+
ConstFold(stripTyped(codeArg.underlyingArgument)).tpe.widenTermRefExpr match {
207+
case ConstantType(Constant(code: String)) =>
210208
val ctx2 = ctx.fresh.setNewTyperState().setTyper(new Typer)
211209
val tree2 = new Parser(SourceFile.virtual("tasty-reflect", code))(ctx2).block()
212210
val res =
@@ -216,7 +214,8 @@ object Inliner {
216214
!ctx2.reporter.hasErrors
217215
}
218216
Literal(Constant(res))
219-
case _ =>
217+
case t =>
218+
assert(ctx.reporter.hasErrors) // at least: argument to inline parameter must be a known value
220219
EmptyTree
221220
}
222221
}

tests/run/i7487.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.compiletime.testing.typeChecks
2+
3+
object Test extends App {
4+
inline val code = "1 + 1"
5+
val result1: Boolean = typeChecks(code) // true
6+
val result2: Boolean = typeChecks("1 + 1") // true
7+
val result3: Boolean = typeChecks("1" + "1") // true
8+
assert(result1)
9+
assert(result2)
10+
assert(result3)
11+
}

tests/run/i7488.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
import scala.compiletime.testing.typeChecks
2+
3+
object Test extends App {
4+
5+
inline final def assertCompiles(inline code: String): Boolean =
6+
if (typeChecks(code)) true else false
7+
8+
inline val code = "1 + 1"
9+
val result = assertCompiles(code)
10+
assert(result)
11+
}

0 commit comments

Comments
 (0)