Skip to content

Commit 0992048

Browse files
committed
Add suppression if nowarn differs
1 parent a115c8d commit 0992048

File tree

3 files changed

+59
-3
lines changed

3 files changed

+59
-3
lines changed

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

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,8 +123,13 @@ extends ImplicitRunInfo, ConstraintRunInfo, cc.CaptureRunInfo {
123123

124124
def addSuppression(sup: Suppression): Unit =
125125
val suppressions = mySuppressions.getOrElseUpdate(sup.annotPos.source, ListBuffer.empty)
126-
if sup.start != sup.end && suppressions.forall(x => x.start != sup.start || x.end != sup.end) then
127-
suppressions += sup
126+
if sup.start != sup.end then
127+
suppressions.find(sup.matches(_)) match
128+
case Some(other) =>
129+
if sup.annotPos != other.annotPos then
130+
report.warning("@nowarn annotation is duplicate", sup.annotPos)
131+
case none =>
132+
suppressions += sup
128133

129134
def reportSuspendedMessages(source: SourceFile)(using Context): Unit = {
130135
// sort suppressions. they are not added in any particular order because of lazy type completion

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

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ import dotty.tools.dotc.interfaces.SourceFile
1010
import dotty.tools.dotc.reporting.MessageFilter.SourcePattern
1111

1212
import java.util.regex.PatternSyntaxException
13+
import scala.PartialFunction.cond
1314
import scala.annotation.internal.sharable
1415
import scala.util.matching.Regex
1516

@@ -136,13 +137,38 @@ object WConf:
136137
if (parseErrorss.nonEmpty) Left(parseErrorss.flatten)
137138
else Right(WConf(configs))
138139

139-
class Suppression(val annotPos: SourcePosition, filters: List[MessageFilter], val start: Int, val end: Int, val verbose: Boolean):
140+
class Suppression(val annotPos: SourcePosition, val filters: List[MessageFilter], val start: Int, val end: Int, val verbose: Boolean):
140141
private var _used = false
141142
def used: Boolean = _used
142143
def markUsed(): Unit =
143144
_used = true
144145
def matches(dia: Diagnostic): Boolean =
145146
val pos = dia.pos
146147
pos.exists && start <= pos.start && pos.end <= end && filters.forall(_.matches(dia))
148+
def matches(other: Suppression): Boolean =
149+
start == other.start
150+
&& end == other.end
151+
&& verbose == other.verbose
152+
&& filters.lengthCompare(other.filters) == 0
153+
&& filters.forall(other.hasFilter)
154+
&& other.filters.forall(hasFilter)
155+
156+
private def hasFilter(filter: MessageFilter): Boolean =
157+
import MessageFilter.*
158+
filters.exists:
159+
case MessageID(errorId) =>
160+
cond(filter):
161+
case MessageID(otherId) => errorId == otherId
162+
case MessagePattern(pattern) =>
163+
cond(filter):
164+
case MessagePattern(otherPattern) => pattern.toString == otherPattern.toString
165+
case SourcePattern(pattern) =>
166+
cond(filter):
167+
case SourcePattern(otherPattern) => pattern.toString == otherPattern.toString
168+
case Origin(pattern) =>
169+
cond(filter):
170+
case Origin(otherPattern) => pattern.toString == otherPattern.toString
171+
case x => x == filter // Any, Deprecated, Feature, Unchecked, None
147172

148173
override def toString = s"Suppress in ${annotPos.source} $start..$end [${filters.mkString(", ")}]"
174+
end Suppression

tests/warn/i23651.scala

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
//> using options -deprecation -Wunused:nowarn
2+
3+
import scala.annotation.nowarn
4+
5+
@deprecated
6+
class A
7+
8+
@deprecated
9+
class B
10+
11+
@nowarn("msg=class A is deprecated")
12+
@nowarn("cat=deprecation&msg=class A is deprecated") // warn unused redundant
13+
@nowarn("cat=deprecation&msg=class B is deprecated")
14+
trait C1 {
15+
def a: A
16+
def b: B
17+
}
18+
19+
@nowarn("cat=deprecation&msg=class B is deprecated")
20+
@nowarn("cat=deprecation&msg=class B is deprecated") // warn duplicate
21+
@nowarn("cat=deprecation&msg=class A is deprecated")
22+
trait C2 {
23+
def a: A
24+
def b: B
25+
}

0 commit comments

Comments
 (0)