Repository navigation
Fix #1229 (array literal to []T segfault) and #1230 (call through a local named like a builtin); two MIR validator rules - #1285
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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 verificationThe return type is a red herring; the bug follows the parameter name.
--dump-mirofboth:The second operand is a 12-byte source location:
lib/std/builtins.w:66ispub 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 keyedfn_decl_nodeson the same name and appended the builtin'sloc = src()default. Sema had already resolved the callee to the local (SemaCheck.w ~15577, D29: scope before the global gate); MirLower never askedlookup_local/lookup_alias_place. Confirmed by prediction:fn(i32) -> boolnamedcheckfails identically; the same program with the parameter renamed passes.Fix (
df26fa94):ident_names_local_callablegates 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-allsaid ok over a two-argument call through afn(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 testtest/internals/mir_indirect_call_arity_test.won the hand-built #1230 body.#1229 —
first([5, 6])withfn first(values: []i32)segfaults--dump-mirofmain:_2 = aggregate(fields=[const 5, const 6]); call first(copy _2)with_2of the slice type.check_array_literal(SemaCheck.w) settarget_tyto the expected type forTY_SLICEexactly as forTY_ARRAY, so the literal was typed as[]i32, was type-compatible, andnote_slice_coerce_call_argnever 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 existingslice_coerce_argspath materializes it and emitslen+slice— the same MIR aslet xs = [5, 6]; first(xs):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 forRK_AGGREGATE. New rule: an aggregate assigned to a slice-typed place is an error (a slice comes only fromslice; no producer in MirLower targets one). Unit testtest/internals/mir_slice_aggregate_test.w.Verification (iterate tier)
with check src/main.wafter every edit;WITH=$PWD/src/main src/main build :devfour times, all green.behav_result_unit_falls_through_after_early_err.w, and the two neighboring validator unit tests).validate_ownership_mir_module, which runs only after the typed pass returns clean).:fixpoint,:test, the battery (per the brief).ABI / ownership
No
FnAbi/PassModeor 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
check --validate-allon any bootstrap stage1 parks at 7 GB invalidate_ownership_mir_module(drop-state driver), verified on main's own stage1 from before this branch; the release binary answers in 0.1 s. Filed with thesamplecall graph and the lldb confirmation.Unresolved
type_is_result_of_unitexemption) is not touched here; it is now testable.obj.f([1, 2])) with a[]Tparameter do not go throughnote_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.