Skip to main content

rustc_hir_analysis/hir_ty_lowering/
errors.rs

1use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
2use rustc_data_structures::sorted_map::SortedMap;
3use rustc_data_structures::unord::UnordMap;
4use rustc_errors::codes::*;
5use rustc_errors::{
6    Applicability, Diag, ErrorGuaranteed, MultiSpan, SuggestionStyle, listify, msg, pluralize,
7    struct_span_code_err,
8};
9use rustc_hir::def::{CtorOf, DefKind, Res};
10use rustc_hir::def_id::DefId;
11use rustc_hir::{self as hir, HirId};
12use rustc_middle::bug;
13use rustc_middle::ty::fast_reject::{TreatParams, simplify_type};
14use rustc_middle::ty::print::{PrintPolyTraitRefExt as _, PrintTraitRefExt as _};
15use rustc_middle::ty::{
16    self, AdtDef, GenericParamDefKind, Ty, TyCtxt, TypeVisitableExt,
17    suggest_constraining_type_param,
18};
19use rustc_session::errors::feature_err;
20use rustc_span::edit_distance::find_best_match_for_name;
21use rustc_span::{BytePos, DUMMY_SP, Ident, Span, Symbol, kw, sym};
22use rustc_trait_selection::error_reporting::traits::report_dyn_incompatibility;
23use rustc_trait_selection::traits::{
24    FulfillmentError, dyn_compatibility_violations_for_assoc_item,
25};
26use smallvec::SmallVec;
27use tracing::debug;
28
29use super::InherentAssocCandidate;
30use crate::diagnostics::{
31    self, AssocItemConstraintsNotAllowedHere, ManualImplementation, ParenthesizedFnTraitExpansion,
32    TraitObjectDeclaredWithNoTraits,
33};
34use crate::hir_ty_lowering::{AssocItemQSelf, HirTyLowerer};
35
36impl<'tcx> dyn HirTyLowerer<'tcx> + '_ {
37    pub(crate) fn report_missing_generic_params(
38        &self,
39        missing_generic_params: Vec<(Symbol, ty::GenericParamDefKind)>,
40        def_id: DefId,
41        span: Span,
42        empty_generic_args: bool,
43    ) {
44        if missing_generic_params.is_empty() {
45            return;
46        }
47
48        self.dcx().emit_err(diagnostics::MissingGenericParams {
49            span,
50            def_span: self.tcx().def_span(def_id),
51            span_snippet: self.tcx().sess.source_map().span_to_snippet(span).ok(),
52            missing_generic_params,
53            empty_generic_args,
54        });
55    }
56
57    /// When the code is using the `Fn` traits directly, instead of the `Fn(A) -> B` syntax, emit
58    /// an error and attempt to build a reasonable structured suggestion.
59    pub(crate) fn report_internal_fn_trait(
60        &self,
61        span: Span,
62        trait_def_id: DefId,
63        trait_segment: &'_ hir::PathSegment<'_>,
64        is_impl: bool,
65    ) {
66        if self.tcx().features().unboxed_closures() {
67            return;
68        }
69
70        let trait_def = self.tcx().trait_def(trait_def_id);
71        if !trait_def.paren_sugar {
72            if trait_segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar {
73                // For now, require that parenthetical notation be used only with `Fn()` etc.
74                feature_err(
75                    &self.tcx().sess,
76                    sym::unboxed_closures,
77                    span,
78                    "parenthetical notation is only stable when used with `Fn`-family traits",
79                )
80                .emit();
81            }
82
83            return;
84        }
85
86        let sess = self.tcx().sess;
87
88        if trait_segment.args().parenthesized != hir::GenericArgsParentheses::ParenSugar {
89            // For now, require that parenthetical notation be used only with `Fn()` etc.
90            let mut err = feature_err(
91                sess,
92                sym::unboxed_closures,
93                span,
94                "the precise format of `Fn`-family traits' type parameters is subject to change",
95            );
96            // Do not suggest the other syntax if we are in trait impl:
97            // the desugaring would contain an associated type constraint.
98            if !is_impl {
99                err.span_suggestion(
100                    span,
101                    "use parenthetical notation instead",
102                    fn_trait_to_string(self.tcx(), trait_segment, true),
103                    Applicability::MaybeIncorrect,
104                );
105            }
106            err.emit();
107        }
108
109        if is_impl {
110            let trait_name = self.tcx().def_path_str(trait_def_id);
111            self.dcx().emit_err(ManualImplementation { span, trait_name });
112        }
113    }
114
115    pub(super) fn report_unresolved_assoc_item<I>(
116        &self,
117        all_candidates: impl Fn() -> I,
118        qself: AssocItemQSelf,
119        assoc_tag: ty::AssocTag,
120        assoc_ident: Ident,
121        span: Span,
122        constraint: Option<&hir::AssocItemConstraint<'tcx>>,
123    ) -> ErrorGuaranteed
124    where
125        I: Iterator<Item = ty::PolyTraitRef<'tcx>>,
126    {
127        let tcx = self.tcx();
128
129        // First and foremost, provide a more user-friendly & “intuitive” error on kind mismatches.
130        if let Some(assoc_item) = all_candidates().find_map(|r| {
131            tcx.associated_items(r.def_id())
132                .filter_by_name_unhygienic(assoc_ident.name)
133                .find(|item| tcx.hygienic_eq(assoc_ident, item.ident(tcx), r.def_id()))
134        }) {
135            return self.report_assoc_kind_mismatch(
136                assoc_item,
137                assoc_tag,
138                assoc_ident,
139                span,
140                constraint,
141            );
142        }
143
144        let assoc_kind = assoc_tag_str(assoc_tag);
145        let qself_str = qself.to_string(tcx);
146
147        // The fallback span is needed because `assoc_name` might be an `Fn()`'s `Output` without a
148        // valid span, so we point at the whole path segment instead.
149        let is_dummy = assoc_ident.span == DUMMY_SP;
150
151        let mut err = diagnostics::AssocItemNotFound {
152            span: if is_dummy { span } else { assoc_ident.span },
153            assoc_ident,
154            assoc_kind,
155            qself: &qself_str,
156            label: None,
157            sugg: None,
158            // Try to get the span of the identifier within the path's syntax context
159            // (if that's different).
160            within_macro_span: assoc_ident.span.within_macro(span, tcx.sess.source_map()),
161        };
162
163        if is_dummy {
164            err.label = Some(diagnostics::AssocItemNotFoundLabel::NotFound {
165                span,
166                assoc_ident,
167                assoc_kind,
168            });
169            return self.dcx().emit_err(err);
170        }
171
172        let all_candidate_names: Vec<_> = all_candidates()
173            .flat_map(|r| tcx.associated_items(r.def_id()).in_definition_order())
174            .filter_map(|item| {
175                if !item.is_impl_trait_in_trait() && item.tag() == assoc_tag {
176                    item.opt_name()
177                } else {
178                    None
179                }
180            })
181            .collect();
182
183        if let Some(suggested_name) =
184            find_best_match_for_name(&all_candidate_names, assoc_ident.name, None)
185        {
186            err.sugg = Some(diagnostics::AssocItemNotFoundSugg::Similar {
187                span: assoc_ident.span,
188                assoc_kind,
189                suggested_name,
190            });
191            return self.dcx().emit_err(err);
192        }
193
194        // If we didn't find a good item in the supertraits (or couldn't get
195        // the supertraits), like in ItemCtxt, then look more generally from
196        // all visible traits. If there's one clear winner, just suggest that.
197
198        let visible_traits: Vec<_> = tcx
199            .visible_traits()
200            .filter(|trait_def_id| {
201                let viz = tcx.visibility(*trait_def_id);
202                let def_id = self.item_def_id();
203                viz.is_accessible_from(def_id, tcx)
204            })
205            .collect();
206
207        let wider_candidate_names: Vec<_> = visible_traits
208            .iter()
209            .flat_map(|trait_def_id| tcx.associated_items(*trait_def_id).in_definition_order())
210            .filter_map(|item| {
211                (!item.is_impl_trait_in_trait() && item.tag() == assoc_tag).then(|| item.name())
212            })
213            .collect();
214
215        if let Some(suggested_name) =
216            find_best_match_for_name(&wider_candidate_names, assoc_ident.name, None)
217        {
218            if let [best_trait] = visible_traits
219                .iter()
220                .copied()
221                .filter(|&trait_def_id| {
222                    tcx.associated_items(trait_def_id)
223                        .filter_by_name_unhygienic(suggested_name)
224                        .any(|item| item.tag() == assoc_tag)
225                })
226                .collect::<Vec<_>>()[..]
227            {
228                let trait_name = tcx.def_path_str(best_trait);
229                err.label = Some(diagnostics::AssocItemNotFoundLabel::FoundInOtherTrait {
230                    span: assoc_ident.span,
231                    assoc_kind,
232                    trait_name: &trait_name,
233                    suggested_name,
234                    identically_named: suggested_name == assoc_ident.name,
235                });
236                if let AssocItemQSelf::TyParam(ty_param_def_id, ty_param_span) = qself
237                    // Not using `self.item_def_id()` here as that would yield the opaque type itself if we're
238                    // inside an opaque type while we're interested in the overarching type alias (TAIT).
239                    // FIXME: However, for trait aliases, this incorrectly returns the enclosing module...
240                    && let item_def_id =
241                        tcx.hir_get_parent_item(tcx.local_def_id_to_hir_id(ty_param_def_id))
242                    // FIXME: ...which obviously won't have any generics.
243                    && let Some(generics) = tcx.hir_get_generics(item_def_id.def_id)
244                {
245                    // FIXME: Suggest adding supertrait bounds if we have a `Self` type param.
246                    // FIXME(trait_alias): Suggest adding `Self: Trait` to
247                    // `trait Alias = where Self::Proj:;` with `trait Trait { type Proj; }`.
248                    if generics
249                        .bounds_for_param(ty_param_def_id)
250                        .flat_map(|pred| pred.bounds.iter())
251                        .any(|b| match b {
252                            hir::GenericBound::Trait(t, ..) => {
253                                t.trait_ref.trait_def_id() == Some(best_trait)
254                            }
255                            _ => false,
256                        })
257                    {
258                        // The type param already has a bound for `trait_name`, we just need to
259                        // change the associated item.
260                        err.sugg = Some(diagnostics::AssocItemNotFoundSugg::SimilarInOtherTrait {
261                            span: assoc_ident.span,
262                            trait_name: &trait_name,
263                            assoc_kind,
264                            suggested_name,
265                        });
266                        return self.dcx().emit_err(err);
267                    }
268
269                    let trait_args = &ty::GenericArgs::identity_for_item(tcx, best_trait)[1..];
270                    let mut trait_ref = trait_name.clone();
271                    let applicability = if let [arg, args @ ..] = trait_args {
272                        use std::fmt::Write;
273                        trait_ref.write_fmt(format_args!("</* {0}", arg))write!(trait_ref, "</* {arg}").unwrap();
274                        args.iter().try_for_each(|arg| trait_ref.write_fmt(format_args!(", {0}", arg))write!(trait_ref, ", {arg}")).unwrap();
275                        trait_ref += " */>";
276                        Applicability::HasPlaceholders
277                    } else {
278                        Applicability::MaybeIncorrect
279                    };
280
281                    let identically_named = suggested_name == assoc_ident.name;
282
283                    if let DefKind::TyAlias = tcx.def_kind(item_def_id)
284                        && !tcx.type_alias_is_lazy(item_def_id)
285                    {
286                        err.sugg =
287                            Some(diagnostics::AssocItemNotFoundSugg::SimilarInOtherTraitQPath {
288                                lo: ty_param_span.shrink_to_lo(),
289                                mi: ty_param_span.shrink_to_hi(),
290                                hi: (!identically_named).then_some(assoc_ident.span),
291                                trait_ref,
292                                identically_named,
293                                suggested_name,
294                                assoc_kind,
295                                applicability,
296                            });
297                    } else {
298                        let mut err = self.dcx().create_err(err);
299                        if suggest_constraining_type_param(
300                            tcx,
301                            generics,
302                            &mut err,
303                            &qself_str,
304                            &trait_ref,
305                            Some(best_trait),
306                            None,
307                        ) && !identically_named
308                        {
309                            // We suggested constraining a type parameter, but the associated item on it
310                            // was also not an exact match, so we also suggest changing it.
311                            err.span_suggestion_verbose(
312                                assoc_ident.span,
313                                rustc_errors::DiagMessage::Inline(std::borrow::Cow::Borrowed("...and changing the associated {$assoc_kind} name"))msg!("...and changing the associated {$assoc_kind} name"),
314                                suggested_name,
315                                Applicability::MaybeIncorrect,
316                            );
317                        }
318                        return err.emit();
319                    }
320                }
321                return self.dcx().emit_err(err);
322            }
323        }
324
325        // If we still couldn't find any associated item, and only one associated item exists,
326        // suggest using it.
327        if let [candidate_name] = all_candidate_names.as_slice() {
328            err.sugg = Some(diagnostics::AssocItemNotFoundSugg::Other {
329                span: assoc_ident.span,
330                qself: &qself_str,
331                assoc_kind,
332                suggested_name: *candidate_name,
333            });
334        } else {
335            err.label = Some(diagnostics::AssocItemNotFoundLabel::NotFound {
336                span: assoc_ident.span,
337                assoc_ident,
338                assoc_kind,
339            });
340        }
341
342        self.dcx().emit_err(err)
343    }
344
345    fn report_assoc_kind_mismatch(
346        &self,
347        assoc_item: &ty::AssocItem,
348        assoc_tag: ty::AssocTag,
349        ident: Ident,
350        span: Span,
351        constraint: Option<&hir::AssocItemConstraint<'tcx>>,
352    ) -> ErrorGuaranteed {
353        let tcx = self.tcx();
354
355        let bound_on_assoc_const_label = if let ty::AssocKind::Const { .. } = assoc_item.kind
356            && let Some(constraint) = constraint
357            && let hir::AssocItemConstraintKind::Bound { .. } = constraint.kind
358        {
359            let lo = if constraint.gen_args.span_ext.is_dummy() {
360                ident.span
361            } else {
362                constraint.gen_args.span_ext
363            };
364            Some(lo.between(span.shrink_to_hi()))
365        } else {
366            None
367        };
368
369        // FIXME(mgca): This has quite a few false positives and negatives.
370        let wrap_in_braces_sugg = if let Some(constraint) = constraint
371            && let Some(hir_ty) = constraint.ty()
372            && let ty = self.lower_ty(hir_ty)
373            && (ty.is_enum() || ty.references_error())
374            && tcx.features().min_generic_const_args()
375        {
376            Some(diagnostics::AssocKindMismatchWrapInBracesSugg {
377                lo: hir_ty.span.shrink_to_lo(),
378                hi: hir_ty.span.shrink_to_hi(),
379            })
380        } else {
381            None
382        };
383
384        // For equality constraints, we want to blame the term (RHS) instead of the item (LHS) since
385        // one can argue that that's more “intuitive” to the user.
386        let (span, expected_because_label, expected, got) = if let Some(constraint) = constraint
387            && let hir::AssocItemConstraintKind::Equality { term } = constraint.kind
388        {
389            let span = match term {
390                hir::Term::Ty(ty) => ty.span,
391                hir::Term::Const(ct) => ct.span,
392            };
393            (span, Some(ident.span), assoc_item.tag(), assoc_tag)
394        } else {
395            (ident.span, None, assoc_tag, assoc_item.tag())
396        };
397
398        self.dcx().emit_err(diagnostics::AssocKindMismatch {
399            span,
400            expected: assoc_tag_str(expected),
401            got: assoc_tag_str(got),
402            expected_because_label,
403            assoc_kind: assoc_tag_str(assoc_item.tag()),
404            def_span: tcx.def_span(assoc_item.def_id),
405            bound_on_assoc_const_label,
406            wrap_in_braces_sugg,
407        })
408    }
409
410    pub(super) fn report_ambiguous_assoc_item(
411        &self,
412        bound1: ty::PolyTraitRef<'tcx>,
413        bound2: ty::PolyTraitRef<'tcx>,
414        matching_candidates: impl Iterator<Item = ty::PolyTraitRef<'tcx>>,
415        qself: AssocItemQSelf,
416        assoc_tag: ty::AssocTag,
417        assoc_ident: Ident,
418        span: Span,
419        constraint: Option<&hir::AssocItemConstraint<'tcx>>,
420    ) -> ErrorGuaranteed {
421        let tcx = self.tcx();
422
423        let assoc_kind_str = assoc_tag_str(assoc_tag);
424        let qself_str = qself.to_string(tcx);
425        let mut err = self.dcx().create_err(crate::diagnostics::AmbiguousAssocItem {
426            span,
427            assoc_kind: assoc_kind_str,
428            assoc_ident,
429            qself: &qself_str,
430        });
431        // Provide a more specific error code index entry for equality bindings.
432        err.code(
433            if let Some(constraint) = constraint
434                && let hir::AssocItemConstraintKind::Equality { .. } = constraint.kind
435            {
436                E0222
437            } else {
438                E0221
439            },
440        );
441
442        // FIXME(#97583): Print associated item bindings properly (i.e., not as equality
443        // predicates!).
444        // FIXME: Turn this into a structured, translatable & more actionable suggestion.
445        let mut where_bounds = ::alloc::vec::Vec::new()vec![];
446        for bound in [bound1, bound2].into_iter().chain(matching_candidates) {
447            let bound_id = bound.def_id();
448            let assoc_item = tcx.associated_items(bound_id).find_by_ident_and_kind(
449                tcx,
450                assoc_ident,
451                assoc_tag,
452                bound_id,
453            );
454            let bound_span = assoc_item.and_then(|item| tcx.hir_span_if_local(item.def_id));
455
456            if let Some(bound_span) = bound_span {
457                err.span_label(
458                    bound_span,
459                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("ambiguous `{1}` from `{0}`",
                bound.print_trait_sugared(), assoc_ident))
    })format!("ambiguous `{assoc_ident}` from `{}`", bound.print_trait_sugared(),),
460                );
461                if let Some(constraint) = constraint {
462                    match constraint.kind {
463                        hir::AssocItemConstraintKind::Equality { term } => {
464                            let term: ty::Term<'_> = match term {
465                                hir::Term::Ty(ty) => self.lower_ty(ty).into(),
466                                hir::Term::Const(ct) => {
467                                    let assoc_item =
468                                        assoc_item.expect("assoc_item should be present");
469                                    let projection_term = bound.map_bound(|trait_ref| {
470                                        let item_segment = hir::PathSegment {
471                                            ident: constraint.ident,
472                                            hir_id: constraint.hir_id,
473                                            res: Res::Err,
474                                            args: Some(constraint.gen_args),
475                                            infer_args: false,
476                                        };
477
478                                        let alias_args = self.lower_generic_args_of_assoc_item(
479                                            constraint.ident.span,
480                                            assoc_item.def_id,
481                                            &item_segment,
482                                            trait_ref.args,
483                                        );
484                                        ty::AliasTerm::new_from_def_id(
485                                            tcx,
486                                            assoc_item.def_id,
487                                            alias_args,
488                                        )
489                                    });
490
491                                    // FIXME(mgca): code duplication with other places we lower
492                                    // the rhs' of associated const bindings
493                                    let ty = projection_term.map_bound(|alias| {
494                                        alias.expect_ct().type_of(tcx).skip_norm_wip()
495                                    });
496                                    let ty = super::bounds::check_assoc_const_binding_type(
497                                        self,
498                                        constraint.ident,
499                                        ty,
500                                        constraint.hir_id,
501                                    );
502
503                                    self.lower_const_arg(ct, ty).into()
504                                }
505                            };
506                            if term.references_error() {
507                                continue;
508                            }
509                            // FIXME(#97583): This isn't syntactically well-formed!
510                            where_bounds.push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("        T: {0}::{1} = {2}",
                bound.print_only_trait_path(), assoc_ident, term))
    })format!(
511                                "        T: {trait}::{assoc_ident} = {term}",
512                                trait = bound.print_only_trait_path(),
513                            ));
514                        }
515                        // FIXME: Provide a suggestion.
516                        hir::AssocItemConstraintKind::Bound { bounds: _ } => {}
517                    }
518                } else {
519                    err.span_suggestion_verbose(
520                        span.with_hi(assoc_ident.span.lo()),
521                        "use fully-qualified syntax to disambiguate",
522                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{1} as {0}>::",
                bound.print_only_trait_path(), qself_str))
    })format!("<{qself_str} as {}>::", bound.print_only_trait_path()),
