Skip to content

CanThrow capability shouldn't be synthesized for nonexhaustive catch clauses #13849

Closed
@prolativ

Description

@prolativ

Compiler version

3.1.1-RC1

Minimized code

import annotation.experimental
import language.experimental.saferExceptions

@experimental
case class Ex(i: Int) extends Exception(s"Exception: $i")

@experimental
def foo(): Unit throws Ex = throw Ex(1)

@experimental
object Main:
  def main(args: Array[String]): Unit =
    try
      foo()
    catch
      case _: Ex if false => println("Caught")

Output

[error] Ex: Exception: 1
[error]         at Ex$.apply(test.scala:4)
[error]         at test$package$.foo(test.scala:8)
[error]         at Main$.main(test.scala:14)
[error]         at Main.main(test.scala)
[error]         at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
[error]         at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
[error]         at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
[error]         at java.lang.reflect.Method.invoke(Method.java:498)

Expectation

This should not compile. The compiler should not synthesize given instances of CanThrow if it cannot prove at compile time that all subcases for a given exception type are handled.
Beside boolean guards we should also handle extractors like

    catch
      case Ex(0) => println("Caught")

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

Milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions