Skip to content

Comprehension over a Drop-class Vec: element copied by VEC_GET into a &T-typed local (garbage to &str params) and the receiver is moved out #934

Closed

Description

@ehartford

Sibling of #925 (the for form is fixed there). Pre-existing on the current seed.

Repro

fn peek_str(s: &str) -> i64: s.len()
fn main:
    var ss: Vec[str] = Vec.new()
    ss.push("hello")
    let a: Vec[i64] = [peek_str(s) for s in ss]          // first = 0
    let b: Vec[i64] = [peek_str(s) for s in ss.iter()]   // first = 4331063280

Expected 5 and 5.

Evidence (--dump-mir, bare form)

_6 = move _1;                                   <- the source Vec is MOVED into a temp; `ss` is blank afterwards
call const fn sym187(copy _6, copy _8) -> [return: _10, next: bb9];   <- VEC_GET (copy) into _10: ty17 (&str)
_11 = copy _10;
call const fn sym659(copy _11) -> ...           <- peek_str(copy s)

lower_comprehension_vec (src/MirLower.w) types the element with infer_for_element_type_frozen (Drop-class → &T, same split as for) but fills it with emit_vec_get_into = the VEC_GET copy intrinsic, and materializes the receiver with materialize_operand (a move). lower_for_iter_ref already does this right for the same case: borrow the receiver place (no header copy — the #607 double-free note) and read elements with VEC_GET_REF into the &T local. The comprehension path should share that lowering rather than re-derive it.

Sema side: after #925, for_loop_element_type also types comprehension bindings over vec.iter() as &T for Drop-class elements, so the types agree; the remaining defect is the MIR read/receiver handling above.

Fixture to add with the fix: comprehension × (bare, .iter()) × (str, Vec[i32], struct, i32) → &T parameter, plus ss still usable after the comprehension.

Activity

  1. ehartford commented on Sep 2, 2026

    @ehartford
    ContributorAuthor

    Stronger evidence: the receiver move blanks the source for every element class, not just Drop-class ones.

    let v: Vec[i32] = [1, 2, 3]
    let w: Vec[i32] = [x * 2 for x in v]
    print(f"{v.len()} {w.len()}")   // 0 3 — expected 3 3
    

    and for Vec[str] the source is blanked and the element read is garbage (7955890216726691840), then a later comprehension panics with "Vec index out of bounds" on the now-empty source.

    Fix in progress: lower_comprehension_vec reads a place receiver through its place (as lower_for_vec does — no header move) and uses VEC_GET_REF for &T element types (as lower_for_iter_ref does). Fixture behav_comprehension_view_to_fn covers bare/.iter() × str/Vec/struct/i32 → &T parameter plus source-still-usable for Copy- and Drop-class sources.

  2. ehartford commented on Sep 2, 2026

    @ehartford
    ContributorAuthor

    Fixed in 891fd01. lower_comprehension_vec now reads a place receiver (ident, field, index) through its own place — no header move, exactly as lower_for_vec — and reads Drop-class elements with VEC_GET_REF into the &T local, exactly as lower_for_iter_ref; Copy-class elements keep VEC_GET by value. Only rvalue receivers still materialize an owning temp. MIR for the bare str case: zero moves of the source Vec, one VEC_GET_REF read.

    Results: [x * 2 for x in v] leaves v intact; [peek(s) for s in strs] and the .iter() form pass the view intact; the follow-on out-of-bounds panic is gone.

    Fixture behav_comprehension_view_to_fn: bare and .iter() comprehensions over str, Vec[i32], a struct, and i32 reach a &T parameter, and Copy- and Drop-class sources remain usable afterwards. Isolated battery: build, fixpoint, move-audit (15 cells, 0 diff), drop-audit (115 cells, 0 regressions), test — all green; reseeded.

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