Skip to content

Commit c4dea7b

Browse files
Merge pull request #11656 from dotty-staging/fix-11630
Also run PostTyper and Inliner from completime.typechecks
2 parents 3a1143b + 86e8fb7 commit c4dea7b

File tree

5 files changed

+32
-17
lines changed

5 files changed

+32
-17
lines changed

Diff for: compiler/src/dotty/tools/dotc/transform/Inlining.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ class Inlining extends MacroTransform {
6767
case _ =>
6868
}
6969

70-
protected def newTransformer(using Context): Transformer = new Transformer {
70+
def newTransformer(using Context): Transformer = new Transformer {
7171
override def transform(tree: tpd.Tree)(using Context): tpd.Tree =
7272
new InliningTreeMap().transform(tree)
7373
}

Diff for: compiler/src/dotty/tools/dotc/transform/PostTyper.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase
7070

7171
override def transformPhase(using Context): Phase = thisPhase.next
7272

73-
protected def newTransformer(using Context): Transformer =
73+
def newTransformer(using Context): Transformer =
7474
new PostTyperTransformer
7575

7676
val superAcc: SuperAccessors = new SuperAccessors(thisPhase)

Diff for: compiler/src/dotty/tools/dotc/typer/Inliner.scala

+20-14
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ import ErrorReporting.errorTree
2525
import dotty.tools.dotc.util.{SimpleIdentityMap, SimpleIdentitySet, EqHashMap, SourceFile, SourcePosition, SrcPos}
2626
import dotty.tools.dotc.parsing.Parsers.Parser
2727
import Nullables._
28+
import transform.{PostTyper, Inlining}
2829

2930
import collection.mutable
3031
import reporting.trace
@@ -293,7 +294,7 @@ object Inliner {
293294
private enum ErrorKind:
294295
case Parser, Typer
295296

296-
private def compileForErrors(tree: Tree, stopAfterParser: Boolean)(using Context): List[(ErrorKind, Error)] =
297+
private def compileForErrors(tree: Tree)(using Context): List[(ErrorKind, Error)] =
297298
assert(tree.symbol == defn.CompiletimeTesting_typeChecks || tree.symbol == defn.CompiletimeTesting_typeCheckErrors)
298299
def stripTyped(t: Tree): Tree = t match {
299300
case Typed(t2, _) => stripTyped(t2)
@@ -311,17 +312,22 @@ object Inliner {
311312
ConstFold(underlyingCodeArg).tpe.widenTermRefExpr match {
312313
case ConstantType(Constant(code: String)) =>
313314
val source2 = SourceFile.virtual("tasty-reflect", code)
314-
val ctx2 = ctx.fresh.setNewTyperState().setTyper(new Typer).setSource(source2)
315-
val tree2 = new Parser(source2)(using ctx2).block()
316-
val res = collection.mutable.ListBuffer.empty[(ErrorKind, Error)]
317-
318-
val parseErrors = ctx2.reporter.allErrors.toList
319-
res ++= parseErrors.map(e => ErrorKind.Parser -> e)
320-
if !stopAfterParser || res.isEmpty then
321-
ctx2.typer.typed(tree2)(using ctx2)
322-
val typerErrors = ctx2.reporter.allErrors.filterNot(parseErrors.contains)
323-
res ++= typerErrors.map(e => ErrorKind.Typer -> e)
324-
res.toList
315+
inContext(ctx.fresh.setNewTyperState().setTyper(new Typer).setSource(source2)) {
316+
val tree2 = new Parser(source2).block()
317+
if ctx.reporter.allErrors.nonEmpty then
318+
ctx.reporter.allErrors.map((ErrorKind.Parser, _))
319+
else
320+
val tree3 = ctx.typer.typed(tree2)
321+
ctx.base.postTyperPhase match
322+
case postTyper: PostTyper if ctx.reporter.allErrors.isEmpty =>
323+
val tree4 = atPhase(postTyper) { postTyper.newTransformer.transform(tree3) }
324+
ctx.base.inliningPhase match
325+
case inlining: Inlining if ctx.reporter.allErrors.isEmpty =>
326+
atPhase(inlining) { inlining.newTransformer.transform(tree4) }
327+
case _ =>
328+
case _ =>
329+
ctx.reporter.allErrors.map((ErrorKind.Typer, _))
330+
}
325331
case t =>
326332
report.error(em"argument to compileError must be a statically known String but was: $codeArg", codeArg1.srcPos)
327333
Nil
@@ -346,12 +352,12 @@ object Inliner {
346352

347353
/** Expand call to scala.compiletime.testing.typeChecks */
348354
def typeChecks(tree: Tree)(using Context): Tree =
349-
val errors = compileForErrors(tree, true)
355+
val errors = compileForErrors(tree)
350356
Literal(Constant(errors.isEmpty)).withSpan(tree.span)
351357

352358
/** Expand call to scala.compiletime.testing.typeCheckErrors */
353359
def typeCheckErrors(tree: Tree)(using Context): Tree =
354-
val errors = compileForErrors(tree, false)
360+
val errors = compileForErrors(tree)
355361
packErrors(errors)
356362

357363
/** Expand call to scala.compiletime.codeOf */

Diff for: tests/run/i11630.scala

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
inline def failme1() = compiletime.error("fail")
2+
transparent inline def failme2() = compiletime.error("fail")
3+
4+
@main def Test: Unit = {
5+
assert(!compiletime.testing.typeChecks("failme1()"))
6+
assert(!compiletime.testing.typeChecks("failme2()"))
7+
assert(!compiletime.testing.typeChecks("a b c"))
8+
assert(!compiletime.testing.typeChecks("true: Int"))
9+
}

0 commit comments

Comments
 (0)