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 #112119

Merged
merged 3 commits into from
Jun 21, 2023

Conversation

zirconium-n
Copy link
Contributor

Fixes #112072

Might have conflict with #112070

r? @lcnr

I'm not sure what's the suitable change in a couple places.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels May 30, 2023
@rustbot
Copy link
Collaborator

rustbot commented May 30, 2023

Some changes occurred to MIR optimizations

cc @rust-lang/wg-mir-opt

This PR changes MIR

cc @oli-obk, @RalfJung, @JakobDegen, @davidtwco, @celinval, @vakaras

Some changes might have occurred in exhaustiveness checking

cc @Nadrieril

Copy link
Contributor Author

@zirconium-n zirconium-n left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I commented on places I'm not sure.

compiler/rustc_middle/src/mir/mod.rs Show resolved Hide resolved
compiler/rustc_middle/src/mir/mod.rs Outdated Show resolved Hide resolved
@zirconium-n zirconium-n force-pushed the issue-113072-merge-borrow-kind branch from 0ec3a2d to c12feb5 Compare May 31, 2023 09:50
Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

quickly skimmed it but need to take some time to think about the places where Unique currently differs from Mut, will only get to that next week probably

compiler/rustc_mir_build/src/thir/cx/expr.rs Show resolved Hide resolved
@zirconium-n
Copy link
Contributor Author

quickly skimmed it but need to take some time to think about the places where Unique currently differs from Mut, will only get to that next week probably

