Clarify what "this" means

This commit is contained in:
Oli Scherer 2022-12-12 12:07:09 +00:00
parent 717294fa04
commit 063b1675b2
26 changed files with 51 additions and 50 deletions

View File

@ -1069,7 +1069,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
);
}
}
CallKind::Normal { self_arg, desugaring, is_option_or_result } => {
CallKind::Normal { self_arg, desugaring, method_did } => {
let self_arg = self_arg.unwrap();
if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
@ -1139,14 +1139,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
),
);
}
let tcx = self.infcx.tcx;
// Avoid pointing to the same function in multiple different
// error messages.
if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
let func = tcx.def_path_str(method_did);
err.span_note(
self_arg.span,
&format!("this function takes ownership of the receiver `self`, which moves {}", place_name)
&format!("`{func}` takes ownership of the receiver `self`, which moves {place_name}")
);
}
let parent_did = tcx.parent(method_did);
let parent_self_ty = (tcx.def_kind(parent_did)
== rustc_hir::def::DefKind::Impl)
.then_some(parent_did)
.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))
});
if is_option_or_result && maybe_reinitialized_locations_is_empty {
err.span_label(
var_span,

View File

@ -5,7 +5,7 @@
use rustc_hir::def_id::DefId;
use rustc_hir::{lang_items, LangItem};
use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AssocItemContainer, DefIdTree, Instance, ParamEnv, Ty, TyCtxt};
use rustc_middle::ty::{AssocItemContainer, Instance, ParamEnv, Ty, TyCtxt};
use rustc_span::symbol::Ident;
use rustc_span::{sym, DesugaringKind, Span};
@ -39,9 +39,7 @@ pub enum CallKind<'tcx> {
Normal {
self_arg: Option<Ident>,
desugaring: Option<(CallDesugaringKind, Ty<'tcx>)>,
/// Whether the self type of the method call has an `.as_ref()` method.
/// Used for better diagnostics.
is_option_or_result: bool,
method_did: DefId,
},
/// A call to `Fn(..)::call(..)`, desugared from `my_closure(a, b, c)`
FnCall { fn_trait_id: DefId, self_ty: Ty<'tcx> },
@ -133,16 +131,6 @@ pub fn call_kind<'tcx>(
} else {
None
};
let parent_did = tcx.parent(method_did);
let parent_self_ty = (tcx.def_kind(parent_did) == rustc_hir::def::DefKind::Impl)
.then_some(parent_did)
.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))
});
CallKind::Normal { self_arg, desugaring, is_option_or_result }
CallKind::Normal { self_arg, desugaring, method_did }
})
}

View File

