Skip to content

Commit dadc123

Browse files
som-snyttWojciechMazur
authored andcommitted
Add origin filter to WConf, DeprecationWarning
[Cherry-picked 6953056]
1 parent 493cf40 commit dadc123

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
@@ -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

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 =
@@ -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

Lines changed: 3 additions & 1 deletion
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

Lines changed: 4 additions & 3 deletions
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))

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)