Skip to content

Lazy include units: top-level variables live in the global scope - #608

Merged
blacktrs merged 1 commit into
mainfrom
fix/unit-scope-globals
Nov 10, 2026
Merged

blacktrs merged 1 commit into
mainfrom
fix/unit-scope-globals

Conversation

@blacktrs

@blacktrs blacktrs commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

Problem

Since #564, an entry-mode application's own `src` files run at their `require` inside a generated `__mc_unit_N` function. Their top-level variables became locals of that function:

  • `global $x` and `$GLOBALS['x']` in the file's functions read null; an object read segfaults;
  • a top-level store never read in the unit itself is removed as a dead store.

php runs a file required from global scope in the global scope, and every release before #564 did the same (the file was flattened into `__main`).

Fix

Unit bodies get the same global-slot declarations as `__main` (`injectGlobalDecls`). The units still run lazily, at their `require`.

Tests

  • `tests/libs/lazy_units` gains a fixture where the file's top-level variables are read through `global`, `$GLOBALS` and from the entry.
  • The smoke never ran before: manifest paths were repo-relative (the manifest resolves them against its own directory) and `expected.out` was missing. Both fixed; the expected output comes from php.
  • `tools/liblazyunits_smoke.sh` is now part of `tools/docker/gate.sh`.
  • Without the fix the smoke is red (SIGSEGV); with it, green. AOT `-k require|global|include` green (macOS).

Found while rebasing the LSP stack (#529): every semantic test reads its fixtures through `global`.

#564 runs an entry app's own src files at their require, wrapped in __mc_unit_N; its
top-level variables became function locals, so `global $x` / $GLOBALS read nothing
(an object global segfaults) and a store never read in the unit was dropped as dead.
Inject the global slots into unit bodies as for __main. The lazy_units smoke gains a
global-scope fixture, a php oracle (expected.out), manifest paths relative to the
manifest, and runs in the gate.
@blacktrs
blacktrs merged commit 2f28dd1 into main Nov 10, 2026
61 of 63 checks passed
@blacktrs
blacktrs deleted the fix/unit-scope-globals branch November 10, 2026 05:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant