Skip to content

Commit

Permalink
Warn instead of fail for invalid -Xlint args
Browse files Browse the repository at this point in the history
  • Loading branch information
schuetzcarl committed Jun 20, 2023
1 parent 175d4f3 commit 07f6581
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 8 deletions.
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/Compiler.scala
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ class Compiler {
protected def frontendPhases: List[List[Phase]] =
List(new Parser) :: // Compiler frontend: scanner, parser
List(new TyperPhase) :: // Compiler frontend: namer, typer
List(new CheckShadowing, new CheckUnused.PostTyper) :: // Check for unused elements // Check for shadowing elements
List(new CheckUnused.PostTyper, new CheckShadowing) :: // Check for unused elements and shadowing elements
List(new YCheckPositions) :: // YCheck positions
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
List(new semanticdb.ExtractSemanticDB) :: // Extract info into .semanticdb files
Expand Down
2 changes: 1 addition & 1 deletion compiler/src/dotty/tools/dotc/config/ScalaSettings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -307,7 +307,7 @@ private sealed trait XSettings:

val XmacroSettings: Setting[List[String]] = MultiStringSetting("-Xmacro-settings", "setting1,setting2,..settingN", "List of settings which exposed to the macros")

val Xlint: Setting[List[ChoiceWithHelp[String]]] = MultiChoiceHelpSetting(
val Xlint: Setting[List[ChoiceWithHelp[String]]] = UncompleteMultiChoiceHelpSetting(
name = "-Xlint",
helpArg = "advanced warning",
descr = "Enable or disable specific `lint` warnings",
Expand Down
16 changes: 14 additions & 2 deletions compiler/src/dotty/tools/dotc/config/Settings.scala
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ object Settings:
prefix: String = "",
aliases: List[String] = Nil,
depends: List[(Setting[?], Any)] = Nil,
ignoreInvalidArgs: Boolean = false,
propertyClass: Option[Class[?]] = None)(private[Settings] val idx: Int) {

private var changed: Boolean = false
Expand Down Expand Up @@ -104,8 +105,16 @@ object Settings:
def fail(msg: String, args: List[String]) =
ArgsSummary(sstate, args, errors :+ msg, warnings)

def warn(msg: String, args: List[String]) =
ArgsSummary(sstate, args, errors, warnings :+ msg)

def missingArg =
fail(s"missing argument for option $name", args)
val msg = s"missing argument for option $name"
if ignoreInvalidArgs then warn(msg + ", the tag was ignored", args) else fail(msg, args)

def invalidChoices(invalid: List[String]) =
val msg = s"invalid choice(s) for $name: ${invalid.mkString(",")}"
if ignoreInvalidArgs then warn(msg + ", the tag was ignored", args) else fail(msg, args)

def setBoolean(argValue: String, args: List[String]) =
if argValue.equalsIgnoreCase("true") || argValue.isEmpty then update(true, args)
Expand Down Expand Up @@ -144,7 +153,7 @@ object Settings:
choices match
case Some(valid) => strings.filterNot(valid.contains) match
case Nil => update(strings, args)
case invalid => fail(s"invalid choice(s) for $name: ${invalid.mkString(",")}", args)
case invalid => invalidChoices(invalid)
case _ => update(strings, args)
case (StringTag, _) if argRest.nonEmpty || choices.exists(_.contains("")) =>
setString(argRest, args)
Expand Down Expand Up @@ -287,6 +296,9 @@ object Settings:
def MultiChoiceHelpSetting(name: String, helpArg: String, descr: String, choices: List[ChoiceWithHelp[String]], default: List[ChoiceWithHelp[String]], aliases: List[String] = Nil): Setting[List[ChoiceWithHelp[String]]] =
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases))

def UncompleteMultiChoiceHelpSetting(name: String, helpArg: String, descr: String, choices: List[ChoiceWithHelp[String]], default: List[ChoiceWithHelp[String]], aliases: List[String] = Nil): Setting[List[ChoiceWithHelp[String]]] =
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases, ignoreInvalidArgs = true))

def IntSetting(name: String, descr: String, default: Int, aliases: List[String] = Nil): Setting[Int] =
publish(Setting(name, descr, default, aliases = aliases))

Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckShadowing.scala
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ class CheckShadowing extends MiniPhase:
ctx

override def prepareForOther(tree: tpd.Tree)(using Context): Context =
importTraverser(tree.symbol).traverse(tree)
importTraverser.traverse(tree)
ctx

override def prepareForValDef(tree: tpd.ValDef)(using Context): Context =
Expand Down Expand Up @@ -152,7 +152,7 @@ class CheckShadowing extends MiniPhase:
end nestedTypeTraverser

// To reach the imports during a miniphase traversal
private def importTraverser(parent: Symbol) = new TreeTraverser:
private def importTraverser = new TreeTraverser:
import tpd._

override def traverse(tree: tpd.Tree)(using Context): Unit =
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/transform/CheckUnused.scala
Original file line number Diff line number Diff line change
Expand Up @@ -291,9 +291,9 @@ class CheckUnused private (phaseMode: CheckUnused.PhaseMode, suffix: String, _ke
case UnusedSymbol(t, _, WarnTypes.PatVars) =>
report.warning(s"unused pattern variable", t)
case UnusedSymbol(t, _, WarnTypes.UnsetLocals) =>
report.warning(s"unset local variable", t)
report.warning(s"unset local variable, consider using an immutable val instead", t)
case UnusedSymbol(t, _, WarnTypes.UnsetPrivates) =>
report.warning(s"unset private variable", t)
report.warning(s"unset private variable, consider using an immutable val instead", t)
}

end CheckUnused
Expand Down

0 comments on commit 07f6581

Please sign in to comment.