Skip to content

Commit 063b167

Browse files
committed
Clarify what "this" means
1 parent 717294f commit 063b167

26 files changed

+51
-50
lines changed

compiler/rustc_borrowck/src/diagnostics/mod.rs

+15-2
Original file line numberDiff line numberDiff line change
@@ -1069,7 +1069,7 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
10691069
);
10701070
}
10711071
}
1072-
CallKind::Normal { self_arg, desugaring, is_option_or_result } => {
1072+
CallKind::Normal { self_arg, desugaring, method_did } => {
10731073
let self_arg = self_arg.unwrap();
10741074
if let Some((CallDesugaringKind::ForLoopIntoIter, _)) = desugaring {
10751075
let ty = moved_place.ty(self.body, self.infcx.tcx).ty;
@@ -1139,14 +1139,27 @@ impl<'cx, 'tcx> MirBorrowckCtxt<'cx, 'tcx> {
11391139
),
11401140
);
11411141
}
1142+
let tcx = self.infcx.tcx;
11421143
// Avoid pointing to the same function in multiple different
11431144
// error messages.
11441145
if span != DUMMY_SP && self.fn_self_span_reported.insert(self_arg.span) {
1146+
let func = tcx.def_path_str(method_did);
11451147
err.span_note(
11461148
self_arg.span,
1147-
&format!("this function takes ownership of the receiver `self`, which moves {}", place_name)
1149+
&format!("`{func}` takes ownership of the receiver `self`, which moves {place_name}")
11481150
);
11491151
}
1152+
let parent_did = tcx.parent(method_did);
1153+
let parent_self_ty = (tcx.def_kind(parent_did)
1154+
== rustc_hir::def::DefKind::Impl)
1155+
.then_some(parent_did)
1156+
.and_then(|did| match tcx.type_of(did).kind() {
1157+
ty::Adt(def, ..) => Some(def.did()),
1158+
_ => None,
1159+
});
1160+
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
1161+
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
1162+
});
11501163
if is_option_or_result && maybe_reinitialized_locations_is_empty {
11511164
err.span_label(
11521165
var_span,

compiler/rustc_const_eval/src/util/call_kind.rs

+3-15
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
use rustc_hir::def_id::DefId;
66
use rustc_hir::{lang_items, LangItem};
77
use rustc_middle::ty::subst::SubstsRef;
8-
use rustc_middle::ty::{self, AssocItemContainer, DefIdTree, Instance, ParamEnv, Ty, TyCtxt};
8+
use rustc_middle::ty::{AssocItemContainer, Instance, ParamEnv, Ty, TyCtxt};
99
use rustc_span::symbol::Ident;
1010
use rustc_span::{sym, DesugaringKind, Span};
1111

@@ -39,9 +39,7 @@ pub enum CallKind<'tcx> {
3939
Normal {
4040
self_arg: Option<Ident>,
4141
desugaring: Option<(CallDesugaringKind, Ty<'tcx>)>,
42-
/// Whether the self type of the method call has an `.as_ref()` method.
43-
/// Used for better diagnostics.
44-
is_option_or_result: bool,
42+
method_did: DefId,
4543
},
4644
/// A call to `Fn(..)::call(..)`, desugared from `my_closure(a, b, c)`
4745
FnCall { fn_trait_id: DefId, self_ty: Ty<'tcx> },
@@ -133,16 +131,6 @@ pub fn call_kind<'tcx>(
133131
} else {
134132
None
135133
};
136-
let parent_did = tcx.parent(method_did);
137-
let parent_self_ty = (tcx.def_kind(parent_did) == rustc_hir::def::DefKind::Impl)
138-
.then_some(parent_did)
139-
.and_then(|did| match tcx.type_of(did).kind() {
140-
ty::Adt(def, ..) => Some(def.did()),
141-
_ => None,
142-
});
143-
let is_option_or_result = parent_self_ty.map_or(false, |def_id| {
144-
matches!(tcx.get_diagnostic_name(def_id), Some(sym::Option | sym::Result))
145-
});
146-
CallKind::Normal { self_arg, desugaring, is_option_or_result }
134+
CallKind::Normal { self_arg, desugaring, method_did }
147135
})
148136
}