523                        Applicability::MaybeIncorrect,
524                    );
525                }
526            } else {
527                let trait_ = tcx.short_string(bound.print_only_trait_path(), err.long_ty_path());
528                err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("associated {0} `{1}` could derive from `{2}`",
                assoc_kind_str, assoc_ident, trait_))
    })format!(
529                    "associated {assoc_kind_str} `{assoc_ident}` could derive from `{trait_}`",
530                ));
531            }
532        }
533        if !where_bounds.is_empty() {
534            err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider introducing a new type parameter `T` and adding `where` constraints:\n    where\n        T: {1},\n{0}",
                where_bounds.join(",\n"), qself_str))
    })format!(
535                "consider introducing a new type parameter `T` and adding `where` constraints:\
536                     \n    where\n        T: {qself_str},\n{}",
537                where_bounds.join(",\n"),
538            ));
539        }
540        err.emit()
541    }
542
543    pub(crate) fn report_missing_self_ty_for_resolved_path(
544        &self,
545        trait_def_id: DefId,
546        span: Span,
547        item_segment: &hir::PathSegment<'tcx>,
548        assoc_tag: ty::AssocTag,
549    ) -> ErrorGuaranteed {
550        let tcx = self.tcx();
551        let path_str = tcx.def_path_str(trait_def_id);
552
553        let def_id = self.item_def_id();
554        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs:554",
                        "rustc_hir_analysis::hir_ty_lowering::errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(554u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering::errors"),
                        ::tracing_core::field::FieldSet::new(&["item_def_id"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&def_id) as
                                            &dyn Value))])
            });
    } else { ; }
};debug!(item_def_id = ?def_id);
555
556        // FIXME: document why/how this is different from `tcx.local_parent(def_id)`
557        let parent_def_id = tcx.hir_get_parent_item(tcx.local_def_id_to_hir_id(def_id)).to_def_id();
558        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs:558",
                        "rustc_hir_analysis::hir_ty_lowering::errors",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_hir_analysis/src/hir_ty_lowering/errors.rs"),
                        ::tracing_core::__macro_support::Option::Some(558u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_hir_analysis::hir_ty_lowering::errors"),
                        ::tracing_core::field::FieldSet::new(&["parent_def_id"],
                            ::tracing_core::callsite::Identifier(&__CALLSITE)),
                        ::tracing::metadata::Kind::EVENT)
                };
            ::tracing::callsite::DefaultCallsite::new(&META)
        };
    let enabled =
        ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
                &&
                ::tracing::Level::DEBUG <=
                    ::tracing::level_filters::LevelFilter::current() &&
            {
                let interest = __CALLSITE.interest();
                !interest.is_never() &&
                    ::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
                        interest)
            };
    if enabled {
        (|value_set: ::tracing::field::ValueSet|
                    {
                        let meta = __CALLSITE.metadata();
                        ::tracing::Event::dispatch(meta, &value_set);
                        ;
                    })({
                #[allow(unused_imports)]
                use ::tracing::field::{debug, display, Value};
                let mut iter = __CALLSITE.metadata().fields().iter();
                __CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
                                    ::tracing::__macro_support::Option::Some(&debug(&parent_def_id)
                                            as &dyn Value))])
            });
    } else { ; }
};debug!(?parent_def_id);
559
560        // If the trait in segment is the same as the trait defining the item,
561        // use the `<Self as ..>` syntax in the error.
562        let is_part_of_self_trait_constraints = def_id.to_def_id() == trait_def_id;
563        let is_part_of_fn_in_self_trait = parent_def_id == trait_def_id;
564
565        let type_names = if is_part_of_self_trait_constraints || is_part_of_fn_in_self_trait {
566            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        ["Self".to_string()]))vec!["Self".to_string()]
567        } else {
568            // Find all the types that have an `impl` for the trait.
569            tcx.all_impls(trait_def_id)
570                .map(|impl_def_id| tcx.impl_trait_header(impl_def_id))
571                .filter(|header| {
572                    // Consider only accessible traits
573                    tcx.visibility(trait_def_id).is_accessible_from(self.item_def_id(), tcx)
574                        && header.polarity != ty::ImplPolarity::Negative
575                })
576                .map(|header| header.trait_ref.instantiate_identity().skip_norm_wip().self_ty())
577                // We don't care about blanket impls.
578                .filter(|self_ty| !self_ty.has_non_region_param())
579                .map(|self_ty| tcx.erase_and_anonymize_regions(self_ty).to_string())
580                .collect()
581        };
582        // FIXME: also look at `tcx.generics_of(self.item_def_id()).params` any that
583        // references the trait. Relevant for the first case in
584        // `src/test/ui/associated-types/associated-types-in-ambiguous-context.rs`
585        self.report_ambiguous_assoc_item_path(
586            span,
587            &type_names,
588            &[path_str],
589            item_segment.ident,
590            assoc_tag,
591        )
592    }
593
594    pub(super) fn report_unresolved_type_relative_path(
595        &self,
596        self_ty: Ty<'tcx>,
597        hir_self_ty: &hir::Ty<'_>,
598        assoc_tag: ty::AssocTag,
599        ident: Ident,
600        qpath_hir_id: HirId,
601        span: Span,
602        variant_def_id: Option<DefId>,
603    ) -> ErrorGuaranteed {
604        let tcx = self.tcx();
605        let kind_str = assoc_tag_str(assoc_tag);
606        if variant_def_id.is_some() {
607            // Variant in type position
608            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("expected {0}, found variant `{1}`",
                kind_str, ident))
    })format!("expected {kind_str}, found variant `{ident}`");
