Skip to content

§21.1 rule 1 not enforced for a plain &local binding: mutate the place, then use the reference — no diagnostic #1249

Closed

Description

@ehartford

Spec §21.1 rule 1: active shared borrows of a place are invalidated when the place is mutated (assignment, a mut self call, IndexPlace writes) and a later use of the borrow is rejected. The checker enforces this for map views (Option[&V] from get, "cannot mutate self while x is a live view into it") and element views (#887), but not for a plain reference binding of a local:

fn main:
    var v: Vec[str] = Vec.new()
    v.push("a".clone())
    let s = &v
    v.push("b".clone())      // also: v.clear(), v.pop(), v[0] = ..., v = Vec.new()
    print(f"{s.len()}")      // accepted; reads through an invalidated view

Matrix (installed compiler, main e16c6db): v.push, v.clear(), v.pop(), v[0] = x, v = Vec.new() — all accepted, both for let s = &v and for the auto-referenced let s: &Vec[str] = v (#1244, same borrow). consume(move v) then use is rejected (rule 2/3 fire; rule 1 does not). Same with str: let s = &x; x = "cd"; s.len().

record_view_binding_from_expr sets the deps and register_view_binding_borrows creates the SHARED borrow, so the borrow exists; the mutation paths (check_mutation_against_views, assignment, mut self dispatch) do not consult it for this binding shape. Own ownership batch.

Activity

  1. ehartford commented on Sep 21, 2026

    @ehartford
    ContributorAuthor

    Mechanism (lldb on the release binary, breakpoint set -r register_view_binding_borrows): the borrow is only ever attached from check_let_binding → record_view_binding_from_expr → register_view_binding_borrows, and it attaches per dep. For let s = &v over an owned v, collect_expr_view_deps yields nothing ("only carries ref-typed origins") and the bare-place fallback in record_view_binding_from_expr adds the root only for an NK_INDEX initializer (#887) — and, since #1250, for an auto-referenced typed let. So s has no dep, the SHARED borrow created at &v keeps borrow_refs[i] == 0, and check_mutation_against_views skips it (if ref_sym == 0: continue, SemaCheck.w ~22481).

    Fix shape: the bare-place fallback should cover every place initializer that is a borrow of an owned root — &place, &v[i], &s.field, the auto-ref — so §21.1 rule 1 has a ref symbol to report. Own ownership batch with the drop/move audits.

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