Repository navigation
Conversation
Contributor
Author
|
Battery GREEN on the stack top |
added 8 commits
September 28, 2026 01:12
…try 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
…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
…istinct 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
…es — 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
…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
…e 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".
…ause 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.
…— 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
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.
Stack layer 3 of #1813. One commit per issue, each with a failing-before test:
v.pop()result (Option[str]) is never dropped: the payload leaks #1786 a discarded call result (incl. intrinsics likeVec.pop) is a statement temp; an entry point's tail is statement position (§9.1). Debug allocator: leak count 1 → 0 across a 10-case matrix.r as strwithr: &Name(a distinct str) casts the reference, not the string — prints garbage #1782 a cast through a reference reads what it names, through distinct wrappers too.[value; N]with a const N builds N copies; non-const N and length mismatches are errors.s[2..],s[..j]) yields a value nothing accepts:let t: str = s[2..]is invalid MIR,print(s[2..])and.replacefail inference #1587 (partial)s[a..b]is a&strview (D71 §4.8a) with range and UTF-8-boundary panics; returning a slice is refused until&stris{ptr,len}(&strpoints at a string header, so a str range view (D71 §4.8a) cannot be returned: make&strits own {ptr, len} #1810).Agent evidence:
:gategreen,:drop-audit0 regressions. Draft until the stack's battery is green.