X-Git-Url: https://git.lizzy.rs/?a=blobdiff_plain;f=compiler%2Frustc_hir_typeck%2Fsrc%2Fmethod%2Fsuggest.rs;h=8190163cba15b4bb8fde3ae5d1f86f3aa2c0439b;hb=9cdfe03b0601d5328406746a7923a8a4eaa0cf3c;hp=e2c5edd0e8833bfa32936b65faa2e1c748d6ab69;hpb=ed97f245f1a15f0ba22a622fb693ac8bdbdbed0c;p=rust.git diff --git a/compiler/rustc_hir_typeck/src/method/suggest.rs b/compiler/rustc_hir_typeck/src/method/suggest.rs index e2c5edd0e88..8190163cba1 100644 --- a/compiler/rustc_hir_typeck/src/method/suggest.rs +++ b/compiler/rustc_hir_typeck/src/method/suggest.rs @@ -114,7 +114,7 @@ pub fn report_method_error( let report_candidates = |span: Span, err: &mut Diagnostic, sources: &mut Vec, - sugg_span: Span| { + sugg_span: Option| { sources.sort(); sources.dedup(); // Dynamic limit to avoid hiding just one candidate, which is silly. @@ -175,7 +175,8 @@ pub fn report_method_error( } else { err.note(¬e_str); } - if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) { + if let Some(sugg_span) = sugg_span + && let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) { let path = self.tcx.def_path_str(trait_ref.def_id); let ty = match item.kind { @@ -224,20 +225,22 @@ pub fn report_method_error( err.span_note(item_span, msg); None }; - let path = self.tcx.def_path_str(trait_did); - print_disambiguation_help( - item_name, - args, - err, - path, - rcvr_ty, - item.kind, - item.def_id, - sugg_span, - idx, - self.tcx.sess.source_map(), - item.fn_has_self_parameter, - ); + if let Some(sugg_span) = sugg_span { + let path = self.tcx.def_path_str(trait_did); + print_disambiguation_help( + item_name, + args, + err, + path, + rcvr_ty, + item.kind, + item.def_id, + sugg_span, + idx, + self.tcx.sess.source_map(), + item.fn_has_self_parameter, + ); + } } } } @@ -407,9 +410,9 @@ pub fn report_method_error( sugg_span, ); - report_candidates(span, &mut err, &mut static_candidates, sugg_span); + report_candidates(span, &mut err, &mut static_candidates, None); } else if static_candidates.len() > 1 { - report_candidates(span, &mut err, &mut static_candidates, sugg_span); + report_candidates(span, &mut err, &mut static_candidates, Some(sugg_span)); } let mut bound_spans = vec![]; @@ -818,10 +821,10 @@ trait bound{s}", ty.is_str() || matches!( ty.kind(), - ty::Adt(adt, _) if self.tcx.is_diagnostic_item(sym::String, adt.did()) + ty::Adt(adt, _) if Some(adt.did()) == self.tcx.lang_items().string() ) } - ty::Adt(adt, _) => self.tcx.is_diagnostic_item(sym::String, adt.did()), + ty::Adt(adt, _) => Some(adt.did()) == self.tcx.lang_items().string(), _ => false, }; if is_string_or_ref_str && item_name.name == sym::iter { @@ -1015,7 +1018,7 @@ trait bound{s}", ); err.span_label(item_name.span, format!("multiple `{}` found", item_name)); - report_candidates(span, &mut err, &mut sources, sugg_span); + report_candidates(span, &mut err, &mut sources, Some(sugg_span)); err.emit(); } @@ -1915,12 +1918,12 @@ fn check_for_deref_method( | ty::Str | ty::Projection(_) | ty::Param(_) => format!("{deref_ty}"), - // we need to test something like <&[_]>::len + // we need to test something like <&[_]>::len or <(&[u32])>::len // and Vec::function(); - // <&[_]>::len doesn't need an extra "<>" between + // <&[_]>::len or <&[u32]>::len doesn't need an extra "<>" between // but for Adt type like Vec::function() // we would suggest <[_]>::function(); - _ if self.tcx.sess.source_map().span_wrapped_by_angle_bracket(ty.span) => format!("{deref_ty}"), + _ if self.tcx.sess.source_map().span_wrapped_by_angle_or_parentheses(ty.span) => format!("{deref_ty}"), _ => format!("<{deref_ty}>"), }; err.span_suggestion_verbose(