Skip to content

Commit

Permalink
Merge pull request scala#9142 from som-snytt/issue/12098
Browse files Browse the repository at this point in the history
MultiStringSetting preserves remaining args
  • Loading branch information
dwijnand committed Jul 30, 2020
2 parents 8aefe5e + 7d285a9 commit 98384fd
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 13 deletions.
22 changes: 10 additions & 12 deletions src/compiler/scala/tools/nsc/settings/MutableSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -764,21 +764,19 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)

withHelpSyntax(name + ":<" + arg + ">")

// try to set. halting means halt at first non-arg
// try to set. halting means halt at first non-arg i.e. at next option
protected def tryToSetArgs(args: List[String], halting: Boolean) = {
@tailrec
def loop(args: List[String]): List[String] = args match {
case arg :: rest =>
if (halting && (arg startsWith "-")) args
else {
if (helpText.isDefined && arg == "help") sawHelp = true
else if (prepend) value ::= arg
else value ++= List(arg)
loop(rest)
}
case Nil => Nil
def loop(seen: List[String], args: List[String]): (List[String], List[String]) = args match {
case Optionlike() :: _ if halting => (seen, args)
case "help" :: rest if helpText.isDefined => sawHelp = true ; loop(seen, rest)
case arg :: rest => loop(arg :: seen, rest)
case Nil => (seen, Nil)
}
Some(loop(if (prepend) args.reverse else args))
val (seen, rest) = loop(Nil, args)
if (prepend) value = value.prependedAll(seen.reverse)
else value = value.appendedAll(seen.reverse)
Some(rest)
}
def tryToSet(args: List[String]) = tryToSetArgs(args, halting = true)
override def tryToSetColon(args: List[String]) = tryToSetArgs(args, halting = false)
Expand Down
7 changes: 7 additions & 0 deletions test/files/neg/t12098.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
t12098.scala:9: warning: method f in class C is deprecated (since 1.0): don't
def g() = f()
^
warning: 1 lint warning; change -Wconf for cat=lint to display individual messages
error: No warnings can be incurred under -Werror.
2 warnings
1 error
10 changes: 10 additions & 0 deletions test/files/neg/t12098.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
//xlint supersedes default Wconf setting, which is ws warn-summary for deprecation
//scalac: -Werror -Wconf:cat=lint-missing-interpolator:ws -Xlint

class C(i: Int) {
def p() = println("hi $i")

@deprecated("don't", since="1.0") def f() = 42

def g() = f()
}
3 changes: 2 additions & 1 deletion test/junit/scala/tools/nsc/reporters/WConfTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class WConfTest extends BytecodeTesting {

def reports(code: String, extraWconf: String = "", lint: Boolean = false): List[Info] = {
// lint has a postSetHook to enable `deprecated`, which in turn adds to `Wconf`,
// but since we override Wconf after, that effect is cancelled
// but since we clear and initialize Wconf after enabling lint, that effect is cancelled.
if (lint) {
global.settings.warnUnused.clear()
global.settings.lint.tryToSet(List("_"))
Expand Down Expand Up @@ -212,6 +212,7 @@ class WConfTest extends BytecodeTesting {
def lint(): Unit = {
check(infos(code, "cat=lint:i"), Nil)
check(infos(code, "cat=lint:i", lint = true), List(l2, l23))
check(reports(code, "any:s,cat=lint:ws", lint = true), Nil)
check(reports(code, "cat=lint:ws,any:s", lint = true), List((-1, "2 lint warnings")))
check(infos(code, "cat=lint-deprecation:i", lint = true), List(l2))
check(infos(code, "cat=lint-adapted-args:i", lint = true), List(l23))
Expand Down
20 changes: 20 additions & 0 deletions test/junit/scala/tools/nsc/settings/SettingsTest.scala
Original file line number Diff line number Diff line change
Expand Up @@ -267,4 +267,24 @@ class SettingsTest {
assertEquals("processing stops at bad option", 2, rest.length)
assertEquals(2, errors.size) // missing arg and bad option
}
@Test def `t12098 MultiStringSetting with prepend handles non-colon args`(): Unit = {
import scala.collection.mutable.ListBuffer
val errors = ListBuffer.empty[String]
val settings = new Settings(errors.addOne)
val (ok, rest) = settings.processArguments("-Wconf" :: "help" :: "-Vdebug" :: "x.scala" :: Nil, true)
assert("processing should succeed", ok)
assertEquals("processing stops at argument", 1, rest.length)
assertEquals("processing stops at the correct argument", "x.scala", rest.head)
assertEquals(0, errors.size)
assert(settings.debug)
assert(settings.Wconf.isHelping)
}
@Test def `t12098 MultiStringSetting prepends`(): Unit = {
val settings = new Settings(msg => fail(s"Unexpected error: $msg"))
val (ok, rest) = settings.processArguments("-Wconf:cat=lint-missing-interpolator:ws" :: "-Xlint" :: "x.scala" :: Nil, true)
assert("processing should succeed", ok)
assert(settings.warnMissingInterpolator)
assert(settings.lintDeprecation)
// test/files/neg/t12098.scala shows that cat=deprecation:w due to xlint supersedes default cat=deprecation:ws
}
}

0 comments on commit 98384fd

Please sign in to comment.