Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

merge BorrowKind::Unique into BorrowKind::Mut #112072

Closed
lcnr opened this issue May 29, 2023 · 2 comments · Fixed by #112119
Closed

merge BorrowKind::Unique into BorrowKind::Mut #112072

lcnr opened this issue May 29, 2023 · 2 comments · Fixed by #112119
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup.

Comments

@lcnr
Copy link
Contributor

lcnr commented May 29, 2023

We currently have a separate mir::BorrowKind for the mutable borrows for closure upvars.

BorrowKind::Unique is an ordinary mutable borrow, requiring its target to be invariant, needing unique access, except that the target is not necessarily required to be marked as mut by the user.

We should merge this into BorrowKind::Mut, resulting in the following setup:

enum BorrowKind {
    Shared,
    Shallow,
    Mut { kind: MutBorrowKind },
}

enum MutBorrowKind {
    Default,
    TwoPhaseBorrow,
    ClosureCapture,
}

It is quite worrying to me that fn BorrowKind::mutability simply returns Mutability::Not for BorrowKind::Unique

pub fn mutability(&self) -> Mutability {
match *self {
BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => Mutability::Not,
BorrowKind::Mut { .. } => Mutability::Mut,
}
}

This already caused a bug in #112056 where we used PlaceContext::NonMutatingUse for unique borrows, which resulted in us getting the wrong variance, cc #112070.

@lcnr lcnr added C-cleanup Category: PRs that clean code up or issues documenting cleanup. beta-nominated Nominated for backporting to the compiler in the beta channel. stable-nominated Nominated for backporting to the compiler in the stable channel. and removed beta-nominated Nominated for backporting to the compiler in the beta channel. stable-nominated Nominated for backporting to the compiler in the stable channel. labels May 29, 2023
@RalfJung
Copy link
Member

Can BorrowKind::Unique be used to perform actual mutation of a let (not let mut) bound local, or can it only be used for mutation through such a local (e.g. a reference stored there)?

@lcnr
Copy link
Contributor Author

lcnr commented May 29, 2023

Can BorrowKind::Unique be used to perform actual mutation of a let (not let mut) bound local, or can it only be used for mutation through such a local (e.g. a reference stored there)?

afaict it is only used to deal with locals whose type is &mut x, so BorrowKind::Unique only allows mutation through such a local

Dylan-DPC added a commit to Dylan-DPC/rust that referenced this issue May 30, 2023
…r=oli-obk

change `BorrowKind::Unique` to be a mutating `PlaceContext`

fixes rust-lang#112056

I believe that `BorrowKind::Unique` is a footgun in general, so I added a FIXME and opened rust-lang#112072. This is a bit too involved for this PR though.
bors added a commit to rust-lang-ci/rust that referenced this issue May 31, 2023
…oli-obk

change `BorrowKind::Unique` to be a mutating `PlaceContext`

fixes rust-lang#112056

I believe that `BorrowKind::Unique` is a footgun in general, so I added a FIXME and opened rust-lang#112072. This is a bit too involved for this PR though.
RalfJung pushed a commit to RalfJung/miri that referenced this issue May 31, 2023
change `BorrowKind::Unique` to be a mutating `PlaceContext`

fixes #112056

I believe that `BorrowKind::Unique` is a footgun in general, so I added a FIXME and opened rust-lang/rust#112072. This is a bit too involved for this PR though.
@bors bors closed this as completed in c55d1ee Jun 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-cleanup Category: PRs that clean code up or issues documenting cleanup.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants