Skip to content

Vec[str].iter() element view passes garbage to &str parameters (binding holds pointer, call site marshals by-value pair) #925

Closed

Description

@ehartford

Found while writing a stock rewrite script during #921. Silently wrong data, no crash — high severity on the brand-new D33 iteration surface.

Repro (stage2, current main)

fn peek(s: &str) -> i64: s.len()
fn main:
    var paths: Vec[str] = Vec.new()
    paths.push("hello")
    for p in paths.iter():
        print(f"{peek(p)}")     // prints 4366090400, expected 5
  • print(p) / f"{p}" / p ++ "x" render the view CORRECTLY — only passing p onward to a &str parameter corrupts.
  • Downstream symptoms observed first: read_file(p) panics "str to C string conversion: interior NUL byte"; p.to_owned() dies SIGTRAP.
  • Vec[i32].iter() views pass fine — the bug needs a fat element type.

Evidence

--dump-abi:

fn peek [sig=192 params=1]
  param[0] ty=17 eff=[read] value_ref_abi=0 -> COPY

The &str parameter is marshalled COPY — the by-value {ptr,len} view pair. But the for binding p (from VecIter[str].next() -> Option[&str]) holds a pointer to the element; the call site copies 16 bytes from the binding storage without dereferencing, so the callee's len lane is stack garbage (reads as a pointer-sized address). Binding representation and call-site marshalling diverge — per D6 this must be settled in ONE place (compute_fn_abi/PassMode), not per-path: either the binding materializes the pair or the call site derefs the element pointer; today each assumes the other.

Spec

D22/D27: a &T stays a reference through inference and pattern projection; the element view from iter() is the canonical D33 borrowing surface. §3.8 signature-authoritative ownership: peek(x) auto-references. Nothing here licenses a representation split between binding and call.

Impact

Every for x in vec_of_str.iter(): f(x) where f takes &str consumes garbage. The D33 campaign's own fixtures (behav_consuming_iteration, behav_for_generic_iterator) pass because they consume/print in-loop rather than passing the view to a user fn — an acceptance-matrix hole to close with the fix (add a pass-view-to-fn fixture over str and another fat element type).

Activity

  1. ehartford commented on Sep 2, 2026

    @ehartford
    ContributorAuthor

    Fixed in c636b8d. Root cause was a Sema/MIR divergence on the loop binding's type, not marshalling: MirLower lowers for x in vec.iter() as §13's implicit form (Drop-class elements bind &T views through VEC_GET_REF; VecIter.next() is never called), while Sema typed the binding from the generic Iter protocol — VecIter[T].next() -> Option[T] — so p was str where MIR held a &str. can_auto_ref_arg_frozen(&str, str) then said auto-reference, MIR emitted _10 = ref(shared, _8) (a pointer to the binding), and the callee read that pointer plus stack garbage as the {ptr,len} pair. Fat element types only, which is why a plain struct (by pointer) and a Copy scalar were fine.

    for_loop_element_type now mirrors MirLower's dispatch predicate at check_for and both comprehension clause sites: an iter call on a Vec-typed receiver takes the bare-Vec split; everything else (HashMap.iter(), user iterators, &Vec receivers) keeps next()'s element. Verified with --dump-mir / --explain-mir-origin / --dump-abi before and after.

    Acceptance fixture behav_iter_view_to_fn: .iter(), bare for, .get, and [i] views of str, Vec[i32], a struct, and i32 all reach a &T parameter intact. Isolated battery: build, fixpoint, move-audit (15 cells, 0 diff), drop-audit (115 cells, 0 regressions), test — all green; reseeded.

    Two neighbours the matrix exposed are filed separately: #934 (comprehensions over Drop-class Vecs copy the element via VEC_GET into a &T local and move the receiver) and #933 (.Some(v.get(0)) through a &Vec[T] parameter traps the compiler).

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