Skip to content

Nullability and exception blocks #21

@mattbasta

Description

@mattbasta

Consider this code:

func foo() {
    somethingThatRaisesAnException();
    var x = new Bar();
    finally {
        x.close();
    }
}

x in this case is not a nullable type, and thus can never be null. BType does not (presently) trace whether a particular statement can throw, and it also does not analyze at runtime whether a variable has been declared or not.

In practice, the variable is hoisted to the top of the function, and the declaration at the original location is replaced with an assignment. While the variable may contain uninitialized memory between the point of declaration and use, symbol use is tracked at compile time and no symbols may reference the variable when it contains an undefined value. That said, in the example above, there is no way to know whether somethingThatRaisesAnException() has in fact raised an exception and whether x contains a value.

As such, one or more of the following must be done:

  • Ban local variable access from inside finally and catch blocks.
  • Require that only nullable local variables (which default to null) may be accessed within finally and catch blocks.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions