Skip to content

Sema cluster A: #1344 #1472 #1493 #1494 #1303 #1477 #1503 #1502 #1517 #1565 #1533 #1220 #1478 (13 parser/Sema/MIR fixes) - #1721

Merged
ehartford merged 16 commits into
mainfrom
sema-cluster-a
Sep 26, 2026
Merged

ehartford merged 16 commits into
mainfrom
sema-cluster-a

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Thirteen separate commits, one per issue, each with a pinning test (test/behavior/behav_*.w or test/compile_errors/err_*.w). Verified on a stage1 built from this branch (:dev, dev rc=0); examples/ and build/release_uat_fixtures/ all check green on it. No battery run yet.

Root causes:

Skipped: #1508 (closure -> T annotation dropped): the spec has no text on whether closures may declare a return type, so whether it becomes a checked annotation or a parse error is a ruling.

Filed during the work: #1710 (optional chain resets its receiver).

Eric Hartford added 14 commits September 26, 2026 02:46
…ecl registered both silently and E.A took one meaning

Fixes #1344
…ontextual Copy); check_bool_condition compared the exact ref type to bool and rejected `if v[i]:`

Fixes #1472
… tail; types_compatible is one-sided on Never so check_fn_body_with_sig_at let it reach the MIR validator

Fixes #1493
…on another is a missing return (§4.10, D43); infer_unannotated_function_return_type substituted T.default()

Fixes #1494
…tin (§18.2); check_method_call_parts matched the builtin by name before user methods

Fixes #1303
…tee's type, and the view materializes as its pointee before the builtin widens; check_binary typed `&i64 + 1` at i32 and contextualize narrowed the view to the peer

Fixes #1477
…ted missing-return rule skips them; an early `return 1` with a fall-off is the exit-status idiom (examples/c-interop)

Refs #1494
…10.3); optional_chain_result_type used the struct-only field lookup and lower_optional_chain_field projected a struct field by symbol

Fixes #1503
…d widens it (§4.4a, §4); lower_cast emitted one enum→float RK_CAST the validator rejected after Sema accepted it

Fixes #1502
…d_access projected a `len` field place that no slice layout names, and the read yielded 0

Fixes #1517
…d a spec'd hole still needs a default display (§15.4.6, §15.4.8); FMT_BUF_WRITE_FMT treated every non-numeric value as a str header and Sema skipped the display check when a spec was present

Fixes #1565
…coverage (§9.7); exh_class treated it as open so every slice-pattern match demanded a `_` arm, and the witness now names the uncovered length

Fixes #1533
…ng is spelled `as`); types_compatible had an int←float arm and builtin_arg_type_compatible accepted it through the operator-promotion arm, so codegen failed with no location

Fixes #1220
…ad of building one copy; the fill desugar read only an integer literal and fell back to fill_count = 1, so a typed binding read uninitialized tail elements

Refs #1478
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery on 9f330e5: build and :fixpoint green; :test red on 2/1318 behavior tests: behav_unannotated_value_return_fallthrough_default.w pins the pre-#1494 defaulting (non-conforming per §4.10/§9.1, becomes an err_ test) and behav_zip_reads_info_zip.w:145 (u8 ^ 0x55 now 'mixed signedness': #1477 regression on bitwise ops beside element views). Repair in progress; stays draft.

Eric Hartford added 2 commits September 26, 2026 05:18
…intee's type (`v[i] ^ 0x55` on a Vec[u8]); the bitwise branch of check_binary read the peer through numeric_operand_type, which does not see through the view, so the literal fell to i32 and the mixed-signedness check rejected it once the view stopped narrowing to the peer (#1477)

