Skip to content

extension method reports missing context value too eagerly #18697

Open
@scf37

Description

@scf37

Compiler version

3.3.1

Minimized code

import scala.language.experimental.saferExceptions

def foo(): CanThrow[Exception] ?=> String = throw Exception("my error")

extension [A](v: CanThrow[Exception] ?=> A)
  inline def handle(inline f: Throwable => A): A = try v catch case e: Exception => f(e)

def test =
  println(foo().handle(_.getMessage))

Output

The capability to throw exception Exception is missing.
The capability can be provided by one of the following:
 - Adding a using clause `(using CanThrow[Exception])` to the definition of the enclosing method
 - Adding `throws Exception` clause after the result type of the enclosing method
 - Wrapping this piece of code with a `try` block that catches Exception

The following import might fix the problem:

  import unsafeExceptions.canThrowAny

Expectation

Successful compilation. foo().handle(_.getMessage) is actually lazy evaluation of foo() within handle so exception gets caught.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions