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

Lines changed: 15 additions & 2 deletions
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

Lines changed: 3 additions & 15 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 2 additions & 2 deletions
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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

Lines changed: 1 addition & 1 deletion
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;

0 commit comments

Comments
 (0)