Skip to content

&str is its own {ptr,len} view (ABI v8): returned string slices, stage links its own generation's runtime (#1810 #1587 #1815) - #1869

Merged
ehartford merged 10 commits into
fix-globalsfrom
fix-strview
Sep 29, 2026
Merged

ehartford merged 10 commits into
fix-globalsfrom
fix-strview

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Stack created with GitHub Stacks CLI • Give Feedback 💬

@ehartford
ehartford added this pull request to stack #1871 September 28, 2026 19:22
@ehartford ehartford changed the title fix strview &str is its own {ptr,len} view (ABI v8): returned string slices, stage links its own generation's runtime (#1810 #1587 #1815) Sep 28, 2026
@ehartford

Copy link
Copy Markdown
Contributor Author

Eric blessed (2026-09-29) the two spec texts on this layer as written: the docs/spec/abi/with-abi.md v8 entry (&str is a {ptr, len} view value; Option[&str] a tagged enum; v7 objects do not link with v8) and the docs/spec/toolchain/wo_bundles.md sentence (the embedded object is the runtime stage1 compiled, #1815). The two commits titled PROPOSED are the ruling itself.

Eric Hartford added 10 commits September 29, 2026 14:40
…piler generation compiled, and a link checks the build's say-so instead of trusting it (D30, #761 class)

stage2's code is stage1's codegen, but stage1 linked it against the
seed-built out/bootstrap-lib (prepare-bootstrap-link-root empties out/lib
and out/lib is compiled by stage2), and stage-embedded-objects gave stage2
and stage3 the same seed-built blobs. It never failed only because no
runtime seam changed representation since the pinned seed; the &str view
ABI (#1810) changes every `with_*`/`wl_*` &str parameter. The D30 check
could not see it: WITH_OUT_DIR vouched for any root the build pointed at.

- A generation is the tree a compiler is built from: sha256 over its src/
  and rt/ sources (build/compiler.w compiler_generation, one owner; the
  .wo keys reuse the same compiler_tree_sha/compiler_sha256_text, moved out
  of build/wo.w). Every stamped compiler carries its generation and the
  generation of the runtime it embeds in two post-link slots beside the ABI
  stamp (compiler.AbiStamp; `with version --generation`,
  `--runtime-generation`).
- Every runtime/bridge object set records its producer's generation in
  <dir>/.producer (`version --generation`, else `foreign:<sha256>` — the
  pinned seed predates the flag): out/bootstrap-lib (seed), NEW
  out/bootstrap/lib (stage1: runtime, bridges, link metadata), out/lib
  (stage2).
- stage2 links out/bootstrap/lib and stage2/stage3 embed it; stage3 and the
  release link out/lib. Each stage link names its root (runtime-root= ->
  WITH_RUNTIME_ROOT) and its embedded set's producer (runtime-producer=).
  `:dev` builds out/bootstrap/lib.
- Link.w: WITH_RUNTIME_ROOT is the only root and must be this generation,
  else a loud error and no fallback. WITH_OUT_DIR no longer vouches: a
  candidate is taken only when its .producer is this generation (or, with
  none recorded, byte-identical to an embedded runtime of this
  generation); the embedded runtime is taken only when it is this
  generation's, else "run `with build :dev`". stage1 run by hand links
  through out/bootstrap/lib (<compiler_dir>/../lib). An unstamped compiler
  keeps the old rule.
- runtime-generation-regression (part of :test): the release is one
  generation with out/lib and its embedded runtime, links through out/lib
  by WITH_RUNTIME_ROOT, and refuses a root of another generation with the
  message and no output.

Evidence (this worktree, seed cffa4a40):
- .producer: out/bootstrap-lib foreign:cffa4a40…, out/bootstrap/lib and
  out/lib e4b881d8…; stage1 gen e4b881d8 rtgen foreign:cffa4a40; stage2,
  stage3 and release gen = rtgen = e4b881d8.
- out/command/stage2/worker.effects: stage1 linked stage2 with
  WITH_RUNTIME_ROOT = sha256(<root>/out/bootstrap/lib) c593c2ed…;
  link-compiler and stage3 with sha256(<root>/out/lib) 20c31de1….
- The objdump marker of #1815 (rt_f64_fixed_digits' frame) cannot tell the
  sets apart here: this seed compiles rt_core.o byte-identical to stage1
  (cmp out/bootstrap-lib/rt_core.o out/bootstrap/lib/rt_core.o), so the
  roots are shown by the recorded env and by the refusals below.
- stage1 by hand: links test/hello.w (prints hello); with out/bootstrap/lib
  moved away: "error: no runtime objects of this compiler's generation
  (e4b881d8…) to link: its embedded runtime is generation
  foreign:cffa4a40… … run `with build :dev` …", no binary;
  WITH_RUNTIME_ROOT=<root>/out/bootstrap-lib: "… holds runtime objects of
  compiler generation foreign:cffa4a40…, but this compiler is generation
  e4b881d8…; linking them would mix compiler generations (D30, #1815)",
  no binary.
- release by hand: hello in the checkout and outside it (embedded);
  WITH_RUNTIME_ROOT=<root>/out/bootstrap-lib refused as above.
- src/main build: green; src/main build :fixpoint: stage2 == stage3 over
  all 16 units; :stage3, :runtime-generation-regression,
  :embedded-runtime-regression, :deep-debug-tool-tests --no-deps: green.

`version --generation` and `--runtime-generation` are build plumbing,
internal flags in the spec inventory like `--link-bundle`. The
wo_bundles.md sentence this changes is proposed in the next commit.

Fixes #1815
…'s embedded runtime is the one stage1 compiled (#1815)

docs/spec/toolchain/wo_bundles.md said the stage-embedded object "reuses
the bootstrap runtime inputs" — the seed-built set. Since the previous
commit it embeds out/bootstrap/lib, the runtime stage1 compiled, because
stage2's code is stage1's codegen. Kept in its own commit so the exact
words can go to Eric before they land.
…ch means the same under either `&str` representation

`unsafe **(&s as *const *const *const u8)` reached a `&str`'s data pointer
by dereferencing the reference's own slot twice: right only while a `&str`
is a pointer to a str header. #1810 makes a `&str` its own `{ptr, len}`,
and on the way through the stage chain the runtime and the LLVM/Clang
bridges are compiled by both representations — by the pinned seed for
stage1 (pointer) and by stage1 for stage2 and its programs (view) — so
every such read must mean one thing under both. `*(s as *const str as
*const *const u8)` does: a `&str` cast to `*const str` names a str header
either way (the seed passes the reference through, #747; a view is its
own header — its storage, or a copy in the frame).

56 sites: rt_core (str_data, with_str_eq_ref/cmp_ref, with_parse_i64_ref,
with_lines_out_ref), panic_runtime.str_data and each platform's
rt_str_data/posix_str_to_c_buf/win_str_data/rt_net_str_data/wasm_str_data,
LlvmBridge (to_cstr, path_to_cstr and 18 wl_* string parameters),
ClangBridge (10), ClangDriver.cc_c_string, both intern pools, std.regex,
std.internal.str_abi, std.collections.trie (the same idiom through
`*const *mut u8`), and Sema.sema_str_has_data (which read the text's first
eight bytes as a pointer under a view).

Evidence: the seed compiles these sources into out/bootstrap-lib (pointer
`&str`) and stage1 links and runs against them (`src/main build :dev`);
stage1 compiles them into out/bootstrap/lib (view `&str`) and the #1810
matrix programs linked through it pass. `grep` for the idiom in rt/ src/
lib/ tools/ build/: none left.
…view value with str's own `{ptr, len}` layout, passed like a slice

docs/spec/abi/with-abi.md describes the ABI the compiler implements; this
is the wording for the #1810 change that follows, kept in its own commit
so the exact words can go to Eric before they land.

- §1: a shared `&str` (or `&T` of a distinct type over str) is a view
  value `{ ptr: *const u8, len: i64 }`, not the address of a str header,
  so a range view `s[a..b]` exists without a header (spec §4.8a); `&mut
  str` stays a pointer. The fact's one owner is
  `TypeLayout.type_layout_is_str_view`.
- §3: the layout row (16 bytes, align 8); `Option[&str]` is an ordinary
  tagged enum, not the D22 nullable pointer (its payload is not an
  address).
- §4: `&str` views pass Fat, `{ptr, len}` by value, indirect under the
  existing Windows x86_64 rule, like `[]T`.
- Header and status line: version 7 (they still said v4 after v5 and v6
  were recorded); v7 history entry.

The spec already rules the language question (§4.8a: a str range is a
`&str` view of its bytes, with the origin of any view); this commit only
describes its ABI consequence.
…len}` view, passed by value like a slice, so a str range needs no header (D71 §4.8a, #1810)

Spec §4.8a makes `s[a..b]` a `&str` view of its bytes, with the origin
of any view. While `&str` was the address of a str header, a range view
had no header to point at: #1587 built one in a hidden frame local and
Sema refused every return of it. A `&str` is now the view itself.

One owner: `Sema.type_layout_is_str_view` (TypeLayout.w) says a shared
`&str` (or `&` of a distinct type over str) has str's layout, 16 bytes,
align 8; `&mut str` stays a pointer. Everything else reads it:

- FnAbi: a view classifies through its LLVM type (the str struct), so
  compute_fn_abi gives it PM_DIRECT by value (PM_INDIRECT under the
  windows-x86_64 rule), for With functions and every runtime `_ref` seam
  alike; `sig_param_is_explicit_ref` no longer calls it a reference.
  WITH_ABI_VERSION 6 -> 7; docs/with-abi.sha256 re-recorded.
- LLVM codegen: `*view` is the view's own storage (no load); a shared
  borrow of a str place loads the header's bits; `*const str -> &str`
  loads, `&str -> *const T` names the view's storage. Every hand-built
  runtime declaration (str methods, fmt, concat, join, panic, cstr lend)
  now goes through `call_internal_runtime_fn`/the str type, so caller and
  callee read one FnAbi. `Option[&str]` is a tagged enum: slot lookups
  (Map.get, SlotMap.get, Vec element and iterator views, map traversal)
  read the view out of the slot (`mir_ref_from_slot_ptr`,
  `option_ref_from_slot_ptr`).
- MirLower: a str range is the view itself, like `[]T`; the hidden header
  local is gone.
- C backend: `&str` is `with_str` by value; `WITH_STR_REF` and every
  `const with_str *` for a `&str` parameter in runtime/with_runtime.h
  and build/package.w are gone; a str or view lent to `const char *`
  goes through `with_cstr_lend`/`with_cstr_release` as the LLVM backend
  lends it (D47; a range view is not NUL-terminated at its end), keyed
  on Sema's `sema_type_is_c_char_pointer` (Codegen's copy of that
  predicate is gone too).

Evidence (compiler facts, `with analyze … matrix:`; the `&str` rows of
the matrix programs, before -> after):
  m1 params:     params pointer 84 -> 0, value 0 -> 84; args
                 existing-pointer -> direct-value, all
  m2 consumers:  params 84 ptr -> 84 value; args 107 existing-pointer +
                 100 direct-value -> 207 direct-value
  m3 Option:     84 -> 84 value; 105 + 97 -> 202 direct-value
  m6 c_import:   83 -> 83 value; 104 + 88 -> 192 direct-value
  m9 join/fmt:   83 -> 83 value; 103 + 91 -> 194 direct-value
Behavior (build + run, before -> after; the programs that do not return
a range view): params, consumers (compare, concat, match, hash, fmt,
Display/Debug), Option lookups, places (iteration, closures, `&&str`,
raw pointers), c_import lending: pass -> pass; `[&str; N]` element
concat: codegen "wrong argument type actual=str expected=ptr" -> pass;
`join(&str sep)` over a Vec: SIGBUS -> pass; `&dyn Display` over a str:
the same phase BUG before and after (#1818, filed). New tests:
behav_1810_str_view_{params,consumers,lookups,c_lend}.w.

`src/main build` green; `src/main build :fixpoint`: stage2 == stage3
over all 16 units. `:emit-c-smoke` green (every test/emit_c fixture, run
by both backends). `:deep-debug-tool-tests --no-deps` green;
`:drop-audit` 213 cells, 0 non-PASS, 0 regressions; `:move-audit` 105
cells, 0 FAIL, 0 DIFF; `with analyze … audit:all` violations=0 on every
matrix program and new test except the `mut fn` str receiver false
positive (#1822, the same on the baseline; debug-alloc leak count 0).
…: &str) -> &str: s[1..]` returns a view of the caller's string (D71 §4.8a, §21.1 Rule 6)

#1587 gave every str range view a second, frame-local origin
(`syms.str_range_view`) because its header lived in a hidden local of
the frame, and refused every return of one by name. Since the previous
commit a `&str` is its own `{ptr, len}`: there is no header, so the
sentinel described nothing. Check_slice now records only the base's
origins, exactly as for `[]T`; a returned range view is checked by the
same Rule 6 as every other returned view.

Tests:
- err_1587_str_range_returned.w becomes
  behav_1810_str_range_returned.w: `tail`, `tail_bound`, a range view
  through an inner function, in `Option`, and in a tuple, all read the
  caller's bytes. err_1587_…_via_binding.w is covered there and removed.
- behav_1810_str_view_aggregates.w: range views in tuples, structs with
  a view field, and arrays, returned and printed with Debug.
- test/emit_c/str_view_representation.w: the same in the C backend
  (range return, runtime seams, `const char *` lending, map lookups).
- The origin rules still refuse what they must:
  err_1810_returned_view_of_consumed_param.w (a view of a consumed
  `str` parameter: "returned view may outlive its origin 's'") and
  err_1810_returned_view_invalidated.w (mutating a string under a live
  range view).
- err_1810_global_view_across_mutating_callee.w: a view of a global str
  live across a callee that reassigns the global. §9.1c and §21.1 refuse
  it; the checker accepts it until #1819, which lands in the layer below
  this one, and under a `{ptr, len}` view the accepted program reads
  freed bytes. This test is red on this layer alone and green on the
  stack; it proves the order.

Matrix (build + run, before this stack -> after): returned range view
refused (#1587) -> "bc"; range views in a returned tuple / struct Debug
refused -> pass; #1819 cell: accepted before and after this layer
(refusal expected from #1819).

Fixes #1810
Fixes #1587
…finition convention that merged first (#1844)

Both changes bumped WITH_ABI_VERSION to 7 in parallel; the variadic layer
landed on main as v7, so the `&str` {ptr, len} view (its with-abi.md entry
already renumbered v8 in the rebase) takes 8. docs/with-abi.sha256 is
re-recorded on the stack top.
…ir — #1840's SDK probe (merged after this layer was written) writes there
…(§4.5, D75)

Under D75 a cast through a reference borrows, so `n as str` with
`n: &Name` is a `&str` view; returning it from `-> str` is refused
(d95cd0c). The fixture predates D75; it now returns `&str`, keeping
what it demonstrates (a view parameter's bytes reach the caller).
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