0038. str, bytes and slices share one representation
- Status: Accepted
- Date: 2026-10-30
- Plan: M5.3, M5.4
- Review: Memory and representation
Context
Text tools need UTF-8 strings, arbitrary bytes and cheap substrings with immutable semantics. Views must obey ADR-0037's region lifetime invariant.
Decision
str is always valid UTF-8; bytes is
distinct raw storage. Both use {data,len,cap}; slices
retain the source type and set cap to zero. Lengths and half-open
slice bounds are byte counts; bounds are i64. Omitted bounds mean
zero and length. Out-of-range or reversed slices trap
slice out of bounds; str endpoints inside a scalar
trap invalid char boundary. Check bounds first.
s[i] is rejected, since there is no char type. UTF-8
validation is explicit when converting bytes to
option<str>. Slicing bytes permits invalid
UTF-8.
The IR Slice aliases its parent. Same-block placement propagates its home to the parent; conditional escaping flows promote the visible bytes, including through records, options, calls and returns. Promotion restores append-capable storage. A view cannot append in place even when ending at its parent's tip. Appending the parent only writes beyond its old length, preserving every view and snapshot. Both backends model the dynamic used-length check.
Builtin sequence methods take precedence over interface
methods; qualified interface calls remain available. Search
offsets are bytes; find and parse return options. Discarded trim
results use E0711, teaching assignment rather than mutation. With
u8 supplied by ADR-0044, s.byte(i) and bytes indexing
b[i] return u8. Indices are exactly i64 and count raw
bytes, including UTF-8 continuations. Negative indices and indices
at or past length trap index out of bounds at the
receiver/base expression, even for discarded results. Lower
byte through a bytes view and the same checked IR
Index as bytes and vectors; receiver and index are evaluated once,
left to right. E0718 diagnoses slice bounds without using
ADR-0044's reserved integer diagnostic range.
Alternatives considered
- Separate slice type — adds conversions without changing immutable semantics.
- Character indexing — ambiguous scalar/grapheme meaning and nonconstant cost.
- Two-word handles with a tip side table — makes the runtime state indirect.
Consequences
Views are O(1) until promotion; no lifetime syntax is exposed. UTF-8 validity survives string slicing/concatenation. Poison/sanitizer tests guard arena escape paths; runtime counters guard linear accumulation. M5.3 includes minimal vector storage, indexing, slicing, equality and deep promotion for chars/split/lines results. M5.4 adds vector constructors, builders, general methods and loops.