Skip to content

Commit

Permalink
Use colon arg uniformly
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Mar 28, 2022
1 parent a1dcb4c commit 9036bd4
Show file tree
Hide file tree
Showing 4 changed files with 59 additions and 30 deletions.
26 changes: 8 additions & 18 deletions src/compiler/scala/tools/nsc/settings/AbsSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package settings

trait AbsSettings extends scala.reflect.internal.settings.AbsSettings {
type Setting <: AbsSetting // Fix to the concrete Setting type
type ResultOfTryToSet // List[String] in mutable, (Settings, List[String]) in immutable
type ResultOfTryToSet // List[String] in MutableSettings
def errorFn: String => Unit
protected def allSettings: scala.collection.Map[String, Setting]

Expand Down Expand Up @@ -66,14 +66,6 @@ trait AbsSettings extends scala.reflect.internal.settings.AbsSettings {
/* For tools which need to populate lists of available choices */
def choices : List[String] = Nil

/** In mutable Settings, these return the same object with a var set.
* In immutable, of course they will return a new object, which means
* we can't use "this.type", at least not in a non-casty manner, which
* is unfortunate because we lose type information without it.
*
* ...but now they're this.type because of scala/bug#3462. The immutable
* side doesn't exist yet anyway.
*/
def withAbbreviation(name: String): this.type
def withHelpSyntax(help: String): this.type
def withDeprecationMessage(msg: String): this.type
Expand Down Expand Up @@ -101,19 +93,17 @@ trait AbsSettings extends scala.reflect.internal.settings.AbsSettings {
/** The help message to be printed if [[isHelping]]. */
def help: String = ""

/** After correct Setting has been selected, tryToSet is called with the
* remainder of the command line. It consumes any applicable arguments and
* returns the unconsumed ones.
/** Setting is presented the remaining command line arguments.
* It should consume any applicable args and return the rest,
* or `None` on error.
*/
protected[nsc] def tryToSet(args: List[String]): Option[ResultOfTryToSet]

/** Commands which can take lists of arguments in form -Xfoo:bar,baz override
* this method and accept them as a list. It returns List[String] for
* consistency with tryToSet, and should return its incoming arguments
* unmodified on failure, and Nil on success.
/** Setting is presented arguments in form -Xfoo:bar,baz.
* It should consume all the arguments and return an empty list,
* or `None` on error. Unconsumed args may error.
*/
protected[nsc] def tryToSetColon(args: List[String]): Option[ResultOfTryToSet] =
errorAndValue(s"'$name' does not accept multiple arguments", None)
protected[nsc] def tryToSetColon(args: List[String]): Option[ResultOfTryToSet]

/** Attempt to set from a properties file style property value.
* Currently used by Eclipse SDT only.
Expand Down
42 changes: 30 additions & 12 deletions src/compiler/scala/tools/nsc/settings/MutableSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -398,11 +398,19 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)

def errorMsg() = errorFn(s"invalid setting for $name $getValidText")

def tryToSet(args: List[String]) =
if (args.isEmpty) errorAndValue("missing argument", None)
else parseArgument(args.head) match {
case Some(i) => value = i ; Some(args.tail)
case None => errorMsg() ; None
def tryToSet(args: List[String]): Option[ResultOfTryToSet] =
args match {
case h :: rest =>
parseArgument(h) match {
case Some(i) => value = i; Some(rest)
case None => errorMsg(); None
}
case Nil => errorAndValue("missing argument", None)
}
def tryToSetColon(args: List[String]): Option[ResultOfTryToSet] =
args match {
case Nil | _ :: Nil => tryToSet(args)
case _ => errorAndValue("too many arguments", None)
}

def unparse: List[String] =
Expand Down Expand Up @@ -452,6 +460,7 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)
case _ =>
None
}
def tryToSetColon(args: List[String]): Option[ResultOfTryToSet] = errorAndValue(s"bad argument for $name", None)
override def respondsTo(token: String) = token startsWith prefix
def unparse: List[String] = value
}
Expand All @@ -475,6 +484,11 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)
case "help" :: rest if helpText.nonEmpty => sawHelp = true ; Some(rest)
case h :: rest => value = h ; Some(rest)
}
def tryToSetColon(args: List[String]): Option[ResultOfTryToSet] =
args match {
case Nil | _ :: Nil => tryToSet(args)
case _ => errorAndValue("too many arguments", None)
}
def unparse: List[String] = if (value == default) Nil else List(name, value)

override def isHelping: Boolean = sawHelp
Expand All @@ -498,15 +512,15 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)

// This method is invoked if there are no colonated args. In this case the default value is
// used. No arguments are consumed.
override def tryToSet(args: List[String]) = {
def tryToSet(args: List[String]) = {
default match {
case Some(d) => value = d
case None => errorFn(s"$name requires an argument, the syntax is $helpSyntax")
}
Some(args)
}

override def tryToSetColon(args: List[String]) = args match {
def tryToSetColon(args: List[String]) = args match {
case x :: xs => value = ScalaVersion(x, errorFn); Some(xs)
case nil => Some(nil)
}
Expand Down Expand Up @@ -678,7 +692,7 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)
}

def tryToSet(args: List[String]) = tryToSetArgs(args, halting = true)
override def tryToSetColon(args: List[String]) = tryToSetArgs(args, halting = false)
def tryToSetColon(args: List[String]) = tryToSetArgs(args, halting = false)
override def tryToSetFromPropertyValue(s: String) = tryToSet(s.trim.split(',').toList) // used from ide

/** Try to set args, handling "help" and default.
Expand Down Expand Up @@ -805,7 +819,7 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)
Some(rest)
}
def tryToSet(args: List[String]) = tryToSetArgs(args, halting = true)
override def tryToSetColon(args: List[String]) = tryToSetArgs(args, halting = false)
def tryToSetColon(args: List[String]) = tryToSetArgs(args, halting = false)
override def tryToSetFromPropertyValue(s: String) = tryToSet(s.trim.split(',').toList) // used from ide

def clear(): Unit = (v = Nil)
Expand Down Expand Up @@ -851,9 +865,13 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)
override def isHelping = sawHelp
override def help = usageErrorMessage

def tryToSet(args: List[String]) = errorAndValue(usageErrorMessage, None)
def tryToSet(args: List[String]) =
args match {
case Nil | Optionlike() :: _ => errorAndValue(usageErrorMessage, None)
case arg :: rest => tryToSetColon(List(arg)).map(_ => rest)
}

override def tryToSetColon(args: List[String]) = args map _preSetHook match {
def tryToSetColon(args: List[String]) = args map _preSetHook match {
case Nil => errorAndValue(usageErrorMessage, None)
case List("help") => sawHelp = true; SomeOfNil
case List(x) if choices contains x => value = x ; SomeOfNil
Expand Down Expand Up @@ -916,7 +934,7 @@ class MutableSettings(val errorFn: String => Unit, val pathFactory: PathFactory)

private def splitDefault = default.split(',').toList

override def tryToSetColon(args: List[String]) = try {
def tryToSetColon(args: List[String]) = try {
args match {
case Nil => if (default == "") errorAndValue("missing phase", None)
else tryToSetColon(splitDefault)
Expand Down
4 changes: 4 additions & 0 deletions test/files/neg/t12543.check
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
t12543.scala:10: error: value of is not a member of object java.util.List
val ss = java.util.List.of("Hello", who)
^
1 error
17 changes: 17 additions & 0 deletions test/files/neg/t12543.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@

// scalac: -Werror -release 8

import scala.jdk.CollectionConverters._
//import jdk.CollectionConverters._ // scala/bug/issues/12566
import sun._

object HelloWorld {
def main(args: Array[String]) = {
val ss = java.util.List.of("Hello", who)
println(ss.asScala.mkString("", ", ", "!"))
}
}

object sun {
val who = "world"
}

0 comments on commit 9036bd4

Please sign in to comment.