Skip to content

Language fixes: discarded results dropped, indentation, casts through refs, const array fills, str range views (#1786 #1781 #1782 #1478 #1587) - #1812

Merged
ehartford merged 8 commits into
mainfrom
fix-lang
Sep 28, 2026
Merged

ehartford merged 8 commits into
mainfrom
fix-lang

Conversation

@ehartford

@ehartford ehartford commented Sep 27, 2026 •

Copy link
Copy Markdown
Contributor

Stack layer 3 of #1813. One commit per issue, each with a failing-before test:

@ehartford
ehartford added this pull request to stack #1813 September 27, 2026 23:34
@ehartford ehartford changed the title fix lang Language fixes: discarded results dropped, indentation, casts through refs, const array fills, str range views (#1786 #1781 #1782 #1478 #1587) Sep 27, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top 3fddbecd (#1801 → #1811 → #1812 → #1821, on main 7fd175c), driven by the new seed nightly-20260927-local-1-99939352ffda: :gate rc=0 in 651 s; :battery-checks (seed-driver, fixpoint, test-with-audits incl. drop and move audits and every test lane, user-programs-safe, last-green) rc=0 in 2563 s. Earlier reds on this stack were each fixed in the layer that caused them (run 1: six lanes; run 3: the native runner skipped the missing-input check — fixed by bea5908, bottom of #1811). Merge: gh stack merge 1821 --yes --squash.

Eric Hartford 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
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