File tree Expand file tree Collapse file tree 3 files changed +30
-9
lines changed
compiler/src/dotty/tools/dotc/typer Expand file tree Collapse file tree 3 files changed +30
-9
lines changed Original file line number Diff line number Diff 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 }
Original file line number Diff line number Diff line change 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+ }
Original file line number Diff line number Diff line change 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+ }
You can’t perform that action at this time.
0 commit comments