Refs #1477
…urn (§4.10, §9.1), not a defaulted 0; behav_unannotated_value_return_fallthrough_default pinned the non-conforming behavior and is replaced by the diagnostic pin (#1494)

Refs #1494
@ehartford

Copy link
Copy Markdown
Contributor Author

Seed-driven battery green on 1c07e10 (host darwin-aarch64, seed per seed.lock):

sema-a build  rc=0
sema-a build :fixpoint rc=0
sema-a build :test rc=0
sema-a build :test-green rc=0
sema-a build :last-green rc=0
sema-a build :user-programs-safe rc=0

@ehartford
ehartford marked this pull request as ready for review September 26, 2026 11:25
@ehartford
ehartford merged commit 410fe68 into main Sep 26, 2026
@ehartford
ehartford deleted the sema-cluster-a branch September 26, 2026 16:23
ehartford pushed a commit that referenced this pull request Sep 28, 2026
…— a `let` is a runtime value, a local `const` is a const

§4.3a: "Length N must be a compile-time constant (integer literal or
`const`)". §9.1b: a const expression is integer literals, arithmetic,
unary negate, `not` and references to other `const`s; "`let` bindings are
runtime values (even if initialized from a constant)".

#1478's array_fill_count asked the comptime evaluator for the count's
value, and the evaluator reads an immutable module `let` (eval_ident's
find_module_let_decl), so `let N: i64 = 4; [7; N]` compiled — the
program err_1478_array_fill_const_count (from #1721) pins as refused. The
count must now be a const expression (expr_is_const_expr: literals,
grouping, casts, unary and binary operators over those, and names of
`const`s) before it is evaluated; otherwise "the count is not a
compile-time constant … a `let` is a runtime value (§9.1b)". The test
keeps its program and now expects that message (it pinned the interim
"needs an integer literal count" refusal "until the fill is evaluated by
Sema").

A local `const` was not a const to either check: the parser's
parse_const_binding did not mark its binding (only a module `const`
was), and the evaluator could not see it. The parser marks it
(mark_const_decl); the evaluator reads a live local `const`'s
initializer (Sema drops a name's decl when its scope ends, and a local
`let` is never read).

Evidence: the fix-lang battery's native compile-error tests, and
`with-stage1 test` on err_1478_array_fill_const_count before this commit:
failed (the program built and printed 4). After: refused with the §9.1b
message; `const M = 5; [1; M]` and `[2; (N + M) * 2 - 5]` build 5 and
13 elements (behav_1478_array_fill_const_count); a function-local `let`
count is still refused.

Refs #1478
ehartford pushed a commit that referenced this pull request Sep 28, 2026
…— a `let` is a runtime value, a local `const` is a const

§4.3a: "Length N must be a compile-time constant (integer literal or
`const`)". §9.1b: a const expression is integer literals, arithmetic,
unary negate, `not` and references to other `const`s; "`let` bindings are
runtime values (even if initialized from a constant)".

#1478's array_fill_count asked the comptime evaluator for the count's
value, and the evaluator reads an immutable module `let` (eval_ident's
find_module_let_decl), so `let N: i64 = 4; [7; N]` compiled — the
program err_1478_array_fill_const_count (from #1721) pins as refused. The
count must now be a const expression (expr_is_const_expr: literals,
grouping, casts, unary and binary operators over those, and names of
`const`s) before it is evaluated; otherwise "the count is not a
compile-time constant … a `let` is a runtime value (§9.1b)". The test
keeps its program and now expects that message (it pinned the interim
"needs an integer literal count" refusal "until the fill is evaluated by
Sema").

A local `const` was not a const to either check: the parser's
parse_const_binding did not mark its binding (only a module `const`
was), and the evaluator could not see it. The parser marks it
(mark_const_decl); the evaluator reads a live local `const`'s
initializer (Sema drops a name's decl when its scope ends, and a local
`let` is never read).

Evidence: the fix-lang battery's native compile-error tests, and
`with-stage1 test` on err_1478_array_fill_const_count before this commit:
failed (the program built and printed 4). After: refused with the §9.1b
message; `const M = 5; [1; M]` and `[2; (N + M) * 2 - 5]` build 5 and
13 elements (behav_1478_array_fill_const_count); a function-local `let`
count is still refused.

Refs #1478
ehartford pushed a commit that referenced this pull request Sep 28, 2026
…— a `let` is a runtime value, a local `const` is a const

§4.3a: "Length N must be a compile-time constant (integer literal or
`const`)". §9.1b: a const expression is integer literals, arithmetic,
unary negate, `not` and references to other `const`s; "`let` bindings are
runtime values (even if initialized from a constant)".

#1478's array_fill_count asked the comptime evaluator for the count's
value, and the evaluator reads an immutable module `let` (eval_ident's
find_module_let_decl), so `let N: i64 = 4; [7; N]` compiled — the
program err_1478_array_fill_const_count (from #1721) pins as refused. The
count must now be a const expression (expr_is_const_expr: literals,
grouping, casts, unary and binary operators over those, and names of
`const`s) before it is evaluated; otherwise "the count is not a
compile-time constant … a `let` is a runtime value (§9.1b)". The test
keeps its program and now expects that message (it pinned the interim
"needs an integer literal count" refusal "until the fill is evaluated by
Sema").

A local `const` was not a const to either check: the parser's
parse_const_binding did not mark its binding (only a module `const`
was), and the evaluator could not see it. The parser marks it
(mark_const_decl); the evaluator reads a live local `const`'s
initializer (Sema drops a name's decl when its scope ends, and a local
`let` is never read).

Evidence: the fix-lang battery's native compile-error tests, and
`with-stage1 test` on err_1478_array_fill_const_count before this commit:
failed (the program built and printed 4). After: refused with the §9.1b
message; `const M = 5; [1; M]` and `[2; (N + M) * 2 - 5]` build 5 and
13 elements (behav_1478_array_fill_const_count); a function-local `let`
count is still refused.

Refs #1478
ehartford added a commit that referenced this pull request Sep 28, 2026
… refs, const array fills, str range views (#1786 #1781 #1782 #1478 #1587) (#1812)

* MirLower/Sema: a discarded call result is a statement temp, and an entry point's tail is a statement — both dropped, never leaked

§2.5.1 / §9.1: a statement's value is discarded and dropped. Two owners
were missing one:

1. MirLower.lower_intrinsic_call (and lower_intrinsic_call_with_receiver_
   operand, the dyn-dispatch path in lower_method_call) never registered
   its result temp as a statement temp; lower_call does
   (register_stmt_temp). A discarded `v.pop()` (VEC_POP) therefore had no
   drop: the popped `Some(payload)` leaked. Now every call result goes
   through call_result_operand: consumed by a binding or an argument it is
   cancelled, discarded it is dropped at the statement's end.
2. Sema.discard_body_tail discarded only an assignment tail. §9.1: `main`,
   `@[entry]` and `test_*` do not infer; their tail is statement position.
   A tail `mk()` in `main` was lowered as the body's value and moved into
   the unit return slot, never dropped. The verdict now covers any tail of
   an entry point (body_tail_is_statement), and a discarded statement tail
   keeps its own type: overwriting it with Unit sized an intrinsic's result
   temp as Unit — an undropped unit slot and a SIGTRAP for a tail `v.pop()`.

Evidence: `with run --debug-alloc` on the issue's program reported
`LEAK addr=... size=16 origin=unknown, leak count=1`; --dump-mir showed
`call sym(pop)(copy _1) -> [return: _11]` with no `drop(_11)` while the
free-call result `_7` had `drop(_7) @ scope-exit`, and --dump-drop-plan
listed no site for _11. A nine-cell matrix (free fn / method / generic
method / intrinsic × statement / tail / `let _`) leaks 0 after; before,
every discarded result in tail position and every intrinsic leaked.

Fixes #1786

* Parser: a block's statements share its column; a line indented to no enclosing block's level is an error, never the outer block's next statement

§29.13 Form 2: the body is the indented block; it ends when indentation
returns to or below the introducing construct's level. parse_indented_block
took any line at a column >= its own as its next statement, so a dedent to
a level no enclosing block has (an `if` body at 12, its next line at 8,
`main`'s body at 4) ended the `if` body and silently joined `main`'s body:
the line ran with the `if` false, reading a binding from the arm that never
ran (base stage1 printed `inner 1`; with one-statement-body scoping it
became `undefined variable`, still with no word about the layout). The
same check names a line indented deeper than the statement before it with
no ':' opening a body.

Evidence: `with check` on the issue's program reported only `undefined
variable` at the print; parse_indented_block's loop had `if next_col <
block_col: break` and no case for `next_col > block_col`. Now: "unexpected
indentation: this line is indented to column 8, which is no enclosing
block's indentation (the block here starts at column 4)" at the line, with
a §29.13 help. The #1391 indentation tests are unchanged.

Fixes #1781

* MirLower: a cast through a reference reads what it names, through a distinct wrapper too — `r as str` on `r: &Name` reads the string, not the pointer

§3.7 / §4.5: a distinct type is a zero-cost wrapper, and a cast through a
reference reads what the reference names. lower_cast read through a `&T`
source only when the target was a slice and the pointee was itself a str,
array or slice (`r as []u8` on `r: &str`, #1719). `r as str` with
`r: &Name` (`Name = distinct str`) took the other path: the reference
VALUE, a pointer, was cast to the string header and `print` showed a
garbage byte. The read-through now resolves both the pointee and the
target through their distinct wrappers (unwrap_builtin_arg_distinct) and
applies to str, array and slice targets.

Evidence: --dump-mir on the issue's program lowered the cast as
`_6 = cast(copy _1 as ty16)` with `_1: &Name` (a pointer local), no deref
projection; the main tree's stage1 prints `\n0\n\n` for the new test, the
fixed one prints `abc / 3 / xyz`. The test uses literals on purpose: the
ownership of a non-Copy value cast into or out of a distinct wrapper is a
separate, pre-existing defect (the allocator reports the same DOUBLE FREE
on the base stage1 for an owned `n as str`; a `Name` drop is a no-op and
`s as Name` leaves the buffer with `s`), filed separately.

Fixes #1782

* Parser/Sema/MirLower: `[value; N]` with a `const` count builds N copies — the count is a node Sema evaluates, never a silent one-copy fallback

§4.3a: "`[value; N]`: array of N copies of value. Length N must be a
compile-time constant (integer literal or `const`)." The parser desugared
the fill at parse time and could read only an integer literal; any other
count fell back to ONE copy with no diagnostic (later: a loud refusal).
Now a non-literal count stays on the literal as its d2 with the value held
once; Sema evaluates it (array_fill_count, comptime_try_eval_expr) and
records it (array_fill_counts) for MirLower (array and Vec targets) and the
comptime evaluator (a top-level `const A = [v; N]` evaluates the count
itself, its literal being checked after it). A runtime count is refused by
name. The AST cost the issue notes stays for literal counts (#1358's dump
shape is kept); a const count is now one node.

Also: an annotated `[K]T` binding required no agreement with the literal's
length, so `let b: [4]i32 = [7; N]` (one copy) compiled and read
uninitialized tail elements (`4 0`); a length mismatch is now an error.

Evidence: the issue's program printed `1` and `4 0` on the base; after,
`4` and `4 7`. Tests: behav_1478_array_fill_const_count (const, const
expression, `2 + 2`, `N * 2`, Vec targets, a comptime const array),
err_1478_array_fill_non_const_count, err_1478_array_len_mismatch.

Fixes #1478

* Sema/MirLower/Codegen: a str range `s[a..b]` is a `&str` view of its bytes (D71 §4.8a); out of range or inside a UTF-8 character panics

D71 / §4.8a: "A range index on a `str` or `&str` — `s[a..b]`, `s[a..]`,
`s[..b]`, `s[..]` — is a `&str` view of the bytes from a to b, with the
origin and ephemerality of any view. Offsets are byte offsets. An offset
past the end, or one that falls inside a UTF-8 character, panics."

- Sema.check_slice types a str range `&str` with the base's origins (the
  invalidation rules follow: `s = …` while the view lives is refused; an
  owned demand `let t: str = s[1..]` materializes under D22). It used to
  refuse the spelling by name (before that: a valueless expression that
  failed generic inference and tripped "invalid MIR before codegen").
- MirLower.lower_slice_expr: a `&str` points at a string header, so the
  range's {ptr, len} is built into a hidden str local of the frame that
  owns nothing, and the view is a reference to it. A range through a
  reference slices what it names (§3.7): the base is dereferenced —
  `a[1..]` on `a: &[4]i32` failed codegen ("slice base has no bounds
  metadata"), and `r[1..]` on `r: &str` read `len(_2.*)` off the pointer
  local's type as 0 (RK_LEN now uses the projected place type).
- Codegen: a str range is checked in every build — "string slice out of
  range", "string slice inside a UTF-8 character" (a `10xxxxxx` byte at
  the offset) — instead of the debug-only slice check.
- Not the ruling yet: returning a str range view. Its header lives in the
  callee's frame, so the view carries the sentinel origin
  syms.str_range_view and a return of it (directly or through a binding)
  is refused by name: "a string slice cannot be returned yet (#1587)".
  Making that valid needs `&str` to carry its own {ptr, len} — the
  `&str → {ptr,len}` campaign — which touches every `&str` producer and
  consumer (FnAbi, c_import marshalling, element views, pattern bindings,
  the runtime's `str_data`), beyond this fix.

Evidence: --dump-mir now `_2 = slice(_1, 0, 5); _3 = ref(shared, _2)`
with `_2: str` undropped; --validate-all ok; --debug-alloc leak count=0
on the test; the test fails before ("a range of a str has no view type
yet"). err_str_range_no_view_type pinned the old refusal and is removed.

Fixes #1587

* Parser: only a statement that begins its line has an indentation — the one after `a; b` starts mid-line (#1781 follow-up)

The #1781 check (a line indented to no enclosing block's level is an
error) measured the column of every next statement, and skip_separators
steps over `;` as well as newlines, so the second statement of
`sum = sum + i; i = i + 1` sat at column 23 of an 8-column block and was
refused (behav_semicolons.w, red on the #1813 stack battery). The check now
applies when the token begins its line (column == line_indent_of). Verified
on stage1: behav_semicolons prints ok; err_1781_dedent_matches_no_block and
err_1781_over_indented_statement still report "unexpected indentation".

* test/parser: four AST tests set their exit code with exit_code(), because main's tail is statement position (#1786, functions.md)

Spec (docs/spec/functions.md): "main, @[entry] functions and test_*
functions do not infer: their return contract is fixed and their tail is
statement position." #1786 made the compiler conform, so ast_basic_decls,
ast_char_literals, ast_expr_forms and ast_labeled_control — which read
`expect-exit: N` from a bare tail value — exited 0 (parser-tests red on
the #1813 stack battery). They were relying on the non-conforming tail; the
observation now goes through std.process exit_code. Verified on stage1:
exit codes 12, 107, 11, 5.

* Sema: a `[v; N]` count is a compile-time constant expression (§9.1b) — a `let` is a runtime value, a local `const` is a const

§4.3a: "Length N must be a compile-time constant (integer literal or
`const`)". §9.1b: a const expression is integer literals, arithmetic,
unary negate, `not` and references to other `const`s; "`let` bindings are
runtime values (even if initialized from a constant)".

#1478's array_fill_count asked the comptime evaluator for the count's
value, and the evaluator reads an immutable module `let` (eval_ident's
find_module_let_decl), so `let N: i64 = 4; [7; N]` compiled — the
program err_1478_array_fill_const_count (from #1721) pins as refused. The
count must now be a const expression (expr_is_const_expr: literals,
grouping, casts, unary and binary operators over those, and names of
`const`s) before it is evaluated; otherwise "the count is not a
compile-time constant … a `let` is a runtime value (§9.1b)". The test
keeps its program and now expects that message (it pinned the interim
"needs an integer literal count" refusal "until the fill is evaluated by
Sema").

A local `const` was not a const to either check: the parser's
parse_const_binding did not mark its binding (only a module `const`
was), and the evaluator could not see it. The parser marks it
(mark_const_decl); the evaluator reads a live local `const`'s
initializer (Sema drops a name's decl when its scope ends, and a local
`let` is never read).

Evidence: the fix-lang battery's native compile-error tests, and
`with-stage1 test` on err_1478_array_fill_const_count before this commit:
failed (the program built and printed 4). After: refused with the §9.1b
message; `const M = 5; [1; M]` and `[2; (N + M) * 2 - 5]` build 5 and
13 elements (behav_1478_array_fill_const_count); a function-local `let`
count is still refused.

Refs #1478

---------

Co-authored-by: Eric Hartford <[email protected]>
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