From my intuition, Unique is just a Mut except it does not require a mutable binding. (Maybe that's oversimplification?)

So here's some personal observation:

  1. Diagnostics obviously should differ.

  2. These ones should unify Unique and Mut. Since they are checking exactly whether it's a mutable borrow.

    fn ref_allows_mutation(&self, kind: mir::BorrowKind, place: mir::Place<'tcx>) -> bool {
    match kind {
    mir::BorrowKind::Mut { .. } => true,
    mir::BorrowKind::Shared | mir::BorrowKind::Shallow | mir::BorrowKind::Unique => {
    self.shared_borrow_allows_mutation(place)
    }
    }
    }

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

    ExprKind::Borrow { borrow_kind, arg } => {
    let mut visitor = LayoutConstrainedPlaceVisitor::new(self.thir, self.tcx);
    visit::walk_expr(&mut visitor, expr);
    if visitor.found {
    match borrow_kind {
    BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique
    if !self.thir[arg].ty.is_freeze(self.tcx, self.param_env) =>
    {
    self.requires_unsafe(expr.span, BorrowOfLayoutConstrainedField)
    }
    BorrowKind::Mut { .. } => {
    self.requires_unsafe(expr.span, MutationOfLayoutConstrainedField)
    }
    BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {}
    }
    }
    }

  3. Same to 1. But I think we can't create a ref uniq binding anyway so it probably does not matter?

    PatKind::Binding { mode: BindingMode::ByRef(borrow_kind), ty, .. } => {
    if self.inside_adt {
    let ty::Ref(_, ty, _) = ty.kind() else {
    span_bug!(
    pat.span,
    "BindingMode::ByRef in pattern, but found non-reference type {}",
    ty
    );
    };
    match borrow_kind {
    BorrowKind::Shallow | BorrowKind::Shared | BorrowKind::Unique => {
    if !ty.is_freeze(self.tcx, self.param_env) {
    self.requires_unsafe(pat.span, BorrowOfLayoutConstrainedField);
    }
    }
    BorrowKind::Mut { .. } => {
    self.requires_unsafe(pat.span, MutationOfLayoutConstrainedField);
    }
    }
    }
    visit::walk_pat(self, pat);
    }

  4. Same to 1. but this is just for diagnostic I guess.

    // FIXME: won't be used after diagnostic migration
    pub fn describe_mutability(&self) -> &str {
    match *self {
    BorrowKind::Shared | BorrowKind::Shallow | BorrowKind::Unique => "immutable",
    BorrowKind::Mut { .. } => "mutable",
    }
    }

  5. This one probably stay different just to be safe.

    fn validate_ref(&mut self, kind: BorrowKind, place: &Place<'tcx>) -> Result<(), Unpromotable> {
    match kind {
    // Reject these borrow types just to be safe.
    // FIXME(RalfJung): could we allow them? Should we? No point in it until we have a usecase.
    BorrowKind::Shallow | BorrowKind::Unique => return Err(Unpromotable),

  6. This one should stay different since this is the motivation to introduce Unique.

    match kind {
    Reservation(WriteKind::MutableBorrow(
    borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
    ))
    | Write(WriteKind::MutableBorrow(
    borrow_kind @ (BorrowKind::Unique | BorrowKind::Mut { .. }),
    )) => {
    let is_local_mutation_allowed = match borrow_kind {
    BorrowKind::Unique => LocalMutationIsAllowed::Yes,
    BorrowKind::Mut { .. } => is_local_mutation_allowed,
    BorrowKind::Shared | BorrowKind::Shallow => unreachable!(),
    };

Copy link
Contributor

@lcnr lcnr left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure whether specialcasing ClosureCapture borrows this much in compiler/rustc_borrowck/src/diagnostics/conflict_errors.rs is necessary or even particularily helpful, but 🤷

after dealing with the comments, r=me

thank you for working on this ❤️

compiler/rustc_borrowck/src/borrow_set.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/diagnostics/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_borrowck/src/lib.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/mir/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/mir/mod.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/mir/syntax.rs Show resolved Hide resolved
@lcnr
Copy link
Contributor

lcnr commented Jun 6, 2023

we should also run crater after the remaining changes as I expect the unsafeck and const checks to be theoretically breaking. Don't have the capacity to try and get an actual test for that though

@lcnr lcnr added S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 6, 2023
@zirconium-n zirconium-n force-pushed the issue-113072-merge-borrow-kind branch 2 times, most recently from e34c364 to ed3d78f Compare June 7, 2023 00:22
@zirconium-n
Copy link
Contributor Author

@rustbot label -S-waiting-on-author +S-waiting-on-review

we should also run crater after the remaining changes as I expect the unsafeck and const checks to be theoretically breaking. Don't have the capacity to try and get an actual test for that though

Const checks should be ok, since we forbid &mut in const so it's rejected anyway?
Not sure about unsafeck.

@rustbot rustbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-author Status: This is awaiting some action (such as code changes or more information) from the author. labels Jun 7, 2023
compiler/rustc_middle/src/mir/syntax.rs Outdated Show resolved Hide resolved
compiler/rustc_middle/src/mir/syntax.rs Outdated Show resolved Hide resolved
@lcnr
Copy link
Contributor

lcnr commented Jun 8, 2023

@bors try

@bors
Copy link
Contributor

bors commented Jun 8, 2023

⌛ Trying commit 9ce76438c3537c38c0614036add7cbe790cbdfd7 with merge d43518feaed04ed9d50bf2337350de8304ca58cf...

@zirconium-n zirconium-n force-pushed the issue-113072-merge-borrow-kind branch from 9ce7643 to d93e93b Compare June 8, 2023 10:55
@lcnr
Copy link
Contributor

lcnr commented Jun 15, 2023

@bors try

@bors
Copy link
Contributor

bors commented Jun 15, 2023

⌛ Trying commit d93e93b3c821de824a888eae65a05f9687ad940c with merge ca6d8d30c65b4fcf60d8a40774045a936ec3c78d...

@bors
Copy link
Contributor

bors commented Jun 15, 2023

☀️ Try build successful - checks-actions
Build commit: ca6d8d30c65b4fcf60d8a40774045a936ec3c78d (ca6d8d30c65b4fcf60d8a40774045a936ec3c78d)

@lcnr
Copy link
Contributor

lcnr commented Jun 16, 2023

@craterbot check

@craterbot
Copy link
Collaborator

👌 Experiment pr-112119 created and queued.
🤖 Automatically detected try build ca6d8d30c65b4fcf60d8a40774045a936ec3c78d
🔍 You can check out the queue and this experiment's details.

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot removed the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Jun 16, 2023
@craterbot craterbot added the S-waiting-on-crater Status: Waiting on a crater run to be completed. label Jun 16, 2023
@craterbot
Copy link
Collaborator

🚧 Experiment pr-112119 is now running

ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot
Copy link
Collaborator

🎉 Experiment pr-112119 is completed!
📊 16 regressed and 4 fixed (298526 total)
📰 Open the full report.

⚠️ If you notice any spurious failure please add them to the blacklist!
ℹ️ Crater is a tool to run experiments across parts of the Rust ecosystem. Learn more

@craterbot craterbot added S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. and removed S-waiting-on-crater Status: Waiting on a crater run to be completed. labels Jun 19, 2023
@lcnr
Copy link
Contributor

lcnr commented Jun 20, 2023

looked at a bunch and they all looked spurious, didn't get a nice testcase for them

r=me after rebase

@zirconium-n zirconium-n force-pushed the issue-113072-merge-borrow-kind branch from d93e93b to b8a250f Compare June 20, 2023 13:09
@lcnr
Copy link
Contributor

lcnr commented Jun 20, 2023

@bors r+ rollup=never

@bors
Copy link
Contributor

bors commented Jun 20, 2023

📌 Commit b8a250f has been approved by lcnr

It is now in the queue for this repository.

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Jun 20, 2023
@bors
Copy link
Contributor

bors commented Jun 21, 2023

⌛ Testing commit b8a250f with merge c55d1ee...

@bors
Copy link
Contributor

bors commented Jun 21, 2023

☀️ Test successful - checks-actions
Approved by: lcnr
Pushing c55d1ee to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Jun 21, 2023
@bors bors merged commit c55d1ee into rust-lang:master Jun 21, 2023
@rustbot rustbot added this to the 1.72.0 milestone Jun 21, 2023
@rust-timer
Copy link
Collaborator

Finished benchmarking commit (c55d1ee): comparison URL.

Overall result: no relevant changes - no action needed

@rustbot label: -perf-regression

Instruction count

This benchmark run did not return any relevant results for this metric.

Max RSS (memory usage)

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
2.3% [2.3%, 2.3%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Cycles

Results

This is a less reliable metric that may be of interest but was not used to determine the overall result at the top of this comment.

mean range count
Regressions ❌
(primary)
- - 0
Regressions ❌
(secondary)
3.0% [3.0%, 3.0%] 1
Improvements ✅
(primary)
- - 0
Improvements ✅
(secondary)
- - 0
All ❌✅ (primary) - - 0

Binary size

This benchmark run did not return any relevant results for this metric.

Bootstrap: 657.959s -> 655.662s (-0.35%)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

merge BorrowKind::Unique into BorrowKind::Mut
6 participants