Skip to content

Commit 53a3d4a

Browse files
committed
Issue warnings instead of errors
1. Warnings are more consistent with exhaustivity check where only warnings are issued 2. Errors are not friendly for prototyping, where the programmer makes assumptions about what values are possible.
1 parent d9d3bea commit 53a3d4a

File tree

3 files changed

+6
-6
lines changed

3 files changed

+6
-6
lines changed

compiler/src/dotty/tools/dotc/typer/Checking.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -684,7 +684,7 @@ trait Checking {
684684
val problem = if (pat.tpe <:< reportedPt) "is more specialized than" else "does not match"
685685
val fix = if (isPatDef) "adding `: @unchecked` after the expression" else "writing `case ` before the full pattern"
686686
val pos = if (isPatDef) sel.srcPos else pat.srcPos
687-
report.errorOrMigrationWarning(
687+
report.warning(
688688
ex"""pattern's type ${pat.tpe} $problem the right hand side expression's type $reportedPt
689689
|
690690
|If the narrowing is intentional, this can be communicated by $fix.${err.rewriteNotice}""",

compiler/test/dotty/tools/dotc/CompilationTests.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -118,7 +118,7 @@ class CompilationTests {
118118
aggregateTests(
119119
compileFilesInDir("tests/neg", defaultOptions),
120120
compileFilesInDir("tests/neg-tailcall", defaultOptions),
121-
compileFilesInDir("tests/neg-strict", defaultOptions.and("-source", "3.1")),
121+
compileFilesInDir("tests/neg-strict", defaultOptions.and("-source", "3.1", "-Xfatal-warnings")),
122122
compileFilesInDir("tests/neg-no-kind-polymorphism", defaultOptions and "-Yno-kind-polymorphism"),
123123
compileFilesInDir("tests/neg-custom-args/deprecation", defaultOptions.and("-Xfatal-warnings", "-deprecation")),
124124
compileFilesInDir("tests/neg-custom-args/fatal-warnings", defaultOptions.and("-Xfatal-warnings")),

docs/docs/reference/changed-features/pattern-bindings.md

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ title: "Pattern Bindings"
66
In Scala 2, pattern bindings in `val` definitions and `for` expressions are
77
loosely typed. Potentially failing matches are still accepted at compile-time,
88
but may influence the program's runtime behavior.
9-
From Scala 3.1 on, type checking rules will be tightened so that errors are reported at compile-time instead.
9+
From Scala 3.1 on, type checking rules will be tightened so that warnings are reported at compile-time instead.
1010

1111
## Bindings in Pattern Definitions
1212

@@ -15,7 +15,7 @@ val xs: List[Any] = List(1, 2, 3)
1515
val (x: String) :: _ = xs // error: pattern's type String is more specialized
1616
// than the right hand side expression's type Any
1717
```
18-
This code gives a compile-time error in Scala 3.1 (and also in Scala 3.0 under the `-source 3.1` setting) whereas it will fail at runtime with a `ClassCastException` in Scala 2. In Scala 3.1, a pattern binding is only allowed if the pattern is _irrefutable_, that is, if the right-hand side's type conforms to the pattern's type. For instance, the following is OK:
18+
This code gives a compile-time warning in Scala 3.1 (and also in Scala 3.0 under the `-source 3.1` setting) whereas it will fail at runtime with a `ClassCastException` in Scala 2. In Scala 3.1, a pattern binding is only allowed if the pattern is _irrefutable_, that is, if the right-hand side's type conforms to the pattern's type. For instance, the following is OK:
1919
```scala
2020
val pair = (1, true)
2121
val (x, y) = pair
@@ -25,7 +25,7 @@ want to decompose it like this:
2525
```scala
2626
val first :: rest = elems // error
2727
```
28-
This works in Scala 2. In fact it is a typical use case for Scala 2's rules. But in Scala 3.1 it will give a type error. One can avoid the error by marking the right-hand side with an `@unchecked` annotation:
28+
This works in Scala 2. In fact it is a typical use case for Scala 2's rules. But in Scala 3.1 it will give a warning. One can avoid the warning by marking the right-hand side with an `@unchecked` annotation:
2929
```scala
3030
val first :: rest = elems: @unchecked // OK
3131
```
@@ -40,7 +40,7 @@ val elems: List[Any] = List((1, 2), "hello", (3, 4))
4040
for ((x, y) <- elems) yield (y, x) // error: pattern's type (Any, Any) is more specialized
4141
// than the right hand side expression's type Any
4242
```
43-
This code gives a compile-time error in Scala 3.1 whereas in Scala 2 the list `elems`
43+
This code gives a compile-time warning in Scala 3.1 whereas in Scala 2 the list `elems`
4444
is filtered to retain only the elements of tuple type that match the pattern `(x, y)`.
4545
The filtering functionality can be obtained in Scala 3 by prefixing the pattern with `case`:
4646
```scala

0 commit comments

Comments
 (0)