609            self.dcx().span_err(span, msg)
610        } else if self_ty.is_enum() {
611            let mut err = self.dcx().create_err(diagnostics::NoVariantNamed {
612                span: ident.span,
613                ident,
614                ty: self_ty,
615            });
616
617            let adt_def = self_ty.ty_adt_def().expect("enum is not an ADT");
618            if let Some(variant_name) = find_best_match_for_name(
619                &adt_def.variants().iter().map(|variant| variant.name).collect::<Vec<Symbol>>(),
620                ident.name,
621                None,
622            ) && let Some(variant) = adt_def.variants().iter().find(|s| s.name == variant_name)
623            {
624                let mut suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(ident.span, variant_name.to_string())]))vec![(ident.span, variant_name.to_string())];
625                if let hir::Node::Stmt(&hir::Stmt { kind: hir::StmtKind::Semi(expr), .. })
626                | hir::Node::Expr(expr) = tcx.parent_hir_node(qpath_hir_id)
627                    && let hir::ExprKind::Struct(..) = expr.kind
628                {
629                    match variant.ctor {
630                        None => {
631                            // struct
632                            suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(ident.span.with_hi(expr.span.hi()),
                    if variant.fields.is_empty() {
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("{0} {{}}", variant_name))
                            })
                    } else {
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("{1} {{ {0} }}",
                                        variant.fields.iter().map(|f|
                                                        ::alloc::__export::must_use({
                                                                ::alloc::fmt::format(format_args!("{0}: /* value */",
                                                                        f.name))
                                                            })).collect::<Vec<_>>().join(", "), variant_name))
                            })
                    })]))vec![(
633                                ident.span.with_hi(expr.span.hi()),
634                                if variant.fields.is_empty() {
635                                    format!("{variant_name} {{}}")
636                                } else {
637                                    format!(
638                                        "{variant_name} {{ {} }}",
639                                        variant
640                                            .fields
641                                            .iter()
642                                            .map(|f| format!("{}: /* value */", f.name))
643                                            .collect::<Vec<_>>()
644                                            .join(", ")
645                                    )
646                                },
647                            )];
648                        }
649                        Some((hir::def::CtorKind::Fn, def_id)) => {
650                            // tuple
651                            let fn_sig = tcx.fn_sig(def_id).instantiate_identity().skip_norm_wip();
652                            let inputs = fn_sig.inputs().skip_binder();
653                            suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(ident.span.with_hi(expr.span.hi()),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!("{1}({0})",
                                    inputs.iter().map(|i|
                                                    ::alloc::__export::must_use({
                                                            ::alloc::fmt::format(format_args!("/* {0} */", i))
                                                        })).collect::<Vec<_>>().join(", "), variant_name))
                        }))]))vec![(
654                                ident.span.with_hi(expr.span.hi()),
655                                format!(
656                                    "{variant_name}({})",
657                                    inputs
658                                        .iter()
659                                        .map(|i| format!("/* {i} */"))
660                                        .collect::<Vec<_>>()
661                                        .join(", ")
662                                ),
663                            )];
664                        }
665                        Some((hir::def::CtorKind::Const, _)) => {
666                            // unit
667                            suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(ident.span.with_hi(expr.span.hi()), variant_name.to_string())]))vec![(
668                                ident.span.with_hi(expr.span.hi()),
669                                variant_name.to_string(),
670                            )];
671                        }
672                    }
673                }
674                err.multipart_suggestion(
675                    "there is a variant with a similar name",
676                    suggestion,
677                    Applicability::HasPlaceholders,
678                );
679            } else {
680                err.span_label(ident.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("variant not found in `{0}`",
                self_ty))
    })format!("variant not found in `{self_ty}`"));
681            }
682
683            if let Some(sp) = tcx.hir_span_if_local(adt_def.did()) {
684                err.span_label(sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("variant `{0}` not found here",
                ident))
    })format!("variant `{ident}` not found here"));
685            }
686
687            err.emit()
688        } else if let Err(reported) = self_ty.error_reported() {
689            reported
690        } else {
691            match self.maybe_report_similar_assoc_fn(span, self_ty, hir_self_ty) {
692                Ok(()) => {}
693                Err(reported) => return reported,
694            }
695
696            let traits: Vec<_> = self.probe_traits_that_match_assoc_ty(self_ty, ident);
697
698            self.report_ambiguous_assoc_item_path(
699                span,
700                &[self_ty.to_string()],
701                &traits,
702                ident,
703                assoc_tag,
704            )
705        }
706    }
707
708    fn report_ambiguous_assoc_item_path(
709        &self,
710        span: Span,
711        types: &[String],
712        traits: &[String],
713        ident: Ident,
714        assoc_tag: ty::AssocTag,
715    ) -> ErrorGuaranteed {
716        let kind_str = assoc_tag_str(assoc_tag);
717        let mut err =
718            {
    self.dcx().struct_span_err(span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("ambiguous associated {0}",
                            kind_str))
                })).with_code(E0223)
}struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated {kind_str}");
719        if self
720            .tcx()
721            .resolutions(())
722            .confused_type_with_std_module
723            .keys()
724            .any(|full_span| full_span.contains(span))
725        {
726            err.span_suggestion_verbose(
727                span.shrink_to_lo(),
728                "you are looking for the module in `std`, not the primitive type",
729                "std::",
730                Applicability::MachineApplicable,
731            );
732        } else {
733            let sugg_sp = span.until(ident.span);
734
735            let mut types = types.to_vec();
736            types.sort();
737            let mut traits = traits.to_vec();
738            traits.sort();
739            match (&types[..], &traits[..]) {
740                ([], []) => {
741                    err.span_suggestion_verbose(
742                        sugg_sp,
743                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if there were a type named `Type` that implements a trait named `Trait` with associated {0} `{1}`, you could use the fully-qualified path",
                kind_str, ident))
    })format!(
744                            "if there were a type named `Type` that implements a trait named \
745                             `Trait` with associated {kind_str} `{ident}`, you could use the \
746                             fully-qualified path",
747                        ),
748                        "<Type as Trait>::",
749                        Applicability::HasPlaceholders,
750                    );
751                }
752                ([], [trait_str]) => {
753                    err.span_suggestion_verbose(
754                        sugg_sp,
755                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if there were a type named `Example` that implemented `{0}`, you could use the fully-qualified path",
                trait_str))
    })format!(
756                            "if there were a type named `Example` that implemented `{trait_str}`, \
757                             you could use the fully-qualified path",
758                        ),
759                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<Example as {0}>::", trait_str))
    })format!("<Example as {trait_str}>::"),
760                        Applicability::HasPlaceholders,
761                    );
762                }
763                ([], traits) => {
764                    err.span_suggestions_with_style(
765                        sugg_sp,
766                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if there were a type named `Example` that implemented one of the traits with associated {0} `{1}`, you could use the fully-qualified path",
                kind_str, ident))
    })format!(
767                            "if there were a type named `Example` that implemented one of the \
768                             traits with associated {kind_str} `{ident}`, you could use the \
769                             fully-qualified path",
770                        ),
771                        traits.iter().map(|trait_str| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<Example as {0}>::", trait_str))
    })format!("<Example as {trait_str}>::")),
772                        Applicability::HasPlaceholders,
773                        SuggestionStyle::ShowAlways,
774                    );
775                }
776                ([type_str], []) => {
777                    err.span_suggestion_verbose(
778                        sugg_sp,
779                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if there were a trait named `Example` with associated {0} `{1}` implemented for `{2}`, you could use the fully-qualified path",
                kind_str, ident, type_str))
    })format!(
780                            "if there were a trait named `Example` with associated {kind_str} `{ident}` \
781                             implemented for `{type_str}`, you could use the fully-qualified path",
782                        ),
783                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0} as Example>::", type_str))
    })format!("<{type_str} as Example>::"),
784                        Applicability::HasPlaceholders,
785                    );
786                }
787                (types, []) => {
788                    err.span_suggestions_with_style(
789                        sugg_sp,
790                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("if there were a trait named `Example` with associated {0} `{1}` implemented for one of the types, you could use the fully-qualified path",
                kind_str, ident))
    })format!(
791                            "if there were a trait named `Example` with associated {kind_str} `{ident}` \
792                             implemented for one of the types, you could use the fully-qualified \
793                             path",
794                        ),
795                        types
796                            .into_iter()
797                            .map(|type_str| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0} as Example>::", type_str))
    })format!("<{type_str} as Example>::")),
