Skip to content

Commit dc70647

Browse files
Backport "Add origin filter to WConf, DeprecationWarning" to LTS (#22109)
Backports #21404 to the 3.3.5. PR submitted by the release tooling. [skip ci]
2 parents 3023607 + 2992889 commit dc70647

12 files changed

+92
-29
lines changed

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

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ abstract class CompilerCommand extends CliCommand:
99

1010
final def helpMsg(using settings: ScalaSettings)(using SettingsState, Context): String =
1111
settings.allSettings.find(isHelping) match
12+
case Some(s @ settings.language) => availableOptionsMsg(_ == s)
1213
case Some(s) => s.description
1314
case _ =>
1415
if (settings.help.value) usageMessage

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

+5-1
Original file line numberDiff line numberDiff line change
@@ -236,7 +236,7 @@ private sealed trait WarningSettings:
236236
"patterns",
237237
default = List(),
238238
descr =
239-
s"""Configure compiler warnings.
239+
raw"""Configure compiler warnings.
240240
|Syntax: -Wconf:<filters>:<action>,<filters>:<action>,...
241241
|multiple <filters> are combined with &, i.e., <filter>&...&<filter>
242242
|
@@ -257,6 +257,9 @@ private sealed trait WarningSettings:
257257
| - Source location: src=regex
258258
| The regex is evaluated against the full source path.
259259
|
260+
| - Origin of warning: origin=regex
261+
| The regex must match the full name (`package.Class.method`) of the deprecated entity.
262+
|
260263
|In verbose warning mode the compiler prints matching filters for warnings.
261264
|Verbose mode can be enabled globally using `-Wconf:any:verbose`, or locally
262265
|using the @nowarn annotation (example: `@nowarn("v") def test = try 1`).
@@ -276,6 +279,7 @@ private sealed trait WarningSettings:
276279
|Examples:
277280
| - change every warning into an error: -Wconf:any:error
278281
| - silence deprecations: -Wconf:cat=deprecation:s
282+
| - silence a deprecation: -Wconf:origin=java\.lang\.Thread\.getId:s
279283
| - silence warnings in src_managed directory: -Wconf:src=src_managed/.*:s
280284
|
281285
|Note: on the command-line you might need to quote configurations containing `*` or `&`

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@ object report:
2222
private def issueWarning(warning: Warning)(using Context): Unit =
2323
ctx.reporter.report(warning)
2424

25-
def deprecationWarning(msg: Message, pos: SrcPos)(using Context): Unit =
26-
issueWarning(new DeprecationWarning(msg, pos.sourcePos))
25+
def deprecationWarning(msg: Message, pos: SrcPos, origin: String = "")(using Context): Unit =
26+
issueWarning(new DeprecationWarning(msg, pos.sourcePos, origin))
2727

2828
def migrationWarning(msg: Message, pos: SrcPos)(using Context): Unit =
2929
issueWarning(new MigrationWarning(msg, pos.sourcePos))

compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala

+2-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,8 @@ object Diagnostic:
7575

7676
class DeprecationWarning(
7777
msg: Message,
78-
pos: SourcePosition
78+
pos: SourcePosition,
79+
val origin: String
7980
) extends ConditionalWarning(msg, pos) {
8081
def enablingOption(using Context): Setting[Boolean] = ctx.settings.deprecation
8182
}

compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala

+22-17
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ import util.{ SourcePosition, NoSourcePosition }
1515
import util.Chars.{ LF, CR, FF, SU }
1616
import scala.annotation.switch
1717

18-
import scala.collection.mutable
18+
import scala.collection.mutable.StringBuilder
1919

2020
trait MessageRendering {
2121
import Highlight.*
@@ -209,22 +209,27 @@ trait MessageRendering {
209209
sb.toString
210210
}
211211

212-
private def appendFilterHelp(dia: Diagnostic, sb: mutable.StringBuilder): Unit =
213-
import dia.*
212+
private def appendFilterHelp(dia: Diagnostic, sb: StringBuilder): Unit =
213+
import dia.msg
214214
val hasId = msg.errorId.errorNumber >= 0
215-
val category = dia match {
216-
case _: UncheckedWarning => "unchecked"
217-
case _: DeprecationWarning => "deprecation"
218-
case _: FeatureWarning => "feature"
219-
case _ => ""
220-
}
221-
if (hasId || category.nonEmpty)
222-
sb.append(EOL).append("Matching filters for @nowarn or -Wconf:")
223-
if (hasId)
224-
sb.append(EOL).append(" - id=E").append(msg.errorId.errorNumber)
225-
sb.append(EOL).append(" - name=").append(msg.errorId.productPrefix.stripSuffix("ID"))
226-
if (category.nonEmpty)
227-
sb.append(EOL).append(" - cat=").append(category)
215+
val (category, origin) = dia match
216+
case _: UncheckedWarning => ("unchecked", "")
217+
case w: DeprecationWarning => ("deprecation", w.origin)
218+
case _: FeatureWarning => ("feature", "")
219+
case _ => ("", "")
220+
var entitled = false
221+
def addHelp(what: String)(value: String): Unit =
222+
if !entitled then
223+
sb.append(EOL).append("Matching filters for @nowarn or -Wconf:")
224+
entitled = true
225+
sb.append(EOL).append(" - ").append(what).append(value)
226+
if hasId then
227+
addHelp("id=E")(msg.errorId.errorNumber.toString)
228+
addHelp("name=")(msg.errorId.productPrefix.stripSuffix("ID"))
229+
if category.nonEmpty then
230+
addHelp("cat=")(category)
231+
if origin.nonEmpty then
232+
addHelp("origin=")(origin)
228233

229234
/** The whole message rendered from `msg` */
230235
def messageAndPos(dia: Diagnostic)(using Context): String = {
@@ -236,7 +241,7 @@ trait MessageRendering {
236241
else 0
237242
given Level = Level(level)
238243
given Offset = Offset(maxLineNumber.toString.length + 2)
239-
val sb = mutable.StringBuilder()
244+
val sb = StringBuilder()
240245
val posString = posStr(pos, msg, diagnosticLevel(dia))
241246
if (posString.nonEmpty) sb.append(posString).append(EOL)
242247
if (pos.exists) {

compiler/src/dotty/tools/dotc/reporting/WConf.scala

+7-2
Original file line numberDiff line numberDiff line change
@@ -19,23 +19,27 @@ enum MessageFilter:
1919
case Deprecated => message.isInstanceOf[Diagnostic.DeprecationWarning]
2020
case Feature => message.isInstanceOf[Diagnostic.FeatureWarning]
2121
case Unchecked => message.isInstanceOf[Diagnostic.UncheckedWarning]
22+
case MessageID(errorId) => message.msg.errorId == errorId
2223
case MessagePattern(pattern) =>
2324
val noHighlight = message.msg.message.replaceAll("\\e\\[[\\d;]*[^\\d;]","")
2425
pattern.findFirstIn(noHighlight).nonEmpty
25-
case MessageID(errorId) => message.msg.errorId == errorId
2626
case SourcePattern(pattern) =>
2727
val source = message.position.orElse(NoSourcePosition).source()
2828
val path = source.jfile()
2929
.map(_.toPath.toAbsolutePath.toUri.normalize().getRawPath)
3030
.orElse(source.path())
3131
pattern.findFirstIn(path).nonEmpty
32-
32+
case Origin(pattern) =>
33+
message match
34+
case message: Diagnostic.DeprecationWarning => pattern.findFirstIn(message.origin).nonEmpty
35+
case _ => false
3336
case None => false
3437

3538
case Any, Deprecated, Feature, Unchecked, None
3639
case MessagePattern(pattern: Regex)
3740
case MessageID(errorId: ErrorMessageID)
3841
case SourcePattern(pattern: Regex)
42+
case Origin(pattern: Regex)
3943

4044
enum Action:
4145
case Error, Warning, Verbose, Info, Silent
@@ -96,6 +100,7 @@ object WConf:
96100
case _ => Left(s"unknown category: $conf")
97101

98102
case "src" => regex(conf).map(SourcePattern.apply)
103+
case "origin" => regex(conf).map(Origin.apply)
99104

100105
case _ => Left(s"unknown filter: $filter")
101106
case _ => Left(s"unknown filter: $s")

compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala

+2-2
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class CrossVersionChecks extends MiniPhase:
7878
do
7979
val msg = annot.argumentConstantString(0).map(msg => s": $msg").getOrElse("")
8080
val since = annot.argumentConstantString(1).map(version => s" (since: $version)").getOrElse("")
81-
report.deprecationWarning(em"inheritance from $psym is deprecated$since$msg", parent.srcPos)
81+
report.deprecationWarning(em"inheritance from $psym is deprecated$since$msg", parent.srcPos, origin=psym.showFullName)
8282
}
8383

8484
override def transformValDef(tree: ValDef)(using Context): ValDef =
@@ -169,7 +169,7 @@ object CrossVersionChecks:
169169
def maybeWarn(annotee: Symbol, annot: Annotation) = if !skipWarning(sym) then
170170
val message = annot.argumentConstantString(0).filter(!_.isEmpty).map(": " + _).getOrElse("")
171171
val since = annot.argumentConstantString(1).filter(!_.isEmpty).map(" since " + _).getOrElse("")
172-
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos)
172+
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos, origin=annotee.showFullName)
173173
sym.getAnnotation(defn.DeprecatedAnnot) match
174174
case Some(annot) => maybeWarn(sym, annot)
175175
case _ =>

compiler/src/dotty/tools/dotc/typer/RefChecks.scala

+3-1
Original file line numberDiff line numberDiff line change
@@ -402,7 +402,9 @@ object RefChecks {
402402
def overrideDeprecation(what: String, member: Symbol, other: Symbol, fix: String): Unit =
403403
report.deprecationWarning(
404404
em"overriding $what${infoStringWithLocation(other)} is deprecated;\n ${infoString(member)} should be $fix.",
405-
if member.owner == clazz then member.srcPos else clazz.srcPos)
405+
if member.owner == clazz then member.srcPos else clazz.srcPos,
406+
origin = other.showFullName
407+
)
406408

407409
def autoOverride(sym: Symbol) =
408410
sym.is(Synthetic) && (

compiler/test/dotty/tools/dotc/config/ScalaSettingsTests.scala

+4-3
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ class ScalaSettingsTests:
8080
val conf = sets.Wconf.valueIn(proc.sstate)
8181
val sut = reporting.WConf.fromSettings(conf).getOrElse(???)
8282
val msg = "There was a problem!".toMessage
83-
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition)
83+
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition, origin="")
8484
assertEquals(Action.Silent, sut.action(depr))
8585
val feat = new Diagnostic.FeatureWarning(msg, util.NoSourcePosition)
8686
assertEquals(Action.Error, sut.action(feat))
@@ -97,7 +97,7 @@ class ScalaSettingsTests:
9797
val proc = sets.processArguments(sumy, processAll = true, skipped = Nil)
9898
val conf = sets.Wconf.valueIn(proc.sstate)
9999
val msg = "Don't use that!".toMessage
100-
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition)
100+
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition, origin="")
101101
val sut = reporting.WConf.fromSettings(conf).getOrElse(???)
102102
assertEquals(Action.Silent, sut.action(depr))
103103

