Skip to content

Commit 201c622

Browse files
committed
Move a diagnostic method to hir_ty_lowering::errors
1 parent 364ad62 commit 201c622

File tree

2 files changed

+112
-112
lines changed

2 files changed

+112
-112
lines changed

compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs

+112
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,118 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
344344
})
345345
}
346346

347+
pub(super) fn report_ambiguous_assoc_type(
348+
&self,
349+
span: Span,
350+
types: &[String],
351+
traits: &[String],
352+
name: Symbol,
353+
) -> ErrorGuaranteed {
354+
let mut err =
355+
struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type");
356+
if self
357+
.tcx()
358+
.resolutions(())
359+
.confused_type_with_std_module
360+
.keys()
361+
.any(|full_span| full_span.contains(span))
362+
{
363+
err.span_suggestion_verbose(
364+
span.shrink_to_lo(),
365+
"you are looking for the module in `std`, not the primitive type",
366+
"std::",
367+
Applicability::MachineApplicable,
368+
);
369+
} else {
370+
let mut types = types.to_vec();
371+
types.sort();
372+
let mut traits = traits.to_vec();
373+
traits.sort();
374+
match (&types[..], &traits[..]) {
375+
([], []) => {
376+
err.span_suggestion_verbose(
377+
span,
378+
format!(
379+
"if there were a type named `Type` that implements a trait named \
380+
`Trait` with associated type `{name}`, you could use the \
381+
fully-qualified path",
382+
),
383+
format!("<Type as Trait>::{name}"),
384+
Applicability::HasPlaceholders,
385+
);
386+
}
387+
([], [trait_str]) => {
388+
err.span_suggestion_verbose(
389+
span,
390+
format!(
391+
"if there were a type named `Example` that implemented `{trait_str}`, \
392+
you could use the fully-qualified path",
393+
),
394+
format!("<Example as {trait_str}>::{name}"),
395+
Applicability::HasPlaceholders,
396+
);
397+
}
398+
([], traits) => {
399+
err.span_suggestions(
400+
span,
401+
format!(
402+
"if there were a type named `Example` that implemented one of the \
403+
traits with associated type `{name}`, you could use the \
404+
fully-qualified path",
405+
),
406+
traits
407+
.iter()
408+
.map(|trait_str| format!("<Example as {trait_str}>::{name}"))
409+
.collect::<Vec<_>>(),
410+
Applicability::HasPlaceholders,
411+
);
412+
}
413+
([type_str], []) => {
414+
err.span_suggestion_verbose(
415+
span,
416+
format!(
417+
"if there were a trait named `Example` with associated type `{name}` \
418+
implemented for `{type_str}`, you could use the fully-qualified path",
419+
),
420+
format!("<{type_str} as Example>::{name}"),
421+
Applicability::HasPlaceholders,
422+
);
423+
}
424+
(types, []) => {
425+
err.span_suggestions(
426+
span,
427+
format!(
428+
"if there were a trait named `Example` with associated type `{name}` \
429+
implemented for one of the types, you could use the fully-qualified \
430+
path",
431+
),
432+
types
433+
.into_iter()
434+
.map(|type_str| format!("<{type_str} as Example>::{name}")),
435+
Applicability::HasPlaceholders,
436+
);
437+
}
438+
(types, traits) => {
439+
let mut suggestions = vec![];
440+
for type_str in types {
441+
for trait_str in traits {
442+
suggestions.push(format!("<{type_str} as {trait_str}>::{name}"));
443+
}
444+
}
445+
err.span_suggestions(
446+
span,
447+
"use fully-qualified syntax",
448+
suggestions,
449+
Applicability::MachineApplicable,
450+
);
451+
}
452+
}
453+
}
454+
let reported = err.emit();
455+
self.set_tainted_by_errors(reported);
456+
reported
457+
}
458+
347459
pub(crate) fn complain_about_ambiguous_inherent_assoc_type(
348460
&self,
349461
name: Ident,

compiler/rustc_hir_analysis/src/hir_ty_lowering/mod.rs

-112
Original file line numberDiff line numberDiff line change
@@ -868,118 +868,6 @@ impl<'o, 'tcx> dyn HirTyLowerer<'tcx> + 'o {
868868
}
869869
}
870870

