Skip to content

Commit e92279f

Browse files
som-snytttgodzik
authored andcommitted
Suppress spurious Suppression (scala#22383)
Guard against multiple registrations of nowarn on related elements. Fixes scala#18341
1 parent 822a29c commit e92279f

File tree

3 files changed

+31
-8
lines changed

3 files changed

+31
-8
lines changed

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

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ import StdNames.nme
3030
import java.io.{BufferedWriter, OutputStreamWriter}
3131
import java.nio.charset.StandardCharsets
3232

33-
import scala.collection.mutable
33+
import scala.collection.mutable, mutable.ListBuffer
3434
import scala.util.control.NonFatal
3535
import scala.io.Codec
3636

@@ -68,7 +68,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
6868
private var myFiles: Set[AbstractFile] = _
6969

7070
// `@nowarn` annotations by source file, populated during typer
71-
private val mySuppressions: mutable.LinkedHashMap[SourceFile, mutable.ListBuffer[Suppression]] = mutable.LinkedHashMap.empty
71+
private val mySuppressions: mutable.LinkedHashMap[SourceFile, ListBuffer[Suppression]] = mutable.LinkedHashMap.empty
7272
// source files whose `@nowarn` annotations are processed
7373
private val mySuppressionsComplete: mutable.Set[SourceFile] = mutable.Set.empty
7474
// warnings issued before a source file's `@nowarn` annotations are processed, suspended so that `@nowarn` can filter them
@@ -98,8 +98,9 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
9898
}
9999

100100
def addSuppression(sup: Suppression): Unit =
101-
val source = sup.annotPos.source
102-
mySuppressions.getOrElseUpdate(source, mutable.ListBuffer.empty) += sup
101+
val suppressions = mySuppressions.getOrElseUpdate(sup.annotPos.source, ListBuffer.empty)
102+
if sup.start != sup.end && suppressions.forall(x => x.start != sup.start || x.end != sup.end) then
103+
suppressions += sup
103104

104105
def reportSuspendedMessages(source: SourceFile)(using Context): Unit = {
105106
// sort suppressions. they are not added in any particular order because of lazy type completion
@@ -114,11 +115,12 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
114115
mySuspendedMessages.keysIterator.toList.foreach(reportSuspendedMessages)
115116
// report unused nowarns only if all all phases are done
116117
if !hasErrors && ctx.settings.WunusedHas.nowarn then
117-
for {
118+
for
118119
source <- mySuppressions.keysIterator.toList
119120
sups <- mySuppressions.remove(source)
120121
sup <- sups.reverse
121-
} if (!sup.used)
122+
if !sup.used
123+
do
122124
report.warning("@nowarn annotation does not suppress any warnings", sup.annotPos)
123125

124126
/** The compilation units currently being compiled, this may return different
@@ -167,7 +169,7 @@ class Run(comp: Compiler, ictx: Context) extends ImplicitRunInfo with Constraint
167169
val staticRefs = util.EqHashMap[Name, Denotation](initialCapacity = 1024)
168170

169171
/** Actions that need to be performed at the end of the current compilation run */
170-
private var finalizeActions = mutable.ListBuffer[() => Unit]()
172+
private var finalizeActions = ListBuffer.empty[() => Unit]
171173

172174
private var _progress: Progress | Null = null // Set if progress reporting is enabled
173175

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -134,7 +134,7 @@ object WConf:
134134
if (parseErrorss.nonEmpty) Left(parseErrorss.flatten)
135135
else Right(WConf(configs))
136136

137-
class Suppression(val annotPos: SourcePosition, filters: List[MessageFilter], val start: Int, end: Int, val verbose: Boolean):
137+
class Suppression(val annotPos: SourcePosition, filters: List[MessageFilter], val start: Int, val end: Int, val verbose: Boolean):
138138
private[this] var _used = false
139139
def used: Boolean = _used
140140
def markUsed(): Unit = { _used = true }

tests/warn/i18341.scala

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
2+
//> using options -Wunused:params,nowarn
3+
4+
import annotation.*
5+
6+
class B(@nowarn useless: Int)
7+
8+
class C(@nowarn("msg=unused") useless: Int)
9+
10+
class D(useless: Int) // warn
11+
12+
class E(@nowarn useful: Int): // warn
13+
def e = useful * 10 // 10x useful
14+
15+
class X:
16+
def extensionInCompanion: String = ???
17+
@nowarn // extensionInCompanion
18+
object X:
19+
implicit def companionConversion(x: X): B = ???
20+
21+
extension (x: X) def extensionInCompanion: String = ???

0 commit comments

Comments
 (0)