Skip to content

REPL: emit parse warnings by reusing the reporter #13209

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 1 commit into from
Jul 30, 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
6 changes: 3 additions & 3 deletions compiler/src/dotty/tools/repl/ParseResult.scala
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import dotc.core.Contexts._
import dotc.core.StdNames.str
import dotc.parsing.Parsers.Parser
import dotc.parsing.Tokens
import dotc.reporting.Diagnostic
import dotc.reporting.{Diagnostic, StoreReporter}
import dotc.util.SourceFile

import scala.annotation.internal.sharable
Expand All @@ -16,7 +16,7 @@ import scala.annotation.internal.sharable
sealed trait ParseResult

/** An error free parsing resulting in a list of untyped trees */
case class Parsed(source: SourceFile, trees: List[untpd.Tree]) extends ParseResult
case class Parsed(source: SourceFile, trees: List[untpd.Tree], reporter: StoreReporter) extends ParseResult

/** A parsing result containing syntax `errors` */
case class SyntaxErrors(sourceCode: String,
Expand Down Expand Up @@ -154,7 +154,7 @@ object ParseResult {
reporter.removeBufferedMessages,
stats)
else
Parsed(source, stats)
Parsed(source, stats, reporter)
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/repl/ReplCompiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ class ReplCompiler extends Compiler {
}

ParseResult(sourceFile)(state) match {
case Parsed(_, trees) =>
case Parsed(_, trees, _) =>
wrap(trees).result
case SyntaxErrors(_, reported, trees) =>
if (errorsAllowed) wrap(trees).result
Expand Down
8 changes: 4 additions & 4 deletions compiler/src/dotty/tools/repl/ReplDriver.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import dotty.tools.dotc.core.StdNames._
import dotty.tools.dotc.core.Symbols.{Symbol, defn}
import dotty.tools.dotc.interactive.Completion
import dotty.tools.dotc.printing.SyntaxHighlighting
import dotty.tools.dotc.reporting.MessageRendering
import dotty.tools.dotc.reporting.{MessageRendering, StoreReporter}
import dotty.tools.dotc.reporting.{Message, Diagnostic}
import dotty.tools.dotc.util.Spans.Span
import dotty.tools.dotc.util.{SourceFile, SourcePosition}
Expand Down Expand Up @@ -174,8 +174,8 @@ class ReplDriver(settings: Array[String],
}
}

private def newRun(state: State) = {
val run = compiler.newRun(rootCtx.fresh.setReporter(newStoreReporter), state)
private def newRun(state: State, reporter: StoreReporter = newStoreReporter) = {
val run = compiler.newRun(rootCtx.fresh.setReporter(reporter), state)
state.copy(context = run.runContext)
}

Expand Down Expand Up @@ -243,7 +243,7 @@ class ReplDriver(settings: Array[String],
unfusedPhases(using ctx).collectFirst { case phase: CollectTopLevelImports => phase.imports }.get

implicit val state = {
val state0 = newRun(istate)
val state0 = newRun(istate, parsed.reporter)
state0.copy(context = state0.context.withSource(parsed.source))
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Btw, before I wired the reporter through newRun I was adding it here with .fresh.setReporter(reporter) but that was breaking something to do with class-loading, which seemed complicated. I'm not sure if that's expected and if anyone knows why that happened.

}
compiler
Expand Down
11 changes: 11 additions & 0 deletions compiler/test-resources/pending/repl/i13208.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
// scalac: -source:future -deprecation
scala> type M[X] = X match { case Int => String case _ => Int }
-- Deprecation Warning:
1 | type M[X] = X match { case Int => String case _ => Int }
| ^
| `_` is deprecated for wildcard arguments of types: use `?` instead
scala> type N[X] = X match { case List[_] => Int }
-- Deprecation Warning:
1 | type N[X] = X match { case List[_] => Int }
| ^
| `_` is deprecated for wildcard arguments of types: use `?` instead
7 changes: 7 additions & 0 deletions compiler/test-resources/repl/i13208.default.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
scala> try 1
-- Warning:
1 | try 1
| ^^^^^
| A try without catch or finally is equivalent to putting
| its body in a block; no exceptions are handled.
val res0: Int = 1
2 changes: 1 addition & 1 deletion compiler/test/dotty/tools/repl/ReplCompilerTests.scala
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ class ReplCompilerTests extends ReplTest {
"""|sealed trait T1
|case class X() extends T1
|case class Y() extends T1
| case object O extends T1
|case object O extends T1
""".stripMargin
}

Expand Down