871-
fn report_ambiguous_assoc_type(
872-
&self,
873-
span: Span,
874-
types: &[String],
875-
traits: &[String],
876-
name: Symbol,
877-
) -> ErrorGuaranteed {
878-
let mut err =
879-
struct_span_code_err!(self.tcx().dcx(), span, E0223, "ambiguous associated type");
880-
if self
881-
.tcx()
882-
.resolutions(())
883-
.confused_type_with_std_module
884-
.keys()
885-
.any(|full_span| full_span.contains(span))
886-
{
887-
err.span_suggestion_verbose(
888-
span.shrink_to_lo(),
889-
"you are looking for the module in `std`, not the primitive type",
890-
"std::",
891-
Applicability::MachineApplicable,
892-
);
893-
} else {
894-
let mut types = types.to_vec();
895-
types.sort();
896-
let mut traits = traits.to_vec();
897-
traits.sort();
898-
match (&types[..], &traits[..]) {
899-
([], []) => {
900-
err.span_suggestion_verbose(
901-
span,
902-
format!(
903-
"if there were a type named `Type` that implements a trait named \
904-
`Trait` with associated type `{name}`, you could use the \
905-
fully-qualified path",
906-
),
907-
format!("<Type as Trait>::{name}"),
908-
Applicability::HasPlaceholders,
909-
);
910-
}
911-
([], [trait_str]) => {
912-
err.span_suggestion_verbose(
913-
span,
914-
format!(
915-
"if there were a type named `Example` that implemented `{trait_str}`, \
916-
you could use the fully-qualified path",
917-
),
918-
format!("<Example as {trait_str}>::{name}"),
919-
Applicability::HasPlaceholders,
920-
);
921-
}
922-
([], traits) => {
923-
err.span_suggestions(
924-
span,
925-
format!(
926-
"if there were a type named `Example` that implemented one of the \
927-
traits with associated type `{name}`, you could use the \
928-
fully-qualified path",
929-
),
930-
traits
931-
.iter()
932-
.map(|trait_str| format!("<Example as {trait_str}>::{name}"))
933-
.collect::<Vec<_>>(),
934-
Applicability::HasPlaceholders,
935-
);
936-
}
937-
([type_str], []) => {
938-
err.span_suggestion_verbose(
939-
span,
940-
format!(
941-
"if there were a trait named `Example` with associated type `{name}` \
942-
implemented for `{type_str}`, you could use the fully-qualified path",
943-
),
944-
format!("<{type_str} as Example>::{name}"),
945-
Applicability::HasPlaceholders,
946-
);
947-
}
948-
(types, []) => {
949-
err.span_suggestions(
950-
span,
951-
format!(
952-
"if there were a trait named `Example` with associated type `{name}` \
953-
implemented for one of the types, you could use the fully-qualified \
954-
path",
955-
),
956-
types
957-
.into_iter()
958-
.map(|type_str| format!("<{type_str} as Example>::{name}")),
959-
Applicability::HasPlaceholders,
960-
);
961-
}
962-
(types, traits) => {
963-
let mut suggestions = vec![];
964-
for type_str in types {
965-
for trait_str in traits {
966-
suggestions.push(format!("<{type_str} as {trait_str}>::{name}"));
967-
}
968-
}
969-
err.span_suggestions(
970-
span,
971-
"use fully-qualified syntax",
972-
suggestions,
973-
Applicability::MachineApplicable,
974-
);
975-
}
976-
}
977-
}
978-
let reported = err.emit();
979-
self.set_tainted_by_errors(reported);
980-
reported
981-
}
982-
983871
/// Search for a bound on a type parameter which includes the associated item given by `assoc_name`.
984872
///
985873
/// `ty_param_def_id` is the `DefId` of the type parameter.

0 commit comments

Comments
 (0)