-
Notifications
You must be signed in to change notification settings - Fork 13.5k
Suggest as_mut
when Pin<T>
is used after move
#93181
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
Closed
Closed
Changes from 1 commit
Commits
Show all changes
2 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 hidden or 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 hidden or 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 | ||||
---|---|---|---|---|---|---|
|
@@ -13,7 +13,11 @@ use rustc_middle::mir::{ | |||||
use rustc_middle::ty::print::Print; | ||||||
use rustc_middle::ty::{self, DefIdTree, Instance, Ty, TyCtxt}; | ||||||
use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult}; | ||||||
use rustc_span::{hygiene::DesugaringKind, symbol::sym, Span}; | ||||||
use rustc_span::{ | ||||||
hygiene::DesugaringKind, | ||||||
symbol::{sym, Symbol}, | ||||||
Span, | ||||||
}; | ||||||
use rustc_target::abi::VariantIdx; | ||||||
|
||||||
use super::borrow_set::BorrowData; | ||||||
|
@@ -577,9 +581,8 @@ pub(super) enum FnSelfUseKind<'tcx> { | |||||
Normal { | ||||||
self_arg: Ident, | ||||||
implicit_into_iter: bool, | ||||||
/// Whether the self type of the method call has an `.as_ref()` method. | ||||||
/// Used for better diagnostics. | ||||||
is_option_or_result: bool, | ||||||
self_name: Option<Symbol>, | ||||||
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.
Suggested change
|
||||||
}, | ||||||
/// A call to `FnOnce::call_once`, desugared from `my_closure(a, b, c)` | ||||||
FnOnceCall, | ||||||
|
@@ -948,18 +951,16 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> { | |||||
let kind = kind.unwrap_or_else(|| { | ||||||
// This isn't a 'special' use of `self` | ||||||
debug!("move_spans: method_did={:?}, fn_call_span={:?}", method_did, fn_call_span); | ||||||
|
||||||
let implicit_into_iter = Some(method_did) == tcx.lang_items().into_iter_fn() | ||||||
&& fn_call_span.desugaring_kind() == Some(DesugaringKind::ForLoop); | ||||||
let parent_self_ty = parent | ||||||
.filter(|did| tcx.def_kind(*did) == rustc_hir::def::DefKind::Impl) | ||||||
.and_then(|did| match tcx.type_of(did).kind() { | ||||||
ty::Adt(def, ..) => Some(def.did), | ||||||
_ => None, | ||||||
}); | ||||||
let is_option_or_result = parent_self_ty.map_or(false, |def_id| { | ||||||
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result)) | ||||||
}); | ||||||
FnSelfUseKind::Normal { self_arg, implicit_into_iter, is_option_or_result } | ||||||
|
||||||
let self_name = self.body.local_decls[target_temp] | ||||||
.ty | ||||||
.ty_adt_def() | ||||||
.and_then(|def| tcx.get_diagnostic_name(def.did)); | ||||||
|
||||||
FnSelfUseKind::Normal { self_arg, implicit_into_iter, self_name } | ||||||
}); | ||||||
|
||||||
return FnSelfUse { | ||||||
|
This file contains hidden or 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 hidden or 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,23 @@ | ||
use std::pin::Pin; | ||
|
||
struct Foo; | ||
|
||
impl Foo { | ||
fn foo(self: Pin<&mut Self>) {} | ||
} | ||
|
||
fn main() { | ||
let mut foo = Foo; | ||
let foo = Pin::new(&mut foo); | ||
foo.foo(); | ||
//~^ HELP consider calling `.as_mut()` to borrow the type's contents | ||
foo.foo(); | ||
//~^ ERROR use of moved value | ||
|
||
let mut x = 1; | ||
let mut x = Pin::new(&mut x); | ||
x.get_mut(); | ||
//~^ HELP consider calling `.as_mut()` to borrow the type's contents | ||
x.get_mut(); | ||
//~^ ERROR use of moved value | ||
} |
This file contains hidden or 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,45 @@ | ||
error[E0382]: use of moved value: `foo` | ||
--> $DIR/pin-content-move.rs:14:5 | ||
| | ||
LL | let foo = Pin::new(&mut foo); | ||
| --- move occurs because `foo` has type `Pin<&mut Foo>`, which does not implement the `Copy` trait | ||
LL | foo.foo(); | ||
| ----- `foo` moved due to this method call | ||
LL | | ||
LL | foo.foo(); | ||
| ^^^ value used here after move | ||
| | ||
note: this function takes ownership of the receiver `self`, which moves `foo` | ||
--> $DIR/pin-content-move.rs:6:12 | ||
| | ||
LL | fn foo(self: Pin<&mut Self>) {} | ||
| ^^^^ | ||
help: consider calling `.as_mut()` to borrow the type's contents | ||
| | ||
LL | foo.as_mut().foo(); | ||
| +++++++++ | ||
|
||
error[E0382]: use of moved value: `x` | ||
--> $DIR/pin-content-move.rs:21:5 | ||
| | ||
LL | let mut x = Pin::new(&mut x); | ||
| ----- move occurs because `x` has type `Pin<&mut i32>`, which does not implement the `Copy` trait | ||
LL | x.get_mut(); | ||
| --------- `x` moved due to this method call | ||
LL | | ||
LL | x.get_mut(); | ||
| ^ value used here after move | ||
| | ||
note: this function takes ownership of the receiver `self`, which moves `x` | ||
--> $SRC_DIR/core/src/pin.rs:LL:COL | ||
| | ||
LL | pub const fn get_mut(self) -> &'a mut T | ||
| ^^^^ | ||
help: consider calling `.as_mut()` to borrow the type's contents | ||
| | ||
LL | x.as_mut().get_mut(); | ||
| +++++++++ | ||
|
||
error: aborting due to 2 previous errors | ||
|
||
For more information about this error, try `rustc --explain E0382`. |
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.
Extra nit, but I personally would love that level of precision in the diagnostics: in the case where
P = &mut _
, that is, when we have to deal withPin<&mut …>
, we already have a borrow; and what we need is a re-borrow.This may look silly, but I think one of the main areas of confusion with
Pin
for beginners is this need for.as_mut()
, precisely because it's a type which, contrary to&mut
, features no implicit reborrowing.So even an extra note for that very case, stating this very fact, could be quite educational:
While that instance seems like a nit, in cases such as
Pin<Rc<…>>
etc. for shared-pinned stuff, the.as_mut()
suggestion is also likely to be incorrect.So, if it weren't too annoying to implement, I'd do a heuristic whereby
Rc
,Arc
, and&mut _
would be special-cased, and would ask for, respectively:.as_ref()
to borrow,.as_ref()
to borrow,.as_mut()
to reborrow, and otherwise defaulting to what we have here:.as_mut()
to borrow.