From 201c6223dfcdc1738ee3643ff93f6b893aebc1f5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Le=C3=B3n=20Orell=20Valerian=20Liehr?= Date: Sun, 11 Feb 2024 21:28:28 +0100 Subject: [PATCH] Move a diagnostic method to hir_ty_lowering::errors --- .../src/hir_ty_lowering/errors.rs | 112 ++++++++++++++++++ .../src/hir_ty_lowering/mod.rs | 112 ------------------ 2 files changed, 112 insertions(+), 112 deletions(-) diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs index 425486753d16..d606d6997a33 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs @@ -344,6 +344,118 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o { }) } + pub(super) fn report_ambiguous_assoc_type( + &self, + span: Span, + types: &[String], + traits: &[String], + name: Symbol, + ) -> ErrorGuaranteed { + let mut err = + struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type"); + if self + .tcx() + .resolutions(()) + .confused_type_with_std_module + .keys() + .any(|full_span| full_span.contains(span)) + { + err.span_suggestion_verbose( + span.shrink_to_lo(), + "you are looking for the module in `std`, not the primitive type", + "std::", + Applicability::MachineApplicable, + ); + } else { + let mut types = types.to_vec(); + types.sort(); + let mut traits = traits.to_vec(); + traits.sort(); + match (&types[..], &traits[..]) { + ([], []) => { + err.span_suggestion_verbose( + span, + format!( + "if there were a type named `Type` that implements a trait named \ + `Trait` with associated type `{name}`, you could use the \ + fully-qualified path", + ), + format!("::{name}"), + Applicability::HasPlaceholders, + ); + } + ([], [trait_str]) => { + err.span_suggestion_verbose( + span, + format!( + "if there were a type named `Example` that implemented `{trait_str}`, \ + you could use the fully-qualified path", + ), + format!("::{name}"), + Applicability::HasPlaceholders, + ); + } + ([], traits) => { + err.span_suggestions( + span, + format!( + "if there were a type named `Example` that implemented one of the \ + traits with associated type `{name}`, you could use the \ + fully-qualified path", + ), + traits + .iter() + .map(|trait_str| format!("::{name}")) + .collect::>(), + Applicability::HasPlaceholders, + ); + } + ([type_str], []) => { + err.span_suggestion_verbose( + span, + format!( + "if there were a trait named `Example` with associated type `{name}` \ + implemented for `{type_str}`, you could use the fully-qualified path", + ), + format!("<{type_str} as Example>::{name}"), + Applicability::HasPlaceholders, + ); + } + (types, []) => { + err.span_suggestions( + span, + format!( + "if there were a trait named `Example` with associated type `{name}` \ + implemented for one of the types, you could use the fully-qualified \ + path", + ), + types + .into_iter() + .map(|type_str| format!("<{type_str} as Example>::{name}")), + Applicability::HasPlaceholders, + ); + } + (types, traits) => { + let mut suggestions = vec![]; + for type_str in types { + for trait_str in traits { + suggestions.push(format!("<{type_str} as {trait_str}>::{name}")); + } + } + err.span_suggestions( + span, + "use fully-qualified syntax", + suggestions, + Applicability::MachineApplicable, + ); + } + } + } + let reported = err.emit(); + self.set_tainted_by_errors(reported); + reported + } + pub(crate) fn complain_about_ambiguous_inherent_assoc_type( &self, name: Ident, diff --git a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs index b546b44f0478..4c4490f2b478 100644 --- a/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs +++ b/compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs @@ -868,118 +868,6 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o { } } - fn report_ambiguous_assoc_type( - &self, - span: Span, - types: &[String], - traits: &[String], - name: Symbol, - ) -> ErrorGuaranteed { - let mut err = - struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type"); - if self - .tcx() - .resolutions(()) - .confused_type_with_std_module - .keys() - .any(|full_span| full_span.contains(span)) - { - err.span_suggestion_verbose( - span.shrink_to_lo(), - "you are looking for the module in `std`, not the primitive type", - "std::", - Applicability::MachineApplicable, - ); - } else { - let mut types = types.to_vec(); - types.sort(); - let mut traits = traits.to_vec(); - traits.sort(); - match (&types[..], &traits[..]) { - ([], []) => { - err.span_suggestion_verbose( - span, - format!( - "if there were a type named `Type` that implements a trait named \ - `Trait` with associated type `{name}`, you could use the \ - fully-qualified path", - ), - format!("::{name}"), - Applicability::HasPlaceholders, - ); - } - ([], [trait_str]) => { - err.span_suggestion_verbose( - span, - format!( - "if there were a type named `Example` that implemented `{trait_str}`, \ - you could use the fully-qualified path", - ), - format!("::{name}"), - Applicability::HasPlaceholders, - ); - } - ([], traits) => { - err.span_suggestions( - span, - format!( - "if there were a type named `Example` that implemented one of the \ - traits with associated type `{name}`, you could use the \ - fully-qualified path", - ), - traits - .iter() - .map(|trait_str| format!("::{name}")) - .collect::>(), - Applicability::HasPlaceholders, - ); - } - ([type_str], []) => { - err.span_suggestion_verbose( - span, - format!( - "if there were a trait named `Example` with associated type `{name}` \ - implemented for `{type_str}`, you could use the fully-qualified path", - ), - format!("<{type_str} as Example>::{name}"), - Applicability::HasPlaceholders, - ); - } - (types, []) => { - err.span_suggestions( - span, - format!( - "if there were a trait named `Example` with associated type `{name}` \ - implemented for one of the types, you could use the fully-qualified \ - path", - ), - types - .into_iter() - .map(|type_str| format!("<{type_str} as Example>::{name}")), - Applicability::HasPlaceholders, - ); - } - (types, traits) => { - let mut suggestions = vec![]; - for type_str in types { - for trait_str in traits { - suggestions.push(format!("<{type_str} as {trait_str}>::{name}")); - } - } - err.span_suggestions( - span, - "use fully-qualified syntax", - suggestions, - Applicability::MachineApplicable, - ); - } - } - } - let reported = err.emit(); - self.set_tainted_by_errors(reported); - reported - } - /// Search for a bound on a type parameter which includes the associated item given by `assoc_name`. /// /// `ty_param_def_id` is the `DefId` of the type parameter.