Skip to content

Closures reference globals in place (#1313) and S.A.tag() lowers on a variant-path receiver (#1310) - #1321

Merged
ehartford merged 2 commits into
mainfrom
fix-1313-1310-lowering
Sep 22, 2026
Merged

ehartford merged 2 commits into
mainfrom
fix-1313-1310-lowering

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Two MIR-lowering bugs from today's issues, one commit each.

#1313 — a closure that names a global panicked MirLower

check_closure (SemaCheck.w) scanned the whole flat scope bind_names[0..outer_count], which holds every module global registered by register_top_level_global_decl, so () => g + 1 over var g listed g as a capture. MirBuilder.prepare_anonymous_body then found no local for the symbol (lookup_local/lookup_alias_type miss; a global is resolved per body by ensure_global_local) and hit sema_phase_bug — lldb: sema_phase_bug <- MirBuilder.prepare_anonymous_body, symbol 747 = the global's flat-scope binding. The same scan drove the other capture loops, which is why a non-Copy global in an escaping closure was marked MOVED (§15.9 error + "use of moved value" on every later use).

Fix: Sema.binding_index_is_global(idx, sym) (module global_value_decl_bindings slot, or bundle-interface storage) and every capture scan in check_closure skips it. A global is referenced in place (§9.1c, D52), never captured.

Evidence (--dump-mir on the fixed stage1): the closure body has _1: ty3 // g [global], zero capture params, _2 = binop(add, copy _1, const 1); with run prints 1.

#1310 — S.A.tag() failed MIR lowering

lower_expr_place's NK_FIELD_ACCESS branch went straight to lower_field_access, projecting the field A off a place named S: lldb on MirBuilder.mark_unsupported → lower_expr_place(S) <- lower_field_base_place_for_field <- lower_field_access <- lower_auto_ref_call_arg <- … <- lower_method_call. lower_expr already recognized the node as a variant value; the place path lacked the check, which is why binding the variant first worked.

Fix: is_enum_variant_path(node) (the same recognition) and lower_expr_place lets such a node fall through to its materialize-as-temporary fallback.

Evidence (--dump-mir): S.A.tag() lowers as _13 = aggregate(kind=1, tag=0); _14 = copy _13; _15 = ref(shared, _14); call tag(copy _15) — the same shape as let x = S.A; x.tag().

Tests

  • test/behavior/behav_closure_names_global.w: read, g += 10, { g = 3 }, a local captured alongside the global, a non-Copy global str appended by an escaping closure (single-threaded; §9.1c proof holds).
  • test/behavior/behav_enum_variant_path_receiver.w: S.A.tag(), S.B(7).tag(), str-returning method, inside an f-string, a second payloadless enum.

Iterate tier on a fresh :dev stage1: both new tests green; the closure/global/variant/enum set (76 files) green except two unrelated cases: behav_migrate_multi_declarator_globals (environmental — main's own stage1 fails it in this worktree, this branch's stage1 passes it in the main checkout; the child with migrate cannot see the build-generated std.re.* modules from the case dir) and enum_discriminant_flags, a pre-existing stage1-only divergence filed as #1320. :fixpoint/:test not run (per task).

Fixes #1313. Fixes #1310.

Eric Hartford added 2 commits September 22, 2026 12:24
…ferenced in place (§9.1c, D52) (#1313)

`check_closure` scanned the whole flat scope `bind_names[0..outer_count]`
for symbols the body uses, and the flat scope holds every module global
registered by `register_top_level_global_decl` (Sema.w:4819). So `() => g + 1`
over `var g: i32` listed `g` as a capture; MirLower's
`prepare_anonymous_body` (MirLower.w:13111-13114) then found no local for
the symbol — `lookup_local` and `lookup_alias_type` both miss, MirLower
resolves a global per body through `ensure_global_local` instead — and
panicked "anonymous capture lacks a concrete local type: node=7474
symbol=747" (lldb: sema_phase_bug <- MirBuilder.prepare_anonymous_body,
sym 747 = the global's flat-scope binding).

The same scan drove the three other capture loops, which is why a non-Copy
global (`var log: str`) in an escaping closure was marked MOVED and
reported "closure that mutates captured place cannot escape its defining
scope (§15.9)" plus "use of moved value" on every later use of the global.

Fix: `Sema.binding_index_is_global(idx, sym)` (the module's
`global_value_decl_bindings` slot, or a bundle interface's storage in
`interface_global_index`/`interface_global_alt_binds`), and every capture
scan in `check_closure` skips such a binding: the capture list, the
implicit-capture pass, the capture-borrow registration, the ephemeral
escape check, and the escaping-closure move/§15.9 pass. A local that
shadows an unseen global holds the same symbol at another index, so the
predicate is by binding index, not by name.

Evidence on the fixed stage1: `--dump-mir` of the repro shows the closure
body `$anonymous$…` with `_1: ty3 // g [global]`, no capture parameters,
and `_2 = binop(add, copy _1, const 1)`; `with run` prints 1.

Test: test/behavior/behav_closure_names_global.w — a read, `g += 10`,
`{ g = 3 }`, a local captured alongside the global (`() => n + g`), and a
non-Copy global `str` appended by an escaping closure; single-threaded so
the §9.1c race proof holds.
…med `Type` — `S.A.tag()` lowers (#1310)

`lower_expr_place`'s NK_FIELD_ACCESS branch went straight to
`lower_field_access`, so a bare unit-variant receiver `S.A` in `S.A.tag()`
was lowered as the field `A` projected off a place named `S`:
lower_auto_ref_call_arg -> lower_field_access ->
lower_field_base_place_for_field -> lower_expr_place(S) -> mark_unsupported
(lldb breakpoint on MirBuilder.mark_unsupported, frames #0-#7 under
lower_method_call), and the whole function failed MIR lowering
(`[mir-lower-fail] kind=27`). `lower_expr` already recognized the same node
as an enum variant (base ident names an enum in `named_types`, the
qualified or bare variant symbol is in `variant_lookup`) and built the
aggregate; only the place path lacked the check, which is why binding the
variant first worked.

Fix: `is_enum_variant_path(node)` — the recognition lower_expr uses — and
`lower_expr_place` lets such a node fall through to its materialize-as-
temporary fallback instead of projecting a field.

Evidence on the fixed stage1, `--dump-mir` of the repro: `S.A.tag()` lowers
as `_13 = aggregate(kind=1, tag=0, fields=[]); _14 = copy _13;
_15 = ref(shared, _14); call tag(copy _15)` — the same shape as
`let x = S.A; x.tag()` in the block before it. `with run` prints 1 twice.

Test: test/behavior/behav_enum_variant_path_receiver.w — `S.A.tag()`,
`S.B(7).tag()`, a str-returning method, the call inside an f-string, and a
second payloadless enum.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery on cfe0a5a0 (rebased on main 980474c; seed-driven): build rc=0, :fixpoint rc=0; :test red only in main's own reds — behav_distinct_compat.w (#1319's assignment-as-if-arm gap, fix forward in progress), da_match_place_observes (#1322), fizzbuzz (print PR). Verified on a clean main stage1 that the distinct_compat failure is identical without this change; this PR's two commits touch only check_closure's capture scans, one Sema predicate and lower_expr_place's field branch. Mergeable on its own.

@ehartford
ehartford merged commit efde829 into main Sep 22, 2026
@ehartford
ehartford deleted the fix-1313-1310-lowering branch September 22, 2026 18:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

1 participant