@@ -193,7 +193,8 @@ class ScalaSettingsTests:
193193
util.SourcePosition(
194194
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
195195
span = util.Spans.Span(1L)
196-
)
196+
),
197+
origin="",
197198
)
198199
)
199200
assertEquals(result, Right(reporting.Action.Error))
+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
-- Deprecation Warning: tests/warn/deprecated-origin-verbose.scala:12:18 -----------------------------------------------
2+
12 | class D extends C // warn
3+
| ^
4+
| class C in package p is deprecated since 1.0: Old style
5+
Matching filters for @nowarn or -Wconf:
6+
- cat=deprecation
7+
- origin=p.C
8+
-- Deprecation Warning: tests/warn/deprecated-origin-verbose.scala:13:20 -----------------------------------------------
9+
13 | class Oil extends Crude // warn
10+
| ^^^^^
11+
| class Crude in package p is deprecated since 1.0: Bad style
12+
Matching filters for @nowarn or -Wconf:
13+
- cat=deprecation
14+
- origin=p.Crude
+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//> using options -deprecation -Wconf:any:verbose
2+
3+
package p:
4+
@deprecated("Old style", since="1.0")
5+
class C
6+
@deprecated("Bad style", since="1.0")
7+
class Crude
8+
9+
package q:
10+
import annotation.*
11+
import p.*
12+
class D extends C // warn
13+
class Oil extends Crude // warn
14+
@nowarn("""origin=p\.Crude""")
15+
class Language extends Crude // nowarn obvs

tests/warn/deprecated-origin.scala

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
//> using options -deprecation -Wconf:origin=p\.C$:s
2+
3+
package p:
4+
@deprecated("Old style", since="1.0")
5+
class C
6+
@deprecated("Bad style", since="1.0")
7+
class Crude
8+
9+
package q:
10+
import annotation.*
11+
import p.*
12+
class D extends C // nowarn - C$ pattern avoids matching Crude
13+
class Oil extends Crude // warn
14+
@nowarn("""origin=p\.Crude""")
15+
class Language extends Crude // nowarn obvs

0 commit comments

Comments
 (0)