X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_borrowck%2Fsrc%2Fdiagnostics%2Fmod.rs;h=cbd590052008c14a46f3ab0b099ea517ef6f7ce9;hb=b8d71fc41fe3476da0e4857294583eff271aa625;hp=4e2271a30672270a5ff8c49c1b4460ab8c11938b;hpb=71ec1457ee9868a838e4521a3510cdd416c0c295;p=rust.git diff --git a/compiler/rustc_borrowck/src/diagnostics/mod.rs b/compiler/rustc_borrowck/src/diagnostics/mod.rs index 4e2271a3067..cbd59005200 100644 --- a/compiler/rustc_borrowck/src/diagnostics/mod.rs +++ b/compiler/rustc_borrowck/src/diagnostics/mod.rs @@ -1059,17 +1059,12 @@ fn explain_captures( ); if self.fn_self_span_reported.insert(fn_span) { err.span_note( - // Check whether the source is accessible - if self.infcx.tcx.sess.source_map().is_span_accessible(self_arg.span) { - self_arg.span - } else { - fn_call_span - }, + self_arg.span, "calling this operator moves the left-hand side", ); } } - 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 +1134,27 @@ fn explain_captures( ), ); } + 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,