-
Notifications
You must be signed in to change notification settings - Fork 13k
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
Suggest using a temporary variable to fix borrowck errors #83174
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains 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
26 changes: 26 additions & 0 deletions
26
compiler/rustc_borrowck/src/diagnostics/find_all_local_uses.rs
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
use std::collections::BTreeSet; | ||
|
||
use rustc_middle::mir::visit::{PlaceContext, Visitor}; | ||
use rustc_middle::mir::{Body, Local, Location}; | ||
|
||
/// Find all uses of (including assignments to) a [`Local`]. | ||
/// | ||
/// Uses `BTreeSet` so output is deterministic. | ||
pub(super) fn find<'tcx>(body: &Body<'tcx>, local: Local) -> BTreeSet<Location> { | ||
let mut visitor = AllLocalUsesVisitor { for_local: local, uses: BTreeSet::default() }; | ||
visitor.visit_body(body); | ||
visitor.uses | ||
} | ||
|
||
struct AllLocalUsesVisitor { | ||
for_local: Local, | ||
uses: BTreeSet<Location>, | ||
} | ||
|
||
impl<'tcx> Visitor<'tcx> for AllLocalUsesVisitor { | ||
fn visit_local(&mut self, local: &Local, _context: PlaceContext, location: Location) { | ||
if *local == self.for_local { | ||
self.uses.insert(location); | ||
} | ||
} | ||
} |
This file contains 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
This file contains 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
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// See issue #77834. | ||
|
||
#![crate_type = "lib"] | ||
|
||
mod method_syntax { | ||
struct Foo; | ||
|
||
impl Foo { | ||
fn foo(&mut self, _: f32) -> i32 { todo!() } | ||
fn bar(&mut self) -> f32 { todo!() } | ||
fn baz(&mut self) { | ||
self.foo(self.bar()); //~ ERROR | ||
} | ||
} | ||
} | ||
|
||
mod fully_qualified_syntax { | ||
struct Foo; | ||
|
||
impl Foo { | ||
fn foo(&mut self, _: f32) -> i32 { todo!() } | ||
fn bar(&mut self) -> f32 { todo!() } | ||
fn baz(&mut self) { | ||
Self::foo(self, Self::bar(self)); //~ ERROR | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
error[E0499]: cannot borrow `*self` as mutable more than once at a time | ||
--> $DIR/suggest-local-var-double-mut.rs:12:22 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ---------^^^^^^^^^^- | ||
| | | | | ||
| | | second mutable borrow occurs here | ||
| | first borrow later used by call | ||
| first mutable borrow occurs here | ||
camelid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| | ||
help: try adding a local storing this argument... | ||
--> $DIR/suggest-local-var-double-mut.rs:12:22 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ^^^^^^^^^^ | ||
help: ...and then using that local as the argument to this call | ||
--> $DIR/suggest-local-var-double-mut.rs:12:13 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
camelid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
error[E0499]: cannot borrow `*self` as mutable more than once at a time | ||
--> $DIR/suggest-local-var-double-mut.rs:24:39 | ||
| | ||
LL | Self::foo(self, Self::bar(self)); | ||
| --------- ---- ^^^^ second mutable borrow occurs here | ||
| | | | ||
| | first mutable borrow occurs here | ||
| first borrow later used by call | ||
| | ||
help: try adding a local storing this argument... | ||
--> $DIR/suggest-local-var-double-mut.rs:24:29 | ||
| | ||
LL | Self::foo(self, Self::bar(self)); | ||
| ^^^^^^^^^^^^^^^ | ||
help: ...and then using that local as the argument to this call | ||
--> $DIR/suggest-local-var-double-mut.rs:24:13 | ||
| | ||
LL | Self::foo(self, Self::bar(self)); | ||
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0499`. |
33 changes: 33 additions & 0 deletions
33
src/test/ui/borrowck/suggest-local-var-imm-and-mut.nll.stderr
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
--> $DIR/suggest-local-var-imm-and-mut.rs:12:22 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ---------^^^^^^^^^^- | ||
| | | | | ||
| | | mutable borrow occurs here | ||
| | immutable borrow later used by call | ||
| immutable borrow occurs here | ||
| | ||
help: try adding a local storing this argument... | ||
--> $DIR/suggest-local-var-imm-and-mut.rs:12:22 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ^^^^^^^^^^ | ||
help: ...and then using that local as the argument to this call | ||
--> $DIR/suggest-local-var-imm-and-mut.rs:12:13 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ^^^^^^^^^^^^^^^^^^^^ | ||
Comment on lines
+11
to
+20
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It's intriguing that the suggestion doesn't show up in normal mode but does (correctly) show up in nll mode. |
||
|
||
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
--> $DIR/suggest-local-var-imm-and-mut.rs:24:39 | ||
| | ||
LL | Self::foo(self, Self::bar(self)); | ||
| --------- ---- ^^^^ mutable borrow occurs here | ||
| | | | ||
| | immutable borrow occurs here | ||
| immutable borrow later used by call | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0502`. |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
// See issue #77834. | ||
|
||
#![crate_type = "lib"] | ||
|
||
mod method_syntax { | ||
struct Foo; | ||
|
||
impl Foo { | ||
fn foo(&self, _: f32) -> i32 { todo!() } | ||
fn bar(&mut self) -> f32 { todo!() } | ||
fn baz(&mut self) { | ||
self.foo(self.bar()); //~ ERROR | ||
} | ||
} | ||
} | ||
|
||
mod fully_qualified_syntax { | ||
struct Foo; | ||
|
||
impl Foo { | ||
fn foo(&self, _: f32) -> i32 { todo!() } | ||
fn bar(&mut self) -> f32 { todo!() } | ||
fn baz(&mut self) { | ||
Self::foo(self, Self::bar(self)); //~ ERROR | ||
} | ||
} | ||
} |
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
--> $DIR/suggest-local-var-imm-and-mut.rs:12:22 | ||
| | ||
LL | self.foo(self.bar()); | ||
| ---------^^^^^^^^^^- | ||
| | | | | ||
| | | mutable borrow occurs here | ||
| | immutable borrow later used by call | ||
| immutable borrow occurs here | ||
camelid marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
error[E0502]: cannot borrow `*self` as mutable because it is also borrowed as immutable | ||
--> $DIR/suggest-local-var-imm-and-mut.rs:24:29 | ||
| | ||
LL | Self::foo(self, Self::bar(self)); | ||
| --------- ---- ^^^^^^^^^^^^^^^ mutable borrow occurs here | ||
| | | | ||
| | immutable borrow occurs here | ||
| immutable borrow later used by call | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0502`. |
This file contains 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
This file contains 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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One idea I have is to track the call's
Location
in theUsedLater
, instead of (or in addition to) the span. However, the "later use" is sometimes incorrect for our purposes; e.g., it may refer to a closure that also borrowsplace
, even if it's not the outer call.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We could point at both spans, would that help?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Or just at the new span if it isn't in a closure?