Skip to content

MirLower: a call through a let-alias of a non-Copy fn field calls the stored callable, not a function named after the binding (#1635) - #1641

Merged
ehartford merged 1 commit into
fix-callable-alias-callfrom
fix-alias-callable-call
Sep 24, 2026
Merged

ehartford merged 1 commit into
fix-callable-alias-callfrom
fix-alias-callable-call

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

MirLower: a call through a let-alias of a non-Copy fn field calls the stored callable, not a function named after the binding (#1635)

let r = c.run; r(21) with run: fn(i32) -> i32 lowered to
call const fn sym(r)() — a direct call to a function SYMBOL named r
with the argument dropped — and codegen died with
FATAL: unhandled MirIntrinsic.GENERIC_CALL sym=r ... arg_count=0.
The build runner's own compile (build/corpora.w:74) hit it.

Root cause (lldb on the :dev stage1, breakpoint on sym_is_generic_fn
conditioned on sym 13356 = r):

frame #0: MirBuilder.sym_is_generic_fn          -> returns 0
frame #1: MirBuilder.generic_fn_node_for_sym    -> returns 0
frame #2: MirBuilder.lower_expr                 (NK_CALL ident dispatch)
frame #3: MirBuilder.lower_block_mode
frame #4: MirBuilder.lower_expr
frame #5: lower_fn_with_sig
frame #6: lower_module
frame #7: Compilation.run_mir_lower

With the generic branch declining, the next ident branch in lower_expr's
NK_CALL dispatch — "Check for builtin calls (embed_file, src, etc.) — no
sig, not a local" — gated on

if bu_sig < 0 and bu_local < 0:      // bu_local = lookup_local(bu_sym)

r is not a MIR local: under D63 a fn(A) -> R field is not Copy, so
let r = c.run is a VIEW binding and lower_let_binding took the alias
path (bind_alias_place, no local). lookup_local(r) < 0, the alias table
was never consulted, and the branch emitted the "unresolved bare
function" GENERIC_CALL with an empty argument list. lower_call /
lower_callable_expr — which do handle place callees — were never reached.

Fix: D29 precedence, applied once at the head of the ident dispatch. A
callee ident that names a local OR an alias place
(ident_names_local_callable, the #1230 helper) is an indirect call
through what the binding names; it goes straight to lower_call before
the variant/distinct/type/generic/builtin interpretations of the text.
lower_callable_expr's in-place read (D63 §12.4: calling through a
binding observes, never moves) now covers alias idents too, so the alias
of a non-Copy callable is read from its place instead of moved out; the
builtin branch's gate uses the same helper.

Fixture: test/behavior/behav_callable_alias_field_call.w — borrowed,
owned, nested field, plus .clone()() and the direct field call in one
expression (42 / 21 / 126). A move-closure with an environment through
every shape (direct, &c.f, alias, owned) returns the right value with
leak count=0 under --debug-alloc.

Verified: with-stage1 analyze build.w audit:codegen -> violations=0 ok
(main: 10096 violations), and violations=0 ok on a :stage2 build too, where check src/main.w is clean and behav_fn_abi_callable.w passes; the 40 closure/callable fixtures green;
:dev green.

Filed from the hunt: #1638 (a non-move closure capturing a local by
reference escapes its frame in a returned struct field, no diagnostic —
independent of this fix, reproduces on the seed) and #1639 (validate-all
was silent on this MIR: a direct call to an unresolvable CK_FN symbol
with the source arguments dropped passed as ok).

… stored callable, not a function named after the binding (#1635)

`let r = c.run; r(21)` with `run: fn(i32) -> i32` lowered to
`call const fn sym(r)()` — a direct call to a function SYMBOL named `r`
with the argument dropped — and codegen died with
`FATAL: unhandled MirIntrinsic.GENERIC_CALL sym=r ... arg_count=0`.
The build runner's own compile (build/corpora.w:74) hit it.

Root cause (lldb on the :dev stage1, breakpoint on sym_is_generic_fn
conditioned on sym 13356 = `r`):

    frame #0: MirBuilder.sym_is_generic_fn          -> returns 0
    frame #1: MirBuilder.generic_fn_node_for_sym    -> returns 0
    frame #2: MirBuilder.lower_expr                 (NK_CALL ident dispatch)
    frame #3: MirBuilder.lower_block_mode
    frame #4: MirBuilder.lower_expr
    frame #5: lower_fn_with_sig
    frame #6: lower_module
    frame #7: Compilation.run_mir_lower

With the generic branch declining, the next ident branch in lower_expr's
NK_CALL dispatch — "Check for builtin calls (embed_file, src, etc.) — no
sig, not a local" — gated on

    if bu_sig < 0 and bu_local < 0:      // bu_local = lookup_local(bu_sym)

`r` is not a MIR local: under D63 a `fn(A) -> R` field is not Copy, so
`let r = c.run` is a VIEW binding and lower_let_binding took the alias
path (bind_alias_place, no local). lookup_local(r) < 0, the alias table
was never consulted, and the branch emitted the "unresolved bare
function" GENERIC_CALL with an empty argument list. lower_call /
lower_callable_expr — which do handle place callees — were never reached.

Fix: D29 precedence, applied once at the head of the ident dispatch. A
callee ident that names a local OR an alias place
(`ident_names_local_callable`, the #1230 helper) is an indirect call
through what the binding names; it goes straight to lower_call before
the variant/distinct/type/generic/builtin interpretations of the text.
lower_callable_expr's in-place read (D63 §12.4: calling through a
binding observes, never moves) now covers alias idents too, so the alias
of a non-Copy callable is read from its place instead of moved out; the
builtin branch's gate uses the same helper.

Fixture: test/behavior/behav_callable_alias_field_call.w — borrowed,
owned, nested field, plus `.clone()()` and the direct field call in one
expression (42 / 21 / 126). A move-closure with an environment through
every shape (direct, `&c.f`, alias, owned) returns the right value with
leak count=0 under --debug-alloc.

Verified: with-stage1 analyze build.w audit:codegen -> violations=0 ok
(main: 10096 violations), and violations=0 ok on a :stage2 build too, where check src/main.w is clean and behav_fn_abi_callable.w passes; the 40 closure/callable fixtures green;
:dev green.

Filed from the hunt: #1638 (a non-move closure capturing a local by
reference escapes its frame in a returned struct field, no diagnostic —
independent of this fix, reproduces on the seed) and #1639 (validate-all
was silent on this MIR: a direct call to an unresolvable CK_FN symbol
with the source arguments dropped passed as ok).
@ehartford
ehartford force-pushed the fix-alias-callable-call branch from 0449133 to 5c6fbee Compare September 24, 2026 16:52
@ehartford
ehartford changed the base branch from main to fix-callable-alias-call September 24, 2026 16:52
@ehartford
ehartford added this pull request to stack #1642 September 24, 2026 16:53
@ehartford
ehartford removed this pull request from stack #1642 September 24, 2026 17:29
@ehartford
ehartford added this pull request to stack #1646 September 24, 2026 17:30
@ehartford
ehartford marked this pull request as ready for review September 24, 2026 18:50
@ehartford
ehartford merged commit fb6753b into main Sep 24, 2026
@ehartford
ehartford deleted the fix-alias-callable-call branch September 24, 2026 18:51
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