diff options
author | Paul Phillips <paulp@improving.org> | 2012-08-06 14:05:24 -0700 |
---|---|---|
committer | Paul Phillips <paulp@improving.org> | 2012-08-06 15:03:22 -0700 |
commit | fd3601a833baac6258d28687d1a73979f4369826 (patch) | |
tree | 3bd5dc91d21382f7a97c1c8641c324bcd4ce36c2 /src/compiler/scala/tools/nsc/interpreter/IMain.scala | |
parent | 963aabbeb45e042f4b0d6f5ec13edb0136cbf441 (diff) | |
download | scala-fd3601a833baac6258d28687d1a73979f4369826.tar.gz scala-fd3601a833baac6258d28687d1a73979f4369826.tar.bz2 scala-fd3601a833baac6258d28687d1a73979f4369826.zip |
Restored :warnings to working order.
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
^
Diffstat (limited to 'src/compiler/scala/tools/nsc/interpreter/IMain.scala')
-rw-r--r-- | src/compiler/scala/tools/nsc/interpreter/IMain.scala | 80 |
1 files changed, 33 insertions, 47 deletions
diff --git a/src/compiler/scala/tools/nsc/interpreter/IMain.scala b/src/compiler/scala/tools/nsc/interpreter/IMain.scala index 7bdbff8627..e6a142934d 100644 --- a/src/compiler/scala/tools/nsc/interpreter/IMain.scala +++ b/src/compiler/scala/tools/nsc/interpreter/IMain.scala @@ -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)) @@ -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 @@ -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 @@ -776,27 +771,25 @@ 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", "")) @@ -804,6 +797,7 @@ class IMain(initialSettings: Settings, protected val out: JPrintWriter) extends private def compileAndSaveRun(label: String, code: String) = { showCodeIfDebugging(code) val (success, run) = compileSourcesKeepingRun(new BatchSourceFile(label, packaged(code))) + updateRecentWarnings(run) lastRun = run success } @@ -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)) } } @@ -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) |