You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
Consuming comprehension with a filter leaks every filtered-out element it would have moved ([r for r in v.into_iter() if c]) #1501
Spec: §13.6 ([expr for x in iter if cond]); D33 — a consuming comprehension's filtered-out owned element drops on its iteration. test/debug_alloc/da_consuming_iter_comprehension.w pins projection-with-filter and identity-without-filter; the identity-WITH-filter cell is missing, and it leaks.
use std.builtins.print_i32
use std.mem.alloc
use std.mem.free_mem
type R { id: i32, ptr: *i8, slot: *mut i32 }
impl Drop for R:
fn drop(move self: Self):
unsafe:
*self.slot = *self.slot + self.id
free_mem(self.ptr)
fn mk(id: i32, slot: *mut i32): R { id, ptr: alloc(16), slot }
fn go(slot: *mut i32):
var v: Vec[R] = Vec.new()
v.push(mk(1, slot))
v.push(mk(2, slot))
v.push(mk(4, slot))
let kept = [r for r in v.into_iter() if r.id != 2]
let _n = kept.len()
fn main:
var drops: i32 = 0
let slot = &raw mut drops
go(slot)
print_i32(drops)
run --debug-alloc (stage1 of origin/main ba2d5d1): prints 5 (expected 7), LEAK size=16 origin=with_alloc, leak count=1 — the filtered-out R (id 2) is never dropped.
MIR of go: the filter's fail edge goes to the per-iteration join bb14: { StorageDead(_17); goto -> bb10; } — no drop of the binding _17. The pass branch moved _17 into the push (sym52(copy _11, move _17)), which marked _17 moved in MirBuilder's (path-insensitive) move state; the scope pop at the join then emitted StorageDead instead of the value drop (emit_drop_entry's moved-skip). The pass path's reset-on-move (_17 = const zst) already makes a guarded drop at the join correct on both paths.
Exact site: MirLower.lower_comprehension_generic_iter, the clause_filter != 0 branch: the pass branch is lowered without save_move_state/restore_move_state around it (lower_if restores the move state after each arm), so pop_scope_with_goto(header_bb) at iter_join_bb sees the binding as moved on every path.
Same leak with a pattern binding: [r for (k, r) in v.into_iter() if k == 0].
Found while building the #1403 comprehension matrix.
Activity
MIR: a comprehension clause's refutable pattern skips what it does no…
MIR: a comprehension clause's refutable pattern skips what it does no…
MIR: a consuming comprehension's filter pass branch moves on its own …
MIR: a consuming comprehension's filter pass branch moves on its own …
MIR: a consuming comprehension's filter pass branch moves on its own …
MIR: a consuming comprehension's filter pass branch moves on its own …
MIR: a consuming comprehension's filter pass branch moves on its own …
MIR: a comprehension drops each iteration's temporaries in that itera…
Ownership: D72 liveness byte, D73 assignment-as-read, validator leak/…
Wipe's compiler issues: float joins, -> Unit bodies, method .Variant,…