Skip to content

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

Closed

Description

@ehartford

Spec reference

§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.

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