Skip to content

Wrongly synthesized CanThrow for exceptions with an invariant type parameter #13864

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
prolativ opened this issue Nov 2, 2021 · 0 comments · Fixed by #13866
Closed

Wrongly synthesized CanThrow for exceptions with an invariant type parameter #13864

prolativ opened this issue Nov 2, 2021 · 0 comments · Fixed by #13866
Assignees
Labels
area:saferExceptions scala.language.experimental.saferExceptions itype:bug
Milestone

Comments

@prolativ
Copy link
Contributor

prolativ commented Nov 2, 2021

Compiler version

3.1.2-RC1-bin-20211101-49b6c08-NIGHTLY

Minimized code

import language.experimental.saferExceptions

case class Ex[A](a: A) extends Exception(s"Ex: $a")

def foo[A](a: A): Unit throws Ex[A] = throw new Ex(a)

def test(): Unit =
  try
    foo(1)
  catch
    case Ex(i: Int) => println("Caught an Int")

Output

[error] The capability can be provided by one of the following:
[error]  - A using clause `(using CanThrow[Ex[Int]])`
[error]  - A `throws` clause in a result type such as `X throws Ex[Int]`
[error]  - an enclosing `try` that catches Ex[Int]
[error] 
[error] The following import might fix the problem:
[error] 
[error]   import unsafeExceptions.canThrowAny
[error] 
[error]     foo(1)
[error]

Expectation

This should compile successfully.
If one tries to compile this code with -Xprint:typer one can see that the entire try/catch block gets desugared to

      try 
        {
          final lazy given erased val evidence$2: CanThrow[Ex[A$1]] = ???
          foo[Int](1).apply(/* missing */summon[CanThrow[Ex[Int]]])
        }
       catch 
        {
          case Ex.unapply[A$1 @ A$1](i @ _:Int):Ex[A$1] => 
            println("Caught an Int")
        }

where A$1 does not get substituted with Int as expected.
Interestingly, the error does not occur if we define case class Ex[+A](a: A) with a covariant type parameter

@prolativ prolativ added itype:bug area:saferExceptions scala.language.experimental.saferExceptions labels Nov 2, 2021
@Kordyjan Kordyjan added this to the 3.1.2 milestone Aug 2, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
area:saferExceptions scala.language.experimental.saferExceptions itype:bug
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants