# Reachable "unreachable" case of union type match

**URL:** <https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566>\
**Category:** Dotty\
**Created:** [February 21, 2024, 1:00am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566 "2024-02-21T01:00:05Z")\
**Posts on this page:** 14\
**Page:** 1

<div class="post-metadata">

**Author:** ![aversey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aversey/32/2980_2.png) [@aversey](https://contributors.scala-lang.org/u/aversey)\
**Post date:** [February 21, 2024, 1:00am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/1 "2024-02-21T01:00:05Z")

</div>

There is a Scala 3 sample which shows that a match case marked with “Unreachable case” warning can still be reached. I am not sure if it is an error (although it seems like one to me), so I decided to start a topic here instead of reporting an issue in GitHub.

* * *

Suppose someone wrote this program:

```scala
case class A()
case class B()
case class C()

def f(ab: A | B): Unit = ab match
  case x: (A | B) => println(s"$x: ok")
  case x => println(s"$x: unreachable")

```

It quite expectedly provokes a warning stating that the second case is unreachable.

However, this code ([Scastie link](https://scastie.scala-lang.org/RrPfmbz5RVOXrG1p9xdP4w)) allows us to reach the case:

```scala
f(C().asInstanceOf[A | B])

```

> **The same without asInstanceOf**
>
> The same can be achieved without `asInstanceOf` ([Scastie link](https://scastie.scala-lang.org/W5bZ9eL1RQm9MvwEJYgkAw)):
> 
> ```scala
> def applyIfApplicable[T](f: T => Unit, x: Any): Unit = x match
> case t: T => f(t)
> 
> applyIfApplicable[A | B](f, C())
> 
> ```
> 
> It provokes another warning, but we still reach the “unreachable” code. (A proper `applyIfApplicable` can be done using `scala.reflect.Typeable`.)

* * *

Do you think anything has to be done about this at all? I think something has to be done, since the warning message is inconsistent with reality. The simplest solution would be to change the warning message by adding “maybe”. Also, I guess it is possible and would make sense to throw an exception if an execution reaches an unreachable case in a match. Is there any reason to allow reaching unreachable case?

Thanks for reading. 🙂

---

<div class="post-metadata">

**Author:** ![nafg](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/nafg/32/38_2.png) [@nafg](https://contributors.scala-lang.org/u/nafg)\
**Post date:** [February 21, 2024, 2:20am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/2 "2024-02-21T02:20:40Z")

</div>

In the second snippet the warning is perfectly consistent with reality.

In the first one, this is the unavoidable fact of asInstanceOf casts: you are lying to the compiler and forcing it to look the other way. So the entire type system is helpless, including the part that computes reachability.

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [February 21, 2024, 2:20am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/3 "2024-02-21T02:20:47Z")

</div>

Perhaps the words used in the message could better distinguish static types from runtime.

The ambiguity is between “unreachable” in the sense of “dead code at runtime” and unreachable in an abstract graph of static cases.

Possible wording: “all applicable patterns have been exhausted” or “the pattern space has been exhausted”.

Maybe there is a word that is both technically precise and in wide usage by users.

The runtime behavior is due to the subversion of the type system.

The usual explanation is that a compiler message is just a shorthand: either the user understands it from previous experience, or the user must become educated, in this instance, about the meaning of “unreachable” in this context and ultimately why `asInstanceOf` is dangerous, or how other warnings must be heeded.

There is a ticket about braceless syntax which turns on whether a warning is heeded. Probably `-Werror` should be recommended for the risk-averse.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 21, 2024, 9:08am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/4 "2024-02-21T09:08:44Z")

</div>

I believe we should write error messages assuming the type system was not subverted, otherwise the problem becomes untractable  
Therefore, I think the current message is perfectly fine

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 21, 2024, 9:12am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/5 "2024-02-21T09:12:13Z")

</div>

Maybe “the type test for T cannot be checked at runtime because it refers to an abstract type member or type parameter” should be an error instead of a warning

The gravity of the situation is not obvious as it uses quite a lot of advenced terminology  
Notably “type test” which is not easy to find the meaning of, as you tend to find `TypeTest` which is not exactly the same thing

---

<div class="post-metadata">

**Author:** ![aversey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aversey/32/2980_2.png) [@aversey](https://contributors.scala-lang.org/u/aversey)\
**Post date:** [February 21, 2024, 9:12am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/6 "2024-02-21T09:12:45Z")

</div>

Thanks for the reply!

_First_, I don’t think that the warning “Unreachable case” makes sense as long as we are still able to reach the case (which is what happens also in the second snippet). I also don’t think that the existence of other warnings in the program should justify this, especially since the first part of code, i.e., definition of the classes and `f`, can be in a very different place than the second part, i.e., the code which uses them.

_Second_, both snippets are “lying” to the compiler, but in other similar cases, we don’t reach an unreachable case. For example ([Scastie link](https://scastie.scala-lang.org/LDs8JIgnRuithX1tqUAt4g)), using List we also get unreachability warning, but it is true — we don’t reach the case:

```scala
case class A()
case class B()

def f(l: List[A]): Unit = l match
  case x: List[A] => println(s"$x: ok")
  case x => println(s"$x: unreachable except for null")

f(List(B()).asInstanceOf[List[A]])

```

There is still a consequence of the “lie”, i.e., that under A in the list we are actually passing B, but it will result in an exception as soon as we will try to access the element.

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 21, 2024, 9:19am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/7 "2024-02-21T09:19:19Z")

</div>

The compiler cannot be safe in the presence of `asInstanceOf`, the whole point of it is to remove compiler checks !

That was actually the reason it has such a long name, it means “Trust me bro, I really know what I’m doing”  
Compared to `(x: Int)` which means “I think `x` is an `Int`, could you check ?”

---

<div class="post-metadata">

**Author:** ![aversey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aversey/32/2980_2.png) [@aversey](https://contributors.scala-lang.org/u/aversey)\
**Post date:** [February 21, 2024, 9:40am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/8 "2024-02-21T09:40:35Z")

</div>

Thank you for your replies!

My problem with both `asInstanceOf` and abstract class / type parameter is that their effect is not local, so the error happens in one place, but the consequences are faced in maybe a very different place.

And speaking about specifically `asInstanceOf`, ~~it is not all-trusting, and does some checks in the runtime~~ it is all-trusting, but its usage is still usually limited, so this results in an exception:

```scala
B().asInstanceOf[A]

```

I guess the same should be done in case of an unreachable case to really make it unreachable. One particular thing I am worried about is that if the unreachability message is interpreted as completely true, an optimization might rely on this assumption and produce incorrect code.

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [February 21, 2024, 9:46am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/9 "2024-02-21T09:46:28Z")

</div>

If you perform a wrong `asInstanceOf`, it’s on you. The compiler is allowed to report bogus warnings, errors, and or generated code past that point. And yes, it can do so in non local ways.

> [@aversey](#):
>
> And speaking about specifically `asInstanceOf`, it is not all-trusting, and does some checks in the runtime, so this results in an exception:
> 
> `B().asInstanceOf[A]`

It is only non-trusting insofar as is necessary for the target VM not to complain that its bytecode is invalid. The JVM requires explicit checks for the monomorphic class, so the compiler emits that. Scala.js does not check anything in `fullLink` mode, not even the monomorphic class. At the _language_ level, `asInstanceOf` is all-trusting. The language does not want to check, but _some_ VMs require _some_ checks. It does not mean you can rely on them.

---

<div class="post-metadata">

**Author:** ![aversey](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/aversey/32/2980_2.png) [@aversey](https://contributors.scala-lang.org/u/aversey)\
**Post date:** [February 21, 2024, 9:53am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/10 "2024-02-21T09:53:33Z")

</div>

Ok, thanks! So, if I understood it correctly, is it kind of similar to undefined behavior? That is, as long as there is such an error in the program, the program is meaningless (on the language level, I mean), and the compiler therefore is free to do pretty much anything. Is it so?

---

<div class="post-metadata">

**Author:** ![sjrd](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sjrd/32/24_2.png) [@sjrd](https://contributors.scala-lang.org/u/sjrd)\
**Post date:** [February 21, 2024, 10:05am UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/11 "2024-02-21T10:05:00Z")

</div>

Yes, it is similar to undefined behavior. Of course undefined behavior is always bounded by the safety guarantees of the underlying VM. (Even C’s undefined behavior is actually bounded by the safety guarantees of the OS wrt. the processes it runs.)

---

<div class="post-metadata">

**Author:** ![Sporarum](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/sporarum/32/2545_2.png) [@Sporarum](https://contributors.scala-lang.org/u/Sporarum)\
**Post date:** [February 21, 2024, 1:55pm UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/12 "2024-02-21T13:55:27Z")

</div>

Maybe we should be more explicit about it being (like) undefined behaviour ?

The word brings a certain aura of danger which I believe is appropriate in this case

---

<div class="post-metadata">

**Author:** ![som-snytt](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/som-snytt/32/595_2.png) [@som-snytt](https://contributors.scala-lang.org/u/som-snytt)\
**Post date:** [February 21, 2024, 5:31pm UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/13 "2024-02-21T17:31:40Z")

</div>

There was a PR just now to remove the illustrative implementation of `asInstanceOf` from the spec.

Instead of leaving it deferred, it could say:

```auto
def asInstanceOf[A]: A = undefined

```

so that only the most intrepid would dare use it.

Or possibly:

`def asInstanceOf[A]: A =` 🧌

---

<div class="post-metadata">

**Author:** ![jeremyrsmith](https://contributors.scala-lang.org/user_avatar/contributors.scala-lang.org/jeremyrsmith/32/2489_2.png) [@jeremyrsmith](https://contributors.scala-lang.org/u/jeremyrsmith)\
**Post date:** [February 21, 2024, 6:46pm UTC](https://contributors.scala-lang.org/t/reachable-unreachable-case-of-union-type-match/6566/14 "2024-02-21T18:46:30Z")

</div>

I think the (potentially) surprising thing is that you don’t get a `ClassCastException` during `C().asInstanceOf[A | B]`. If you did, the `unreachable` case would indeed be unreachable – exactly as if you replaced `A | B` with just `A` in the definition and type signature of `f`. There’s an inconsistency introduced by the detail that `A | B` erases to `Object` (and thus the cast is successful and the method call is allowed)
