From 0684e7104bfa29cecb99e725e4e6b912727989d0 Mon Sep 17 00:00:00 2001 From: Som Snytt Date: Sat, 24 Aug 2024 09:39:04 -0700 Subject: [PATCH] More doc [Cherry-picked 3c873cf495412a83d1f0dfc960c343c311db4a5c] --- .../tools/dotc/config/ScalaSettings.scala | 6 ++- .../dotc/reporting/MessageRendering.scala | 39 +++++++++++-------- tests/warn/deprecated-origin-verbose.check | 14 +++++++ tests/warn/deprecated-origin-verbose.scala | 15 +++++++ 4 files changed, 56 insertions(+), 18 deletions(-) create mode 100644 tests/warn/deprecated-origin-verbose.check create mode 100644 tests/warn/deprecated-origin-verbose.scala diff --git a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala index 99568d2eee19..0123c8f85e7a 100644 --- a/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala +++ b/compiler/src/dotty/tools/dotc/config/ScalaSettings.scala @@ -236,7 +236,7 @@ private sealed trait WarningSettings: "patterns", default = List(), descr = - s"""Configure compiler warnings. + raw"""Configure compiler warnings. |Syntax: -Wconf::,:,... |multiple are combined with &, i.e., &...& | @@ -257,6 +257,9 @@ private sealed trait WarningSettings: | - Source location: src=regex | The regex is evaluated against the full source path. | + | - Origin of warning: origin=regex + | The regex must match the full name (`package.Class.method`) of the deprecated entity. + | |In verbose warning mode the compiler prints matching filters for warnings. |Verbose mode can be enabled globally using `-Wconf:any:verbose`, or locally |using the @nowarn annotation (example: `@nowarn("v") def test = try 1`). @@ -276,6 +279,7 @@ private sealed trait WarningSettings: |Examples: | - change every warning into an error: -Wconf:any:error | - silence deprecations: -Wconf:cat=deprecation:s + | - silence a deprecation: -Wconf:origin=java\.lang\.Thread\.getId:s | - silence warnings in src_managed directory: -Wconf:src=src_managed/.*:s | |Note: on the command-line you might need to quote configurations containing `*` or `&` diff --git a/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala b/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala index 6881235e3dc1..7db5112b6674 100644 --- a/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala +++ b/compiler/src/dotty/tools/dotc/reporting/MessageRendering.scala @@ -15,7 +15,7 @@ import util.{ SourcePosition, NoSourcePosition } import util.Chars.{ LF, CR, FF, SU } import scala.annotation.switch -import scala.collection.mutable +import scala.collection.mutable.StringBuilder trait MessageRendering { import Highlight.* @@ -209,22 +209,27 @@ trait MessageRendering { sb.toString } - private def appendFilterHelp(dia: Diagnostic, sb: mutable.StringBuilder): Unit = - import dia.* + private def appendFilterHelp(dia: Diagnostic, sb: StringBuilder): Unit = + import dia.msg val hasId = msg.errorId.errorNumber >= 0 - val category = dia match { - case _: UncheckedWarning => "unchecked" - case _: DeprecationWarning => "deprecation" - case _: FeatureWarning => "feature" - case _ => "" - } - if (hasId || category.nonEmpty) - sb.append(EOL).append("Matching filters for @nowarn or -Wconf:") - if (hasId) - sb.append(EOL).append(" - id=E").append(msg.errorId.errorNumber) - sb.append(EOL).append(" - name=").append(msg.errorId.productPrefix.stripSuffix("ID")) - if (category.nonEmpty) - sb.append(EOL).append(" - cat=").append(category) + val (category, origin) = dia match + case _: UncheckedWarning => ("unchecked", "") + case w: DeprecationWarning => ("deprecation", w.origin) + case _: FeatureWarning => ("feature", "") + case _ => ("", "") + var entitled = false + def addHelp(what: String)(value: String): Unit = + if !entitled then + sb.append(EOL).append("Matching filters for @nowarn or -Wconf:") + entitled = true + sb.append(EOL).append(" - ").append(what).append(value) + if hasId then + addHelp("id=E")(msg.errorId.errorNumber.toString) + addHelp("name=")(msg.errorId.productPrefix.stripSuffix("ID")) + if category.nonEmpty then + addHelp("cat=")(category) + if origin.nonEmpty then + addHelp("origin=")(origin) /** The whole message rendered from `msg` */ def messageAndPos(dia: Diagnostic)(using Context): String = { @@ -236,7 +241,7 @@ trait MessageRendering { else 0 given Level = Level(level) given Offset = Offset(maxLineNumber.toString.length + 2) - val sb = mutable.StringBuilder() + val sb = StringBuilder() val posString = posStr(pos, msg, diagnosticLevel(dia)) if (posString.nonEmpty) sb.append(posString).append(EOL) if (pos.exists) { diff --git a/tests/warn/deprecated-origin-verbose.check b/tests/warn/deprecated-origin-verbose.check new file mode 100644 index 000000000000..e67efaf8668d --- /dev/null +++ b/tests/warn/deprecated-origin-verbose.check @@ -0,0 +1,14 @@ +-- Deprecation Warning: tests/warn/deprecated-origin-verbose.scala:12:18 ----------------------------------------------- +12 | class D extends C // warn + | ^ + | class C in package p is deprecated since 1.0: Old style +Matching filters for @nowarn or -Wconf: + - cat=deprecation + - origin=p.C +-- Deprecation Warning: tests/warn/deprecated-origin-verbose.scala:13:20 ----------------------------------------------- +13 | class Oil extends Crude // warn + | ^^^^^ + | class Crude in package p is deprecated since 1.0: Bad style +Matching filters for @nowarn or -Wconf: + - cat=deprecation + - origin=p.Crude diff --git a/tests/warn/deprecated-origin-verbose.scala b/tests/warn/deprecated-origin-verbose.scala new file mode 100644 index 000000000000..0a960744bdcf --- /dev/null +++ b/tests/warn/deprecated-origin-verbose.scala @@ -0,0 +1,15 @@ +//> using options -deprecation -Wconf:any:verbose + +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 // warn + class Oil extends Crude // warn + @nowarn("""origin=p\.Crude""") + class Language extends Crude // nowarn obvs