ReviewOS

also looking at this

home-lang/home

fix(types): give catch clauses the enclosing scope as their parent

#933
Open mkantautas wants to merge fix/types-catch-scope-parent into main
2 files +31 -7
8 days ago

Problem

home check crashes on a catch clause that reads a variable from the enclosing scope:

fn main() {
  let x = 1
  try {
  } catch (e) {
    let y = x
  }
  print("ok")
}

The type checker gives the catch clause its own scope for e by setting catch_env.parent = &self.env and then copying catch_env into self.env. After that copy the scope's parent is the scope itself, so looking up x, which is not in the catch scope, follows the parent pointer forever and the process dies with a stack overflow (Segmentation fault).

Fix

The catch clause sets up its scope the way checkMatchArm already does: it saves a copy of the enclosing environment, makes that copy the parent, and in a defer deinitializes the catch scope and restores the saved environment. This also deinitializes the live catch scope rather than the copy taken before the swap.

Tests

New checker resolves enclosing variables inside a catch clause in packages/types/tests/control_flow_checker_test.zig checks that the program above type-checks and that an undefined name inside a catch clause is still reported as "Undefined variable". On main the test runner crashes with Segmentation fault; with this change it passes. zig build test -Denable_jsc=false -Dfilter=types on macOS: 121/121, with the testing allocator's leak checks. On Linux this test step does not compile on main or on this branch, because the parser's module resolver calls libc access and the test target does not link libc.

GitHub Actions does not run on this repo at the moment, so I ran the full suite on Linux x86_64 with the pinned Zig 0.17.0-dev.2163, on main (854d00f) and on this branch: zig build test --summary all passes 9226/9237 on both, with the same 3 failing tests, 8 skips and 23 failed build steps, and the diagnostic snapshot harness output is identical. With the built home, home check on the program above segfaults (exit 134) on main and passes on this branch; home run prints ok on both.

2 changed files on the files tab, with 0 review threads.

Sign in to comment on this pull request.