798                        Applicability::HasPlaceholders,
799                        SuggestionStyle::ShowAlways,
800                    );
801                }
802                (types, traits) => {
803                    let mut suggestions = ::alloc::vec::Vec::new()vec![];
804                    for type_str in types {
805                        for trait_str in traits {
806                            suggestions.push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0} as {1}>::", type_str,
                trait_str))
    })format!("<{type_str} as {trait_str}>::"));
807                        }
808                    }
809                    err.span_suggestions_with_style(
810                        sugg_sp,
811                        "use fully-qualified syntax",
812                        suggestions,
813                        Applicability::MachineApplicable,
814                        SuggestionStyle::ShowAlways,
815                    );
816                }
817            }
818        }
819        err.emit()
820    }
821
822    pub(crate) fn report_ambiguous_inherent_assoc_item(
823        &self,
824        name: Ident,
825        candidates: Vec<DefId>,
826        span: Span,
827    ) -> ErrorGuaranteed {
828        let mut err = {
    self.dcx().struct_span_err(name.span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("multiple applicable items in scope"))
                })).with_code(E0034)
}struct_span_code_err!(
829            self.dcx(),
830            name.span,
831            E0034,
832            "multiple applicable items in scope"
833        );
834        err.span_label(name.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("multiple `{0}` found", name))
    })format!("multiple `{name}` found"));
835        self.note_ambiguous_inherent_assoc_item(&mut err, candidates, span);
836        err.emit()
837    }
838
839    // FIXME(fmease): Heavily adapted from `rustc_hir_typeck::method::suggest`. Deduplicate.
840    fn note_ambiguous_inherent_assoc_item(
841        &self,
842        err: &mut Diag<'_>,
843        candidates: Vec<DefId>,
844        span: Span,
845    ) {
846        let tcx = self.tcx();
847
848        // Dynamic limit to avoid hiding just one candidate, which is silly.
849        let limit = if candidates.len() == 5 { 5 } else { 4 };
850
851        for (index, &item) in candidates.iter().take(limit).enumerate() {
852            let impl_ = tcx.parent(item);
853
854            let note_span = if item.is_local() {
855                Some(tcx.def_span(item))
856            } else if impl_.is_local() {
857                Some(tcx.def_span(impl_))
858            } else {
859                None
860            };
861
862            let title = if candidates.len() > 1 {
863                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("candidate #{0}", index + 1))
    })format!("candidate #{}", index + 1)
864            } else {
865                "the candidate".into()
866            };
867
868            let impl_ty = tcx.at(span).type_of(impl_).instantiate_identity().skip_norm_wip();
869            let note = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} is defined in an impl for the type `{1}`",
                title, impl_ty))
    })format!("{title} is defined in an impl for the type `{impl_ty}`");
870
871            if let Some(span) = note_span {
872                err.span_note(span, note);
873            } else {
874                err.note(note);
875            }
876        }
877        if candidates.len() > limit {
878            err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("and {0} others",
                candidates.len() - limit))
    })format!("and {} others", candidates.len() - limit));
879        }
880    }
881
882    // FIXME(inherent_associated_types): Find similarly named associated types and suggest them.
883    pub(crate) fn report_unresolved_inherent_assoc_item(
884        &self,
885        name: Ident,
886        self_ty: Ty<'tcx>,
887        candidates: Vec<InherentAssocCandidate>,
888        fulfillment_errors: Vec<FulfillmentError<'tcx>>,
889        span: Span,
890        assoc_tag: ty::AssocTag,
891    ) -> ErrorGuaranteed {
892        // FIXME(fmease): This was copied in parts from an old version of `rustc_hir_typeck::method::suggest`.
893        // Either
894        // * update this code by applying changes similar to #106702 or by taking a
895        //   Vec<(DefId, (DefId, DefId), Option<Vec<FulfillmentError<'tcx>>>)> or
896        // * deduplicate this code across the two crates.
897
898        let tcx = self.tcx();
899
900        let assoc_tag_str = assoc_tag_str(assoc_tag);
901        let adt_did = self_ty.ty_adt_def().map(|def| def.did());
902        let add_def_label = |err: &mut Diag<'_>| {
903            if let Some(did) = adt_did {
904                err.span_label(
905                    tcx.def_span(did),
906                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("associated {1} `{2}` not found for this {0}",
                tcx.def_descr(did), assoc_tag_str, name))
    })format!(
907                        "associated {assoc_tag_str} `{name}` not found for this {}",
908                        tcx.def_descr(did)
909                    ),
910                );
911            }
912        };
913
914        if fulfillment_errors.is_empty() {
915            // FIXME(fmease): Copied from `rustc_hir_typeck::method::probe`. Deduplicate.
916
917            let limit = if candidates.len() == 5 { 5 } else { 4 };
918            let type_candidates = candidates
919                .iter()
920                .take(limit)
921                .map(|cand| {
922                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("- `{0}`",
                tcx.at(span).type_of(cand.impl_).instantiate_identity().skip_norm_wip()))
    })format!(
923                        "- `{}`",
924                        tcx.at(span).type_of(cand.impl_).instantiate_identity().skip_norm_wip()
925                    )
926                })
927                .collect::<Vec<_>>()
928                .join("\n");
929            let additional_types = if candidates.len() > limit {
930                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("\nand {0} more types",
                candidates.len() - limit))
    })format!("\nand {} more types", candidates.len() - limit)
931            } else {
932                String::new()
933            };
934
935            let mut err = {
    self.dcx().struct_span_err(name.span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("associated {0} `{1}` not found for `{2}` in the current scope",
                            assoc_tag_str, name, self_ty))
                })).with_code(E0220)
}struct_span_code_err!(
936                self.dcx(),
937                name.span,
938                E0220,
939                "associated {assoc_tag_str} `{name}` not found for `{self_ty}` in the current scope"
940            );
941            err.span_label(name.span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("associated item not found in `{0}`",
                self_ty))
    })format!("associated item not found in `{self_ty}`"));
942            err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the associated {0} was found for\n{1}{2}",
                assoc_tag_str, type_candidates, additional_types))
    })format!(
943                "the associated {assoc_tag_str} was found for\n{type_candidates}{additional_types}",
944            ));
945            add_def_label(&mut err);
946            return err.emit();
947        }
948
949        let mut bound_spans: SortedMap<Span, Vec<String>> = Default::default();
950
951        let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
952            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`",
                if obligation.len() > 50 { quiet } else { obligation }))
    })format!("`{}`", if obligation.len() > 50 { quiet } else { obligation });
953            match self_ty.kind() {
954                // Point at the type that couldn't satisfy the bound.
955                ty::Adt(def, _) => {
956                    bound_spans.get_mut_or_insert_default(tcx.def_span(def.did())).push(msg)
957                }
958                // Point at the trait object that couldn't satisfy the bound.
959                ty::Dynamic(preds, _) => {
960                    for pred in preds.iter() {
961                        match pred.skip_binder() {
962                            ty::ExistentialPredicate::Trait(tr) => {
963                                bound_spans
964                                    .get_mut_or_insert_default(tcx.def_span(tr.def_id))
965                                    .push(msg.clone());
966                            }
967                            ty::ExistentialPredicate::Projection(_)
968                            | ty::ExistentialPredicate::AutoTrait(_) => {}
969                        }
970                    }
971                }
972                // Point at the closure that couldn't satisfy the bound.
973                ty::Closure(def_id, _) => {
974                    bound_spans
975                        .get_mut_or_insert_default(tcx.def_span(*def_id))
976                        .push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", quiet))
    })format!("`{quiet}`"));
977                }
978                _ => {}
979            }
980        };
981
982        let format_pred = |pred: ty::Predicate<'tcx>| {
983            let bound_predicate = pred.kind();
984            match bound_predicate.skip_binder() {
985                ty::PredicateKind::Clause(ty::ClauseKind::Projection(pred)) => {
986                    // `<Foo as Iterator>::Item = String`.
987                    let projection_term = pred.projection_term;
988                    let quiet_projection_term = projection_term
989                        .with_replaced_self_ty(tcx, Ty::new_var(tcx, ty::TyVid::ZERO));
990
991                    let term = pred.term;
992                    let obligation = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} = {1}", projection_term, term))
    })format!("{projection_term} = {term}");
993                    let quiet = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} = {1}", quiet_projection_term,
                term))
    })format!("{quiet_projection_term} = {term}");
994
995                    bound_span_label(projection_term.self_ty(), &obligation, &quiet);
996                    Some((obligation, projection_term.self_ty()))
997                }
998                ty::PredicateKind::Clause(ty::ClauseKind::Trait(poly_trait_ref)) => {
999                    let p = poly_trait_ref.trait_ref;
1000                    let self_ty = p.self_ty();
1001                    let path = p.print_only_trait_path();
1002                    let obligation = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}: {1}", self_ty, path))
    })format!("{self_ty}: {path}");
1003                    let quiet = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("_: {0}", path))
    })format!("_: {path}");
1004                    bound_span_label(self_ty, &obligation, &quiet);
1005                    Some((obligation, self_ty))
1006                }
1007                _ => None,
1008            }
1009        };
1010
1011        // FIXME(fmease): `rustc_hir_typeck::method::suggest` uses a `skip_list` to filter out some bounds.
1012        // I would do the same here if it didn't mean more code duplication.
1013        let mut bounds: Vec<_> = fulfillment_errors
1014            .into_iter()
1015            .map(|error| error.root_obligation.predicate)
1016            .filter_map(format_pred)
1017            .map(|(p, _)| ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`", p))
    })format!("`{p}`"))
1018            .collect();
1019        bounds.sort();
1020        bounds.dedup();
1021
1022        let mut err = self.dcx().struct_span_err(
1023            name.span,
1024            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the associated {0} `{1}` exists for `{2}`, but its trait bounds were not satisfied",
                assoc_tag_str, name, self_ty))
    })format!("the associated {assoc_tag_str} `{name}` exists for `{self_ty}`, but its trait bounds were not satisfied")
1025        );
1026        if !bounds.is_empty() {
1027            err.note(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the following trait bounds were not satisfied:\n{0}",
                bounds.join("\n")))
    })format!(
1028                "the following trait bounds were not satisfied:\n{}",
1029                bounds.join("\n")
1030            ));
1031        }
1032        err.span_label(
1033            name.span,
1034            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("associated {0} cannot be referenced on `{1}` due to unsatisfied trait bounds",
                assoc_tag_str, self_ty))
    })format!("associated {assoc_tag_str} cannot be referenced on `{self_ty}` due to unsatisfied trait bounds")
1035        );
1036
1037        for (span, mut bounds) in bound_spans {
1038            if !tcx.sess.source_map().is_span_accessible(span) {
1039                continue;
1040            }
1041            bounds.sort();
1042            bounds.dedup();
1043            let msg = match &bounds[..] {
1044                [bound] => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("doesn\'t satisfy {0}", bound))
    })format!("doesn't satisfy {bound}"),
1045                bounds if bounds.len() > 4 => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("doesn\'t satisfy {0} bounds",
                bounds.len()))
    })format!("doesn't satisfy {} bounds", bounds.len()),
1046                [bounds @ .., last] => ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("doesn\'t satisfy {0} or {1}",
                bounds.join(", "), last))
    })format!("doesn't satisfy {} or {last}", bounds.join(", ")),
