-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Add a note about implicit temporaries on &mut (fn or const) #104857
Closed
sharnoff
wants to merge
3
commits into
rust-lang:master
from
sharnoff:mut-borrow-const-or-fn-borrowck-error
Closed
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1836,6 +1836,8 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { | |
"value referencing" | ||
}; | ||
|
||
let mut implicit_temporary = None; | ||
|
||
let (place_desc, note) = if let Some(place_desc) = opt_place_desc { | ||
let local_kind = if let Some(local) = borrow.borrowed_place.as_local() { | ||
match self.body.local_kind(local) { | ||
|
@@ -1861,7 +1863,22 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { | |
let root_place = | ||
self.prefixes(borrow.borrowed_place.as_ref(), PrefixSet::All).last().unwrap(); | ||
let local = root_place.local; | ||
match self.body.local_kind(local) { | ||
let local_kind = self.body.local_kind(local); | ||
if let LocalKind::Temp = local_kind { | ||
// Mutable references to constants and functions will implicitly create | ||
// temporaries, even though immutable references don't. We use these below to add a | ||
// note about this. | ||
match &self.body.local_decls[local].local_info { | ||
Some(box LocalInfo::FnDefRef) => { | ||
implicit_temporary = Some(("function", "function pointer")); | ||
} | ||
Some(box LocalInfo::ConstRef { .. }) => { | ||
implicit_temporary = Some(("constant", "value")); | ||
} | ||
_ => {} | ||
} | ||
} | ||
match local_kind { | ||
LocalKind::ReturnPointer | LocalKind::Temp => { | ||
("temporary value".to_string(), "temporary value created here".to_string()) | ||
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. As you already mentioned, this message is the misleading part. Maybe pick a different message here for ConstRef and FnDefRef instead of adding a new note? |
||
} | ||
|
@@ -1906,6 +1923,13 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { | |
} | ||
} | ||
|
||
if let Some((item_name, value_desc)) = implicit_temporary { | ||
err.note(format!( | ||
"mutably borrowing a {} copies the {} to a temporary", | ||
item_name, value_desc | ||
)); | ||
} | ||
|
||
Some(err) | ||
} | ||
|
||
|
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
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
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
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
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,34 @@ | ||
// See PR #104857 for details | ||
|
||
// don't want to tie this test to the lint, even though it's related | ||
#![allow(const_item_mutation)] | ||
|
||
fn main() {} | ||
|
||
const X: i32 = 42; | ||
|
||
fn borrow_const_immut() -> &'static i32 { | ||
&X | ||
} | ||
|
||
fn borrow_const_immut_explicit_return() -> &'static i32 { | ||
return &X; | ||
} | ||
|
||
fn borrow_const_immut_into_temp() -> &'static i32 { | ||
let x_ref = &X; | ||
x_ref | ||
} | ||
|
||
fn borrow_const_mut() -> &'static mut i32 { | ||
return &mut X; //~ ERROR | ||
} | ||
|
||
fn borrow_const_mut_explicit_return() -> &'static mut i32 { | ||
return &mut X; //~ ERROR | ||
} | ||
|
||
fn borrow_const_mut_into_temp() -> &'static mut i32 { | ||
let x_ref = &mut X; | ||
x_ref //~ 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,35 @@ | ||
error[E0515]: cannot return reference to temporary value | ||
--> $DIR/return-borrow-mut-const.rs:24:12 | ||
| | ||
LL | return &mut X; | ||
| ^^^^^- | ||
| | | | ||
| | temporary value created here | ||
| returns a reference to data owned by the current function | ||
| | ||
= note: mutably borrowing a constant copies the value to a temporary | ||
|
||
error[E0515]: cannot return reference to temporary value | ||
--> $DIR/return-borrow-mut-const.rs:28:12 | ||
| | ||
LL | return &mut X; | ||
| ^^^^^- | ||
| | | | ||
| | temporary value created here | ||
| returns a reference to data owned by the current function | ||
| | ||
= note: mutably borrowing a constant copies the value to a temporary | ||
|
||
error[E0515]: cannot return value referencing temporary value | ||
--> $DIR/return-borrow-mut-const.rs:33:5 | ||
| | ||
LL | let x_ref = &mut X; | ||
| - temporary value created here | ||
LL | x_ref | ||
| ^^^^^ returns a value referencing data owned by the current function | ||
| | ||
= note: mutably borrowing a constant copies the value to a temporary | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0515`. |
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,31 @@ | ||
// See PR #104857 for details | ||
|
||
fn main() {} | ||
|
||
fn do_nothing() {} | ||
|
||
fn borrow_fn_immut() -> &'static dyn Fn() { | ||
&do_nothing | ||
} | ||
|
||
fn borrow_fn_immut_explicit_return() -> &'static dyn Fn() { | ||
&do_nothing | ||
} | ||
|
||
fn borrow_fn_immut_into_temp() -> &'static dyn Fn() { | ||
let f = &do_nothing; | ||
f | ||
} | ||
|
||
fn borrow_fn_mut() -> &'static mut dyn FnMut() { | ||
&mut do_nothing //~ ERROR | ||
} | ||
|
||
fn borrow_fn_mut_explicit_return() -> &'static mut dyn FnMut() { | ||
&mut do_nothing //~ ERROR | ||
} | ||
|
||
fn borrow_fn_mut_into_temp() -> &'static mut dyn FnMut() { | ||
let f = &mut do_nothing; | ||
f //~ 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,35 @@ | ||
error[E0515]: cannot return reference to temporary value | ||
--> $DIR/return-borrow-mut-fn.rs:21:5 | ||
| | ||
LL | &mut do_nothing | ||
| ^^^^^---------- | ||
| | | | ||
| | temporary value created here | ||
| returns a reference to data owned by the current function | ||
| | ||
= note: mutably borrowing a function copies the function pointer to a temporary | ||
|
||
error[E0515]: cannot return reference to temporary value | ||
--> $DIR/return-borrow-mut-fn.rs:25:5 | ||
| | ||
LL | &mut do_nothing | ||
| ^^^^^---------- | ||
| | | | ||
| | temporary value created here | ||
| returns a reference to data owned by the current function | ||
| | ||
= note: mutably borrowing a function copies the function pointer to a temporary | ||
|
||
error[E0515]: cannot return value referencing temporary value | ||
--> $DIR/return-borrow-mut-fn.rs:30:5 | ||
| | ||
LL | let f = &mut do_nothing; | ||
| ---------- temporary value created here | ||
LL | f | ||
| ^ returns a value referencing data owned by the current function | ||
| | ||
= note: mutably borrowing a function copies the function pointer to a temporary | ||
|
||
error: aborting due to 3 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0515`. |
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.
it actually doesn't put the function pointer into a temporary, but creates a temporary of the function item type (a zst). Maybe it could literally mention the zero sized nature, as that should make it obvious that you get no data to mutate (in case someone expected a function pointer).