Skip to content

Vec index/get element copies of Drop elements are ungated (double free); #607 gate covers for/match only #715

Closed

Description

@ehartford

1e9e587 gated consuming for-loops and match-destructures of Vec[Drop]
(#607). The index-binding spelling was never gated:

let entry = state.entries[i]   // Entry owns a Vec — element handle copied

Post-#691 this double-frees: the binding's drop and the vec's element drop
both free the element's buffers (issue61_query_state_stress reproduced it:
DOUBLE FREE first_drop=__drop_struct_106 second_drop=__drop_struct_107).
Vec.get(i) -> T by copy is the same class. This is the D23 surface (D22 §2.2
explicitly defers Vec/slice/array/iterator element ownership).

Until D23 lands, the sound spelling is let entry = &state.entries[i] (the
issue61 lib now uses it). A minimal gate matching 1e9e587's rule (non-Copy
needs-drop element, index/get copy into a binding) would close the silent
unsoundness; note it will also flag Vec.get copies inside the compiler and
stdlib, so it needs its own sweep.

Residual: after the borrow fix, issue61_query_state_stress still leaks 9
16-byte allocations (origin unknown) under WITH_DEBUG_ALLOC — likely the same
element/str copy debt; needs its own allocator pass.

Activity

  1. ehartford commented on Jul 28, 2026

    @ehartford
    ContributorAuthor

    Shipped in 61eaf45 + 1b41ef2 (battery green: build, fixpoint, :test, :move-audit, :drop-audit, :test-green, :last-green; seed + installed compiler now v0.15.1-g1b41ef21e).

    What landed

    • Sema gate: a non-Copy, Drop-bearing element reached via vec.get(i) / vec[i] cannot satisfy an owned demand — by-value call argument, assignment into an owned place, or struct-literal field. Pinned by test/compile_errors/err_elem_copy_{call_arg,assign,struct_field}.w. While adding the assignment site I found plain x = vec[i] was never checked (the old reject call sat in the multi-index branch only) — fixed.
    • A plain let binding is deliberately NOT gated: D22 rules a view materializes an owned T only when an owned demand has already been established, and the issue-64 pins assert let item = inners.get(0); item.tags.push(99) mutates in place. First draft gated let and broke exactly those pins. Scope call recorded as decisions.md D26.
    • Vec[i32][1, 2] collection literals: gate consults index_expr_is_type_level (same predicate as check_index) so a type-level base is never misread as an element read.
    • Compiler + stdlib migrated off the copy-out pattern: ~45 sites across Mir, MirLower, MirSuspendCheck, AsyncMir, Zcu, CodegenUnits, Analysis, Lsp, BuildGraph*, main, and lib/std/build.w now borrow the element. CCodegen gained mir_body_at, the same frozen-module raw element handle Codegen uses (raw pointer hoisted into a local — deriving the reference straight from self.mir_mod.bodies.ptr still pins self in the view checker).

    For Eric — two connected design points surfaced by this work

    1. The real cure is D22 plan line 334: Vec element access as uniform view (&T) semantics. The interim gate keys on the get/index expression, so a let-bound element that is consumed later escapes it by design; under uniform view semantics the binding IS a &T and every later owned demand is checked structurally. When that lands, this gate should be subsumed/replaced.
    2. "Is a let binding an owned demand?" currently has two answers: NO for Vec elements (this issue, per the issue-64 pins and D22's letter), YES for field projections through an explicit borrow (Checker accepts owning-field read through a view as a silent aliasing bit-copy (move-out-of-borrow unrejected) #730's gate fires at let sites). One uniform answer should be picked when element-view semantics land — D26 flags it.
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