Skip to content

names.iter() |> filter(...) double-frees every kept element of a Vec[str] #2145

Closed

Description

@ehartford

Spec: §2.3 (transport is not duplication: no second live value from one unless the type is Copy), D44 (traversal observes).

Repro (installed v0.15.3.0, main a45324c):

fn a: "A".to_lower()

fn main:
    let names: Vec[str] = [a(), a()]
    print(names.iter() |> filter(it.len() > 0) |> count())

with run --debug-alloc:

debug-alloc: DOUBLE FREE addr=… size=16 origin=unknown first_drop=drop#struct __drop_struct_16 second_drop=drop#struct __drop_struct_214

Same with |> filter(...) |> map(it.len()) |> sum() and |> filter(...) |> collect[Vec](). names.iter() |> count() (no filter) is clean. Any element type that owns something is affected (an enum with a str payload too).

Reading so far (to be confirmed at the line): the filter stage builds its result vector by storing each kept element's bytes and pushing them (with_vec_push in the filter intrinsic of src/CodegenDispatch.w), and that vector is then dropped as if it owned the elements, while names still owns them.

Found while testing #2137; not caused by it (the installed compiler reproduces it).

Activity

  1. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Located. lib/std/collections.w: pub type VecIter[T] ephemeral { data_ptr, len, idx } with impl[T] Iter[T] for VecIter[T]: mut fn next() -> Option[T], so names.iter() hands each element out by value, and the stage type is FilterIter[VecIter[str], str] with pred: fn(T) -> bool. The predicate receives a byte copy of the element and drops it; names drops it again. names.iter() |> count() is clean only because nothing binds the copies.

    The spec already rules it: §9.7's example says .iter() yields &(str, i32), and the stdlib inventory says of VecIter[T] "iteration must preserve element views" (D27/D44: traversal observes). The implementation is non-compliant: Vec.iter() must yield &T, and the adapters carry that view. That is the fix; its reach is every .iter() pipeline, so it gets its own change after #2137.

  2. added 2 commits that reference this issue on Oct 5, 2026
  3. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Status (2026-10-05): the swap itself works and is on the draft #2150 (v.iter() over non-Copy elements is the view iterator, except as a loop's own iterable and where the by-value type is expected; every double-free form is clean under the allocator). It was taken out of the landed stack because six existing tests depend on collecting owned collections straight from .iter() over non-Copy elements, and the conforming spellings hit gaps:

    1. xs.into_iter() |> collect[HashMap[K, V]]() / collect[BTreeMap] / collect[BTreeSet]: "collect[] over a VecIntoIter[…] is not available: an Iter[T] implementor collects into a Vec".
    2. xs.iter() |> map(it.clone()) |> collect[…] over Vec[(str, i32)]: "unknown method 'clone' for type '&(str, i32)'" (a tuple of Clone elements has no clone).
    3. iter_ref().next() on a temporary skipped §15.3's place-receiver rule (fixed on the branch).
    4. for x in v.iter_ref() and a comprehension over it fail in MIR lowering on main (see for cannot iterate an iterator adapter: §13.5's for (i, item) in collection.enumerate() does not compile #2152).

    Tests to move once 1 and 2 exist: spec_ss13_3_collect_targets (two lines), spec_ss13_3_collection_operations, err_collect_btree_unimplemented, err_iter_next_rvalue_receiver. Order of work: tuple clone, collect targets from a consuming iterator, then this.

  4. ehartford commented on Oct 5, 2026

    @ehartford
    ContributorAuthor

    Fixed on main baa84b2 (#2150); battery green on the stack.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions