Skip to content

Commit 7820b5f

Browse files
authored
Merge pull request #2832 from dotty-staging/fix/error-messages-calls
Allow multiple calls to `checkMessagesAfter` in ErrorMessagesTests
2 parents 0a7e6c9 + 0a7e4dd commit 7820b5f

File tree

8 files changed

+273
-288
lines changed

8 files changed

+273
-288
lines changed

compiler/test/dotty/tools/DottyTest.scala

+7-3
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ trait DottyTest extends ContextEscapeDetection {
1818

1919
dotc.parsing.Scanners // initialize keywords
2020

21-
implicit var ctx: Context = {
21+
implicit var ctx: Context = initialCtx
22+
23+
protected def initialCtx: FreshContext = {
2224
val base = new ContextBase {}
2325
import base.settings._
2426
val ctx = base.initialCtx.fresh
@@ -55,18 +57,20 @@ trait DottyTest extends ContextEscapeDetection {
5557
}
5658
}
5759

58-
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Unit = {
60+
def checkCompile(checkAfterPhase: String, source: String)(assertion: (tpd.Tree, Context) => Unit): Context = {
5961
val c = compilerWithChecker(checkAfterPhase)(assertion)
6062
c.rootContext(ctx)
6163
val run = c.newRun
6264
run.compile(source)
65+
run.runContext
6366
}
6467

65-
def checkCompile(checkAfterPhase: String, sources: List[String])(assertion: (tpd.Tree, Context) => Unit): Unit = {
68+
def checkCompile(checkAfterPhase: String, sources: List[String])(assertion: (tpd.Tree, Context) => Unit): Context = {
6669
val c = compilerWithChecker(checkAfterPhase)(assertion)
6770
c.rootContext(ctx)
6871
val run = c.newRun
6972
run.compile(sources)
73+
run.runContext
7074
}
7175

7276
def methType(names: String*)(paramTypes: Type*)(resultType: Type = defn.UnitType) =

compiler/test/dotty/tools/dotc/ast/DesugarTests.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ class DesugarTests extends DottyTest {
2323
)
2424
}
2525

26-
@Test def caseClassHasCorrectMembers =
26+
@Test def caseClassHasCorrectMembers: Unit =
2727
checkCompile("frontend", "case class Foo(x: Int, y: String)") { (tree, context) =>
2828
implicit val ctx = context
2929
val ccTree = tree.find(tree => tree.symbol.name == typeName("Foo")).get
@@ -38,7 +38,7 @@ class DesugarTests extends DottyTest {
3838
rest.foreach(validSym)
3939
}
4040

41-
@Test def caseClassCompanionHasCorrectMembers =
41+
@Test def caseClassCompanionHasCorrectMembers: Unit =
4242
checkCompile("frontend", "case class Foo(x: Int, y: String)") { (tree, context) =>
4343
implicit val ctx = context
4444
val ccTree = tree.find(tree => tree.symbol.name == termName("Foo")).get

compiler/test/dotty/tools/dotc/ast/TreeInfoTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ class TreeInfoTest extends DottyTest {
1414
import tpd._
1515

1616
@Test
17-
def testDefPath = checkCompile("frontend", "class A { def bar = { val x = { val z = 0; 0} }} ") {
17+
def testDefPath: Unit = checkCompile("frontend", "class A { def bar = { val x = { val z = 0; 0} }} ") {
1818
(tree, context) =>
1919
implicit val ctx = context
2020
val xTree = tree.find(tree => tree.symbol.name == termName("x")).get

compiler/test/dotty/tools/dotc/parsing/DocstringTest.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ trait DocstringTest extends DottyTest {
2525
assert(false, s"Couldn't match resulting AST to expected AST in: $x")
2626
}
2727

28-
def checkFrontend(source: String)(docAssert: PartialFunction[Tree[Untyped], Unit]) = {
28+
def checkFrontend(source: String)(docAssert: PartialFunction[Tree[Untyped], Unit]): Unit = {
2929
checkCompile("frontend", source) { (_, ctx) =>
3030
implicit val c: Context = ctx
3131
(docAssert orElse defaultAssertion)(ctx.compilationUnit.untpdTree)

compiler/test/dotty/tools/dotc/parsing/DocstringTests.scala

+1-1
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ class DocstringTests extends DocstringTest {
4949
checkDocString(t.rawComment.map(_.raw), "/** Hello /* multiple open */ world! */")
5050
}
5151
}
52-
@Test def multipleClassesInPackage = {
52+
@Test def multipleClassesInPackage: Unit = {
5353
val source =
5454
"""
5555
|package a

compiler/test/dotty/tools/dotc/reporting/ErrorMessagesTest.scala

+15-34
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,15 @@ import org.junit.Assert._
1111

1212
trait ErrorMessagesTest extends DottyTest {
1313

14-
ctx = freshReporter(ctx)
15-
16-
private def freshReporter(ctx: Context) =
17-
ctx.fresh.setReporter(new CapturingReporter)
14+
private def newContext = {
15+
val rep = new StoreReporter(null)
16+
with UniqueMessagePositions with HideNonSensicalMessages
17+
initialCtx.setReporter(rep)
18+
}
1819

1920
class Report(messages: List[Message], ictx: Context) {
20-
def expect(f: (Context, List[Message]) => Unit): Unit = {
21+
def expect(f: (Context, List[Message]) => Unit): Unit =
2122
f(ictx, messages)
22-
}
2323

2424
def expectNoErrors: Unit =
2525
assert(this.isInstanceOf[EmptyReport], "errors found when not expected")
@@ -32,35 +32,16 @@ trait ErrorMessagesTest extends DottyTest {
3232
|there are no errors or the compiler crashes.""".stripMargin)
3333
}
3434

35-
class CapturingReporter extends Reporter
36-
with UniqueMessagePositions with HideNonSensicalMessages {
37-
private[this] val buffer = new mutable.ListBuffer[Message]
38-
private[this] var capturedContext: Context = _
39-
40-
def doReport(m: MessageContainer)(implicit ctx: Context) = {
41-
capturedContext = ctx
42-
buffer append m.contained()
43-
}
44-
45-
def toReport: Report =
46-
if (capturedContext eq null)
47-
new EmptyReport
48-
else {
49-
val xs = buffer.reverse.toList
50-
buffer.clear()
51-
52-
val ctx = capturedContext
53-
capturedContext = null
54-
55-
new Report(xs, ctx)
56-
}
57-
}
58-
5935
def checkMessagesAfter(checkAfterPhase: String)(source: String): Report = {
60-
checkCompile(checkAfterPhase, source) { (_,ictx) => () }
61-
val rep = ctx.reporter.asInstanceOf[CapturingReporter].toReport
62-
ctx = freshReporter(ctx)
63-
rep
36+
ctx = newContext
37+
val runCtx = checkCompile(checkAfterPhase, source) { (_, _) => () }
38+
39+
if (!runCtx.reporter.hasErrors) new EmptyReport
40+
else {
41+
val rep = runCtx.reporter.asInstanceOf[StoreReporter]
42+
val msgs = rep.removeBufferedMessages(runCtx).map(_.contained()).reverse
43+
new Report(msgs, runCtx)
44+
}
6445
}
6546

6647
def assertMessageCount(expected: Int, messages: List[Message]): Unit =

0 commit comments

Comments
 (0)