Repository navigation
for over a slice or array observes: no second owner, no consumed array (#1197) - #1207
Merged
Merged
Conversation
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
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]>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1197, and a memory-safety bug found while fixing it.
#1197:
for x in xsover a&[T]parameter failed MIR lowering (Sema accepted it). No branch oflower_fortook a reference to a slice or array.What the scribbling debug allocator then showed, on the installed compiler:
lower_for_slicecopied 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; onlyWITH_DEBUG_ALLOC_SCRIBBLE=1shows it.Fix.
&Tviews, directly or through a reference, as Vec does. Copy elements still bind by value.&[T]/&[N]Thave an element type.RK_REFof 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_LENreads 0 through a field or deref projection, sofor n in holder.namesnever ran once the array stopped being moved.Tests:
behav_for_over_slice_parameter.w;behav_sequence_traversal_observes.wrunslib/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.