Skip to content

let s: &T = place borrows the place instead of moving it (#1244, §3.8 auto-referencing) - #1250

Merged
ehartford merged 1 commit into
mainfrom
typed-let-borrow
Sep 21, 2026
Merged

ehartford merged 1 commit into
mainfrom
typed-let-borrow

Conversation

@ehartford

Copy link
Copy Markdown
Contributor

Fixes #1244.

types_compatible_fast already accepts an owned T for an expected &T ("Auto-referencing: T → &T"), so let s: &str = x type-checked — but the let path marked x moved ("use of moved value" at the next read) and lower_let_binding lowered a str into a &str slot, which the MIR validator refused (over a global it reached the #1242 copy path). The validator did its job; the let path never implemented the borrow.

Now Sema records the auto-referenced initializer (auto_ref_binding_values) when the annotation is a shared reference the value's type satisfies and the value is a place; the binding is not a consume and its root is registered as a view dep, so the SHARED borrow exists exactly as for &x. MirLower reuses lower_auto_ref_call_arg — the third auto-ref boundary after call arguments and returns.

Test: behav_typed_let_borrows_place (str, Vec, struct places; owner still live; debug-alloc leak count=0).

Found on the way, filed as #1249: §21.1 rule 1 (mutate the place, then use the reference) is not diagnosed for a plain &local binding — the explicit let s = &v spelling and this auto-ref binding behave identically there. Lead: check_mutation_against_views skips borrows with no attached ref symbol and collect_expr_view_deps carries only ref-typed origins.

Battery (seed-driven, worktree, ownership change alone): build, :fixpoint, :move-audit, :drop-audit, :test, :test-green, :last-green all rc=0.

….8 auto-referencing)

types_compatible_fast accepts an owned T for an expected &T ("Auto-referencing:
T → &T", Sema.w ~7097), so `let s: &str = x` type-checked, but the let path
then ran mark_moved_if_consumed on `x` ("use of moved value" at the next
read) and lower_let_binding lowered the bytes of a str into a &str slot —
the MIR validator refused it ("use rvalue type is incompatible with assign
destination", dest kind=14 src kind=5); over a global the same spelling hit
the #1242 copy path.

Sema records the auto-referenced initializer (`auto_ref_binding_values`) when
the annotation is a shared reference whose pointee the value satisfies and
the value is a place; the binding is not a consume, and
record_view_binding_from_expr registers the place's root as a view dep so the
SHARED borrow exists exactly as for `&x`. MirLower reuses
lower_auto_ref_call_arg (RK_REF into a temp of the annotated type) — the
third boundary after call arguments and returns
(adjust_ret_operand_auto_ref).

behav_typed_let_borrows_place: str, Vec and struct places borrowed by a typed
let, the owner still live, debug-alloc leak count=0. Invalidation of the
borrow (mutate the place, then use `s`) is not diagnosed for this binding
shape or for the explicit `&v` spelling — §21.1 rule 1 gap filed as #1249.
@ehartford
ehartford merged commit 38f93e2 into main Sep 21, 2026
5 checks passed
@ehartford
ehartford deleted the typed-let-borrow branch September 24, 2026 16:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

let s: &T = x moves x instead of binding a view (typed-let borrow over an ident)

1 participant