Skip to content

False unreachable warning when matching alternative projection types #18661

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
swaldman opened this issue Oct 6, 2023 · 1 comment · Fixed by #18706
Closed

False unreachable warning when matching alternative projection types #18661

swaldman opened this issue Oct 6, 2023 · 1 comment · Fixed by #18706

Comments

@swaldman
Copy link

swaldman commented Oct 6, 2023

Compiler version

3.3.1

Minimized code

While attempting a workaround of #18655 (thanks @aly!), the compiler emits a false warning that code is unreachable, even though the program it generates does correctly reach that code.

//> using scala 3.3.1

class Jacket[T]:
  sealed trait BodyType:
    sealed trait OrganType:
      case class Heart() extends Body.Organ
      case class Brain() extends Body.Organ
    object Organ extends OrganType
    sealed trait Organ
  object Body extends BodyType
  sealed trait Body

type AnyJacket         = Jacket[?]
type AnyBodyOrgan      = AnyJacket#BodyType#Organ
type AnyBodyOrganHeart = AnyJacket#BodyType#OrganType#Heart
type AnyBodyOrganBrain = AnyJacket#BodyType#OrganType#Brain

def check( asr : AnyBodyOrgan ) : String =
  asr match
    case c : AnyBodyOrganHeart => "Heart"
    case s : AnyBodyOrganBrain => "Brain"

val jacket = new Jacket[Unit]
val heart = new jacket.Body.Organ.Heart()
val brain = new jacket.Body.Organ.Brain()

@main
def go =
  println( check( heart ) )
  println( check( brain ) )  

Output

% scala-cli run spurious-warning-jacket.scala
[warn] ./spurious-warning-jacket.scala:24:10
[warn] Unreachable case
[warn]     case s : AnyBodyOrganBrain => "Brain"
[warn]          ^^^^^^^^^^^^^^^^^^^^^
Heart
Brain

Expectation

The code should execute precisely as it does, but the compiler shouldn't emit a warning complaining that it won't reach a case it correctly reaches.

Notes

Eliminating the type parameter on Jacket eliminates the warning. Making the nesting thinner (e.g. AnyJacket#BodyPartType#Heart rather than AnyJacket#BodyType#OrganType#Heart) eliminates the warning. Whatever triggers it is persnickety.

Thanks!

Thanks to @aly on the Scala discord for suggesting this effective (putting aside the ugly warning) workaround to #18655 !

@swaldman swaldman added itype:bug stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 6, 2023
@bishabosha bishabosha added area:pattern-matching and removed stat:needs triage Every issue needs to have an "area" and "itype" label labels Oct 9, 2023
@dwijnand dwijnand linked a pull request Oct 17, 2023 that will close this issue
@swaldman
Copy link
Author

@dwijnand @bishabosha @liufengyun Thank you!

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.

3 participants