1047                [] => ::core::panicking::panic("internal error: entered unreachable code")unreachable!(),
1048            };
1049            err.span_label(span, msg);
1050        }
1051        add_def_label(&mut err);
1052        err.emit()
1053    }
1054
1055    /// If there are any missing associated items, emit an error instructing the user to provide
1056    /// them unless that's impossible due to shadowing. Moreover, if any corresponding trait refs
1057    /// are dyn incompatible due to associated items we emit an dyn incompatibility error instead.
1058    pub(crate) fn check_for_required_assoc_items(
1059        &self,
1060        spans: SmallVec<[Span; 1]>,
1061        missing_assoc_items: FxIndexSet<(DefId, ty::PolyTraitRef<'tcx>)>,
1062        potential_assoc_items: Vec<usize>,
1063        trait_bounds: &[hir::PolyTraitRef<'_>],
1064    ) -> Result<(), ErrorGuaranteed> {
1065        if missing_assoc_items.is_empty() {
1066            return Ok(());
1067        }
1068
1069        let tcx = self.tcx();
1070        let principal_span = *spans.first().unwrap();
1071
1072        // FIXME: This logic needs some more care w.r.t handling of conflicts
1073        let missing_assoc_items: Vec<_> = missing_assoc_items
1074            .into_iter()
1075            .map(|(def_id, trait_ref)| (tcx.associated_item(def_id), trait_ref))
1076            .collect();
1077        let mut names: FxIndexMap<_, Vec<_>> = Default::default();
1078        let mut names_len = 0;
1079        let mut descr = None;
1080
1081        enum Descr {
1082            Item,
1083            Tag(ty::AssocTag),
1084        }
1085
1086        for &(assoc_item, trait_ref) in &missing_assoc_items {
1087            // We don't want to suggest specifying associated items if there's something wrong with
1088            // any of them that renders the trait dyn incompatible; providing them certainly won't
1089            // fix the issue and we could also risk suggesting invalid code.
1090            //
1091            // Note that this check is only truly necessary in item ctxts where we merely perform
1092            // *minimal* dyn compatibility checks. In fn ctxts we would've already bailed out with
1093            // an error by this point if the trait was dyn incompatible.
1094            let violations =
1095                dyn_compatibility_violations_for_assoc_item(tcx, trait_ref.def_id(), assoc_item);
1096            if !violations.is_empty() {
1097                return Err(report_dyn_incompatibility(
1098                    tcx,
1099                    principal_span,
1100                    None,
1101                    trait_ref.def_id(),
1102                    &violations,
1103                )
1104                .emit());
1105            }
1106
1107            names.entry(trait_ref).or_default().push(assoc_item.name());
1108            names_len += 1;
1109
1110            descr = match descr {
1111                None => Some(Descr::Tag(assoc_item.tag())),
1112                Some(Descr::Tag(tag)) if tag != assoc_item.tag() => Some(Descr::Item),
1113                _ => continue,
1114            };
1115        }
1116
1117        // related to issue #91997, turbofishes added only when in an expr or pat
1118        let mut in_expr_or_pat = false;
1119        if let ([], [bound]) = (&potential_assoc_items[..], &trait_bounds) {
1120            let grandparent = tcx.parent_hir_node(tcx.parent_hir_id(bound.trait_ref.hir_ref_id));
1121            in_expr_or_pat = match grandparent {
1122                hir::Node::Expr(_) | hir::Node::Pat(_) => true,
1123                _ => false,
1124            };
1125        }
1126
1127        // We get all the associated items that *are* set, so that we can check if any of
1128        // their names match one of the ones we are missing.
1129        // This would mean that they are shadowing the associated item we are missing, and
1130        // we can then use their span to indicate this to the user.
1131        //
1132        // FIXME: This does not account for trait aliases. I think we should just make
1133        //        `lower_trait_object_ty` compute the list of all specified items or give us the
1134        //        necessary ingredients if it's too expensive to compute in the happy path.
1135        let bound_names: UnordMap<_, _> =
1136            trait_bounds
1137                .iter()
1138                .filter_map(|poly_trait_ref| {
1139                    let path = poly_trait_ref.trait_ref.path.segments.last()?;
1140                    let args = path.args?;
1141                    let Res::Def(DefKind::Trait, trait_def_id) = path.res else { return None };
1142
1143                    Some(args.constraints.iter().filter_map(move |constraint| {
1144                        let hir::AssocItemConstraintKind::Equality { term } = constraint.kind
1145                        else {
1146                            return None;
1147                        };
1148                        let tag = match term {
1149                            hir::Term::Ty(_) => ty::AssocTag::Type,
1150                            hir::Term::Const(_) => ty::AssocTag::Const,
1151                        };
1152                        let assoc_item = tcx
1153                            .associated_items(trait_def_id)
1154                            .find_by_ident_and_kind(tcx, constraint.ident, tag, trait_def_id)?;
1155                        Some(((constraint.ident.name, tag), assoc_item.def_id))
1156                    }))
1157                })
1158                .flatten()
1159                .collect();
1160
1161        let mut names: Vec<_> = names
1162            .into_iter()
1163            .map(|(trait_, mut assocs)| {
1164                assocs.sort();
1165                let trait_ = trait_.print_trait_sugared();
1166                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} in `{1}`",
                listify(&assocs[..],
                        |a|
                            ::alloc::__export::must_use({
                                    ::alloc::fmt::format(format_args!("`{0}`", a))
                                })).unwrap_or_default(), trait_))
    })format!(
1167                    "{} in `{trait_}`",
1168                    listify(&assocs[..], |a| format!("`{a}`")).unwrap_or_default()
1169                )
1170            })
1171            .collect();
1172        names.sort();
1173        let names = names.join(", ");
1174
1175        let descr = match descr.unwrap() {
1176            Descr::Item => "associated item",
1177            Descr::Tag(tag) => tag.descr(),
1178        };
1179        let mut err = {
    self.dcx().struct_span_err(principal_span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("the value of the {1}{0} {2} must be specified",
                            if names_len == 1 { "" } else { "s" }, descr, names))
                })).with_code(E0191)
}struct_span_code_err!(
1180            self.dcx(),
1181            principal_span,
1182            E0191,
1183            "the value of the {descr}{s} {names} must be specified",
1184            s = pluralize!(names_len),
1185        );
1186        let mut suggestions = ::alloc::vec::Vec::new()vec![];
1187        let mut items_count = 0;
1188        let mut where_constraints = ::alloc::vec::Vec::new()vec![];
1189        let mut already_has_generics_args_suggestion = false;
1190
1191        let mut names: UnordMap<_, usize> = Default::default();
1192        for (item, _) in &missing_assoc_items {
1193            items_count += 1;
1194            *names.entry((item.name(), item.tag())).or_insert(0) += 1;
1195        }
1196        let mut dupes = false;
1197        let mut shadows = false;
1198        for (item, trait_ref) in &missing_assoc_items {
1199            let name = item.name();
1200            let key = (name, item.tag());
1201
1202            if names[&key] > 1 {
1203                dupes = true;
1204            } else if bound_names.get(&key).is_some_and(|&def_id| def_id != item.def_id) {
1205                shadows = true;
1206            }
1207
1208            let prefix = if dupes || shadows {
1209                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::",
                tcx.def_path_str(trait_ref.def_id())))
    })format!("{}::", tcx.def_path_str(trait_ref.def_id()))
1210            } else {
1211                String::new()
1212            };
1213            let mut is_shadowed = false;
1214
1215            if let Some(&def_id) = bound_names.get(&key)
1216                && def_id != item.def_id
1217            {
1218                is_shadowed = true;
1219
1220                let rename_message = if def_id.is_local() { ", consider renaming it" } else { "" };
1221                err.span_label(
1222                    tcx.def_span(def_id),
1223                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}{1}` shadowed here{2}", prefix,
                name, rename_message))
    })format!("`{prefix}{name}` shadowed here{rename_message}"),
1224                );
1225            }
1226
1227            let rename_message = if is_shadowed { ", consider renaming it" } else { "" };
1228
1229            if let Some(sp) = tcx.hir_span_if_local(item.def_id) {
1230                err.span_label(sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}{1}` defined here{2}", prefix,
                name, rename_message))
    })format!("`{prefix}{name}` defined here{rename_message}"));
1231            }
1232        }
1233        if potential_assoc_items.len() == missing_assoc_items.len() {
1234            // When the amount of missing associated types equals the number of
1235            // extra type arguments present. A suggesting to replace the generic args with
1236            // associated types is already emitted.
1237            already_has_generics_args_suggestion = true;
1238        } else if let (Ok(snippet), false, false) =
1239            (tcx.sess.source_map().span_to_snippet(principal_span), dupes, shadows)
1240        {
1241            let bindings: Vec<_> = missing_assoc_items
1242                .iter()
1243                .map(|(item, _)| {
1244                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} = /* {1} */", item.name(),
                match item.kind {
                    ty::AssocKind::Const { .. } => "CONST",
                    ty::AssocKind::Type { .. } => "Type",
                    ty::AssocKind::Fn { .. } =>
                        ::core::panicking::panic("internal error: entered unreachable code"),
                }))
    })format!(
1245                        "{} = /* {} */",
1246                        item.name(),
1247                        match item.kind {
1248                            ty::AssocKind::Const { .. } => "CONST",
1249                            ty::AssocKind::Type { .. } => "Type",
1250                            ty::AssocKind::Fn { .. } => unreachable!(),
1251                        }
1252                    )
1253                })
1254                .collect();
1255            let code = if let Some(snippet) = snippet.strip_suffix("<>") {
1256                // Empty generics
1257                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{1}<{0}>", bindings.join(", "),
                snippet))
    })format!("{snippet}<{}>", bindings.join(", "))
1258            } else if let Some(snippet) = snippet.strip_suffix('>') {
1259                // Non-empty generics
1260                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{1}, {0}>", bindings.join(", "),
                snippet))
    })format!("{snippet}, {}>", bindings.join(", "))
1261            } else if in_expr_or_pat {
1262                // The user wrote `Trait`, so we don't have a term we can suggest, but at least we
1263                // can clue them to the correct syntax `Trait::<Item = /* ... */>`.
1264                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}::<{1}>", snippet,
                bindings.join(", ")))
    })format!("{}::<{}>", snippet, bindings.join(", "))
1265            } else {
1266                // The user wrote `Trait`, so we don't have a term we can suggest, but at least we
1267                // can clue them to the correct syntax `Trait<Item = /* ... */>`.
1268                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}<{1}>", snippet,
                bindings.join(", ")))
    })format!("{}<{}>", snippet, bindings.join(", "))
1269            };
1270            suggestions.push((principal_span, code));
1271        } else if dupes {
1272            where_constraints.push(principal_span);
1273        }
1274
1275        // FIXME: This note doesn't make sense, get rid of this outright.
1276        //        I don't see how adding a type param (to the trait?) would help.
1277        //        If the user can modify the trait, they should just rename one of the assoc tys.
1278        //        What does it mean with the rest of the message?
1279        //        Does it suggest adding equality predicates (unimplemented) to the trait object
1280        //        type? (pseudo) "dyn B + <Self as B>::X = T + <Self as A>::X = U"?
1281        //        Instead, maybe mention shadowing if applicable (yes, even when no "relevant"
1282        //        bindings were provided).
1283        let where_msg = "consider introducing a new type parameter, adding `where` constraints \
1284                         using the fully-qualified path to the associated types";
1285        if !where_constraints.is_empty() && suggestions.is_empty() {
1286            // If there are duplicates associated type names and a single trait bound do not
1287            // use structured suggestion, it means that there are multiple supertraits with
1288            // the same associated type name.
1289            err.help(where_msg);
1290        }
1291        if suggestions.len() != 1 || already_has_generics_args_suggestion {
1292            // We don't need this label if there's an inline suggestion, show otherwise.
1293            let mut names: FxIndexMap<_, usize> = FxIndexMap::default();
1294            for (item, _) in &missing_assoc_items {
1295                items_count += 1;
1296                *names.entry(item.name()).or_insert(0) += 1;
1297            }
1298            let mut label = ::alloc::vec::Vec::new()vec![];
1299            for (item, trait_ref) in &missing_assoc_items {
1300                let name = item.name();
1301                let postfix = if names[&name] > 1 {
1302                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(" (from trait `{0}`)",
                trait_ref.print_trait_sugared()))
    })format!(" (from trait `{}`)", trait_ref.print_trait_sugared())
