Repository navigation
let s: &T = place borrows the place instead of moving it (#1244, §3.8 auto-referencing) - #1250
Merged
Merged
Conversation
….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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Fixes #1244.
types_compatible_fastalready accepts an ownedTfor an expected&T("Auto-referencing: T → &T"), solet s: &str = xtype-checked — but the let path markedxmoved ("use of moved value" at the next read) andlower_let_bindinglowered astrinto a&strslot, 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 reuseslower_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
&localbinding — the explicitlet s = &vspelling and this auto-ref binding behave identically there. Lead:check_mutation_against_viewsskips borrows with no attached ref symbol andcollect_expr_view_depscarries 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.