src/test/ui/borrowck/borrowck-move-out-of-overloaded-auto-deref.stderr

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

1313
error: aborting due to previous error

src/test/ui/borrowck/issue-83760.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ LL | foo = Some(Struct);
2727
LL | let _y = foo;
2828
| ^^^ value used here after move
2929
|
30-
note: this function takes ownership of the receiver `self`, which moves `foo`
30+
note: `Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `foo`
3131
--> $SRC_DIR/core/src/option.rs:LL:COL
3232

3333
error[E0382]: use of moved value: `foo`
@@ -52,7 +52,7 @@ LL | foo = Some(Struct);
5252
LL | } else if true {
5353
LL | foo = Some(Struct);
5454
| ^^^^^^^^^^^^^^^^^^
55-
note: this function takes ownership of the receiver `self`, which moves `foo`
55+
note: `Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `foo`
5656
--> $SRC_DIR/core/src/option.rs:LL:COL
5757

5858
error: aborting due to 3 previous errors

src/test/ui/borrowck/reborrow-sugg-move-then-borrow.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL |
99
LL | fill_segment(state);
1010
| ^^^^^ value borrowed here after move
1111
|
12-
note: this function takes ownership of the receiver `self`, which moves `state`
12+
note: `into_iter` takes ownership of the receiver `self`, which moves `state`
1313
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
1414
help: consider creating a fresh reborrow of `state` here
1515
|

src/test/ui/borrowck/suggest-as-ref-on-mut-closure.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | cb.map(|cb| cb());
88
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
99
| move occurs because `*cb` has type `Option<&mut dyn FnMut()>`, which does not implement the `Copy` trait
1010
|
11-
note: this function takes ownership of the receiver `self`, which moves `*cb`
11+
note: `Option::<T>::map` takes ownership of the receiver `self`, which moves `*cb`
1212
--> $SRC_DIR/core/src/option.rs:LL:COL
1313

1414
error[E0596]: cannot borrow `*cb` as mutable, as it is behind a `&` reference

src/test/ui/borrowck/unboxed-closures-move-upvar-from-non-once-ref-closure.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | y.into_iter();
1010
| |
1111
| move occurs because `y` has type `Vec<String>`, which does not implement the `Copy` trait
1212
|
13-
note: this function takes ownership of the receiver `self`, which moves `y`
13+
note: `into_iter` takes ownership of the receiver `self`, which moves `y`
1414
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
1515

1616
error: aborting due to previous error

src/test/ui/codemap_tests/tab_3.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ LL | {
99
LL | println!("{:?}", some_vec);
1010
| ^^^^^^^^ value borrowed here after move
1111
|
12-
note: this function takes ownership of the receiver `self`, which moves `some_vec`
12+
note: `into_iter` takes ownership of the receiver `self`, which moves `some_vec`
1313
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
1414
= 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)
1515
help: consider cloning the value if the performance cost is acceptable

src/test/ui/error-codes/E0507.stderr

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

src/test/ui/issues/issue-34721.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ LL | };
1313
LL | x.zero()
1414
| ^ value used here after move
1515
|
16-
note: this function takes ownership of the receiver `self`, which moves `x`
16+
note: `Foo::zero` takes ownership of the receiver `self`, which moves `x`
1717
--> $DIR/issue-34721.rs:4:13
1818
|
1919
LL | fn zero(self) -> Self;

src/test/ui/issues/issue-61108.stderr

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

src/test/ui/issues/issue-64559.stderr

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