1303                } else {
1304                    String::new()
1305                };
1306                label.push(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}`{1}", name, postfix))
    })format!("`{}`{}", name, postfix));
1307            }
1308            if !label.is_empty() {
1309                err.span_label(
1310                    principal_span,
1311                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{2}{0} {1} must be specified",
                if label.len() == 1 { "" } else { "s" }, label.join(", "),
                descr))
    })format!(
1312                        "{descr}{s} {names} must be specified",
1313                        s = pluralize!(label.len()),
1314                        names = label.join(", "),
1315                    ),
1316                );
1317            }
1318        }
1319        suggestions.sort_by_key(|&(span, _)| span);
1320        // There are cases where one bound points to a span within another bound's span, like when
1321        // you have code like the following (#115019), so we skip providing a suggestion in those
1322        // cases to avoid having a malformed suggestion.
1323        //
1324        // pub struct Flatten<I> {
1325        //     inner: <IntoIterator<Item: IntoIterator<Item: >>::IntoIterator as Item>::core,
1326        //             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
1327        //             |                  ^^^^^^^^^^^^^^^^^^^^^
1328        //             |                  |
1329        //             |                  associated types `Item`, `IntoIter` must be specified
1330        //             associated types `Item`, `IntoIter` must be specified
1331        // }
1332        let overlaps = suggestions.windows(2).any(|pair| pair[0].0.overlaps(pair[1].0));
1333        if !suggestions.is_empty() && !overlaps {
1334            err.multipart_suggestion(
1335                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("specify the {1}{0}",
                if items_count == 1 { "" } else { "s" }, descr))
    })format!("specify the {descr}{s}", s = pluralize!(items_count)),
1336                suggestions,
1337                Applicability::HasPlaceholders,
1338            );
1339            if !where_constraints.is_empty() {
1340                err.span_help(where_constraints, where_msg);
1341            }
1342        }
1343
1344        Err(err.emit())
1345    }
1346
1347    /// On ambiguous associated type, look for an associated function whose name matches the
1348    /// extended path and, if found, emit an E0223 error with a structured suggestion.
1349    /// e.g. for `String::from::utf8`, suggest `String::from_utf8` (#109195)
1350    pub(crate) fn maybe_report_similar_assoc_fn(
1351        &self,
1352        span: Span,
1353        qself_ty: Ty<'tcx>,
1354        qself: &hir::Ty<'_>,
1355    ) -> Result<(), ErrorGuaranteed> {
1356        let tcx = self.tcx();
1357        if let Some((_, node)) = tcx.hir_parent_iter(qself.hir_id).skip(1).next()
1358            && let hir::Node::Expr(hir::Expr {
1359                kind:
1360                    hir::ExprKind::Path(hir::QPath::TypeRelative(
1361                        hir::Ty {
1362                            kind:
1363                                hir::TyKind::Path(hir::QPath::TypeRelative(
1364                                    _,
1365                                    hir::PathSegment { ident: ident2, .. },
1366                                )),
1367                            ..
1368                        },
1369                        hir::PathSegment { ident: ident3, .. },
1370                    )),
1371                ..
1372            }) = node
1373            && let Some(inherent_impls) = qself_ty
1374                .ty_adt_def()
1375                .map(|adt_def| tcx.inherent_impls(adt_def.did()))
1376                .or_else(|| {
1377                    simplify_type(tcx, qself_ty, TreatParams::InstantiateWithInfer)
1378                        .map(|simple_ty| tcx.incoherent_impls(simple_ty))
1379                })
1380            && let name = Symbol::intern(&::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}_{1}", ident2, ident3))
    })format!("{ident2}_{ident3}"))
1381            && let Some(item) = inherent_impls
1382                .iter()
1383                .flat_map(|&inherent_impl| {
1384                    tcx.associated_items(inherent_impl).filter_by_name_unhygienic(name)
1385                })
1386                .next()
1387            && item.is_fn()
1388        {
1389            Err({
    self.dcx().struct_span_err(span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("ambiguous associated type"))
                })).with_code(E0223)
}struct_span_code_err!(self.dcx(), span, E0223, "ambiguous associated type")
1390                .with_span_suggestion_verbose(
1391                    ident2.span.to(ident3.span),
1392                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("there is an associated function with a similar name: `{0}`",
                name))
    })format!("there is an associated function with a similar name: `{name}`"),
1393                    name,
1394                    Applicability::MaybeIncorrect,
1395                )
1396                .emit())
1397        } else {
1398            Ok(())
1399        }
1400    }
1401
1402    pub fn report_prohibited_generic_args<'a>(
1403        &self,
1404        segments: impl Iterator<Item = &'a hir::PathSegment<'a>> + Clone,
1405        args_visitors: impl Iterator<Item = &'a hir::GenericArg<'a>> + Clone,
1406        err_extend: GenericsArgsErrExtend<'a>,
1407    ) -> ErrorGuaranteed {
1408        #[derive(#[automatically_derived]
impl ::core::cmp::PartialEq for ProhibitGenericsArg {
    #[inline]
    fn eq(&self, other: &ProhibitGenericsArg) -> bool {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        let __arg1_discr = ::core::intrinsics::discriminant_value(other);
        __self_discr == __arg1_discr
    }
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for ProhibitGenericsArg {
    #[inline]
    #[doc(hidden)]
    #[coverage(off)]
    fn assert_fields_are_eq(&self) {}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for ProhibitGenericsArg {
    #[inline]
    fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
        let __self_discr = ::core::intrinsics::discriminant_value(self);
        ::core::hash::Hash::hash(&__self_discr, state)
    }
}Hash)]
1409        enum ProhibitGenericsArg {
1410            Lifetime,
1411            Type,
1412            Const,
1413            Infer,
1414        }
1415
1416        let mut prohibit_args = FxIndexSet::default();
1417        args_visitors.for_each(|arg| {
1418            match arg {
1419                hir::GenericArg::Lifetime(_) => prohibit_args.insert(ProhibitGenericsArg::Lifetime),
1420                hir::GenericArg::Type(_) => prohibit_args.insert(ProhibitGenericsArg::Type),
1421                hir::GenericArg::Const(_) => prohibit_args.insert(ProhibitGenericsArg::Const),
1422                hir::GenericArg::Infer(_) => prohibit_args.insert(ProhibitGenericsArg::Infer),
1423            };
1424        });
1425
1426        let segments: Vec<_> = segments.collect();
1427        let types_and_spans: Vec<_> = segments
1428            .iter()
1429            .flat_map(|segment| {
1430                if segment.args().args.is_empty() {
1431                    None
1432                } else {
1433                    Some((
1434                        match segment.res {
1435                            Res::PrimTy(ty) => {
1436                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}`", segment.res.descr(),
                ty.name()))
    })format!("{} `{}`", segment.res.descr(), ty.name())
