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
Mutating the iterated collection inside for x in xs is accepted (no live-view diagnostic for the loop binding) #1317
Found while fixing #1297. §21.1 rule 1 / D44: for e in xs is xs.iter() and e is a view into xs; mutating xs while e is live must be rejected as it is for let e = xs.get(0); xs.push(..); use e ("cannot mutate `xs` while `e` is a live view into it"). The for binding registers no borrow, so this compiles and reads through a reallocated buffer:
type KV { key: str, n: i32 }
fn main:
var xs: Vec[KV] = Vec.new()
xs.push(KV { key: "a".clone(), n: 1 })
for e in xs:
xs.push(KV { key: "b".clone(), n: 2 }) // accepted; `e` dangles after the realloc
print(e.key)
Expected: cannot mutate \xs` while `e` is a live view into it`. Actual: check passes.
Not folded into #1297 because the borrow registration is whole-root: registering self for for e in self.items would also reject for e in self.items: self.count += 1, which is accepted today and idiomatic across the compiler (today let e = self.items.get(0); self.count += 1 already trips the coarse whole-self borrow). The loop-binding borrow needs the field path (self.items), not the root.
Activity
Sema: a field view of a borrowed collection element keeps its origin …
Sema: a `for` binding borrows the iterated place for the whole loop —…
Sema: a mutation that ends the loop does not conflict with the loop b…
Sema: a generic callee's body check sets the caller's borrows aside i…
Sema: a `for` binding borrows the iterated place for the whole loop —…
Sema: a mutation that ends the loop does not conflict with the loop b…
Sema: a generic callee's body check sets the caller's borrows aside i…
Fix withlang-dev#1317: a for binding borrows the iterated place for t…