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
Sema: a write inside a nested block does not see a live view used after the block (view_last_use scans only the current block) #1722
§21.1 Rule 2 (a write to a viewed place while the view is live is refused); §3.8 / D22 (a let of a non-Copy field binds a view).
Repro
type S { s: str, n: i32 }
fn main:
var x = S { s: "ab" ++ "cd", n: 0 }
let p = x.s
for i in 0..2:
x.s = "zz" ++ "zz"
print(p)
with check says ok; with run prints zzzz: p reads the replacement string after the one it viewed was dropped.
Cause
check_mutation_against_views decides whether a view is still live with view_last_use, which scans only the current block (the loop body here) for a later use of the view. print(p) sits in the enclosing block, so the mutation inside the loop finds no later use, drops the borrow (remove_borrow_at), and the write is accepted. The same-block case (let p = x.s; x.s = …; print(p)) is refused correctly (err_move_field_under_live_view).
Seen while conforming the compiler's own sources to #1530 on sema-cluster-b (PR #1716): the borrow trace showed the loop-body write clearing the outer view's entry. Present on the pinned seed too, so it predates that branch.
Expected
The mutation is refused: the view p is used after the loop. Liveness must consider uses in every enclosing block up to the view's declaring scope (the scan find_last_use_in_block performs, applied to each enclosing block), not just the block that holds the mutation.
Activity
Compiler sources: a view of `self` is not held across a `mut fn` call…
Sema: a view is live through the blocks enclosing a write and a loop'…
Sema.copy_sig_alias: the parameter count is an independent value, not…
Conform the stack's save/restore bindings to the nested-block view-li…
Ownership: D72 liveness byte, D73 assignment-as-read, validator leak/…