Skip to content

Commit

Permalink
Add origin filter to WConf, DeprecationWarning
Browse files Browse the repository at this point in the history
  • Loading branch information
som-snytt committed Aug 20, 2024
1 parent 355910d commit 77d194f
Show file tree
Hide file tree
Showing 7 changed files with 35 additions and 11 deletions.
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/report.scala
Original file line number Diff line number Diff line change
Expand Up @@ -23,8 +23,8 @@ object report:
private def issueWarning(warning: Warning)(using Context): Unit =
ctx.reporter.report(warning)

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

def migrationWarning(msg: Message, pos: SrcPos)(using Context): Unit =
issueWarning(new MigrationWarning(msg, pos.sourcePos))
Expand Down
3 changes: 2 additions & 1 deletion compiler/src/dotty/tools/dotc/reporting/Diagnostic.scala
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ object Diagnostic:

class DeprecationWarning(
msg: Message,
pos: SourcePosition
pos: SourcePosition,
val origin: String
) extends ConditionalWarning(msg, pos) {
def enablingOption(using Context): Setting[Boolean] = ctx.settings.deprecation
}
Expand Down
9 changes: 7 additions & 2 deletions compiler/src/dotty/tools/dotc/reporting/WConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,23 +19,27 @@ enum MessageFilter:
case Deprecated => message.isInstanceOf[Diagnostic.DeprecationWarning]
case Feature => message.isInstanceOf[Diagnostic.FeatureWarning]
case Unchecked => message.isInstanceOf[Diagnostic.UncheckedWarning]
case MessageID(errorId) => message.msg.errorId == errorId
case MessagePattern(pattern) =>
val noHighlight = message.msg.message.replaceAll("\\e\\[[\\d;]*[^\\d;]","")
pattern.findFirstIn(noHighlight).nonEmpty
case MessageID(errorId) => message.msg.errorId == errorId
case SourcePattern(pattern) =>
val source = message.position.orElse(NoSourcePosition).source()
val path = source.jfile()
.map(_.toPath.toAbsolutePath.toUri.normalize().getRawPath)
.orElse(source.path())
pattern.findFirstIn(path).nonEmpty

case Origin(pattern) =>
message match
case message: Diagnostic.DeprecationWarning => pattern.findFirstIn(message.origin).nonEmpty
case _ => false
case None => false

case Any, Deprecated, Feature, Unchecked, None
case MessagePattern(pattern: Regex)
case MessageID(errorId: ErrorMessageID)
case SourcePattern(pattern: Regex)
case Origin(pattern: Regex)

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

case "src" => regex(conf).map(SourcePattern.apply)
case "origin" => regex(conf).map(Origin.apply)

case _ => Left(s"unknown filter: $filter")
case _ => Left(s"unknown filter: $s")
Expand Down
4 changes: 2 additions & 2 deletions compiler/src/dotty/tools/dotc/typer/CrossVersionChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ class CrossVersionChecks extends MiniPhase:
do
val msg = annot.argumentConstantString(0).map(msg => s": $msg").getOrElse("")
val since = annot.argumentConstantString(1).map(version => s" (since: $version)").getOrElse("")
report.deprecationWarning(em"inheritance from $psym is deprecated$since$msg", parent.srcPos)
report.deprecationWarning(em"inheritance from $psym is deprecated$since$msg", parent.srcPos, origin=psym.showFullName)
}

override def transformValDef(tree: ValDef)(using Context): ValDef =
Expand Down Expand Up @@ -171,7 +171,7 @@ object CrossVersionChecks:
def maybeWarn(annotee: Symbol, annot: Annotation) = if !skipWarning(sym) then
val message = annot.argumentConstantString(0).filter(!_.isEmpty).map(": " + _).getOrElse("")
val since = annot.argumentConstantString(1).filter(!_.isEmpty).map(" since " + _).getOrElse("")
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos)
report.deprecationWarning(em"${annotee.showLocated} is deprecated${since}${message}", pos, origin=annotee.showFullName)
sym.getAnnotation(defn.DeprecatedAnnot) match
case Some(annot) => maybeWarn(sym, annot)
case _ =>
Expand Down
4 changes: 3 additions & 1 deletion compiler/src/dotty/tools/dotc/typer/RefChecks.scala
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,9 @@ object RefChecks {
def overrideDeprecation(what: String, member: Symbol, other: Symbol, fix: String): Unit =
report.deprecationWarning(
em"overriding $what${infoStringWithLocation(other)} is deprecated;\n ${infoString(member)} should be $fix.",
if member.owner == clazz then member.srcPos else clazz.srcPos)
if member.owner == clazz then member.srcPos else clazz.srcPos,
origin = other.showFullName
)

def autoOverride(sym: Symbol) =
sym.is(Synthetic) && (
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ class ScalaSettingsTests:
val conf = sets.Wconf.valueIn(proc.sstate)
val sut = reporting.WConf.fromSettings(conf).getOrElse(???)
val msg = "There was a problem!".toMessage
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition)
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition, origin="")
assertEquals(Action.Silent, sut.action(depr))
val feat = new Diagnostic.FeatureWarning(msg, util.NoSourcePosition)
assertEquals(Action.Error, sut.action(feat))
Expand Down Expand Up @@ -197,7 +197,7 @@ class ScalaSettingsTests:
val proc = sets.processArguments(sumy, processAll = true, skipped = Nil)
val conf = sets.Wconf.valueIn(proc.sstate)
val msg = "Don't use that!".toMessage
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition)
val depr = new Diagnostic.DeprecationWarning(msg, util.NoSourcePosition, origin="")
val sut = reporting.WConf.fromSettings(conf).getOrElse(???)
assertEquals(Action.Silent, sut.action(depr))

Expand Down Expand Up @@ -293,7 +293,8 @@ class ScalaSettingsTests:
util.SourcePosition(
source = util.SourceFile.virtual(new URI("file:///some/path/file.scala"), ""),
span = util.Spans.Span(1L)
)
),
origin="",
)
)
assertEquals(result, Right(reporting.Action.Error))
Expand Down
15 changes: 15 additions & 0 deletions tests/warn/deprecated-origin.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
//> using options -deprecation -Wconf:origin=p\.C$:s

package p:
@deprecated("Old style", since="1.0")
class C
@deprecated("Bad style", since="1.0")
class Crude

package q:
import annotation.*
import p.*
class D extends C // nowarn - C$ pattern avoids matching Crude
class Oil extends Crude // warn
@nowarn("""origin=p\.Crude""")
class Language extends Crude // nowarn obvs

0 comments on commit 77d194f

Please sign in to comment.