1437                            }
1438                            Res::Def(_, def_id)
1439                                if let Some(name) = self.tcx().opt_item_name(def_id) =>
1440                            {
1441                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}`", segment.res.descr(),
                name))
    })format!("{} `{name}`", segment.res.descr())
1442                            }
1443                            Res::Err => "this type".to_string(),
1444                            _ => segment.res.descr().to_string(),
1445                        },
1446                        segment.ident.span,
1447                    ))
1448                }
1449            })
1450            .collect();
1451        let this_type = listify(&types_and_spans, |(t, _)| t.to_string())
1452            .expect("expected one segment to deny");
1453
1454        let arg_spans: Vec<Span> =
1455            segments.iter().flat_map(|segment| segment.args().args).map(|arg| arg.span()).collect();
1456
1457        let mut kinds = Vec::with_capacity(4);
1458        prohibit_args.iter().for_each(|arg| match arg {
1459            ProhibitGenericsArg::Lifetime => kinds.push("lifetime"),
1460            ProhibitGenericsArg::Type => kinds.push("type"),
1461            ProhibitGenericsArg::Const => kinds.push("const"),
1462            ProhibitGenericsArg::Infer => kinds.push("generic"),
1463        });
1464
1465        let s = if kinds.len() == 1 { "" } else { "s" }pluralize!(kinds.len());
1466        let kind =
1467            listify(&kinds, |k| k.to_string()).expect("expected at least one generic to prohibit");
1468        let last_span = *arg_spans.last().unwrap();
1469        let span: MultiSpan = arg_spans.into();
1470        let mut err = {
    self.dcx().struct_span_err(span,
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("{0} arguments are not allowed on {1}",
                            kind, this_type))
                })).with_code(E0109)
}struct_span_code_err!(
1471            self.dcx(),
1472            span,
1473            E0109,
1474            "{kind} arguments are not allowed on {this_type}",
1475        );
1476        err.span_label(last_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} argument{1} not allowed", kind,
                s))
    })format!("{kind} argument{s} not allowed"));
1477        for (what, span) in types_and_spans {
1478            err.span_label(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("not allowed on {0}", what))
    })format!("not allowed on {what}"));
1479        }
1480        generics_args_err_extend(self.tcx(), segments.into_iter(), &mut err, err_extend);
1481        err.emit()
1482    }
1483
1484    pub fn report_trait_object_addition_traits(
1485        &self,
1486        regular_traits: &Vec<(ty::PolyTraitPredicate<'tcx>, SmallVec<[Span; 1]>)>,
1487    ) -> ErrorGuaranteed {
1488        // we use the last span to point at the traits themselves,
1489        // and all other preceding spans are trait alias expansions.
1490        let (&first_span, first_alias_spans) = regular_traits[0].1.split_last().unwrap();
1491        let (&second_span, second_alias_spans) = regular_traits[1].1.split_last().unwrap();
1492        let mut err = {
    self.dcx().struct_span_err(*regular_traits[1].1.first().unwrap(),
            ::alloc::__export::must_use({
                    ::alloc::fmt::format(format_args!("only auto traits can be used as additional traits in a trait object"))
                })).with_code(E0225)
}struct_span_code_err!(
1493            self.dcx(),
1494            *regular_traits[1].1.first().unwrap(),
1495            E0225,
1496            "only auto traits can be used as additional traits in a trait object"
1497        );
1498        err.span_label(first_span, "first non-auto trait");
1499        for &alias_span in first_alias_spans {
1500            err.span_label(alias_span, "first non-auto trait comes from this alias");
1501        }
1502        err.span_label(second_span, "additional non-auto trait");
1503        for &alias_span in second_alias_spans {
1504            err.span_label(alias_span, "second non-auto trait comes from this alias");
1505        }
1506        err.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider creating a new trait with all of these as supertraits and using that trait here instead: `trait NewTrait: {0} {{}}`",
                regular_traits.iter().map(|(pred, _)|
                                pred.map_bound(|pred|
                                                pred.trait_ref).print_only_trait_path().to_string()).collect::<Vec<_>>().join(" + ")))
    })format!(
1507            "consider creating a new trait with all of these as supertraits and using that \
1508             trait here instead: `trait NewTrait: {} {{}}`",
1509            regular_traits
1510                .iter()
1511                // FIXME: This should `print_sugared`, but also needs to integrate projection bounds...
1512                .map(|(pred, _)| pred
1513                    .map_bound(|pred| pred.trait_ref)
1514                    .print_only_trait_path()
1515                    .to_string())
1516                .collect::<Vec<_>>()
1517                .join(" + "),
1518        ));
1519        err.note(
1520            "auto-traits like `Send` and `Sync` are traits that have special properties; \
1521             for more information on them, visit \
1522             <https://doc.rust-lang.org/reference/special-types-and-traits.html#auto-traits>",
1523        );
1524        err.emit()
1525    }
1526
1527    pub fn report_trait_object_with_no_traits(
1528        &self,
1529        span: Span,
1530        user_written_clauses: impl IntoIterator<Item = (ty::Clause<'tcx>, Span)>,
1531    ) -> ErrorGuaranteed {
1532        let tcx = self.tcx();
1533        let trait_alias_span = user_written_clauses
1534            .into_iter()
1535            .filter_map(|(clause, _)| clause.as_trait_clause())
1536            .find(|trait_ref| tcx.is_trait_alias(trait_ref.def_id()))
1537            .map(|trait_ref| tcx.def_span(trait_ref.def_id()));
1538
1539        self.dcx().emit_err(TraitObjectDeclaredWithNoTraits { span, trait_alias_span })
1540    }
1541}
1542
1543/// Emit an error for the given associated item constraint.
1544pub fn prohibit_assoc_item_constraint(
1545    cx: &dyn HirTyLowerer<'_>,
1546    constraint: &hir::AssocItemConstraint<'_>,
1547    segment: Option<(DefId, &hir::PathSegment<'_>, Span)>,
1548) -> ErrorGuaranteed {
1549    let tcx = cx.tcx();
1550    let mut err = cx.dcx().create_err(AssocItemConstraintsNotAllowedHere {
1551        span: constraint.span,
1552        fn_trait_expansion: if let Some((_, segment, span)) = segment
1553            && segment.args().parenthesized == hir::GenericArgsParentheses::ParenSugar
1554        {
1555            Some(ParenthesizedFnTraitExpansion {
1556                span,
1557                expanded_type: fn_trait_to_string(tcx, segment, false),
1558            })
1559        } else {
1560            None
1561        },
1562    });
1563
1564    // Emit a suggestion to turn the assoc item binding into a generic arg
1565    // if the relevant item has a generic param whose name matches the binding name;
1566    // otherwise suggest the removal of the binding.
1567    if let Some((def_id, segment, _)) = segment
1568        && segment.args().parenthesized == hir::GenericArgsParentheses::No
1569    {
1570        // Suggests removal of the offending binding
1571        let suggest_removal = |e: &mut Diag<'_>| {
1572            let constraints = segment.args().constraints;
1573            let args = segment.args().args;
1574
1575            // Compute the span to remove based on the position
1576            // of the binding. We do that as follows:
1577            //  1. Find the index of the binding in the list of bindings
1578            //  2. Locate the spans preceding and following the binding.
1579            //     If it's the first binding the preceding span would be
1580            //     that of the last arg
1581            //  3. Using this information work out whether the span
1582            //     to remove will start from the end of the preceding span,
1583            //     the start of the next span or will simply be the
1584            //     span encomassing everything within the generics brackets
1585
1586            let Some(index) = constraints.iter().position(|b| b.hir_id == constraint.hir_id) else {
1587                ::rustc_middle::util::bug::bug_fmt(format_args!("a type binding exists but its HIR ID not found in generics"));bug!("a type binding exists but its HIR ID not found in generics");
1588            };
1589
1590            let preceding_span = if index > 0 {
1591                Some(constraints[index - 1].span)
1592            } else {
1593                args.last().map(|a| a.span())
1594            };
1595
1596            let next_span = constraints.get(index + 1).map(|constraint| constraint.span);
1597
1598            let removal_span = match (preceding_span, next_span) {
1599                (Some(prec), _) => constraint.span.with_lo(prec.hi()),
1600                (None, Some(next)) => constraint.span.with_hi(next.lo()),
1601                (None, None) => {
1602                    let Some(generics_span) = segment.args().span_ext() else {
1603                        ::rustc_middle::util::bug::bug_fmt(format_args!("a type binding exists but generic span is empty"));bug!("a type binding exists but generic span is empty");
1604                    };
1605
1606                    generics_span
1607                }
1608            };
1609
1610            // Now emit the suggestion
1611            e.span_suggestion_verbose(
1612                removal_span,
1613                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider removing this associated item {0}",
                constraint.kind.descr()))
    })format!("consider removing this associated item {}", constraint.kind.descr()),
1614                "",
1615                Applicability::MaybeIncorrect,
1616            );
1617        };
1618
1619        // Suggest replacing the associated item binding with a generic argument.
1620        // i.e., replacing `<..., T = A, ...>` with `<..., A, ...>`.
1621        let suggest_direct_use = |e: &mut Diag<'_>, sp: Span| {
1622            if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(sp) {
1623                e.span_suggestion_verbose(
1624                    constraint.span,
1625                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("to use `{0}` as a generic argument specify it directly",
                snippet))
    })format!("to use `{snippet}` as a generic argument specify it directly"),
1626                    snippet,
1627                    Applicability::MaybeIncorrect,
1628                );
1629            }
1630        };
1631
1632        // Check if the type has a generic param with the same name
1633        // as the assoc type name in the associated item binding.
1634        let generics = tcx.generics_of(def_id);
1635        let matching_param = generics.own_params.iter().find(|p| p.name == constraint.ident.name);
1636
1637        // Now emit the appropriate suggestion
1638        if let Some(matching_param) = matching_param {
1639            match (constraint.kind, &matching_param.kind) {
1640                (
1641                    hir::AssocItemConstraintKind::Equality { term: hir::Term::Ty(ty) },
1642                    GenericParamDefKind::Type { .. },
1643                ) => suggest_direct_use(&mut err, ty.span),
1644                (
1645                    hir::AssocItemConstraintKind::Equality { term: hir::Term::Const(c) },
1646                    GenericParamDefKind::Const { .. },
1647                ) => {
1648                    suggest_direct_use(&mut err, c.span);
1649                }
1650                (hir::AssocItemConstraintKind::Bound { bounds }, _) => {
1651                    // Suggest `impl<T: Bound> Trait<T> for Foo` when finding
1652                    // `impl Trait<T: Bound> for Foo`
1653
1654                    // Get the parent impl block based on the binding we have
1655                    // and the trait DefId
1656                    let impl_block = tcx
1657                        .hir_parent_iter(constraint.hir_id)
1658                        .find_map(|(_, node)| node.impl_block_of_trait(def_id));
1659
1660                    let type_with_constraints =
1661                        tcx.sess.source_map().span_to_snippet(constraint.span);
1662
1663                    if let Some(impl_block) = impl_block
1664                        && let Ok(type_with_constraints) = type_with_constraints
1665                    {
1666                        // Filter out the lifetime parameters because
1667                        // they should be declared before the type parameter
1668                        let lifetimes: String = bounds
1669                            .iter()
1670                            .filter_map(|bound| {
1671                                if let hir::GenericBound::Outlives(lifetime) = bound {
1672                                    Some(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}, ", lifetime))
    })format!("{lifetime}, "))
1673                                } else {
1674                                    None
1675                                }
1676                            })
1677                            .collect();
1678                        // Figure out a span and suggestion string based on
1679                        // whether there are any existing parameters
1680                        let param_decl = if let Some(param_span) =
1681                            impl_block.generics.span_for_param_suggestion()
1682                        {
1683                            (param_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", {0}{1}", lifetimes,
                type_with_constraints))
    })format!(", {lifetimes}{type_with_constraints}"))
1684                        } else {
1685                            (
1686                                impl_block.generics.span.shrink_to_lo(),
1687                                ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}{1}>", lifetimes,
                type_with_constraints))
    })format!("<{lifetimes}{type_with_constraints}>"),
1688                            )
1689                        };
1690                        let suggestions = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [param_decl,
                (constraint.span.with_lo(constraint.ident.span.hi()),
                    String::new())]))vec![
1691                            param_decl,
1692                            (constraint.span.with_lo(constraint.ident.span.hi()), String::new()),
1693                        ];
1694
1695                        err.multipart_suggestion(
1696                            "declare the type parameter right after the `impl` keyword",
1697                            suggestions,
1698                            Applicability::MaybeIncorrect,
1699                        );
1700                    }
1701                }
1702                _ => suggest_removal(&mut err),
1703            }
1704        } else {
1705            suggest_removal(&mut err);
1706        }
1707    }
1708
1709    err.emit()
1710}
1711
1712pub(crate) fn fn_trait_to_string(
1713    tcx: TyCtxt<'_>,
1714    trait_segment: &hir::PathSegment<'_>,
1715    parenthesized: bool,
1716) -> String {
1717    let args = trait_segment
1718        .args
1719        .and_then(|args| args.args.first())
1720        .and_then(|arg| match arg {
1721            hir::GenericArg::Type(ty) => match ty.kind {
1722                hir::TyKind::Tup(t) => t
1723                    .iter()
1724                    .map(|e| tcx.sess.source_map().span_to_snippet(e.span))
1725                    .collect::<Result<Vec<_>, _>>()
1726                    .map(|a| a.join(", ")),
1727                _ => tcx.sess.source_map().span_to_snippet(ty.span),
1728            }
1729            .map(|s| {
1730                // `is_empty()` checks to see if the type is the unit tuple, if so we don't want a comma
1731                if parenthesized || s.is_empty() { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0})", s))
    })format!("({s})") } else { ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("({0},)", s))
    })format!("({s},)") }
1732            })
1733            .ok(),
1734            _ => None,
1735        })
1736        .unwrap_or_else(|| "()".to_string());
1737
1738    let ret = trait_segment
1739        .args()
1740        .constraints
1741        .iter()
1742        .find_map(|c| {
1743            if c.ident.name == sym::Output
1744                && let Some(ty) = c.ty()
1745                && ty.span != tcx.hir_span(trait_segment.hir_id)
1746            {
1747                tcx.sess.source_map().span_to_snippet(ty.span).ok()
1748            } else {
1749                None
1750            }
1751        })
1752        .unwrap_or_else(|| "()".to_string());
1753
1754    if parenthesized {
1755        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}{1} -> {2}",
                trait_segment.ident, args, ret))
    })format!("{}{} -> {}", trait_segment.ident, args, ret)
1756    } else {
1757        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}<{1}, Output={2}>",
                trait_segment.ident, args, ret))
    })format!("{}<{}, Output={}>", trait_segment.ident, args, ret)
1758    }
1759}
1760
1761/// Used for generics args error extend.
1762pub enum GenericsArgsErrExtend<'tcx> {
1763    EnumVariant {
1764        qself: &'tcx hir::Ty<'tcx>,
1765        assoc_segment: &'tcx hir::PathSegment<'tcx>,
1766        adt_def: AdtDef<'tcx>,
1767    },
1768    OpaqueTy,
1769    PrimTy(hir::PrimTy),
1770    SelfTyAlias {
1771        def_id: DefId,
1772        span: Span,
1773    },
1774    SelfTyParam(Span),
1775    Param(DefId),
1776    DefVariant(&'tcx [hir::PathSegment<'tcx>]),
1777    None,
1778}
1779
1780fn generics_args_err_extend<'a>(
1781    tcx: TyCtxt<'_>,
1782    segments: impl Iterator<Item = &'a hir::PathSegment<'a>> + Clone,
1783    err: &mut Diag<'_>,
1784    err_extend: GenericsArgsErrExtend<'a>,
1785) {
1786    match err_extend {
1787        GenericsArgsErrExtend::EnumVariant { qself, assoc_segment, adt_def } => {
1788            err.note("enum variants can't have type parameters");
1789            let type_name = tcx.item_name(adt_def.did());
1790            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you might have meant to specify type parameters on enum `{0}`",
                type_name))
    })format!(
1791                "you might have meant to specify type parameters on enum \
1792                `{type_name}`"
1793            );
1794            let Some(args) = assoc_segment.args else {
1795                return;
1796            };
1797            // Get the span of the generics args *including* the leading `::`.
1798            // We do so by stretching args.span_ext to the left by 2. Earlier
1799            // it was done based on the end of assoc segment but that sometimes
1800            // led to impossible spans and caused issues like #116473
1801            let args_span = args.span_ext.with_lo(args.span_ext.lo() - BytePos(2));
1802            if tcx.generics_of(adt_def.did()).is_empty() {
1803                // FIXME(estebank): we could also verify that the arguments being
1804                // work for the `enum`, instead of just looking if it takes *any*.
1805                err.span_suggestion_verbose(
1806                    args_span,
1807                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} doesn\'t have generic parameters",
                type_name))
    })format!("{type_name} doesn't have generic parameters"),
1808                    "",
1809                    Applicability::MachineApplicable,
1810                );
1811                return;
1812            }
1813            let Ok(snippet) = tcx.sess.source_map().span_to_snippet(args_span) else {
1814                err.note(msg);
1815                return;
1816            };
1817            let (qself_sugg_span, is_self) =
1818                if let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = &qself.kind {
1819                    // If the path segment already has type params, we want to overwrite
1820                    // them.
1821                    match &path.segments {
1822                        // `segment` is the previous to last element on the path,
1823                        // which would normally be the `enum` itself, while the last
1824                        // `_` `PathSegment` corresponds to the variant.
1825                        [
1826                            ..,
1827                            hir::PathSegment {
1828                                ident, args, res: Res::Def(DefKind::Enum, _), ..
1829                            },
1830                            _,
1831                        ] => (
1832                            // We need to include the `::` in `Type::Variant::<Args>`
1833                            // to point the span to `::<Args>`, not just `<Args>`.
1834                            ident
1835                                .span
1836                                .shrink_to_hi()
1837                                .to(args.map_or(ident.span.shrink_to_hi(), |a| a.span_ext)),
1838                            false,
1839                        ),
1840                        [segment] => {
1841                            (
1842                                // We need to include the `::` in `Type::Variant::<Args>`
1843                                // to point the span to `::<Args>`, not just `<Args>`.
1844                                segment.ident.span.shrink_to_hi().to(segment
1845                                    .args
1846                                    .map_or(segment.ident.span.shrink_to_hi(), |a| a.span_ext)),
1847                                kw::SelfUpper == segment.ident.name,
1848                            )
1849                        }
1850                        _ => {
1851                            err.note(msg);
1852                            return;
1853                        }
1854                    }
1855                } else {
1856                    err.note(msg);
1857                    return;
1858                };
1859            let suggestion = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [if is_self {
                    (qself.span,
                        ::alloc::__export::must_use({
                                ::alloc::fmt::format(format_args!("{0}{1}", type_name,
                                        snippet))
                            }))
                } else { (qself_sugg_span, snippet) },
                (args_span, String::new())]))vec![
1860                if is_self {
1861                    // Account for people writing `Self::Variant::<Args>`, where
1862                    // `Self` is the enum, and suggest replacing `Self` with the
1863                    // appropriate type: `Type::<Args>::Variant`.
1864                    (qself.span, format!("{type_name}{snippet}"))
1865                } else {
1866                    (qself_sugg_span, snippet)
1867                },
1868                (args_span, String::new()),
1869            ];
1870            err.multipart_suggestion(msg, suggestion, Applicability::MaybeIncorrect);
1871        }
1872        GenericsArgsErrExtend::DefVariant(segments) => {
1873            let args: Vec<Span> = segments
1874                .iter()
1875                .filter_map(|segment| match segment.res {
1876                    Res::Def(
1877                        DefKind::Ctor(CtorOf::Variant, _) | DefKind::Variant | DefKind::Enum,
1878                        _,
1879                    ) => segment.args().span_ext().map(|s| s.with_lo(segment.ident.span.hi())),
1880                    _ => None,
1881                })
1882                .collect();
1883            if args.len() > 1
1884                && let Some(span) = args.into_iter().next_back()
1885            {
1886                err.note(
1887                    "generic arguments are not allowed on both an enum and its variant's path \
1888                     segments simultaneously; they are only valid in one place or the other",
1889                );
1890                err.span_suggestion_verbose(
1891                    span,
1892                    "remove the generics arguments from one of the path segments",
1893                    String::new(),
1894                    Applicability::MaybeIncorrect,
1895                );
1896            }
1897        }
1898        GenericsArgsErrExtend::PrimTy(prim_ty) => {
1899            let name = prim_ty.name_str();
1900            for segment in segments {
1901                if let Some(args) = segment.args {
1902                    err.span_suggestion_verbose(
1903                        segment.ident.span.shrink_to_hi().to(args.span_ext),
1904                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("primitive type `{0}` doesn\'t have generic parameters",
                name))
    })format!("primitive type `{name}` doesn't have generic parameters"),
1905                        "",
1906                        Applicability::MaybeIncorrect,
1907                    );
1908                }
1909            }
1910        }
1911        GenericsArgsErrExtend::OpaqueTy => {
1912            err.note("`impl Trait` types can't have type parameters");
1913        }
1914        GenericsArgsErrExtend::Param(def_id) => {
1915            let span = tcx.def_ident_span(def_id).unwrap();
1916            let kind = tcx.def_descr(def_id);
1917            let name = tcx.item_name(def_id);
1918            err.span_note(span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} `{1}` defined here", kind,
                name))
    })format!("{kind} `{name}` defined here"));
1919        }
1920        GenericsArgsErrExtend::SelfTyParam(span) => {
1921            err.span_suggestion_verbose(
1922                span,
1923                "the `Self` type doesn't accept type parameters",
1924                "",
1925                Applicability::MaybeIncorrect,
1926            );
1927        }
1928        GenericsArgsErrExtend::SelfTyAlias { def_id, span } => {
1929            let ty = tcx.at(span).type_of(def_id).instantiate_identity().skip_norm_wip();
1930            let span_of_impl = tcx.span_of_impl(def_id);
1931            let ty::Adt(self_def, _) = *ty.kind() else { return };
1932            let def_id = self_def.did();
1933
1934            let type_name = tcx.item_name(def_id);
1935            let span_of_ty = tcx.def_ident_span(def_id);
1936            let generics = tcx.generics_of(def_id).count();
1937
1938            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`Self` is of type `{0}`", ty))
    })format!("`Self` is of type `{ty}`");
1939            if let (Ok(i_sp), Some(t_sp)) = (span_of_impl, span_of_ty) {
1940                let mut span: MultiSpan = ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [t_sp]))vec![t_sp].into();
1941                span.push_span_label(
1942                    i_sp,
1943                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`Self` is on type `{0}` in this `impl`",
                type_name))
    })format!("`Self` is on type `{type_name}` in this `impl`"),
1944                );
1945                let mut postfix = "";
1946                if generics == 0 {
1947                    postfix = ", which doesn't have generic parameters";
1948                }
1949                span.push_span_label(t_sp, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`Self` corresponds to this type{0}",
                postfix))
    })format!("`Self` corresponds to this type{postfix}"));
1950                err.span_note(span, msg);
1951            } else {
1952                err.note(msg);
1953            }
1954            for segment in segments {
1955                if let Some(args) = segment.args
1956                    && segment.ident.name == kw::SelfUpper
1957                {
1958                    if generics == 0 {
1959                        // FIXME(estebank): we could also verify that the arguments being
1960                        // work for the `enum`, instead of just looking if it takes *any*.
1961                        err.span_suggestion_verbose(
1962                            segment.ident.span.shrink_to_hi().to(args.span_ext),
1963                            "the `Self` type doesn't accept type parameters",
1964                            "",
1965                            Applicability::MachineApplicable,
1966                        );
1967                        return;
1968                    } else {
1969                        err.span_suggestion_verbose(
1970                            segment.ident.span,
1971                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("the `Self` type doesn\'t accept type parameters, use the concrete type\'s name `{0}` instead if you want to specify its type parameters",
                type_name))
    })format!(
1972                                "the `Self` type doesn't accept type parameters, use the \
1973                                concrete type's name `{type_name}` instead if you want to \
1974                                specify its type parameters"
1975                            ),
1976                            type_name,
1977                            Applicability::MaybeIncorrect,
1978                        );
1979                    }
1980                }
1981            }
1982        }
1983        _ => {}
1984    }
1985}
1986
1987pub(super) struct AmbiguityBetweenVariantAndAssocItem<'tcx> {
1988    pub(super) variant_def_id: DefId,
1989    pub(super) item_def_id: DefId,
1990    pub(super) span: Span,
1991    pub(super) segment_ident: Ident,
1992    pub(super) bound_def_id: DefId,
1993    pub(super) self_ty: Ty<'tcx>,
1994    pub(super) tcx: TyCtxt<'tcx>,
1995    pub(super) mode: super::LowerTypeRelativePathMode,
1996}
1997
1998impl<'a, 'tcx> rustc_errors::Diagnostic<'a, ()> for AmbiguityBetweenVariantAndAssocItem<'tcx> {
1999    fn into_diag(
2000        self,
2001        dcx: rustc_errors::DiagCtxtHandle<'a>,
2002        level: rustc_errors::Level,
2003    ) -> Diag<'a, ()> {
2004        let Self {
2005            variant_def_id,
2006            item_def_id,
2007            span,
2008            segment_ident,
2009            bound_def_id,
2010            self_ty,
2011            tcx,
2012            mode,
2013        } = self;
2014        let mut lint = Diag::new(dcx, level, "ambiguous associated item");
2015
2016        let mut could_refer_to = |kind: DefKind, def_id, also| {
2017            let note_msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` could{1} refer to the {2} defined here",
                segment_ident, also, tcx.def_kind_descr(kind, def_id)))
    })format!(
2018                "`{}` could{} refer to the {} defined here",
2019                segment_ident,
2020                also,
2021                tcx.def_kind_descr(kind, def_id)
2022            );
2023            lint.span_note(tcx.def_span(def_id), note_msg);
2024        };
2025
2026        could_refer_to(DefKind::Variant, variant_def_id, "");
2027        could_refer_to(mode.def_kind_for_diagnostics(), item_def_id, " also");
2028
2029        lint.span_suggestion(
2030            span,
2031            "use fully-qualified syntax",
2032            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0} as {1}>::{2}", self_ty,
                tcx.item_name(bound_def_id), segment_ident))
    })format!("<{} as {}>::{}", self_ty, tcx.item_name(bound_def_id), segment_ident),
2033            Applicability::MachineApplicable,
2034        );
2035        lint
2036    }
2037}
2038
2039fn assoc_tag_str(assoc_tag: ty::AssocTag) -> &'static str {
2040    match assoc_tag {
2041        ty::AssocTag::Fn => "function",
2042        ty::AssocTag::Const => "constant",
2043        ty::AssocTag::Type => "type",
2044    }
2045}