Skip to content

Commit fd85735

Browse files
Backport "Implement -Xlint:private-shadow, type-parameter-shadow" to LTS (#20628)
Backports #17622 to the LTS branch. PR submitted by the release tooling. [skip ci]
2 parents 12a3aab + bb55300 commit fd85735

17 files changed

+670
-8
lines changed

compiler/src/dotty/tools/dotc/Compiler.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,8 @@ class Compiler {
3535
protected def frontendPhases: List[List[Phase]] =
3636
List(new Parser) :: // Compiler frontend: scanner, parser
3737
List(new TyperPhase) :: // Compiler frontend: namer, typer
38-
List(new CheckUnused.PostTyper) :: // Check for unused elements
38+
List(new CheckUnused.PostTyper) :: // Check for unused elements
39+
List(new CheckShadowing) :: // Check shadowing elements
3940
List(new YCheckPositions) :: // YCheck positions
4041
List(new sbt.ExtractDependencies) :: // Sends information on classes' dependencies to sbt via callbacks
4142
List(new semanticdb.ExtractSemanticDB) :: // Extract info into .semanticdb files

compiler/src/dotty/tools/dotc/config/ScalaSettings.scala

+22-1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import dotty.tools.dotc.config.SourceVersion
99
import dotty.tools.dotc.core.Contexts._
1010
import dotty.tools.dotc.rewrites.Rewrites
1111
import dotty.tools.io.{AbstractFile, Directory, JDK9Reflectors, PlainDirectory}
12+
import Setting.ChoiceWithHelp
1213

1314
import scala.util.chaining._
1415

@@ -156,7 +157,6 @@ private sealed trait VerboseSettings:
156157
*/
157158
private sealed trait WarningSettings:
158159
self: SettingGroup =>
159-
import Setting.ChoiceWithHelp
160160

161161
val Whelp: Setting[Boolean] = BooleanSetting("-W", "Print a synopsis of warning options.")
162162
val XfatalWarnings: Setting[Boolean] = BooleanSetting("-Werror", "Fail the compilation if there are any warnings.", aliases = List("-Xfatal-warnings"))
@@ -307,6 +307,27 @@ private sealed trait XSettings:
307307
}
308308

309309
val XmacroSettings: Setting[List[String]] = MultiStringSetting("-Xmacro-settings", "setting1,setting2,..settingN", "List of settings which exposed to the macros")
310+
311+
val Xlint: Setting[List[ChoiceWithHelp[String]]] = UncompleteMultiChoiceHelpSetting(
312+
name = "-Xlint",
313+
helpArg = "advanced warning",
314+
descr = "Enable or disable specific `lint` warnings",
315+
choices = List(
316+
ChoiceWithHelp("all", ""),
317+
ChoiceWithHelp("private-shadow", "Warn if a private field or class parameter shadows a superclass field"),
318+
ChoiceWithHelp("type-parameter-shadow", "Warn when a type parameter shadows a type already in the scope"),
319+
),
320+
default = Nil
321+
)
322+
323+
object XlintHas:
324+
def allOr(s: String)(using Context) =
325+
Xlint.value.pipe(us => us.contains("all") || us.contains(s))
326+
def privateShadow(using Context) =
327+
allOr("private-shadow")
328+
def typeParameterShadow(using Context) =
329+
allOr("type-parameter-shadow")
330+
310331
end XSettings
311332

312333
/** -Y "Forking" as in forked tongue or "Private" settings */

compiler/src/dotty/tools/dotc/config/Settings.scala

+14-2
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@ object Settings:
6262
prefix: String = "",
6363
aliases: List[String] = Nil,
6464
depends: List[(Setting[?], Any)] = Nil,
65+
ignoreInvalidArgs: Boolean = false,
6566
propertyClass: Option[Class[?]] = None)(private[Settings] val idx: Int) {
6667

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

108+
def warn(msg: String, args: List[String]) =
109+
ArgsSummary(sstate, args, errors, warnings :+ msg)
110+
107111
def missingArg =
108-
fail(s"missing argument for option $name", args)
112+
val msg = s"missing argument for option $name"
113+
if ignoreInvalidArgs then warn(msg + ", the tag was ignored", args) else fail(msg, args)
114+
115+
def invalidChoices(invalid: List[String]) =
116+
val msg = s"invalid choice(s) for $name: ${invalid.mkString(",")}"
117+
if ignoreInvalidArgs then warn(msg + ", the tag was ignored", args) else fail(msg, args)
109118

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

299+
def UncompleteMultiChoiceHelpSetting(name: String, helpArg: String, descr: String, choices: List[ChoiceWithHelp[String]], default: List[ChoiceWithHelp[String]], aliases: List[String] = Nil): Setting[List[ChoiceWithHelp[String]]] =
300+
publish(Setting(name, descr, default, helpArg, Some(choices), aliases = aliases, ignoreInvalidArgs = true))
301+
290302
def IntSetting(name: String, descr: String, default: Int, aliases: List[String] = Nil): Setting[Int] =
291303
publish(Setting(name, descr, default, aliases = aliases))
292304

0 commit comments

Comments
 (0)