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

Incorrect Match case Unreachable warning #18736

Open
Anghammar opened this issue Oct 20, 2023 · 3 comments
Open

Incorrect Match case Unreachable warning #18736

Anghammar opened this issue Oct 20, 2023 · 3 comments

Comments

@Anghammar
Copy link

Compiler version

3.3.1

Minimized code

object ReachableUnreachableCase {

  sealed trait SuperTrait {
    type Self
  }

  trait SubTrait extends SuperTrait

  case class Foo() extends SubTrait {
    type Self = Foo
  }

  def printError[T <: SuperTrait { type Self = T }](x: Either[String, T]): Unit = {
    x match {
      case Right(_) => println("No error found")
      case Left(message) => println(message)
    }
  }

  @main def main(): Unit = {
    printError(Right(Foo()))
    printError(Left("Error!"))
  }
}

Output

-- [E030] Match case Unreachable Warning: scalaBug:15:11 -----------------------
15 |      case Right(_) => println("No error found")
   |           ^^^^^^^^
   |           Unreachable case

Expectation

Running the main prints both cases as expected, so both cases are in fact reachable.

@Anghammar Anghammar added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 20, 2023
@Anghammar
Copy link
Author

If "sealed" is removed, the compiler gives no warning. If the intermediate "SubTrait" is removed, the compiler gives no warning. If "{ type Self = T }" in the type bound is removed, the compiler gives no warning.

@nicolasstucki nicolasstucki added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 20, 2023
@dwijnand dwijnand removed their assignment Oct 21, 2023
@dwijnand
Copy link
Member

The issue here is we don't infer type members. So when considering SubTrait as a child class of SuperTrait we don't infer from the scrutinee T that we need the type member Self = T as the instantiation of the SubTrait class in order to be a valid subtype. So because we don't infer that, we discard SubTrait as a child, meaning we consider the type T to be uninhabited. Which means that case Left(_) is reachable but case Right(_) never is.

@odersky
Copy link
Contributor

odersky commented Dec 1, 2023

Should the issue be classified as an enhancement or is it rather a bug? I would think a false warning of an unreachable case is always an error. We are permitted to not warn if a case is unreachable in the runtime semantics, but we are not allowed to issue spurious warnings like this one.

An alternative would be if we had a linter option to report "suspected unreachable warnings". That could have false positives. But this unconditional warning should never fire spuriously.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants