Back to Scala3

E123: Type Test Always Diverges

docs/_docs/reference/error-codes/E123.md

3.8.41.5 KB
Original Source

E123: Type Test Always Diverges

This warning is emitted when a type test (via isInstanceOf or pattern matching) is performed on a scrutinee whose type cannot contain any value, meaning the test can never return a result.

This typically occurs when the scrutinee has type Nothing, such as expressions like ???, throw, or values explicitly typed as Nothing. Since Nothing has no instances, the type test will never complete - evaluating the scrutinee will always diverge before the test can be performed.


Example

scala
def example: Boolean = ???.isInstanceOf[String]

Error

scala
-- [E123] Syntax Warning: example.scala:1:39 -----------------------------------
1 |def example: Boolean = ???.isInstanceOf[String]
  |                       ^^^^^^^^^^^^^^^^^^^^^^^^
  |This type test will never return a result since the scrutinee type (??? : => Nothing) does not contain any value.

Solution

scala
// Use a type that can contain values
def example(x: Any): Boolean = x.isInstanceOf[String]
scala
// Or acknowledge that the code path is unreachable
def example: Boolean = ??? // Returns Nothing, no type test needed
<!-- SOURCE-ONLY: Remove the notice below once this page has been manually updated. --> <aside class="warning"> This reference page was created with LLM assistance - the description of the error code may not be accurate or cover all possible scenarios. </aside>