Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Better warning for matching values with flexible types in explicit nulls #21577

Open
noti0na1 opened this issue Sep 11, 2024 · 1 comment · May be fixed by #21623
Open

Better warning for matching values with flexible types in explicit nulls #21577

noti0na1 opened this issue Sep 11, 2024 · 1 comment · May be fixed by #21623
Labels
area:nullability area:private options Issues tied to -Y private/internal compiler settings. area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages good first issue Perfect for someone who wants to get started contributing itype:enhancement

Comments

@noti0na1
Copy link
Member

noti0na1 commented Sep 11, 2024

The code is from discussion in Scala 3.5.0 unreachable case which was working with 3.4.2.

Compiler version

Since 3.5 (after the flexible type was introduced)

Minimized example

-Yexplicit-nulls

def f(s: String) =
  val s2 = s.trim()
  s2 match
    case s3: String => println(1)
    case _ => println(2)

Output Error/Warning message

-- [E030] Match case Unreachable Warning: Stest.scala:1026:7 -------------------
1026 |  case _ => println(2)
     |       ^
     |       Unreachable case
1 warning found

Why this Error/Warning was not helpful

Should produce a better warning similar to without explicit nulls:

-- [E121] Pattern Match Warning: Stest.scala:1026:7 ----------------------------
1026 |  case _ => println(2)
     |       ^
     |Unreachable case except for null (if this is intentional, consider writing case null => instead).
1 warning found
@noti0na1 noti0na1 added itype:enhancement area:reporting Error reporting including formatting, implicit suggestions, etc stat:needs triage Every issue needs to have an "area" and "itype" label better-errors Issues concerned with improving confusing/unhelpful diagnostic messages area:nullability good first issue Perfect for someone who wants to get started contributing labels Sep 11, 2024
@Gedochao Gedochao added area:private options Issues tied to -Y private/internal compiler settings. and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Sep 12, 2024
@ajits089
Copy link

ajits089 commented Sep 14, 2024

As per my preliminary investigation what i found is this warning was introduced since 3.0.2. So if we look at the Scala compiler 3.0.0, 3.0.1 and 3.0.2 we receive following response from compiler after defining method f with the given signature and body.
//3.0.2
def f(s: String): Unit -- Warning: 5 | case _ => println(2) | ^ |Unreachable case except for null (if this is intentional, consider writing case null => instead).
// 3.0.1
def f(s: String): Unit 5 | case _ => println(2) | ^ |Unreachable case except for null (if this is intentional, consider writing case null => instead).
//3.0.0
def f(s: String): Unit 5 | case _ => println(2) | ^ | Only null is matched. Consider using case null => instead.

Also, If we consider this case:
def f(s: String | Null) = | val s2 = s.nn.trim() | s2 match | case s3: String => println(1) | case _ => println(2) |
we get warning too where there could be any type involved as the wild card pattern not just null.
1 warning found
def f(s: String | Null): Unit
-- [E121] Pattern Match Warning: -----------------------------------------------
5 | case _ => println(2)
| ^
|Unreachable case except for null (if this is intentional, consider writing case null => instead).

HarrisL2 added a commit to HarrisL2/scala3 that referenced this issue Sep 21, 2024
Adds a more detailed warning message when a wildcard case is unreachable on an
non-nullable type to suggest adding a nullable type annotation.

Fixes scala#21577
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:nullability area:private options Issues tied to -Y private/internal compiler settings. area:reporting Error reporting including formatting, implicit suggestions, etc better-errors Issues concerned with improving confusing/unhelpful diagnostic messages good first issue Perfect for someone who wants to get started contributing itype:enhancement
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants