Skip to content

Unreachable case in two-level sealed hierarchy is not detected #4314

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

Closed
smarter opened this issue Apr 13, 2018 · 1 comment
Closed

Unreachable case in two-level sealed hierarchy is not detected #4314

smarter opened this issue Apr 13, 2018 · 1 comment

Comments

@smarter
Copy link
Member

smarter commented Apr 13, 2018

Given the following code:

sealed trait Foo[A]
case class One[A]() extends Foo[A]
sealed abstract case class Bar[A]() extends Foo[A]
class Two() extends Bar[String]

object Test {
  def test(x: Foo[Int]) = x match {
    case One() =>
  }
}

The exhaustiveness checker is smart enough to not warn, because no instance of Bar is a subtype of Foo[Int].

However, if we add a case for Bar() like this:

sealed trait Foo[A]
case class One[A]() extends Foo[A]
sealed abstract case class Bar[A]() extends Foo[A]
class Two() extends Bar[String]

object Test {
  def test(x: Foo[Int]) = x match {
    case One() =>
    case Bar() =>
  }
}

We should get an unreachable code warning (and indeed, scalac produces one), but we don't.

@smarter
Copy link
Member Author

smarter commented Apr 13, 2018

Related, but maybe a different issue, in the following code:

trait Foo[A]
case class One() extends Foo[String]

object Test {
  def test(x: Foo[Int]) = x match {
    case One() =>
  }
}

We get an error with scalac but nothing in Dotty:

try/pattern-no-instance.scala:6: error: constructor cannot be instantiated to expected type;
 found   : One
 required: Foo[Int]
    case One() =>
         ^
one error found

@liufengyun liufengyun self-assigned this Apr 13, 2018
liufengyun added a commit to dotty-staging/dotty that referenced this issue Apr 17, 2018
1. ApproximatingTypeMap produces Nothing, which is not what is needed.
2. Avoid blind erasure which lose information about the pattern, see tests/patmat/i4314b.scala
liufengyun added a commit that referenced this issue Apr 20, 2018
gsps pushed a commit to gsps/dotty that referenced this issue Apr 21, 2018
1. ApproximatingTypeMap produces Nothing, which is not what is needed.
2. Avoid blind erasure which lose information about the pattern, see tests/patmat/i4314b.scala
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants