Skip to content

Commit

Permalink
Restored :warnings to working order.
Browse files Browse the repository at this point in the history
As seen here.

scala> class A { @deprecated("foo") def a = 1 }
warning: there were 1 deprecation warnings; re-run with -deprecation for details
defined class A

scala> :warnings
<console>:7: warning: @deprecated now takes two arguments; see the scaladoc.
       class A { @deprecated("foo") def a = 1 }
                  ^

scala> val x = 5 toString
warning: there were 1 feature warnings; re-run with -feature for details
x: String = 5

scala> :warnings
<console>:7: warning: postfix operator toString should be enabled
by making the implicit value language.postfixOps visible.
This can be achieved by adding the import clause 'import language.postfixOps'
or by setting the compiler option -language:postfixOps.
See the Scala docs for value scala.language.postfixOps for a discussion
why the feature should be explicitly enabled.
       val x = 5 toString
                 ^
  • Loading branch information
paulp committed Aug 6, 2012
1 parent 963aabb commit fd3601a
Show file tree
Hide file tree
Showing 6 changed files with 407 additions and 426 deletions.
5 changes: 4 additions & 1 deletion src/compiler/scala/tools/nsc/interpreter/ILoop.scala
Expand Up @@ -438,7 +438,10 @@ class ILoop(in0: Option[BufferedReader], protected val out: JPrintWriter)
}

private def warningsCommand(): Result = {
intp.lastWarnings foreach { case (pos, msg) => intp.reporter.warning(pos, msg) }
if (intp.lastWarnings.isEmpty)
"Can't find any cached warnings."
else
intp.lastWarnings foreach { case (pos, msg) => intp.reporter.warning(pos, msg) }
}

private def javapCommand(line: String): Result = {
Expand Down
80 changes: 33 additions & 47 deletions src/compiler/scala/tools/nsc/interpreter/IMain.scala
Expand Up @@ -108,27 +108,19 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
else new PathResolver(settings).result.asURLs // the compiler's classpath
)
def settings = currentSettings
def savingSettings[T](fn: Settings => Unit)(body: => T): T = {
val saved = currentSettings
currentSettings = saved.copy()
fn(currentSettings)
try body
finally currentSettings = saved
}
def mostRecentLine = prevRequestList match {
case Nil => ""
case req :: _ => req.originalLine
}
def rerunWith(names: String*) = {
savingSettings((ss: Settings) => {
import ss._
names flatMap lookupSetting foreach {
case s: BooleanSetting => s.value = true
case _ => ()
}
})(interpret(mostRecentLine))
// Run the code body with the given boolean settings flipped to true.
def withoutWarnings[T](body: => T): T = beQuietDuring {
val saved = settings.nowarn.value
if (!saved)
settings.nowarn.value = true

try body
finally if (!saved) settings.nowarn.value = false
}
def rerunForWarnings = rerunWith("-deprecation", "-unchecked", "-Xlint")

/** construct an interpreter that reports to Console */
def this(settings: Settings) = this(settings, new NewLinePrintWriter(new ConsoleWriter, true))
Expand Down Expand Up @@ -699,6 +691,10 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
class ReadEvalPrint(lineId: Int) {
def this() = this(freshLineId())

private var lastRun: Run = _
private var evalCaught: Option[Throwable] = None
private var conditionalWarnings: List[ConditionalWarning] = Nil

val packageName = sessionNames.line + lineId
val readName = sessionNames.read
val evalName = sessionNames.eval
Expand Down Expand Up @@ -754,7 +750,6 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
catch { case ex: Throwable => evalError(path, unwrap(ex)) }
}

var evalCaught: Option[Throwable] = None
lazy val evalClass = load(evalPath)
lazy val evalValue = callEither(resultName) match {
case Left(ex) => evalCaught = Some(ex) ; None
Expand All @@ -776,34 +771,33 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
/** We get a bunch of repeated warnings for reasons I haven't
* entirely figured out yet. For now, squash.
*/
private def removeDupWarnings(xs: List[(Position, String)]): List[(Position, String)] = {
if (xs.isEmpty)
return Nil

val ((pos, msg)) :: rest = xs
val filtered = rest filter { case (pos0, msg0) =>
(msg != msg0) || (pos.lineContent.trim != pos0.lineContent.trim) || {
// same messages and same line content after whitespace removal
// but we want to let through multiple warnings on the same line
// from the same run. The untrimmed line will be the same since
// there's no whitespace indenting blowing it.
(pos.lineContent == pos0.lineContent)
}
private def updateRecentWarnings(run: Run) {
def loop(xs: List[(Position, String)]): List[(Position, String)] = xs match {
case Nil => Nil
case ((pos, msg)) :: rest =>
val filtered = rest filter { case (pos0, msg0) =>
(msg != msg0) || (pos.lineContent.trim != pos0.lineContent.trim) || {
// same messages and same line content after whitespace removal
// but we want to let through multiple warnings on the same line
// from the same run. The untrimmed line will be the same since
// there's no whitespace indenting blowing it.
(pos.lineContent == pos0.lineContent)
}
}
((pos, msg)) :: loop(filtered)
}
((pos, msg)) :: removeDupWarnings(filtered)
val warnings = loop(run.allConditionalWarnings flatMap (_.warnings))
if (warnings.nonEmpty)
mostRecentWarnings = warnings
}
def lastWarnings: List[(Position, String)] = (
if (lastRun == null) Nil
else removeDupWarnings(lastRun.allConditionalWarnings flatMap (_.warnings))
)
private var lastRun: Run = _
private def evalMethod(name: String) = evalClass.getMethods filter (_.getName == name) match {
case Array(method) => method
case xs => sys.error("Internal error: eval object " + evalClass + ", " + xs.mkString("\n", "\n", ""))
}
private def compileAndSaveRun(label: String, code: String) = {
showCodeIfDebugging(code)
val (success, run) = compileSourcesKeepingRun(new BatchSourceFile(label, packaged(code)))
updateRecentWarnings(run)
lastRun = run
success
}
Expand Down Expand Up @@ -953,11 +947,7 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
}

// compile the result-extraction object
beQuietDuring {
savingSettings(_.nowarn.value = true) {
lineRep compile ResultObjectSourceCode(handlers)
}
}
withoutWarnings(lineRep compile ResultObjectSourceCode(handlers))
}
}

Expand Down Expand Up @@ -1008,12 +998,8 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends
case _ => naming.mostRecentVar
})

def lastWarnings: List[(global.Position, String)] = (
prevRequests.reverseIterator
map (_.lineRep.lastWarnings)
find (_.nonEmpty)
getOrElse Nil
)
private var mostRecentWarnings: List[(global.Position, String)] = Nil
def lastWarnings = mostRecentWarnings

def treesForRequestId(id: Int): List[Tree] =
requestForReqId(id).toList flatMap (_.trees)
Expand Down

0 comments on commit fd3601a

Please sign in to comment.