Skip to content

Also run PostTyper and Inliner from completime.typechecks #11656

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 6 commits into from
Mar 9, 2021
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
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/Inlining.scala
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ class Inlining extends MacroTransform {
case _ =>
}

protected def newTransformer(using Context): Transformer = new Transformer {
def newTransformer(using Context): Transformer = new Transformer {
override def transform(tree: tpd.Tree)(using Context): tpd.Tree =
new InliningTreeMap().transform(tree)
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/transform/PostTyper.scala
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class PostTyper extends MacroTransform with IdentityDenotTransformer { thisPhase

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

protected def newTransformer(using Context): Transformer =
def newTransformer(using Context): Transformer =
new PostTyperTransformer

val superAcc: SuperAccessors = new SuperAccessors(thisPhase)
Expand Down
34 changes: 20 additions & 14 deletions compiler/src/dotty/tools/dotc/typer/Inliner.scala
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import ErrorReporting.errorTree
import dotty.tools.dotc.util.{SimpleIdentityMap, SimpleIdentitySet, EqHashMap, SourceFile, SourcePosition, SrcPos}
import dotty.tools.dotc.parsing.Parsers.Parser
import Nullables._
import transform.{PostTyper, Inlining}

import collection.mutable
import reporting.trace
Expand Down Expand Up @@ -293,7 +294,7 @@ object Inliner {
private enum ErrorKind:
case Parser, Typer

private def compileForErrors(tree: Tree, stopAfterParser: Boolean)(using Context): List[(ErrorKind, Error)] =
private def compileForErrors(tree: Tree)(using Context): List[(ErrorKind, Error)] =
assert(tree.symbol == defn.CompiletimeTesting_typeChecks || tree.symbol == defn.CompiletimeTesting_typeCheckErrors)
def stripTyped(t: Tree): Tree = t match {
case Typed(t2, _) => stripTyped(t2)
Expand All @@ -311,17 +312,22 @@ object Inliner {
ConstFold(underlyingCodeArg).tpe.widenTermRefExpr match {
case ConstantType(Constant(code: String)) =>
val source2 = SourceFile.virtual("tasty-reflect", code)
val ctx2 = ctx.fresh.setNewTyperState().setTyper(new Typer).setSource(source2)
val tree2 = new Parser(source2)(using ctx2).block()
val res = collection.mutable.ListBuffer.empty[(ErrorKind, Error)]

val parseErrors = ctx2.reporter.allErrors.toList
res ++= parseErrors.map(e => ErrorKind.Parser -> e)
if !stopAfterParser || res.isEmpty then
ctx2.typer.typed(tree2)(using ctx2)
val typerErrors = ctx2.reporter.allErrors.filterNot(parseErrors.contains)
res ++= typerErrors.map(e => ErrorKind.Typer -> e)
res.toList
inContext(ctx.fresh.setNewTyperState().setTyper(new Typer).setSource(source2)) {
val tree2 = new Parser(source2).block()
if ctx.reporter.allErrors.nonEmpty then
ctx.reporter.allErrors.map((ErrorKind.Parser, _))
else
val tree3 = ctx.typer.typed(tree2)
ctx.base.postTyperPhase match
case postTyper: PostTyper if ctx.reporter.allErrors.isEmpty =>
val tree4 = atPhase(postTyper) { postTyper.newTransformer.transform(tree3) }
ctx.base.inliningPhase match
case inlining: Inlining if ctx.reporter.allErrors.isEmpty =>
atPhase(inlining) { inlining.newTransformer.transform(tree4) }
case _ =>
case _ =>
ctx.reporter.allErrors.map((ErrorKind.Typer, _))
}
case t =>
report.error(em"argument to compileError must be a statically known String but was: $codeArg", codeArg1.srcPos)
Nil
Expand All @@ -346,12 +352,12 @@ object Inliner {

/** Expand call to scala.compiletime.testing.typeChecks */
def typeChecks(tree: Tree)(using Context): Tree =
val errors = compileForErrors(tree, true)
val errors = compileForErrors(tree)
Literal(Constant(errors.isEmpty)).withSpan(tree.span)

/** Expand call to scala.compiletime.testing.typeCheckErrors */
def typeCheckErrors(tree: Tree)(using Context): Tree =
val errors = compileForErrors(tree, false)
val errors = compileForErrors(tree)
packErrors(errors)

/** Expand call to scala.compiletime.codeOf */
Expand Down
9 changes: 9 additions & 0 deletions tests/run/i11630.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
inline def failme1() = compiletime.error("fail")
transparent inline def failme2() = compiletime.error("fail")

@main def Test: Unit = {
assert(!compiletime.testing.typeChecks("failme1()"))
assert(!compiletime.testing.typeChecks("failme2()"))
assert(!compiletime.testing.typeChecks("a b c"))
assert(!compiletime.testing.typeChecks("true: Int"))
}