src/test/ui/issues/issue-83924.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | for n in v {
1010
LL | for n in v {
1111
| ^ value used here after move
1212
|
13-
note: this function takes ownership of the receiver `self`, which moves `v`
13+
note: `into_iter` takes ownership of the receiver `self`, which moves `v`
1414
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
1515
help: consider creating a fresh reborrow of `v` here
1616
|

src/test/ui/loops/issue-82916.stderr

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

src/test/ui/moves/move-fn-self-receiver.stderr

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | val.0.into_iter().next();
66
LL | val.0;
77
| ^^^^^ value used here after move
88
|
9-
note: this function takes ownership of the receiver `self`, which moves `val.0`
9+
note: `into_iter` takes ownership of the receiver `self`, which moves `val.0`
1010
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
1111
= note: move occurs because `val.0` has type `Vec<bool>`, which does not implement the `Copy` trait
1212

@@ -20,7 +20,7 @@ LL | foo.use_self();
2020
LL | foo;
2121
| ^^^ value used here after move
2222
|
23-
note: this function takes ownership of the receiver `self`, which moves `foo`
23+
note: `Foo::use_self` takes ownership of the receiver `self`, which moves `foo`
2424
--> $DIR/move-fn-self-receiver.rs:13:17
2525
|
2626
LL | fn use_self(self) {}
@@ -46,7 +46,7 @@ LL | boxed_foo.use_box_self();
4646
LL | boxed_foo;
4747
| ^^^^^^^^^ value used here after move
4848
|
49-
note: this function takes ownership of the receiver `self`, which moves `boxed_foo`
49+
note: `Foo::use_box_self` takes ownership of the receiver `self`, which moves `boxed_foo`
5050
--> $DIR/move-fn-self-receiver.rs:14:21
5151
|
5252
LL | fn use_box_self(self: Box<Self>) {}
@@ -62,7 +62,7 @@ LL | pin_box_foo.use_pin_box_self();
6262
LL | pin_box_foo;
6363
| ^^^^^^^^^^^ value used here after move
6464
|
65-
note: this function takes ownership of the receiver `self`, which moves `pin_box_foo`
65+
note: `Foo::use_pin_box_self` takes ownership of the receiver `self`, which moves `pin_box_foo`
6666
--> $DIR/move-fn-self-receiver.rs:15:25
6767
|
6868
LL | fn use_pin_box_self(self: Pin<Box<Self>>) {}
@@ -88,7 +88,7 @@ LL | rc_foo.use_rc_self();
8888
LL | rc_foo;
8989
| ^^^^^^ value used here after move
9090
|
91-
note: this function takes ownership of the receiver `self`, which moves `rc_foo`
91+
note: `Foo::use_rc_self` takes ownership of the receiver `self`, which moves `rc_foo`
9292
--> $DIR/move-fn-self-receiver.rs:16:20
9393
|
9494
LL | fn use_rc_self(self: Rc<Self>) {}
@@ -154,7 +154,7 @@ LL | for _val in container.custom_into_iter() {}
154154
LL | container;
155155
| ^^^^^^^^^ value used here after move
156156
|
157-
note: this function takes ownership of the receiver `self`, which moves `container`
157+
note: `Container::custom_into_iter` takes ownership of the receiver `self`, which moves `container`
158158
--> $DIR/move-fn-self-receiver.rs:23:25
159159
|
160160
LL | fn custom_into_iter(self) -> impl Iterator<Item = bool> {

src/test/ui/moves/moves-based-on-type-access-to-field.stderr

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

src/test/ui/moves/moves-based-on-type-exprs.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ LL | let _y = x.into_iter().next().unwrap();
160160
LL | touch(&x);
161161
| ^^ value borrowed here after move
162162
|
163-
note: this function takes ownership of the receiver `self`, which moves `x`
163+
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
164164
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
165165
help: consider cloning the value if the performance cost is acceptable
166166
|
@@ -177,7 +177,7 @@ LL | let _y = [x.into_iter().next().unwrap(); 1];
177177
LL | touch(&x);
178178
| ^^ value borrowed here after move
179179
|
180-
note: this function takes ownership of the receiver `self`, which moves `x`
180+
note: `into_iter` takes ownership of the receiver `self`, which moves `x`
181181
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
182182
help: consider cloning the value if the performance cost is acceptable
183183
|

src/test/ui/suggestions/as-ref-2.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ LL | let _x: Option<Struct> = foo.map(|s| bar(&s));
1010
LL | let _y = foo;
1111
| ^^^ value used here after move
1212
|
13-
note: this function takes ownership of the receiver `self`, which moves `foo`
13+
note: `Option::<T>::map` takes ownership of the receiver `self`, which moves `foo`
1414
--> $SRC_DIR/core/src/option.rs:LL:COL
1515

1616
error: aborting due to previous error

src/test/ui/suggestions/borrow-for-loop-head.stderr

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

src/test/ui/suggestions/for-i-in-vec.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | for _ in self.v {
77
| `self.v` moved due to this implicit call to `.into_iter()`
88
| move occurs because `self.v` has type `Vec<u32>`, which does not implement the `Copy` trait
99
|
10-
note: this function takes ownership of the receiver `self`, which moves `self.v`
10+
note: `into_iter` takes ownership of the receiver `self`, which moves `self.v`
1111
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
1212
help: consider iterating over a slice of the `Vec<u32>`'s content to avoid moving into the `for` loop
1313
|
@@ -37,7 +37,7 @@ LL | for loader in *LOADERS {
3737
| value moved due to this implicit call to `.into_iter()`
3838
| move occurs because value has type `Vec<&u8>`, which does not implement the `Copy` trait
3939
|
40-
note: this function takes ownership of the receiver `self`, which moves value
40+
note: `into_iter` takes ownership of the receiver `self`, which moves value
4141
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
4242
help: consider iterating over a slice of the `Vec<&u8>`'s content to avoid moving into the `for` loop
4343
|

src/test/ui/suggestions/option-content-move.stderr

+2-2
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ LL | if selection.1.unwrap().contains(selection.0) {
77
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
88
| move occurs because `selection.1` has type `Option<String>`, which does not implement the `Copy` trait
99
|
10-
note: this function takes ownership of the receiver `self`, which moves `selection.1`
10+
note: `Option::<T>::unwrap` takes ownership of the receiver `self`, which moves `selection.1`
1111
--> $SRC_DIR/core/src/option.rs:LL:COL
1212

1313
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) {
1919
| help: consider calling `.as_ref()` or `.as_mut()` to borrow the type's contents
2020
| move occurs because `selection.1` has type `Result<String, String>`, which does not implement the `Copy` trait
2121
|
22-
note: this function takes ownership of the receiver `self`, which moves `selection.1`
22+
note: `Result::<T, E>::unwrap` takes ownership of the receiver `self`, which moves `selection.1`
2323
--> $SRC_DIR/core/src/result.rs:LL:COL
2424

2525
error: aborting due to 2 previous errors

src/test/ui/unsized-locals/borrow-after-move.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ LL | y.foo();
5959
LL | println!("{}", &y);
6060
| ^^ value borrowed here after move
6161
|
62-
note: this function takes ownership of the receiver `self`, which moves `y`
62+
note: `Foo::foo` takes ownership of the receiver `self`, which moves `y`
6363
--> $DIR/borrow-after-move.rs:5:12
6464
|
6565
LL | fn foo(self) -> String;

src/test/ui/unsized-locals/double-move.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ LL | y.foo();
5555
LL | y.foo();
5656
| ^ value used here after move
5757
|
58-
note: this function takes ownership of the receiver `self`, which moves `y`
58+
note: `Foo::foo` takes ownership of the receiver `self`, which moves `y`
5959
--> $DIR/double-move.rs:5:12
6060
|
6161
LL | fn foo(self) -> String;

src/test/ui/use/use-after-move-self-based-on-type.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | self.bar();
88
LL | return self.x;
99
| ^^^^^^ value used here after move
1010
|
11-
note: this function takes ownership of the receiver `self`, which moves `self`
11+
note: `S::bar` takes ownership of the receiver `self`, which moves `self`
1212
--> $DIR/use-after-move-self-based-on-type.rs:15:16
1313
|
1414
LL | pub fn bar(self) {}

src/test/ui/use/use-after-move-self.stderr

+1-1
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ LL | self.bar();
88
LL | return *self.x;
99
| ^^^^^^^ value used here after move
1010
|
11-
note: this function takes ownership of the receiver `self`, which moves `self`
11+
note: `S::bar` takes ownership of the receiver `self`, which moves `self`
1212
--> $DIR/use-after-move-self.rs:13:16
1313
|
1414
LL | pub fn bar(self) {}

src/test/ui/walk-struct-literal-with.stderr

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

0 commit comments

Comments
 (0)