Skip to content

Commit bb1fd4a

Browse files
committed
Check if a fatal warning issued in typer is silenced, before converting it into an error
closes lampepfl#17741 closes lampepfl#17735
1 parent 0749271 commit bb1fd4a

File tree

4 files changed

+41
-34
lines changed

4 files changed

+41
-34
lines changed

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

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -149,14 +149,10 @@ abstract class Reporter extends interfaces.ReporterResult {
149149
val key = w.enablingOption.name
150150
addUnreported(key, 1)
151151
case _ =>
152-
// conditional warnings that are not enabled are not fatal
153-
val d = dia match
154-
case w: Warning if ctx.settings.XfatalWarnings.value => w.toError
155-
case _ => dia
156-
if !isHidden(d) then // avoid isHidden test for summarized warnings so that message is not forced
157-
markReported(d)
158-
withMode(Mode.Printing)(doReport(d))
159-
d match {
152+
if !isHidden(dia) then // avoid isHidden test for summarized warnings so that message is not forced
153+
markReported(dia)
154+
withMode(Mode.Printing)(doReport(dia))
155+
dia match {
160156
case _: Warning => _warningCount += 1
161157
case e: Error =>
162158
errors = e :: errors
@@ -171,14 +167,19 @@ abstract class Reporter extends interfaces.ReporterResult {
171167
def issueIfNotSuppressed(dia: Diagnostic)(using Context): Unit =
172168
def go() =
173169
import Action._
174-
dia match
170+
val d = dia match
171+
case w: Warning if ctx.settings.silentWarnings.value => dia
172+
case w: ConditionalWarning if w.isSummarizedConditional => dia
173+
case w: Warning if !ctx.settings.silentWarnings.value && ctx.settings.XfatalWarnings.value => w.toError
174+
case _ => dia
175+
d match
175176
case w: Warning => WConf.parsed.action(w) match
176177
case Error => issueUnconfigured(w.toError)
177178
case Warning => issueUnconfigured(w)
178179
case Verbose => issueUnconfigured(w.setVerbose())
179180
case Info => issueUnconfigured(w.toInfo)
180181
case Silent =>
181-
case _ => issueUnconfigured(dia)
182+
case _ => issueUnconfigured(d)
182183

183184
// `ctx.run` can be null in test, also in the repl when parsing the first line. The parser runs early, the Run is
184185
// only created in ReplDriver.compile when a line is submitted. This means that `@nowarn` doesnt work on parser

tests/neg/refutable-pattern-binding-messages.check

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,11 @@
1+
-- Error: tests/neg/refutable-pattern-binding-messages.scala:5:14 ------------------------------------------------------
2+
5 | val Positive(p) = 5 // error: refutable extractor
3+
| ^^^^^^^^^^^^^^^
4+
| pattern binding uses refutable extractor `Test.Positive`
5+
|
6+
| If this usage is intentional, this can be communicated by adding `: @unchecked` after the expression,
7+
| which may result in a MatchError at runtime.
8+
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
19
-- Error: tests/neg/refutable-pattern-binding-messages.scala:6:14 ------------------------------------------------------
210
6 | for Positive(i) <- List(1, 2, 3) do () // error: refutable extractor
311
| ^^^^^^^^^^^
@@ -6,6 +14,14 @@
614
| If this usage is intentional, this can be communicated by adding the `case` keyword before the full pattern,
715
| which will result in a filtering for expression (using `withFilter`).
816
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
17+
-- Error: tests/neg/refutable-pattern-binding-messages.scala:10:20 -----------------------------------------------------
18+
10 | val i :: is = List(1, 2, 3) // error: pattern type more specialized
19+
| ^^^^^^^^^^^^^
20+
| pattern's type ::[Int] is more specialized than the right hand side expression's type List[Int]
21+
|
22+
| If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
23+
| which may result in a MatchError at runtime.
24+
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
925
-- Error: tests/neg/refutable-pattern-binding-messages.scala:11:11 -----------------------------------------------------
1026
11 | for ((x: String) <- xs) do () // error: pattern type more specialized
1127
| ^^^^^^
@@ -22,22 +38,6 @@
2238
| If the narrowing is intentional, this can be communicated by adding the `case` keyword before the full pattern,
2339
| which will result in a filtering for expression (using `withFilter`).
2440
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
25-
-- Error: tests/neg/refutable-pattern-binding-messages.scala:5:14 ------------------------------------------------------
26-
5 | val Positive(p) = 5 // error: refutable extractor
27-
| ^^^^^^^^^^^^^^^
28-
| pattern binding uses refutable extractor `Test.Positive`
29-
|
30-
| If this usage is intentional, this can be communicated by adding `: @unchecked` after the expression,
31-
| which may result in a MatchError at runtime.
32-
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
33-
-- Error: tests/neg/refutable-pattern-binding-messages.scala:10:20 -----------------------------------------------------
34-
10 | val i :: is = List(1, 2, 3) // error: pattern type more specialized
35-
| ^^^^^^^^^^^^^
36-
| pattern's type ::[Int] is more specialized than the right hand side expression's type List[Int]
37-
|
38-
| If the narrowing is intentional, this can be communicated by adding `: @unchecked` after the expression,
39-
| which may result in a MatchError at runtime.
40-
| This patch can be rewritten automatically under -rewrite -source 3.2-migration.
4141
-- Error: tests/neg/refutable-pattern-binding-messages.scala:16:10 -----------------------------------------------------
4242
16 | val 1 = 2 // error: pattern type does not match
4343
| ^

tests/neg/warn-value-discard.check

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,3 @@
1-
-- [E175] Potential Issue Error: tests/neg/warn-value-discard.scala:15:35 ----------------------------------------------
2-
15 | firstThing().map(_ => secondThing()) // error
3-
| ^^^^^^^^^^^^^
4-
| discarded non-Unit value of type Either[Failed, Unit]
5-
-- [E175] Potential Issue Error: tests/neg/warn-value-discard.scala:18:35 ----------------------------------------------
6-
18 | firstThing().map(_ => secondThing()) // error
7-
| ^^^^^^^^^^^^^
8-
| discarded non-Unit value of type Either[Failed, Unit]
91
-- [E175] Potential Issue Error: tests/neg/warn-value-discard.scala:27:36 ----------------------------------------------
102
27 | mutable.Set.empty[String].remove("") // error
113
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@@ -18,3 +10,11 @@
1810
59 | mutable.Set.empty[String] += "" // error
1911
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2012
| discarded non-Unit value of type scala.collection.mutable.Set[String]
13+
-- [E175] Potential Issue Error: tests/neg/warn-value-discard.scala:15:35 ----------------------------------------------
14+
15 | firstThing().map(_ => secondThing()) // error
15+
| ^^^^^^^^^^^^^
16+
| discarded non-Unit value of type Either[Failed, Unit]
17+
-- [E175] Potential Issue Error: tests/neg/warn-value-discard.scala:18:35 ----------------------------------------------
18+
18 | firstThing().map(_ => secondThing()) // error
19+
| ^^^^^^^^^^^^^
20+
| discarded non-Unit value of type Either[Failed, Unit]
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
case class F(i: Int)
2+
3+
object Main {
4+
def example() =
5+
List(1, 2, 3).map(F): @annotation.nowarn
6+
}

0 commit comments

Comments
 (0)