Skip to content

Suggest removing &mut from a &mut borrow #77110

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

Merged
merged 1 commit into from
Oct 8, 2020
Merged

Conversation

tesuji
Copy link
Contributor

@tesuji tesuji commented Sep 23, 2020

Modify the code added in #54720.

Closes #75871

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Sep 23, 2020
@tesuji tesuji force-pushed the e0596-amp_mut branch 3 times, most recently from 3f4fa7f to ff4c2e1 Compare September 23, 2020 15:35
@tesuji tesuji marked this pull request as ready for review September 23, 2020 16:23
}
})
.unwrap_or(false)
if match self.body.local_decls.get(local) {
Copy link
Contributor

Choose a reason for hiding this comment

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

Should we refactor this into another function?

pub fn can_be_made_mutable(&self) -> bool {

as

                if self.body.local_decls
                    .get(local)
                    .map_or(false, |local_decl| local_decl.mut_borrow_of_mutable_ref(self.local_names[local])) => {

But I wonder if it will even failed to get local? The next block just use local_decls[local] but not sure if it will work here.

                if self.body.local_decls[local]
                    .mut_borrow_of_mutable_ref(self.local_names[local]) => {

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't get it. Did you mean to make a method on LocalDecls?

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't think there are code elsewhere that need to reuse the logic.

Copy link
Contributor

@pickfire pickfire Sep 24, 2020

Choose a reason for hiding this comment

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

Yes, that's true but I don't know if putting it as another function on itself would be good since it may easily be lost on context. Maybe refactor it as an implementation under MirBorrowckCtxt which is the same thing?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Honestly I prefer free function as it since it don't need other states than passed arguments.
But I don't have strong opinion, let's let it for Esteban or David.

Copy link
Member

Choose a reason for hiding this comment

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

I don't know if it is worth extracting this into a method, but I would prefer something like this than a match here:

self.body.local_decls
    .get(local)
    .map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])
    .unwrap_or(false)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

@pickfire
Copy link
Contributor

Just wondering, could there be multiple layers for this? Like multiple &mut &mut.

@tesuji
Copy link
Contributor Author

tesuji commented Oct 8, 2020

estebank's backlog is really long, so let's find another reviewer:

r? @davidtwco

@rust-highfive rust-highfive assigned davidtwco and unassigned estebank Oct 8, 2020
Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

LGTM, minor change requested, sorry for the delay in reviewing this.

}
})
.unwrap_or(false)
if match self.body.local_decls.get(local) {
Copy link
Member

Choose a reason for hiding this comment

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

I don't know if it is worth extracting this into a method, but I would prefer something like this than a match here:

self.body.local_decls
    .get(local)
    .map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local])
    .unwrap_or(false)

@tesuji
Copy link
Contributor Author

tesuji commented Oct 8, 2020

Sorry, I had to rebase because my local copy doesn't build with old worktree.

Copy link
Member

@davidtwco davidtwco left a comment

Choose a reason for hiding this comment

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

LGTM, r=me once CI passes; I'd like it if you could squash though.

Fix a typo: minding -> binding
Add test for &mut &mut
@estebank
Copy link
Contributor

estebank commented Oct 8, 2020

@bors r=davidtwco

@bors
Copy link
Collaborator

bors commented Oct 8, 2020

📌 Commit ab226bd has been approved by davidtwco

@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 Oct 8, 2020
.local_decls
.get(local)
.map(|l| mut_borrow_of_mutable_ref(l, self.local_names[local]))
.unwrap_or(false) =>
{
err.span_label(span, format!("cannot {ACT}", ACT = act));
err.span_label(span, "try removing `&mut` here");
Copy link
Contributor

Choose a reason for hiding this comment

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

It'd be nice to come back to this at some other time to gather a good span to make this a structured suggestion :)

@bors
Copy link
Collaborator

bors commented Oct 8, 2020

⌛ Testing commit ab226bd with merge 3525087...

@bors
Copy link
Collaborator

bors commented Oct 8, 2020

☀️ Test successful - checks-actions, checks-azure
Approved by: davidtwco
Pushing 3525087 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Oct 8, 2020
@bors bors merged commit 3525087 into rust-lang:master Oct 8, 2020
@rustbot rustbot added this to the 1.49.0 milestone Oct 8, 2020
@tesuji tesuji deleted the e0596-amp_mut branch October 8, 2020 23:43
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.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Confusing error when trying to re-borrow a function argument
7 participants