Skip to content

Fix #1229 (array literal to []T segfault) and #1230 (call through a local named like a builtin); two MIR validator rules - #1285

Merged
ehartford merged 4 commits into
mainfrom
fix-1229-1230
Sep 22, 2026
Merged

ehartford merged 4 commits into
mainfrom
fix-1229-1230

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1229 and #1230. Four commits, one logical change each; the two validator commits close the tools that stayed silent over the bad MIR.

#1230 — fn both(check: fn(i32) -> Result[Unit, AgeError]) fails LLVM verification

The return type is a red herring; the bug follows the parameter name. --dump-mir of both:

call copy _1(const 1ty3, const "sym1077") -> [return: _3, next: bb1];

The second operand is a 12-byte source location: lib/std/builtins.w:66 is pub fn check(cond: bool, msg: &str, loc: &str = src()). lower_call (src/MirLower.w) resolved the callee ident by name twice — call_sig_for_expr → call_sig_for_sym("check") returned the builtin's signature, and the default-fill block keyed fn_decl_nodes on the same name and appended the builtin's loc = src() default. Sema had already resolved the callee to the local (SemaCheck.w ~15577, D29: scope before the global gate); MirLower never asked lookup_local/lookup_alias_place. Confirmed by prediction: fn(i32) -> bool named check fails identically; the same program with the parameter renamed passes.

Fix (df26fa94): ident_names_local_callable gates both name-keyed lookups; a lexical binding lowers by its fn type. Test: test/behavior/behav_call_local_callable_named_like_builtin.w (Result and bool shapes).

Silent tool (194e8b9f): --validate-all said ok over a two-argument call through a fn(i32) value. New typed-MIR rule: a call through a fn-typed value passes exactly the arguments its type declares (extern/variadic and named callees excluded). Unit test test/internals/mir_indirect_call_arity_test.w on the hand-built #1230 body.

#1229 — first([5, 6]) with fn first(values: []i32) segfaults

--dump-mir of main: _2 = aggregate(fields=[const 5, const 6]); call first(copy _2) with _2 of the slice type. check_array_literal (SemaCheck.w) set target_ty to the expected type for TY_SLICE exactly as for TY_ARRAY, so the literal was typed as []i32, was type-compatible, and note_slice_coerce_call_arg never ran; codegen passed {ptr = 5, len = 6}. Same for []str (summed 0) and []Vec[i32]; a returned literal (fn view() -> []i32: [1, 2]) also segfaulted.

Fix (7eea1346): a slice expectation types the elements; the literal is its [T; N] array ([T; 0] when empty). The call-site coercion accepts the literal where it demanded a place (it is a statement temporary that outlives the call) and the existing slice_coerce_args path materializes it and emits len + slice — the same MIR as let xs = [5, 6]; first(xs):

_2 = aggregate(kind=0, tag=0, fields=[const 5ty3, const 6ty3]);
_4 = len(_3);
_5 = slice(_3, const 0ty4, copy _4);
call const fn sym324(copy _5) -> [return: _6, next: bb2];

Matrix on the rebuilt stage1: first([5, 6]) → 5, first([]) → -1, total(["ab", "cde"]) → 5, count([a, b]) (Vec elements) → 3; bump([5, 6]) into []mut i32 → error "written in place ([]mut) but an array literal is a temporary; bind it"; returned literal → "return type mismatch"; let s: []i32 = [5, 6] keeps #622's diagnostic. Tests: behav_array_literal_to_slice_param.w, err_mut_slice_param_from_array_literal.w, err_return_array_literal_as_slice.w.

Silent tool (71f38c6b): the typed validator had no rule for RK_AGGREGATE. New rule: an aggregate assigned to a slice-typed place is an error (a slice comes only from slice; no producer in MirLower targets one). Unit test test/internals/mir_slice_aggregate_test.w.

