Skip to content

D111: a str is a value (counted buffers, holds, last use moves) - #2275

Merged
ehartford merged 37 commits into
sig-modesfrom
str-values
Oct 8, 2026
Merged

ehartford merged 37 commits into
sig-modesfrom
str-values

Conversation

@ehartford

@ehartford ehartford commented Oct 8, 2026 •

Copy link
Copy Markdown
Contributor

D111: a str is a value.

  • Runtime: a str buffer's holder count lives in its allocation header; with_str_retain/release, in-place append only when unique, keys copied into maps retain.
  • Sema: str is Copy with drop glue (copy_is_bits separates the two kinds of Copy).
  • MIR: a consumed copy carries an operand hold (retain, or take at the local's last use, so a last use is a move with no count traffic). Codegen applies the hold where the consumer reads the operand; --dump-mir shows it (copy+retain, copy+take).
  • Fixed on the way (each with a behavior test run under the debug allocator): matching on a str leaked it and bindings blanked observed subjects; a &str passed to an owning parameter double-freed (plain, generic, library map key, implicit fill); iteration over a plain non-Copy struct crashed LLVM (MIR disagreed with Sema's view rule); clone(&self) on a view leaked a count per call; [s; 65] of a str read garbage (§4.3a: evaluated per element unless plain bits); a cast through a reference to str stays a view; slice bounds naming an element (also broken on main).
  • Tools: WITH_DUMP_LLIR_PRE works on the codegen-units path; the typed validator's fill check covers glue types; the view-into-temporary diagnostic names the annotation, not .clone(), for a Copy value.
  • Filed: Typed MIR validator accepts a field projection through a reference with no deref #2269 (validator silent on a field projection through a reference), D111 non-compliance: an owned str demand does not materialize a &str through Ok-wrap, collect, or a &&str join #2271 (owned-str demands not yet met: implicit Ok tail, collect of views, &&str join).

Found by the stack's first battery, fixed here (each with a behavior test):

  • a str field read through its binding moved the field out; a comprehension stored a generator's yielded view (also on main); a D60 tail assignment's read leaked a hold; the comptime mutable-global check ignored a shadowing local; sub in text had no D110 row.
  • env() wrapped libc's environment bytes, so a saved value was rewritten by the next set_env (also on main). It returns an owned copy; every other make_str in rt was audited.
  • Tests that asserted a str move (45 compile-error fixtures, the move-audit's str cells, a bundle-interface fixture, a debug-alloc fixture) keep their rules on a Vec. The D22 phase fixtures pin the program's own adjustment count (--dump-typed prints contextual-copy-adjustments-outside-std), which is unchanged by D111. spec_ss13_2 now passes (Spec test spec_ss04_8_unit_elision fails: §4.8 unit elision unimplemented #723 noted).

Eric Hartford added 30 commits October 8, 2026 08:53
A Copy subject with drop glue (str, Option[str]) took the bit-copy path:
copied into a temp whose owner's drop was then cancelled (a regex match on a
str local leaked it, validate-ownership). Only bit-copy subjects skip
observation now. Over an observed subject a Copy binding (payload, whole
subject, slice element) reads by copy with its own hold instead of moving
the part out and blanking it (a later match on the subject read empty). The
observed let-else lowers its failing path before the bindings, as the
consuming path does (#1365): a str binding's drop reached the else path.
classify_cast_ownership met a Copy target with a copy (D22 §6.2). With str
Copy under D111, `n as str` for `n: &Name` became an owned temporary, and
`fn name_text(n: &Name) -> &str: n as str` (behav_1810) was refused as a
view of a temporary. Only a bit-copy target converts at the cast now; a str
stays the reference until an owned demand copies it.
A &str element passed to `s: str` double-freed: the contextual-copy
materialization minted a fresh buffer as a statement temp that a consuming
Copy argument never cancelled, and the auto-deref/contextual-copy argument
paths returned before the consume step, so a plain pointee copy took no
hold. The str materializes as every Copy value does (a pointee copy), the
early argument paths consume into an owning parameter (call_param_borrows,
one owner for the share-place decision), and a D87 implicit fill of a str
takes its hold. A generic parameter already bound to a Copy X (a
BTreeMap[str, V] receiver's K) meets a &X argument with a copy instead of
refusing the inference.
…one()

D22 §6.2 binds an unannotated let to the view; an owned demand copies a
Copy value. The diagnostic told an i32 or (D111) a str to write `.clone()`;
it now names the annotation that copies it.
…s copy per element

The behavior corpus found four classes on the D111 stack:
- MIR's element-view test for Vec/map iteration became "not plain bits",
  while Sema binds a view only for an element with drop glue. A plain
  struct (no Copy, no glue) bound by value in Sema and read as a view in
  MIR: a field read GEP'd into the pointer slot and LLVM's SROA crashed
  (behav_iter_view_to_fn, repr_packed_n, 2022, same_named_types). The
  four sites that were "needs drop and not Copy" are "needs drop".
- A contextual copy into a `&T` parameter took a hold: `k.clone()` on a
  map-key view leaked a count per call. call_param_owns names the owning
  parameters; `&T` and share-place ones borrow.
- `[s; N]` of a str took the one-evaluation fill (N slots, no holds,
  garbage past 64). §4.3a evaluates the value per element; only a bit
  copy keeps the single evaluation, and the validator's fill check covers
  glue types.
- Tools: --dump-mir shows a copy's hold (copy+retain, copy+take); the
  codegen-units path honours WITH_DUMP_LLIR_PRE (it ran SROA before any
  dump).
check_slice checked its bounds and recorded nothing: `s[at..at + 3]` with
`let at = cuts[0]` (a view, D27) reached codegen as the element's address
and failed LLVM verification (also on main). A bound is an owned demand
for its integer (D22 §6.2); a shared view of a Copy one is copied, as
check_range does.
`[s; 65]` with `s: &str` under a `[str; 65]` demand asked whether the
spelled value (a `&str`, plain bits) could be bit-copied, took the
one-evaluation fill, and put one str in every slot without holds; the
typed validator refused it. The array's element type decides.
@ehartford
ehartford added this pull request to stack #2278 October 8, 2026 22:07
@ehartford ehartford changed the title str values D111: a str is a value (counted buffers, holds, last use moves) Oct 8, 2026
Eric Hartford added 4 commits October 8, 2026 15:38
- A str field read through its binding (`let p = x.s; seen = p`) moved
  out of the field and blanked it; a Copy field is read by copy and its
  consumer takes the hold (behav_view_dead_across_loop_blocks).
- A comprehension's element is an owned demand (the collection stores it):
  a view of a Copy value is copied in. `[s for s in gen()]` collected the
  generator's yielded `&buf`, which the next resume overwrote (also on
  main; the explicit loop was already refused).
- The tail read of a D60 tail assignment moves the local out, as a plain
  tail does; it cancelled the local's drop and then read it with a hold
  (validate-all: a leak).
- The comptime mutable-global check ignored that a local shadows the
  global: a user's `var name` broke std.os's comptime `let name`.
- `sub in text` records the D110 signature row for the contains it lowers
  as (a BUG at run time).
… on a Vec

24 fixtures asserted "use of moved value", a D32 implicit field move, a D52
global move or a D95 discard for a str. D111 makes each of those str
spellings valid (a str is copied), so each fixture now spells its rule
with a Vec, the nearest non-Copy owner, and says why. The rules themselves
are unchanged; the str forms are pinned as valid by the D111 behavior
tests.
- move-audit: a str field read in any position copies, so its 24 str cells
  expect OK; the Vec cells keep D32.
- bundle-interface: the consuming closure captures a Vec (a str capture is
  copied, so that closure may run twice).
- da_str_double_free_reported: `*(&raw const s)` is now a copy with its own
  hold; the second holder is made by copying the {ptr, len} words as plain
  bits.
- dump_typed_d22_*: the adjustment counts are program-wide, and every
  `&str` meeting an owned demand in std is now a contextual Copy
  adjustment (41 -> 1599, 13 -> 1571).
- spec_ss13_2_iterator_borrowing passes (known-issue #723 directive
  removed; the issue's other three tests stay).
Eric Hartford added 3 commits October 8, 2026 15:57
Consuming closures, D60 tails, D73 assignment views, distinct casts, an
element field projection and a let-else reuse each asserted their rule on a
str, which D111 copies. Each now spells the rule with a Vec (or a distinct
Vec) and says why; the rules are unchanged. A Copy field bound from an
element copies, for a str as for an i32 (main accepts the i32 form).
The two D22 phase fixtures pinned the program-wide count, which moves with
every edit to the embedded stdlib (D111 made each std `&str` at an owned
demand an adjustment: 41 -> 1599; the next layer's std change moved it
again). `contextual-copy-adjustments-outside-std` counts those in the
program's own sources; the fixtures pin that. Their own counts (29, 1) are
what they were before D111 (41 and 13 less std's 12).
with_getenv_str wrapped getenv's pointer as a str. A later setenv rewrote
those bytes in place, so a saved value changed under its holder (on main
too: `let saved = env(X); set_env(X, ...)` read the new bytes). Under D111
every copy shares the buffer, and the build runner's save-and-restore of
WITH_OUT_DIR restored garbage. The value is copied into an owned buffer.

Audited every make_str in rt_core: the others wrap a static literal, a
fresh allocation, the format buffer handed over, a uniquely held buffer
appended in place, or argv (process-lifetime, never rewritten). The
allocator reads its settings through rt_getenv, never this function, so
the allocation here cannot re-enter it.
@ehartford

Copy link
Copy Markdown
Contributor Author

Battery GREEN on the stack top 42c8b5a (seed-driven, with run tools/battery.w): gate rc=0 761 s, uat rc=0 30 s, battery-checks rc=0 858 s (fixpoint, drop and move audits, every test target). Logs: out/battery/ in the str-sweep worktree.

@ehartford
ehartford marked this pull request as ready for review October 8, 2026 23:37
@ehartford
ehartford merged commit 9a8b978 into main Oct 8, 2026
@ehartford
ehartford deleted the str-values branch October 8, 2026 23:38
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