Skip to content

Commit 77d194f

Browse files
committed
Add origin filter to WConf, DeprecationWarning
1 parent 355910d commit 77d194f

File tree

7 files changed

+35
-11
lines changed

7 files changed

+35
-11
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,8 +23,8 @@ object report:
2323
private def issueWarning(warning: Warning)(using Context): Unit =
2424
ctx.reporter.report(warning)
2525

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

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

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

Lines changed: 2 additions & 1 deletion
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/WConf.scala

Lines changed: 7 additions & 2 deletions
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

Lines changed: 2 additions & 2 deletions
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 =
@@ -171,7 +171,7 @@ object CrossVersionChecks:
171171
def maybeWarn(annotee: Symbol, annot: Annotation) = if !skipWarning(sym) then
172172
val message = annot.argumentConstantString(0).filter(!_.isEmpty).map(": " + _).getOrElse("")
173173
val since = annot.argumentConstantString(1).filter(!_.isEmpty).map(" since " + _).getOrElse("")
174-
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos)
174+
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos, origin=annotee.showFullName)
175175
sym.getAnnotation(defn.DeprecatedAnnot) match
176176
case Some(annot) => maybeWarn(sym, annot)
177177
case _ =>

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

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -484,7 +484,9 @@ object RefChecks {
484484
def overrideDeprecation(what: String, member: Symbol, other: Symbol, fix: String): Unit =
485485
report.deprecationWarning(
486486
em"overriding $what${infoStringWithLocation(other)} is deprecated;\n ${infoString(member)} should be $fix.",
487-
if member.owner == clazz then member.srcPos else clazz.srcPos)
487+
if member.owner == clazz then member.srcPos else clazz.srcPos,
488+
origin = other.showFullName
489+
)
488490

489491
def autoOverride(sym: Symbol) =
490492
sym.is(Synthetic) && (

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

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ScalaSettingsTests:
8181
val conf = sets.Wconf.valueIn(proc.sstate)
8282
val sut = reporting.WConf.fromSettings(conf).getOrElse(???)
8383
val msg = "There was a problem!".toMessage
84-
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition)
84+
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition, origin="")
8585
assertEquals(Action.Silent, sut.action(depr))
8686
val feat = new Diagnostic.FeatureWarning(msg, util.NoSourcePosition)
8787
assertEquals(Action.Error, sut.action(feat))
@@ -197,7 +197,7 @@ class ScalaSettingsTests:
197197
val proc = sets.processArguments(sumy, processAll = true, skipped = Nil)
198198
val conf = sets.Wconf.valueIn(proc.sstate)
199199
val msg = "Don't use that!".toMessage
200-
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition)
200+
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition, origin="")
201201
val sut = reporting.WConf.fromSettings(conf).getOrElse(???)
202202
assertEquals(Action.Silent, sut.action(depr))
203203

@@ -293,7 +293,8 @@ class ScalaSettingsTests:
293293
util.SourcePosition(
294294
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
295295
span = util.Spans.Span(1L)
296-
)
296+
),
297+
origin="",
297298
)
298299
)
299300
assertEquals(result, Right(reporting.Action.Error))

tests/warn/deprecated-origin.scala

Lines changed: 15 additions & 0 deletions
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)