Skip to content

Flow-typing does not record non-null info for try-catch blocks #24296

@HarrisL2

Description

@HarrisL2

Compiler version

3.7

Minimized code

//> using options -Yexplicit-nulls

def main() =
  var cache: String | Null = null
  try {
    cache = ""
  } catch {
    case e =>
      throw e
  }
  cache.trim()

Output

-- [E008] Not Found Error: local/flow.scala:10:8 -------------------------------
10 |  cache.trim()
   |  ^^^^^^^^^^
   |  value trim is not a member of String | Null.
   |  Since explicit-nulls is enabled, the selection is rejected because
   |  String | Null could be null at runtime.
   |  If you want to select trim without checking for a null value,
   |  insert a .nn before .trim or import scala.language.unsafeNulls.

Expectation

If the try block sets a variable to non-null, and the catch block does not retract it, we can expect the variable outside of the try-catch to be non-null as well

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