@ -7,7 +7,7 @@ LL | let _x = Rc::new(vec![1, 2]).into_iter();
| | value moved due to this method call
| move occurs because value has type `Vec<i32>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves value
note: `into_iter` takes ownership of the receiver `self`, which moves value
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
error: aborting due to previous error

View File

@ -27,7 +27,7 @@ LL | foo = Some(Struct);
LL | let _y = foo;
| ^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `foo`
note: `Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `foo`
--> $SRC_DIR/core/src/option.rs:LL:COL
error[E0382]: use of moved value: `foo`
@ -52,7 +52,7 @@ LL | foo = Some(Struct);
LL | } else if true {
LL | foo = Some(Struct);
| ^^^^^^^^^^^^^^^^^^
note: this function takes ownership of the receiver `self`, which moves `foo`
note: `Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `foo`
--> $SRC_DIR/core/src/option.rs:LL:COL
error: aborting due to 3 previous errors

View File

@ -9,7 +9,7 @@ LL |
LL | fill_segment(state);
| ^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `state`
note: `into_iter` takes ownership of the receiver `self`, which moves `state`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider creating a fresh reborrow of `state` here
|

View File

@ -8,7 +8,7 @@ LL | cb.map(|cb| cb());
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
| move occurs because `*cb` has type `Option<&mut dyn FnMut()>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `*cb`
note: `Option::<T>::map` takes ownership of the receiver `self`, which moves `*cb`
--> $SRC_DIR/core/src/option.rs:LL:COL
error[E0596]: cannot borrow `*cb` as mutable, as it is behind a `&` reference

View File

@ -10,7 +10,7 @@ LL | y.into_iter();
| |
| move occurs because `y` has type `Vec<String>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `y`
note: `into_iter` takes ownership of the receiver `self`, which moves `y`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
error: aborting due to previous error

View File

@ -9,7 +9,7 @@ LL | {
LL | println!("{:?}", some_vec);
| ^^^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `some_vec`
note: `into_iter` takes ownership of the receiver `self`, which moves `some_vec`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
= note: this error originates in the macro `$crate::format_args_nl` which comes from the expansion of the macro `println` (in Nightly builds, run with -Z macro-backtrace for more info)
help: consider cloning the value if the performance cost is acceptable

View File

@ -7,7 +7,7 @@ LL | x.borrow().nothing_is_true();
| | value moved due to this method call
| move occurs because value has type `TheDarkKnight`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves value
note: `TheDarkKnight::nothing_is_true` takes ownership of the receiver `self`, which moves value
--> $DIR/E0507.rs:6:24
|
LL | fn nothing_is_true(self) {}

View File

@ -13,7 +13,7 @@ LL | };
LL | x.zero()
| ^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `x`
note: `Foo::zero` takes ownership of the receiver `self`, which moves `x`
--> $DIR/issue-34721.rs:4:13
|
LL | fn zero(self) -> Self;

View File

@ -9,7 +9,7 @@ LL | for l in bad_letters {
LL | bad_letters.push('s');
| ^^^^^^^^^^^^^^^^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `bad_letters`
note: `into_iter` takes ownership of the receiver `self`, which moves `bad_letters`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider iterating over a slice of the `Vec<char>`'s content to avoid moving into the `for` loop
|

View File

@ -10,7 +10,7 @@ LL | let _closure = || orig;
| |
| value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `orig`
note: `into_iter` takes ownership of the receiver `self`, which moves `orig`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider iterating over a slice of the `Vec<bool>`'s content to avoid moving into the `for` loop
|

View File

@ -10,7 +10,7 @@ LL | for n in v {
LL | for n in v {
| ^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `v`
note: `into_iter` takes ownership of the receiver `self`, which moves `v`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider creating a fresh reborrow of `v` here
|

View File

@ -9,7 +9,7 @@ LL | for y in x {
LL | let z = x;
| ^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `x`
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider iterating over a slice of the `Vec<S>`'s content to avoid moving into the `for` loop
|

View File

@ -6,7 +6,7 @@ LL | val.0.into_iter().next();
LL | val.0;
| ^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `val.0`
note: `into_iter` takes ownership of the receiver `self`, which moves `val.0`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
= note: move occurs because `val.0` has type `Vec<bool>`, which does not implement the `Copy` trait
@ -20,7 +20,7 @@ LL | foo.use_self();
LL | foo;
| ^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `foo`
note: `Foo::use_self` takes ownership of the receiver `self`, which moves `foo`
--> $DIR/move-fn-self-receiver.rs:13:17
|
LL | fn use_self(self) {}
@ -46,7 +46,7 @@ LL | boxed_foo.use_box_self();
LL | boxed_foo;
| ^^^^^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `boxed_foo`
note: `Foo::use_box_self` takes ownership of the receiver `self`, which moves `boxed_foo`
--> $DIR/move-fn-self-receiver.rs:14:21
|
LL | fn use_box_self(self: Box<Self>) {}
@ -62,7 +62,7 @@ LL | pin_box_foo.use_pin_box_self();
LL | pin_box_foo;
| ^^^^^^^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `pin_box_foo`
note: `Foo::use_pin_box_self` takes ownership of the receiver `self`, which moves `pin_box_foo`
--> $DIR/move-fn-self-receiver.rs:15:25
|
LL | fn use_pin_box_self(self: Pin<Box<Self>>) {}
@ -88,7 +88,7 @@ LL | rc_foo.use_rc_self();
LL | rc_foo;
| ^^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `rc_foo`
note: `Foo::use_rc_self` takes ownership of the receiver `self`, which moves `rc_foo`
--> $DIR/move-fn-self-receiver.rs:16:20
|
LL | fn use_rc_self(self: Rc<Self>) {}
@ -154,7 +154,7 @@ LL | for _val in container.custom_into_iter() {}
LL | container;
| ^^^^^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `container`
note: `Container::custom_into_iter` takes ownership of the receiver `self`, which moves `container`
--> $DIR/move-fn-self-receiver.rs:23:25
|
LL | fn custom_into_iter(self) -> impl Iterator<Item = bool> {

View File

@ -8,7 +8,7 @@ LL | consume(x.into_iter().next().unwrap());
LL | touch(&x[0]);
| ^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `x`
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider cloning the value if the performance cost is acceptable
|

View File

@ -160,7 +160,7 @@ LL | let _y = x.into_iter().next().unwrap();
LL | touch(&x);
| ^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `x`
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider cloning the value if the performance cost is acceptable
|
@ -177,7 +177,7 @@ LL | let _y = [x.into_iter().next().unwrap(); 1];
LL | touch(&x);
| ^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `x`
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider cloning the value if the performance cost is acceptable
|

View File

@ -10,7 +10,7 @@ LL | let _x: Option<Struct> = foo.map(|s| bar(&s));
LL | let _y = foo;
| ^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `foo`
note: `Option::<T>::map` takes ownership of the receiver `self`, which moves `foo`
--> $SRC_DIR/core/src/option.rs:LL:COL
error: aborting due to previous error

View File

@ -16,7 +16,7 @@ LL | for i in &a {
LL | for j in a {
| ^ `a` moved due to this implicit call to `.into_iter()`, in previous iteration of loop
|
note: this function takes ownership of the receiver `self`, which moves `a`
note: `into_iter` takes ownership of the receiver `self`, which moves `a`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider iterating over a slice of the `Vec<i32>`'s content to avoid moving into the `for` loop
|

View File

@ -7,7 +7,7 @@ LL | for _ in self.v {
| `self.v` moved due to this implicit call to `.into_iter()`
| move occurs because `self.v` has type `Vec<u32>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `self.v`
note: `into_iter` takes ownership of the receiver `self`, which moves `self.v`
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider iterating over a slice of the `Vec<u32>`'s content to avoid moving into the `for` loop
|
@ -37,7 +37,7 @@ LL | for loader in *LOADERS {
| value moved due to this implicit call to `.into_iter()`
| move occurs because value has type `Vec<&u8>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves value
note: `into_iter` takes ownership of the receiver `self`, which moves value
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider iterating over a slice of the `Vec<&u8>`'s content to avoid moving into the `for` loop
|

View File

@ -7,7 +7,7 @@ LL | if selection.1.unwrap().contains(selection.0) {
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
| move occurs because `selection.1` has type `Option<String>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `selection.1`
note: `Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `selection.1`
--> $SRC_DIR/core/src/option.rs:LL:COL
error[E0507]: cannot move out of `selection.1` which is behind a shared reference
@ -19,7 +19,7 @@ LL | if selection.1.unwrap().contains(selection.0) {
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
| move occurs because `selection.1` has type `Result<String, String>`, which does not implement the `Copy` trait
|
note: this function takes ownership of the receiver `self`, which moves `selection.1`
note: `Result::<T, E>::unwrap` takes ownership of the receiver `self`, which moves `selection.1`
--> $SRC_DIR/core/src/result.rs:LL:COL
error: aborting due to 2 previous errors

View File

@ -59,7 +59,7 @@ LL | y.foo();
LL | println!("{}", &y);
| ^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `y`
note: `Foo::foo` takes ownership of the receiver `self`, which moves `y`
--> $DIR/borrow-after-move.rs:5:12
|
LL | fn foo(self) -> String;

View File

@ -55,7 +55,7 @@ LL | y.foo();
LL | y.foo();
| ^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `y`
note: `Foo::foo` takes ownership of the receiver `self`, which moves `y`
--> $DIR/double-move.rs:5:12
|
LL | fn foo(self) -> String;

View File

@ -8,7 +8,7 @@ LL | self.bar();
LL | return self.x;
| ^^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `self`
note: `S::bar` takes ownership of the receiver `self`, which moves `self`
--> $DIR/use-after-move-self-based-on-type.rs:15:16
|
LL | pub fn bar(self) {}

View File

@ -8,7 +8,7 @@ LL | self.bar();
LL | return *self.x;
| ^^^^^^^ value used here after move
|
note: this function takes ownership of the receiver `self`, which moves `self`
note: `S::bar` takes ownership of the receiver `self`, which moves `self`
--> $DIR/use-after-move-self.rs:13:16
|
LL | pub fn bar(self) {}

View File

@ -8,7 +8,7 @@ LL | let end = Mine{other_val:1, ..start.make_string_bar()};
LL | println!("{}", start.test);
| ^^^^^^^^^^ value borrowed here after move
|
note: this function takes ownership of the receiver `self`, which moves `start`
note: `Mine::make_string_bar` takes ownership of the receiver `self`, which moves `start`
--> $DIR/walk-struct-literal-with.rs:7:28
|
LL | fn make_string_bar(mut self) -> Mine{