Verification (iterate tier)

  • with check src/main.w after every edit; WITH=$PWD/src/main src/main build :dev four times, all green.
  • 11 tests green on the final stage1 (the 6 new ones, Sema accepts length-less [T] binding annotation; produces silently corrupt slice (xs.len() wrong) #622's two, behav_result_unit_falls_through_after_early_err.w, and the two neighboring validator unit tests).
  • Typed-validator sweep with both new rules over the two repros plus 14 fn-type/closure behavior tests: no false positive (oracle: an lldb breakpoint on validate_ownership_mir_module, which runs only after the typed pass returns clean).
  • Not run: :fixpoint, :test, the battery (per the brief).

ABI / ownership

No FnAbi/PassMode or drop-scheduling change. #1230 changes which signature a call site selects (the local's fn type instead of a same-named global's), not how any parameter is passed; #1229 reuses the existing slice-coerce lowering for a materialized statement temporary. I do not think this needs its own batch, but flagging the #1229 Sema typing change as touching call-argument classification for a second opinion.

Found on the way

Unresolved

  • The closure half of the §4.9 item mentioned in Calling a fn-typed parameter that returns Result[Unit, E] fails LLVM verification #1230 (the "missing return" site in SemaCheck.w ~14274 needing the type_is_result_of_unit exemption) is not touched here; it is now testable.
  • Method calls (obj.f([1, 2])) with a []T parameter do not go through note_slice_coerce_call_arg (only the free-fn signature path does), so a literal there is now a type mismatch rather than a slice; the named-Vec form has the same limitation today.

Eric Hartford added 4 commits September 22, 2026 08:33
…ed fn tables (#1230)

`fn both(check: fn(i32) -> Result[Unit, AgeError])` failed LLVM
verification: "Incorrect number of arguments passed to called function!
  %3 = call { i32, [8 x i8] } %1(ptr %2, i32 1, %str %.fca.1.insert10)".

The Result return is a red herring; the bug follows the parameter NAME.
`--dump-mir` of `both`:

    call copy _1(const 1ty3, const "sym1077") -> [return: _3, next: bb1];

The second operand is a 12-byte source location. `lib/std/builtins.w:66`
declares `pub fn check(cond: bool, msg: &str, loc: &str = src())`, and
`lower_call` (src/MirLower.w) resolved the callee ident by name twice:
`call_sig_for_expr` -> `call_sig_for_sym("check")` returned the builtin's
signature, and the default-fill block keyed `fn_decl_nodes` on the same
name, appending the builtin's `loc = src()` default to a call through the
local. Sema had already resolved the callee to the local (SemaCheck.w
~15577, D29: the scope lookup runs before the global gate); MirLower's
`lookup_local`/`lookup_alias_place` were never asked.

Same failure with `fn(i32) -> bool`, none with the parameter renamed.

Fix: `ident_names_local_callable` gates both lookups; a lexical binding
lowers by its fn type (`callable_fn_tid`). Test: the Result[Unit, E] and
bool shapes, both named `check`.
… slices it (#1229)

`first([5, 6])` with `fn first(values: []i32)` compiled clean and
segfaulted. `--dump-mir` of main:

    _2 = aggregate(kind=0, tag=0, fields=[const 5ty3, const 6ty3]);
    call const fn sym324(copy _2) -> [return: _3, next: bb2];

with `_2: ty408`, the slice type: `check_array_literal` (SemaCheck.w) set
`target_ty` to the expected type for TY_SLICE exactly as for TY_ARRAY, so
the literal was typed AS `[]i32`, the argument was type-compatible and
`note_slice_coerce_call_arg` never ran; NK_ARRAY_LIT lowering then
aggregated the elements into the slice-typed temp and codegen passed
`{ptr = 5, len = 6}`. Same for `[]str` (summed 0) and `[]Vec[i32]`.

A slice expectation now types the elements and the literal is its
`[T; N]` array (`[T; 0]` for an empty literal). The call-site coercion
accepts the literal where it demanded a place: it is a statement
temporary that outlives the call, `lower_expr_place` materializes it and
the `slice_coerce_args` path emits `len` + `slice` -- the MIR of the
named form:

    _2 = aggregate(kind=0, tag=0, fields=[const 5ty3, const 6ty3]);
    _4 = len(_3);
    _5 = slice(_3, const 0ty4, copy _4);
    call const fn sym324(copy _5) -> [return: _6, next: bb2];

A `[]mut T` parameter rejects the literal (writes would die with the
temporary; bind it). Every other slice context -- a returned literal,
which also segfaulted -- is now a type mismatch instead of a bit-cast.
…or (#1229)

`--validate-all` reported ok over `_2 = aggregate(fields=[5, 6])` into a
slice-typed local, the MIR that segfaulted in #1229. The typed validator
had no rule for RK_AGGREGATE at all. A slice is a view of storage that
already exists and is produced only by `slice`; no lowering builds one
from fields (every RK_AGGREGATE producer in MirLower targets a struct,
tuple, enum, Option or array). Reject the shape by destination type.

Unit test: the hand-built body from #1229 is rejected; the same aggregate
into an array-typed place (the literal itself) passes.
…ype declares (#1230)

`--validate-all` reported ok over

    call copy _1(const 1ty3, const "sym1077") -> [return: _3, next: bb1];

with `_1: fn(i32) -> Result[Unit, AgeError]` -- two arguments against a
one-parameter fn type -- and LLVM's verifier was the first to object
("Incorrect number of arguments passed to called function!"). The typed
validator judged named callees (#1180's Unit rule) but never a call
through a fn-typed value. A fn type's d1 is its parameter count and
codegen adds the environment pointer itself, so the call's argument count
must equal it; extern fn types (variadic) and named callees are left to
their own rules.

Unit test: the hand-built #1230 call (2 args, and 0 args) is rejected; the
matching call passes.
@ehartford
ehartford merged commit 83b3f1f into main Sep 22, 2026
5 checks passed
@ehartford
ehartford deleted the fix-1229-1230 branch September 22, 2026 13:37
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.

Array literal passed to a []T parameter segfaults: the literal is typed as the slice and lowered as its fat pointer

1 participant