Skip to content

for over a slice or array observes: no second owner, no consumed array (#1197) - #1207

Merged
ehartford merged 1 commit into
mainfrom
fix-1197-for-over-slice-param
Sep 19, 2026
Merged

ehartford merged 1 commit into
mainfrom
fix-1197-for-over-slice-param

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1197, and a memory-safety bug found while fixing it.

#1197: for x in xs over a &[T] parameter failed MIR lowering (Sema accepted it). No branch of lower_for took a reference to a slice or array.

What the scribbling debug allocator then showed, on the installed compiler:

fn join(words: []str) -> str: ... for w in words: ...
print(join(words[..]))      // one!,two?,
print(join(words[..]))      // one!,<freed memory>,

lower_for_slice copied each element into a temp, and the temp's drop freed the sequence's own string. A local or struct-field array was worse: the iterable was lowered as a value, which moved the array into the loop — the second traversal saw empty strings, with no use-after-move diagnostic. D44 / §2.3 ("traversal observes") was applied to Vec and HashMap; slices and arrays were left behind. A plain run passes; only WITH_DEBUG_ALLOC_SCRIBBLE=1 shows it.

Fix.

  • Sema: a slice or array of Drop-class elements binds &T views, directly or through a reference, as Vec does. Copy elements still bind by value. &[T] / &[N]T have an element type.
  • Lowering: a place iterable is read through its own place; only an rvalue materializes a temp. A Drop-class element is borrowed (RK_REF of its place). A slice behind a reference is a Copy view and is read out once; an array behind a reference is read in place. An array's length comes from its type — RK_LEN reads 0 through a field or deref projection, so for n in holder.names never ran once the array stopped being moved.

Tests: behav_for_over_slice_parameter.w; behav_sequence_traversal_observes.w runs lib/sequence_traversal_cases.w (every case traverses twice: slice, &[T], &[N]T, local array, field array, Copy elements, empty) under the debug allocator, scribbled, with and without block reuse, zero leaks. The compiler's source checks clean under the new compiler.

Ownership change: alone in its batch. Battery (with :move-audit) is queued behind the two ahead of it; not run yet. I will update this line.

Found while fixing #1197 (`for x in xs` over a `&[T]` parameter failed MIR
lowering). Under the scribbling debug allocator, on the installed compiler:

    fn join(words: []str) -> str: ... for w in words: ...
    print(join(words[..]))      // one!,two?,
    print(join(words[..]))      // one!,<freed memory>,

lower_for_slice copied each element into a temp, and the temp's drop freed
the sequence's own string. A local or field array was worse: the iterable
was lowered as a value, which MOVED the array into the loop; the second
traversal saw empty strings, and Sema never called it a use after move.
D44 / §2.3 ("traversal observes") was applied to Vec and HashMap; slices and
arrays were left behind.

- Sema: a slice or array of Drop-class elements binds `&T` views, directly
  or through a reference, as Vec does; Copy elements still bind by value.
  `&[T]` and `&[N]T` have an element type (they had none).
- Lowering: a place iterable is read through its own place; only an rvalue
  materializes a temp. A Drop-class element is borrowed (RK_REF of its
  place), not copied. A slice behind a reference is a Copy view and is read
  out once; an array behind a reference is read in place. An array's length
  comes from its type: RK_LEN reads 0 through a field or deref projection,
  so `for n in holder.names` never ran once the array stopped being moved.

The cases traverse everything twice under the debug allocator, scribbled,
with and without block reuse, zero leaks. The compiler's source checks clean
under the new compiler.

Fixes #1197
@ehartford
ehartford merged commit f5cba02 into main Sep 19, 2026
1 of 5 checks passed
ehartford added a commit that referenced this pull request Sep 19, 2026
… D45, D46 (#1212)

* Main is red after #1205 and #1207: two behavior tests

Both merged before their batteries ran. A battery on main (f5cba02) fails
behav_tls_module_compiles.w and behav_error_handling.w.

#1205: the "return type of 'X' was not known here" report fired on
`i31_reduce_once(x, m, mlen)` in std/crypto/bigint.w. That call is a
statement: its value is discarded, so the placeholder type cost it nothing.
A block now marks the statement it is checking, and a call in that position
is not recorded. A call that is a block's tail still is — under D43 the tail
is the function's type.

#1207: `for s in inputs` over an array of str now binds `&str` (traversal
observes), and the test passed `s` to `parse_positive(s: str)`, which only
reads it. The function takes `&str`, as an observer should (§3.8).

* Hoist only the callee; traverse sequences the same way in comprehensions

The battery on the first fix found two more tests red on main.

err_thread_spawn_closure_captures_ref.w (#1205): checking every unannotated
function first put `bad` ahead of std.thread.spawn_os, whose body gives its
closure parameter the escape effect `bad`'s call is judged by, so a weaker
diagnostic fired. Bodies are checked in declaration order again; a function
that takes its type from its body is checked before the first declaration
that calls it, and nothing else moves. The calls come from the AST: nodes are
appended children first, so a declaration's subtree is the ids between it and
the nearest declaration node below it. A chain of unannotated functions now
types (it was reported before); a cycle gets D43's "cannot infer return
type ... add `-> T`".

spec_ss13_6_comprehensions.w (#1207): lower_comprehension_slice was a copy of
lower_for_slice's setup with the same defects — it moved the iterable, took
RK_LEN through projections and copied Drop-class elements — while Sema now
binds views. The receiver, length and element binding are shared helpers,
used by both. A map comprehension stores what it builds: a `&T` key or value
with T: Copy materializes a T (D22); a view of a Drop-class value is an error
that says to clone it, where it said "`&str` must implement Hash". The test's
str keys are cloned; on the pinned seed the same comprehension over a
Vec[(str, i32)] already failed, and the array form only passed by making the
map a second owner of the array's strings.

* The archiver is the compiler invoking itself; record D45

spec-inventory-check on main: "implementation has unspec'd ar / cc /
ranlib / --from-source" (from #1208, which merged without a battery). `ar`
and `ranlib` are what a CMake build asks of an archiver, not commands a user
types: they are `with __ar` and `with __ranlib`, the prefix the inventory
already treats as the compiler invoking itself. `with cc` and
`with get --from-source` are user surface and wait for spec words.

D45 (Eric, 2026-09-19): a copy is never implicit unless it is O(1). The
comprehension fix in this branch follows it; #1211 tracks the measurement
that could reopen it.

* Spec: `with cc`, source builds, and cloned comprehension elements (blessed)

Eric blessed this wording on 2026-09-19.

§18.5 lists `with cc`. §18.8 lists `with get --from-source c.X` and says how
`with get` builds a package from source when Conan Center has no linkable
binary: the recipe read as data, the package's own CMake build under
`with cc`, installed and locked like a binary package, and a missing tool
named rather than worked around. §13.6's examples that build owning
collections of `str` from views clone their elements, and the section says
why (D22, D45).

The inventory checker lists `--from-source` beside `--force-reinstall`: a
`with get` flag, documented in §18.8. D46 records the source-build
decisions and what was rejected.

---------

Co-authored-by: Eric Hartford <[email protected]>
@ehartford
ehartford deleted the fix-1197-for-over-slice-param branch September 24, 2026 16:54
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.

for x in xs over a &[T] parameter fails MIR lowering

1 participant