Skip to content

Commit ae844bb

Browse files
committed
allow filtering messages by error id
1 parent ce64c43 commit ae844bb

File tree

4 files changed

+42
-13
lines changed

4 files changed

+42
-13
lines changed

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

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,10 +149,13 @@ private sealed trait WarningSettings:
149149
| - Message content: msg=regex
150150
| The regex need only match some part of the message, not all of it.
151151
|
152+
| - Message id: id=E129
153+
| The message id is printed with the warning.
154+
|
152155
|<action>
153156
| - error / e
154157
| - warning / w
155-
| - info / i (infos are not counted as warnings and don't affect `-Werror`)
158+
| - info / i (infos are not counted as warnings and not affected by `-Werror`)
156159
| - silent / s
157160
|
158161
|The default configuration is empty.

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

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import dotty.tools.dotc.core.Contexts._
66
import dotty.tools.dotc.util.SourcePosition
77

88
import java.util.regex.PatternSyntaxException
9+
import scala.annotation.internal.sharable
910
import scala.collection.mutable.ListBuffer
1011
import scala.util.matching.Regex
1112

@@ -17,10 +18,12 @@ enum MessageFilter:
1718
case MessagePattern(pattern) =>
1819
val noHighlight = message.msg.rawMessage.replaceAll("\\e\\[[\\d;]*[^\\d;]","")
1920
pattern.findFirstIn(noHighlight).nonEmpty
21+
case MessageID(errorId) => message.msg.errorId == errorId
2022
case None => false
2123
}
2224
case Any, Deprecated, Feature, None
2325
case MessagePattern(pattern: Regex)
26+
case MessageID(errorId: ErrorMessageID)
2427

2528
enum Action:
2629
case Error, Warning, Info, Silent
@@ -48,18 +51,28 @@ object WConf:
4851
try Right(s.r)
4952
catch { case e: PatternSyntaxException => Left(s"invalid pattern `$s`: ${e.getMessage}") }
5053

51-
def parseFilter(s: String): Either[String, MessageFilter] =
52-
val splitter = raw"([^=]+)=(.+)".r
53-
s match
54-
case "any" => Right(Any)
55-
case splitter(filter, conf) => filter match
56-
case "msg" => regex(conf).map(MessagePattern.apply)
57-
case "cat" => conf match
58-
case "deprecation" => Right(Deprecated)
59-
case "feature" => Right(Feature)
60-
case _ => Left(s"unknown category: $conf")
61-
case _ => Left(s"unknown filter: $filter")
62-
case _ => Left(s"unknown filter: $s")
54+
@sharable val Splitter = raw"([^=]+)=(.+)".r
55+
@sharable val ErrorId = raw"E?(\d+)".r
56+
57+
def parseFilter(s: String): Either[String, MessageFilter] = s match
58+
case "any" => Right(Any)
59+
case Splitter(filter, conf) => filter match
60+
case "msg" => regex(conf).map(MessagePattern.apply)
61+
case "id" => conf match
62+
case ErrorId(num) =>
63+
val n = num.toInt + 2
64+
if n < ErrorMessageID.values.length then
65+
Right(MessageID(ErrorMessageID.fromOrdinal(n)))
66+
else
67+
Left(s"unknonw error message id: E$n")
68+
case _ =>
69+
Left(s"invalid error message id: $conf")
70+
case "cat" => conf match
71+
case "deprecation" => Right(Deprecated)
72+
case "feature" => Right(Feature)
73+
case _ => Left(s"unknown category: $conf")
74+
case _ => Left(s"unknown filter: $filter")
75+
case _ => Left(s"unknown filter: $s")
6376

6477
def parsed(using Context): WConf =
6578
val setting = ctx.settings.Wconf.value

tests/neg-custom-args/nowarn/nowarn-parser-typer.check

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,13 @@
44
| A try without catch or finally is equivalent to putting
55
| its body in a block; no exceptions are handled.
66

7+
longer explanation available when compiling with `-explain`
8+
-- [E000] Syntax Error: tests/neg-custom-args/nowarn/nowarn-parser-typer.scala:22:26 -----------------------------------
9+
22 |@nowarn("id=1") def t5d = try 1 // error, wrong id
10+
| ^^^^^
11+
| A try without catch or finally is equivalent to putting
12+
| its body in a block; no exceptions are handled.
13+
714
longer explanation available when compiling with `-explain`
815
-- [E129] Potential Issue Error: tests/neg-custom-args/nowarn/nowarn-parser-typer.scala:16:11 --------------------------
916
16 |def t3 = { 1; 2 } // error, the invalid nowarn doesn't silence this warning

tests/neg-custom-args/nowarn/nowarn-parser-typer.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,9 @@ def t2 = f // not reported because refchecks doesn't run
1414

1515
@nowarn("wat?") // error, invalid filter
1616
def t3 = { 1; 2 } // error, the invalid nowarn doesn't silence this warning
17+
18+
@nowarn("id=E129") def t4 = { 1; 2 }
19+
@nowarn("id=E000") def t5a = try 1
20+
@nowarn("id=E0") def t5b = try 1
21+
@nowarn("id=0") def t5c = try 1
22+
@nowarn("id=1") def t5d = try 1 // error, wrong id

0 commit comments

Comments
 (0)