Skip to main content

rustc_trait_selection/error_reporting/infer/
note_and_explain.rs

1use rustc_errors::Applicability::{MachineApplicable, MaybeIncorrect};
2use rustc_errors::{Diag, MultiSpan, pluralize};
3use rustc_hir as hir;
4use rustc_hir::def::DefKind;
5use rustc_hir::find_attr;
6use rustc_middle::traits::{ObligationCause, ObligationCauseCode};
7use rustc_middle::ty::error::{ExpectedFound, TypeError};
8use rustc_middle::ty::fast_reject::DeepRejectCtxt;
9use rustc_middle::ty::print::{FmtPrinter, Printer};
10use rustc_middle::ty::{self, Ty, suggest_constraining_type_param};
11use rustc_span::def_id::DefId;
12use rustc_span::{BytePos, Span, Symbol};
13use tracing::debug;
14
15use crate::error_reporting::TypeErrCtxt;
16use crate::infer::InferCtxtExt;
17
18impl<'tcx> TypeErrCtxt<'_, 'tcx> {
19    pub fn note_and_explain_type_err(
20        &self,
21        diag: &mut Diag<'_>,
22        err: TypeError<'tcx>,
23        cause: &ObligationCause<'tcx>,
24        sp: Span,
25        body_owner_def_id: Option<DefId>,
26    ) {
27        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs:27",
                        "rustc_trait_selection::error_reporting::infer::note_and_explain",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs"),
                        ::tracing_core::__macro_support::Option::Some(27u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::note_and_explain"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("note_and_explain_type_err err={0:?} cause={1:?}",
                                                    err, cause) as &dyn Value))])
            });
    } else { ; }
};debug!("note_and_explain_type_err err={:?} cause={:?}", err, cause);
28
29        let tcx = self.tcx;
30
31        let body_generics = body_owner_def_id.map(|def_id| tcx.generics_of(def_id));
32
33        match err {
34            TypeError::ArgumentSorts(values, _) | TypeError::Sorts(values) => {
35                match (*values.expected.kind(), *values.found.kind()) {
36                    (ty::Closure(..), ty::Closure(..)) => {
37                        diag.note("no two closures, even if identical, have the same type");
38                        diag.help("consider boxing your closure and/or using it as a trait object");
39                    }
40                    (ty::Coroutine(def_id1, ..), ty::Coroutine(def_id2, ..))
41                        if self.tcx.coroutine_is_async(def_id1)
42                            && self.tcx.coroutine_is_async(def_id2) =>
43                    {
44                        diag.note("no two async blocks, even if identical, have the same type");
45                        diag.help(
46                            "consider pinning your async block and casting it to a trait object",
47                        );
48                    }
49                    (
50                        ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }),
51                        ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }),
52                    ) => {
53                        // Issue #63167
54                        diag.note("distinct uses of `impl Trait` result in different opaque types");
55                    }
56                    (ty::Float(_), ty::Infer(ty::IntVar(_)))
57                        if let Ok(
58                            // Issue #53280
59                            snippet,
60                        ) = tcx.sess.source_map().span_to_snippet(sp) =>
61                    {
62                        if snippet.chars().all(|c| c.is_digit(10) || c == '-' || c == '_') {
63                            diag.span_suggestion_verbose(
64                                sp.shrink_to_hi(),
65                                "use a float literal",
66                                ".0",
67                                MachineApplicable,
68                            );
69                        }
70                    }
71                    (ty::Param(expected), ty::Param(found)) => {
72                        if let Some(generics) = body_generics {
73                            let e_span = tcx.def_span(generics.type_param(expected, tcx).def_id);
74                            if !sp.contains(e_span) {
75                                diag.span_label(e_span, "expected type parameter");
76                            }
77                            let f_span = tcx.def_span(generics.type_param(found, tcx).def_id);
78                            if !sp.contains(f_span) {
79                                diag.span_label(f_span, "found type parameter");
80                            }
81                        }
82                        diag.note(
83                            "a type parameter was expected, but a different one was found; \
84                             you might be missing a type parameter or trait bound",
85                        );
86                        diag.note(
87                            "for more information, visit \
88                             https://doc.rust-lang.org/book/ch10-02-traits.html\
89                             #traits-as-parameters",
90                        );
91                    }
92                    (
93                        ty::Alias(ty::AliasTy {
94                            kind: ty::Projection { .. } | ty::Inherent { .. },
95                            ..
96                        }),
97                        ty::Alias(ty::AliasTy {
98                            kind: ty::Projection { .. } | ty::Inherent { .. },
99                            ..
100                        }),
101                    ) => {
102                        diag.note("an associated type was expected, but a different one was found");
103                    }
104                    // FIXME(inherent_associated_types): Extend this to support `ty::Inherent`, too.
105                    (
106                        ty::Param(p),
107                        ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. }),
108                    )
109                    | (
110                        ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. }),
111                        ty::Param(p),
112                    ) if !tcx.is_impl_trait_in_trait(def_id)
113                        && let Some(generics) = body_generics =>
114                    {
115                        let param = generics.type_param(p, tcx);
116                        let p_def_id = param.def_id;
117                        let p_span = tcx.def_span(p_def_id);
118                        let expected = match (values.expected.kind(), values.found.kind()) {
119                            (ty::Param(_), _) => "expected ",
120                            (_, ty::Param(_)) => "found ",
121                            _ => "",
122                        };
123                        if !sp.contains(p_span) {
124                            diag.span_label(p_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}this type parameter", expected))
    })format!("{expected}this type parameter"));
125                        }
126                        let param_def_id = match *proj.self_ty().kind() {
127                            ty::Param(param) => generics.type_param(param, tcx).def_id,
128                            _ => p_def_id,
129                        };
130                        let parent = param_def_id.as_local().and_then(|id| {
131                            let local_id = tcx.local_def_id_to_hir_id(id);
132                            let generics = tcx.parent_hir_node(local_id).generics()?;
133                            Some((id, generics))
134                        });
135                        let mut note = true;
136                        if let Some((local_id, generics)) = parent {
137                            // Synthesize the associated type restriction `Add<Output = Expected>`.
138                            // FIXME: extract this logic for use in other diagnostics.
139                            let (trait_ref, assoc_args) = proj.trait_ref_and_own_args(tcx);
140                            let item_name = tcx.item_name(def_id);
141                            let item_args = self.format_generic_args(assoc_args);
142
143                            // Here, we try to see if there's an existing
144                            // trait implementation that matches the one that
145                            // we're suggesting to restrict. If so, find the
146                            // "end", whether it be at the end of the trait
147                            // or the end of the generic arguments.
148                            let mut matching_span = None;
149                            let mut matched_end_of_args = false;
150                            for bound in generics.bounds_for_param(local_id) {
151                                let potential_spans = bound.bounds.iter().find_map(|bound| {
152                                    let bound_trait_path = bound.trait_ref()?.path;
153                                    let def_id = bound_trait_path.res.opt_def_id()?;
154                                    let generic_args = bound_trait_path
155                                        .segments
156                                        .iter()
157                                        .last()
158                                        .map(|path| path.args());
159                                    (def_id == trait_ref.def_id)
160                                        .then_some((bound_trait_path.span, generic_args))
161                                });
162
163                                if let Some((end_of_trait, end_of_args)) = potential_spans {
164                                    let args_span = end_of_args.and_then(|args| args.span());
165                                    matched_end_of_args = args_span.is_some();
166                                    matching_span = args_span
167                                        .or_else(|| Some(end_of_trait))
168                                        .map(|span| span.shrink_to_hi());
169                                    break;
170                                }
171                            }
172
173                            if matched_end_of_args {
174                                // Append suggestion to the end of our args
175                                let path = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", {0}{1} = {2}", item_name,
                item_args, p))
    })format!(", {item_name}{item_args} = {p}");
176                                note = !suggest_constraining_type_param(
177                                    tcx,
178                                    generics,
179                                    diag,
180                                    &proj.self_ty().to_string(),
181                                    &path,
182                                    None,
183                                    matching_span,
184                                );
185                            } else {
186                                // Suggest adding a bound to an existing trait
187                                // or if the trait doesn't exist, add the trait
188                                // and the suggested bounds.
189                                let path = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}{1} = {2}>", item_name,
                item_args, p))
    })format!("<{item_name}{item_args} = {p}>");
190                                note = !suggest_constraining_type_param(
191                                    tcx,
192                                    generics,
193                                    diag,
194                                    &proj.self_ty().to_string(),
195                                    &path,
196                                    None,
197                                    matching_span,
198                                );
199                            }
200                        }
201                        if note {
202                            diag.note("you might be missing a type parameter or trait bound");
203                        }
204                    }
205                    (
206                        ty::Param(p),
207                        ty::Dynamic(..) | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }),
208                    )
209                    | (
210                        ty::Dynamic(..) | ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }),
211                        ty::Param(p),
212                    ) => {
213                        if let Some(generics) = body_generics {
214                            let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
215                            let expected = match (values.expected.kind(), values.found.kind()) {
216                                (ty::Param(_), _) => "expected ",
217                                (_, ty::Param(_)) => "found ",
218                                _ => "",
219                            };
220                            if !sp.contains(p_span) {
221                                diag.span_label(p_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}this type parameter", expected))
    })format!("{expected}this type parameter"));
222                            }
223                        }
224                        diag.help("type parameters must be constrained to match other types");
225                        if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
226                            diag.help(
227                                "given a type parameter `T` and a method `foo`:
228```
229trait Trait<T> { fn foo(&self) -> T; }
230```
231the only ways to implement method `foo` are:
232- constrain `T` with an explicit type:
233```
234impl Trait<String> for X {
235    fn foo(&self) -> String { String::new() }
236}
237```
238- add a trait bound to `T` and call a method on that trait that returns `Self`:
239```
240impl<T: std::default::Default> Trait<T> for X {
241    fn foo(&self) -> T { <T as std::default::Default>::default() }
242}
243```
244- change `foo` to return an argument of type `T`:
245```
246impl<T> Trait<T> for X {
247    fn foo(&self, x: T) -> T { x }
248}
249```",
250                            );
251                        }
252                        diag.note(
253                            "for more information, visit \
254                             https://doc.rust-lang.org/book/ch10-02-traits.html\
255                             #traits-as-parameters",
256                        );
257                    }
258                    (
259                        ty::Param(p),
260                        ty::Closure(..) | ty::CoroutineClosure(..) | ty::Coroutine(..),
261                    ) => {
262                        if let Some(generics) = body_generics {
263                            let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
264                            if !sp.contains(p_span) {
265                                diag.span_label(p_span, "expected this type parameter");
266                            }
267                        }
268                        diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("every closure has a distinct type and so could not always match the caller-chosen type of parameter `{0}`",
                p))
    })format!(
269                            "every closure has a distinct type and so could not always match the \
270                             caller-chosen type of parameter `{p}`"
271                        ));
272                    }
273                    (ty::Param(p), _) | (_, ty::Param(p)) if let Some(generics) = body_generics => {
274                        let p_span = tcx.def_span(generics.type_param(p, tcx).def_id);
275                        let expected = match (values.expected.kind(), values.found.kind()) {
276                            (ty::Param(_), _) => "expected ",
277                            (_, ty::Param(_)) => "found ",
278                            _ => "",
279                        };
280                        if !sp.contains(p_span) {
281                            diag.span_label(p_span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0}this type parameter", expected))
    })format!("{expected}this type parameter"));
282                        }
283                    }
284                    (
285                        ty::Alias(
286                            proj_ty @ ty::AliasTy {
287                                kind: ty::Projection { def_id } | ty::Inherent { def_id },
288                                ..
289                            },
290                        ),
291                        _,
292                    ) if !tcx.is_impl_trait_in_trait(def_id) => {
293                        self.expected_projection(
294                            diag,
295                            proj_ty,
296                            values,
297                            body_owner_def_id,
298                            cause.code(),
299                        );
300                    }
301                    // Don't suggest constraining a projection to something
302                    // containing itself, e.g. `Item = &<I as Iterator>::Item`.
303                    (
304                        _,
305                        ty::Alias(
306                            proj_ty @ ty::AliasTy {
307                                kind: ty::Projection { def_id } | ty::Inherent { def_id },
308                                ..
309                            },
310                        ),
311                    ) if !tcx.is_impl_trait_in_trait(def_id)
312                        && !tcx
313                            .erase_and_anonymize_regions(values.expected)
314                            .contains(tcx.erase_and_anonymize_regions(values.found)) =>
315                    {
316                        let msg = || {
317                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider constraining the associated type `{0}` to `{1}`",
                values.found, values.expected))
    })format!(
318                                "consider constraining the associated type `{}` to `{}`",
319                                values.found, values.expected,
320                            )
321                        };
322                        let suggested_projection_constraint =
323                            #[allow(non_exhaustive_omitted_patterns)] match proj_ty.kind {
    ty::Projection { .. } => true,
    _ => false,
}matches!(proj_ty.kind, ty::Projection { .. })
324                                && (self.suggest_constraining_opaque_associated_type(
325                                    diag,
326                                    msg,
327                                    proj_ty,
328                                    values.expected,
329                                ) || self.suggest_constraint(
330                                    diag,
331                                    &msg,
332                                    body_owner_def_id,
333                                    proj_ty,
334                                    values.expected,
335                                ));
336                        if !suggested_projection_constraint {
337                            diag.help(msg());
338                            diag.note(
339                                "for more information, visit \
340                                https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
341                            );
342                        }
343                    }
344                    (
345                        ty::Dynamic(t, _),
346                        ty::Alias(ty::AliasTy {
347                            kind: ty::Opaque { def_id: opaque_def_id }, ..
348                        }),
349                    ) if let Some(def_id) = t.principal_def_id()
350                        && tcx
351                            .explicit_item_self_bounds(opaque_def_id)
352                            .skip_binder()
353                            .iter()
354                            .any(|(pred, _span)| match pred.kind().skip_binder() {
355                                ty::ClauseKind::Trait(trait_predicate)
356                                    if trait_predicate.polarity
357                                        == ty::PredicatePolarity::Positive =>
358                                {
359                                    trait_predicate.def_id() == def_id
360                                }
361                                _ => false,
362                            }) =>
363                    {
364                        diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("you can box the `{0}` to coerce it to `Box<{1}>`, but you\'ll have to change the expected type as well",
                values.found, values.expected))
    })format!(
365                            "you can box the `{}` to coerce it to `Box<{}>`, but you'll have to \
366                             change the expected type as well",
367                            values.found, values.expected,
368                        ));
369                    }
370                    (ty::Dynamic(t, _), _) if let Some(def_id) = t.principal_def_id() => {
371                        let mut has_matching_impl = false;
372                        tcx.for_each_relevant_impl(def_id, values.found, |did| {
373                            if DeepRejectCtxt::relate_rigid_infer(tcx)
374                                .types_may_unify(values.found, tcx.type_of(did).skip_binder())
375                            {
376                                has_matching_impl = true;
377                            }
378                        });
379                        if has_matching_impl {
380                            let trait_name = tcx.item_name(def_id);
381                            diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you could box the found value and coerce it to the trait object `Box<dyn {1}>`, you will have to change the expected type as well",
                values.found, trait_name))
    })format!(
382                                "`{}` implements `{trait_name}` so you could box the found value \
383                                 and coerce it to the trait object `Box<dyn {trait_name}>`, you \
384                                 will have to change the expected type as well",
385                                values.found,
386                            ));
387                        }
388                    }
389                    (_, ty::Dynamic(t, _)) if let Some(def_id) = t.principal_def_id() => {
390                        let mut has_matching_impl = false;
391                        tcx.for_each_relevant_impl(def_id, values.expected, |did| {
392                            if DeepRejectCtxt::relate_rigid_infer(tcx)
393                                .types_may_unify(values.expected, tcx.type_of(did).skip_binder())
394                            {
395                                has_matching_impl = true;
396                            }
397                        });
398                        if has_matching_impl {
399                            let trait_name = tcx.item_name(def_id);
400                            diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you could change the expected type to `Box<dyn {1}>`",
                values.expected, trait_name))
    })format!(
401                                "`{}` implements `{trait_name}` so you could change the expected \
402                                 type to `Box<dyn {trait_name}>`",
403                                values.expected,
404                            ));
405                        }
406                    }
407                    (_, ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }))
408                    | (ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }), _) => {
409                        if let Some(body_owner_def_id) = body_owner_def_id
410                            && def_id.is_local()
411                            && #[allow(non_exhaustive_omitted_patterns)] match tcx.def_kind(body_owner_def_id)
    {
    DefKind::Fn | DefKind::Static { .. } | DefKind::Const { .. } |
        DefKind::AssocFn | DefKind::AssocConst { .. } => true,
    _ => false,
}matches!(
412                                tcx.def_kind(body_owner_def_id),
413                                DefKind::Fn
414                                    | DefKind::Static { .. }
415                                    | DefKind::Const { .. }
416                                    | DefKind::AssocFn
417                                    | DefKind::AssocConst { .. }
418                            )
419                            && #[allow(non_exhaustive_omitted_patterns)] match tcx.opaque_ty_origin(def_id) {
    hir::OpaqueTyOrigin::TyAlias { .. } => true,
    _ => false,
}matches!(
420                                tcx.opaque_ty_origin(def_id),
421                                hir::OpaqueTyOrigin::TyAlias { .. }
422                            )
423                            && !tcx
424                                .opaque_types_defined_by(body_owner_def_id.expect_local())
425                                .contains(&def_id.expect_local())
426                        {
427                            let sp = tcx
428                                .def_ident_span(body_owner_def_id)
429                                .unwrap_or_else(|| tcx.def_span(body_owner_def_id));
430                            let mut alias_def_id = def_id;
431                            while let DefKind::OpaqueTy = tcx.def_kind(alias_def_id) {
432                                alias_def_id = tcx.parent(alias_def_id);
433                            }
434                            let opaque_path = tcx.def_path_str(alias_def_id);
435                            // FIXME(type_alias_impl_trait): make this a structured suggestion
436                            match tcx.opaque_ty_origin(def_id) {
437                                rustc_hir::OpaqueTyOrigin::FnReturn { .. } => {}
438                                rustc_hir::OpaqueTyOrigin::AsyncFn { .. } => {}
439                                rustc_hir::OpaqueTyOrigin::TyAlias {
440                                    in_assoc_ty: false, ..
441                                } => {
442                                    diag.span_note(
443                                        sp,
444                                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("this item must have a `#[define_opaque({0})]` attribute to be able to define hidden types",
                opaque_path))
    })format!("this item must have a `#[define_opaque({opaque_path})]` \
445                                        attribute to be able to define hidden types"),
446                                    );
447                                }
448                                rustc_hir::OpaqueTyOrigin::TyAlias {
449                                    in_assoc_ty: true, ..
450                                } => {}
451                            }
452                        }
453                        // If two if arms can be coerced to a trait object, provide a structured
454                        // suggestion.
455                        let ObligationCauseCode::IfExpression { expr_id, .. } = cause.code() else {
456                            return;
457                        };
458                        let hir::Node::Expr(&hir::Expr {
459                            kind:
460                                hir::ExprKind::If(
461                                    _,
462                                    &hir::Expr {
463                                        kind:
464                                            hir::ExprKind::Block(
465                                                &hir::Block { expr: Some(then), .. },
466                                                _,
467                                            ),
468                                        ..
469                                    },
470                                    Some(&hir::Expr {
471                                        kind:
472                                            hir::ExprKind::Block(
473                                                &hir::Block { expr: Some(else_), .. },
474                                                _,
475                                            ),
476                                        ..
477                                    }),
478                                ),
479                            ..
480                        }) = self.tcx.hir_node(*expr_id)
481                        else {
482                            return;
483                        };
484                        let expected = match values.found.kind() {
485                            ty::Alias(..) => values.expected,
486                            _ => values.found,
487                        };
488                        let preds = tcx.explicit_item_self_bounds(def_id);
489                        for (pred, _span) in preds.skip_binder() {
490                            let ty::ClauseKind::Trait(trait_predicate) = pred.kind().skip_binder()
491                            else {
492                                continue;
493                            };
494                            if trait_predicate.polarity != ty::PredicatePolarity::Positive {
495                                continue;
496                            }
497                            let def_id = trait_predicate.def_id();
498                            let mut impl_def_ids = ::alloc::vec::Vec::new()vec![];
499                            tcx.for_each_relevant_impl(def_id, expected, |did| {
500                                impl_def_ids.push(did)
501                            });
502                            if let [_] = &impl_def_ids[..] {
503                                let trait_name = tcx.item_name(def_id);
504                                diag.multipart_suggestion(
505                                    ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you can box both arms and coerce to the trait object `Box<dyn {1}>`",
                expected, trait_name))
    })format!(
506                                        "`{expected}` implements `{trait_name}` so you can box \
507                                         both arms and coerce to the trait object \
508                                         `Box<dyn {trait_name}>`",
509                                    ),
510                                    ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(then.span.shrink_to_lo(), "Box::new(".to_string()),
                (then.span.shrink_to_hi(),
                    ::alloc::__export::must_use({
                            ::alloc::fmt::format(format_args!(") as Box<dyn {0}>",
                                    tcx.def_path_str(def_id)))
                        })), (else_.span.shrink_to_lo(), "Box::new(".to_string()),
                (else_.span.shrink_to_hi(), ")".to_string())]))vec![
511                                        (then.span.shrink_to_lo(), "Box::new(".to_string()),
512                                        (
513                                            then.span.shrink_to_hi(),
514                                            format!(") as Box<dyn {}>", tcx.def_path_str(def_id)),
515                                        ),
516                                        (else_.span.shrink_to_lo(), "Box::new(".to_string()),
517                                        (else_.span.shrink_to_hi(), ")".to_string()),
518                                    ],
519                                    MachineApplicable,
520                                );
521                            }
522                        }
523                    }
524                    (ty::FnPtr(_, hdr), ty::FnDef(def_id, _))
525                    | (ty::FnDef(def_id, _), ty::FnPtr(_, hdr)) => {
526                        if tcx.fn_sig(def_id).skip_binder().safety() < hdr.safety {
527                            if !tcx.codegen_fn_attrs(def_id).safe_target_features {
528                                diag.note(
529                                "unsafe functions cannot be coerced into safe function pointers",
530                                );
531                            }
532                        }
533                    }
534                    (ty::Adt(_, _), ty::Adt(def, args))
535                        if let ObligationCauseCode::IfExpression { expr_id, .. } = cause.code()
536                            && let hir::Node::Expr(if_expr) = self.tcx.hir_node(*expr_id)
537                            && let hir::ExprKind::If(_, then_expr, _) = if_expr.kind
538                            && let hir::ExprKind::Block(blk, _) = then_expr.kind
539                            && let Some(then) = blk.expr
540                            && def.is_box()
541                            && let boxed_ty = args.type_at(0)
542                            && let ty::Dynamic(t, _) = boxed_ty.kind()
543                            && let Some(def_id) = t.principal_def_id()
544                            && let mut impl_def_ids = ::alloc::vec::Vec::new()vec![]
545                            && let _ =
546                                tcx.for_each_relevant_impl(def_id, values.expected, |did| {
547                                    impl_def_ids.push(did)
548                                })
549                            && let [_] = &impl_def_ids[..] =>
550                    {
551                        // We have divergent if/else arms where the expected value is a type that
552                        // implements the trait of the found boxed trait object.
553                        diag.multipart_suggestion(
554                            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("`{0}` implements `{1}` so you can box it to coerce to the trait object `{2}`",
                values.expected, tcx.item_name(def_id), values.found))
    })format!(
555                                "`{}` implements `{}` so you can box it to coerce to the trait \
556                                 object `{}`",
557                                values.expected,
558                                tcx.item_name(def_id),
559                                values.found,
560                            ),
561                            ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
        [(then.span.shrink_to_lo(), "Box::new(".to_string()),
                (then.span.shrink_to_hi(), ")".to_string())]))vec![
562                                (then.span.shrink_to_lo(), "Box::new(".to_string()),
563                                (then.span.shrink_to_hi(), ")".to_string()),
564                            ],
565                            MachineApplicable,
566                        );
567                    }
568                    _ => {}
569                }
570                {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs:570",
                        "rustc_trait_selection::error_reporting::infer::note_and_explain",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs"),
                        ::tracing_core::__macro_support::Option::Some(570u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::note_and_explain"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("note_and_explain_type_err expected={0:?} ({1:?}) found={2:?} ({3:?})",
                                                    values.expected, values.expected.kind(), values.found,
                                                    values.found.kind()) as &dyn Value))])
            });
    } else { ; }
};debug!(
571                    "note_and_explain_type_err expected={:?} ({:?}) found={:?} ({:?})",
572                    values.expected,
573                    values.expected.kind(),
574                    values.found,
575                    values.found.kind(),
576                );
577            }
578            TypeError::CyclicTy(ty) => {
579                // Watch out for various cases of cyclic types and try to explain.
580                if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() {
581                    diag.note(
582                        "closures cannot capture themselves or take themselves as argument;\n\
583                         this error may be the result of a recent compiler bug-fix,\n\
584                         see issue #46062 <https://github.com/rust-lang/rust/issues/46062>\n\
585                         for more information",
586                    );
587                }
588            }
589            TypeError::TargetFeatureCast(def_id) => {
590                let target_spans = {
    {
        'done:
            {
            for i in ::rustc_hir::attrs::HasAttrs::get_attrs(def_id, &tcx) {
                #[allow(unused_imports)]
                use rustc_hir::attrs::AttributeKind::*;
                let i: &rustc_hir::Attribute = i;
                match i {
                    rustc_hir::Attribute::Parsed(TargetFeature {
                        attr_span: span, was_forced: false, .. }) => {
                        break 'done Some(*span);
                    }
                    rustc_hir::Attribute::Unparsed(..) =>
                        {}
                        #[deny(unreachable_patterns)]
                        _ => {}
                }
            }
            None
        }
    }
}find_attr!(tcx, def_id, TargetFeature{attr_span: span, was_forced: false, ..} => *span);
591                diag.note(
592                    "functions with `#[target_feature]` can only be coerced to `unsafe` function pointers"
593                );
594                diag.span_labels(target_spans, "`#[target_feature]` added here");
595            }
596            _ => {}
597        }
598    }
599
600    fn suggest_constraint(
601        &self,
602        diag: &mut Diag<'_>,
603        msg: impl Fn() -> String,
604        body_owner_def_id: Option<DefId>,
605        proj_ty: ty::AliasTy<'tcx>,
606        ty: Ty<'tcx>,
607    ) -> bool {
608        let tcx = self.tcx;
609        let Some(body_owner_def_id) = body_owner_def_id else {
610            return false;
611        };
612        let assoc = tcx.associated_item(proj_ty.kind.def_id());
613        let (trait_ref, assoc_args) = proj_ty.trait_ref_and_own_args(tcx);
614        let Some(item) = tcx.hir_get_if_local(body_owner_def_id) else {
615            return false;
616        };
617        let Some(hir_generics) = item.generics() else {
618            return false;
619        };
620        // Get the `DefId` for the type parameter corresponding to `A` in `<A as T>::Foo`.
621        // This will also work for `impl Trait`.
622        let ty::Param(param_ty) = *proj_ty.self_ty().kind() else {
623            return false;
624        };
625        let generics = tcx.generics_of(body_owner_def_id);
626        let def_id = generics.type_param(param_ty, tcx).def_id;
627        let Some(def_id) = def_id.as_local() else {
628            return false;
629        };
630
631        // First look in the `where` clause, as this might be
632        // `fn foo<T>(x: T) where T: Trait`.
633        for pred in hir_generics.bounds_for_param(def_id) {
634            if self.constrain_generic_bound_associated_type_structured_suggestion(
635                diag,
636                trait_ref,
637                pred.bounds,
638                assoc,
639                assoc_args,
640                ty,
641                &msg,
642                false,
643            ) {
644                return true;
645            }
646        }
647        if (param_ty.index as usize) >= generics.parent_count {
648            // The param comes from the current item, do not look at the parent. (#117209)
649            return false;
650        }
651        // If associated item, look to constrain the params of the trait/impl.
652        let hir_id = match item {
653            hir::Node::ImplItem(item) => item.hir_id(),
654            hir::Node::TraitItem(item) => item.hir_id(),
655            _ => return false,
656        };
657        let parent = tcx.hir_get_parent_item(hir_id).def_id;
658        self.suggest_constraint(diag, msg, Some(parent.into()), proj_ty, ty)
659    }
660
661    /// An associated type was expected and a different type was found.
662    ///
663    /// We perform a few different checks to see what we can suggest:
664    ///
665    ///  - In the current item, look for associated functions that return the expected type and
666    ///    suggest calling them. (Not a structured suggestion.)
667    ///  - If any of the item's generic bounds can be constrained, we suggest constraining the
668    ///    associated type to the found type.
669    ///  - If the associated type has a default type and was expected inside of a `trait`, we
670    ///    mention that this is disallowed.
671    ///  - If all other things fail, and the error is not because of a mismatch between the `trait`
672    ///    and the `impl`, we provide a generic `help` to constrain the assoc type or call an assoc
673    ///    fn that returns the type.
674    fn expected_projection(
675        &self,
676        diag: &mut Diag<'_>,
677        proj_ty: ty::AliasTy<'tcx>,
678        values: ExpectedFound<Ty<'tcx>>,
679        body_owner_def_id: Option<DefId>,
680        cause_code: &ObligationCauseCode<'_>,
681    ) {
682        let tcx = self.tcx;
683
684        // Don't suggest constraining a projection to something containing itself
685        if self
686            .tcx
687            .erase_and_anonymize_regions(values.found)
688            .contains(self.tcx.erase_and_anonymize_regions(values.expected))
689        {
690            return;
691        }
692
693        let msg = || {
694            ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider constraining the associated type `{0}` to `{1}`",
                values.expected, values.found))
    })format!(
695                "consider constraining the associated type `{}` to `{}`",
696                values.expected, values.found
697            )
698        };
699
700        let body_owner = body_owner_def_id.and_then(|id| tcx.hir_get_if_local(id));
701        let current_method_ident = body_owner.and_then(|n| n.ident()).map(|i| i.name);
702
703        // We don't want to suggest calling an assoc fn in a scope where that isn't feasible.
704        let callable_scope = #[allow(non_exhaustive_omitted_patterns)] match body_owner {
    Some(hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. }) |
        hir::Node::TraitItem(hir::TraitItem {
        kind: hir::TraitItemKind::Fn(..), .. }) |
        hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..),
        .. })) => true,
    _ => false,
}matches!(
705            body_owner,
706            Some(
707                hir::Node::Item(hir::Item { kind: hir::ItemKind::Fn { .. }, .. })
708                    | hir::Node::TraitItem(hir::TraitItem { kind: hir::TraitItemKind::Fn(..), .. })
709                    | hir::Node::ImplItem(hir::ImplItem { kind: hir::ImplItemKind::Fn(..), .. }),
710            )
711        );
712        let impl_comparison = #[allow(non_exhaustive_omitted_patterns)] match cause_code {
    ObligationCauseCode::CompareImplItem { .. } => true,
    _ => false,
}matches!(cause_code, ObligationCauseCode::CompareImplItem { .. });
713        if impl_comparison {
714            // We do not want to suggest calling functions when the reason of the
715            // type error is a comparison of an `impl` with its `trait`.
716        } else {
717            let point_at_assoc_fn = if callable_scope
718                && self.point_at_methods_that_satisfy_associated_type(
719                    diag,
720                    tcx.parent(proj_ty.kind.def_id()),
721                    current_method_ident,
722                    proj_ty.kind.def_id(),
723                    values.expected,
724                ) {
725                // If we find a suitable associated function that returns the expected type, we
726                // don't want the more general suggestion later in this method about "consider
727                // constraining the associated type or calling a method that returns the associated
728                // type".
729                true
730            } else {
731                false
732            };
733            // Possibly suggest constraining the associated type to conform to the
734            // found type.
735            if self.suggest_constraint(diag, &msg, body_owner_def_id, proj_ty, values.found)
736                || point_at_assoc_fn
737            {
738                return;
739            }
740        }
741
742        self.suggest_constraining_opaque_associated_type(diag, &msg, proj_ty, values.found);
743
744        if self.point_at_associated_type(diag, body_owner_def_id, values.found) {
745            return;
746        }
747
748        if !impl_comparison {
749            // Generic suggestion when we can't be more specific.
750            if callable_scope {
751                diag.help(::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} or calling a method that returns `{1}`",
                msg(), values.expected))
    })format!(
752                    "{} or calling a method that returns `{}`",
753                    msg(),
754                    values.expected
755                ));
756            } else {
757                diag.help(msg());
758            }
759            diag.note(
760                "for more information, visit \
761                 https://doc.rust-lang.org/book/ch19-03-advanced-traits.html",
762            );
763        }
764        if diag.code.is_some_and(|code| tcx.sess.teach(code)) {
765            diag.help(
766                "given an associated type `T` and a method `foo`:
767```
768trait Trait {
769type T;
770fn foo(&self) -> Self::T;
771}
772```
773the only way of implementing method `foo` is to constrain `T` with an explicit associated type:
774```
775impl Trait for X {
776type T = String;
777fn foo(&self) -> Self::T { String::new() }
778}
779```",
780            );
781        }
782    }
783
784    /// When the expected `impl Trait` is not defined in the current item, it will come from
785    /// a return type. This can occur when dealing with `TryStream` (#71035).
786    fn suggest_constraining_opaque_associated_type(
787        &self,
788        diag: &mut Diag<'_>,
789        msg: impl Fn() -> String,
790        proj_ty: ty::AliasTy<'tcx>,
791        ty: Ty<'tcx>,
792    ) -> bool {
793        let tcx = self.tcx;
794
795        let assoc = tcx.associated_item(proj_ty.kind.def_id());
796        if let ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) =
797            *proj_ty.self_ty().kind()
798        {
799            let opaque_local_def_id = def_id.as_local();
800            let opaque_hir_ty = if let Some(opaque_local_def_id) = opaque_local_def_id {
801                tcx.hir_expect_opaque_ty(opaque_local_def_id)
802            } else {
803                return false;
804            };
805
806            let (trait_ref, assoc_args) = proj_ty.trait_ref_and_own_args(tcx);
807
808            self.constrain_generic_bound_associated_type_structured_suggestion(
809                diag,
810                trait_ref,
811                opaque_hir_ty.bounds,
812                assoc,
813                assoc_args,
814                ty,
815                msg,
816                true,
817            )
818        } else {
819            false
820        }
821    }
822
823    fn point_at_methods_that_satisfy_associated_type(
824        &self,
825        diag: &mut Diag<'_>,
826        assoc_container_id: DefId,
827        current_method_ident: Option<Symbol>,
828        proj_ty_item_def_id: DefId,
829        expected: Ty<'tcx>,
830    ) -> bool {
831        let tcx = self.tcx;
832
833        let items = tcx.associated_items(assoc_container_id);
834        // Find all the methods in the trait that could be called to construct the
835        // expected associated type.
836        // FIXME: consider suggesting the use of associated `const`s.
837        let methods: Vec<(Span, String)> = items
838            .in_definition_order()
839            .filter(|item| {
840                item.is_fn()
841                    && Some(item.name()) != current_method_ident
842                    && !tcx.is_doc_hidden(item.def_id)
843            })
844            .filter_map(|item| {
845                let method = tcx.fn_sig(item.def_id).instantiate_identity();
846                match *method.output().skip_binder().kind() {
847                    ty::Alias(ty::AliasTy {
848                        kind: ty::Projection { def_id: item_def_id }, ..
849                    }) if item_def_id == proj_ty_item_def_id => Some((
850                        tcx.def_span(item.def_id),
851                        ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("consider calling `{0}`",
                tcx.def_path_str(item.def_id)))
    })format!("consider calling `{}`", tcx.def_path_str(item.def_id)),
852                    )),
853                    _ => None,
854                }
855            })
856            .collect();
857        if !methods.is_empty() {
858            // Use a single `help:` to show all the methods in the trait that can
859            // be used to construct the expected associated type.
860            let mut span: MultiSpan =
861                methods.iter().map(|(sp, _)| *sp).collect::<Vec<Span>>().into();
862            let msg = ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("{0} method{1} {2} available that return{3} `{4}`",
                if methods.len() == 1 { "a" } else { "some" },
                if methods.len() == 1 { "" } else { "s" },
                if methods.len() == 1 { "is" } else { "are" },
                if methods.len() == 1 { "s" } else { "" }, expected))
    })format!(
863                "{some} method{s} {are} available that return{r} `{ty}`",
864                some = if methods.len() == 1 { "a" } else { "some" },
865                s = pluralize!(methods.len()),
866                are = pluralize!("is", methods.len()),
867                r = if methods.len() == 1 { "s" } else { "" },
868                ty = expected
869            );
870            for (sp, label) in methods.into_iter() {
871                span.push_span_label(sp, label);
872            }
873            diag.span_help(span, msg);
874            return true;
875        }
876        false
877    }
878
879    fn point_at_associated_type(
880        &self,
881        diag: &mut Diag<'_>,
882        body_owner_def_id: Option<DefId>,
883        found: Ty<'tcx>,
884    ) -> bool {
885        let tcx = self.tcx;
886
887        let Some(def_id) = body_owner_def_id.and_then(|id| id.as_local()) else {
888            return false;
889        };
890
891        // When `body_owner` is an `impl` or `trait` item, look in its associated types for
892        // `expected` and point at it.
893        let hir_id = tcx.local_def_id_to_hir_id(def_id);
894        let parent_id = tcx.hir_get_parent_item(hir_id);
895        let item = tcx.hir_node_by_def_id(parent_id.def_id);
896
897        {
    use ::tracing::__macro_support::Callsite as _;
    static __CALLSITE: ::tracing::callsite::DefaultCallsite =
        {
            static META: ::tracing::Metadata<'static> =
                {
                    ::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs:897",
                        "rustc_trait_selection::error_reporting::infer::note_and_explain",
                        ::tracing::Level::DEBUG,
                        ::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/note_and_explain.rs"),
                        ::tracing_core::__macro_support::Option::Some(897u32),
                        ::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::note_and_explain"),
                        ::tracing_core::field::FieldSet::new(&["message"],
                            ::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(&format_args!("expected_projection parent item {0:?}",
                                                    item) as &dyn Value))])
            });
    } else { ; }
};debug!("expected_projection parent item {:?}", item);
898
899        let param_env = tcx.param_env(def_id);
900
901        if let DefKind::Trait | DefKind::Impl { .. } = tcx.def_kind(parent_id) {
902            let assoc_items = tcx.associated_items(parent_id);
903            // FIXME: account for `#![feature(specialization)]`
904            for assoc_item in assoc_items.in_definition_order() {
905                if assoc_item.is_type()
906                    // FIXME: account for returning some type in a trait fn impl that has
907                    // an assoc type as a return type (#72076).
908                    && let hir::Defaultness::Default { has_value: true } = assoc_item.defaultness(tcx)
909                    && let assoc_ty = tcx.type_of(assoc_item.def_id).instantiate_identity()
910                    && self.infcx.can_eq(param_env, assoc_ty, found)
911                {
912                    let msg = match assoc_item.container {
913                        ty::AssocContainer::Trait => {
914                            "associated type defaults can't be assumed inside the \
915                                            trait defining them"
916                        }
917                        ty::AssocContainer::InherentImpl | ty::AssocContainer::TraitImpl(_) => {
918                            "associated type is `default` and may be overridden"
919                        }
920                    };
921                    diag.span_label(tcx.def_span(assoc_item.def_id), msg);
922                    return true;
923                }
924            }
925        }
926
927        false
928    }
929
930    /// Given a slice of `hir::GenericBound`s, if any of them corresponds to the `trait_ref`
931    /// requirement, provide a structured suggestion to constrain it to a given type `ty`.
932    ///
933    /// `is_bound_surely_present` indicates whether we know the bound we're looking for is
934    /// inside `bounds`. If that's the case then we can consider `bounds` containing only one
935    /// trait bound as the one we're looking for. This can help in cases where the associated
936    /// type is defined on a supertrait of the one present in the bounds.
937    fn constrain_generic_bound_associated_type_structured_suggestion(
938        &self,
939        diag: &mut Diag<'_>,
940        trait_ref: ty::TraitRef<'tcx>,
941        bounds: hir::GenericBounds<'_>,
942        assoc: ty::AssocItem,
943        assoc_args: &[ty::GenericArg<'tcx>],
944        ty: Ty<'tcx>,
945        msg: impl Fn() -> String,
946        is_bound_surely_present: bool,
947    ) -> bool {
948        // FIXME: we would want to call `resolve_vars_if_possible` on `ty` before suggesting.
949
950        let trait_bounds = bounds.iter().filter_map(|bound| match bound {
951            hir::GenericBound::Trait(ptr) if ptr.modifiers == hir::TraitBoundModifiers::NONE => {
952                Some(ptr)
953            }
954            _ => None,
955        });
956
957        let matching_trait_bounds = trait_bounds
958            .clone()
959            .filter(|ptr| ptr.trait_ref.trait_def_id() == Some(trait_ref.def_id))
960            .collect::<Vec<_>>();
961
962        let span = match &matching_trait_bounds[..] {
963            &[ptr] => ptr.span,
964            &[] if is_bound_surely_present => match &trait_bounds.collect::<Vec<_>>()[..] {
965                &[ptr] => ptr.span,
966                _ => return false,
967            },
968            _ => return false,
969        };
970
971        self.constrain_associated_type_structured_suggestion(diag, span, assoc, assoc_args, ty, msg)
972    }
973
974    /// Given a span corresponding to a bound, provide a structured suggestion to set an
975    /// associated type to a given type `ty`.
976    fn constrain_associated_type_structured_suggestion(
977        &self,
978        diag: &mut Diag<'_>,
979        span: Span,
980        assoc: ty::AssocItem,
981        assoc_args: &[ty::GenericArg<'tcx>],
982        ty: Ty<'tcx>,
983        msg: impl Fn() -> String,
984    ) -> bool {
985        let tcx = self.tcx;
986
987        if let Ok(has_params) =
988            tcx.sess.source_map().span_to_snippet(span).map(|snippet| snippet.ends_with('>'))
989        {
990            let (span, sugg) = if has_params {
991                let pos = span.hi() - BytePos(1);
992                let span = Span::new(pos, pos, span.ctxt(), span.parent());
993                (span, ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!(", {0} = {1}", assoc.ident(tcx),
                ty))
    })format!(", {} = {}", assoc.ident(tcx), ty))
994            } else {
995                let item_args = self.format_generic_args(assoc_args);
996                (span.shrink_to_hi(), ::alloc::__export::must_use({
        ::alloc::fmt::format(format_args!("<{0}{1} = {2}>", assoc.ident(tcx),
                item_args, ty))
    })format!("<{}{} = {}>", assoc.ident(tcx), item_args, ty))
997            };
998            diag.span_suggestion_verbose(span, msg(), sugg, MaybeIncorrect);
999            return true;
1000        }
1001        false
1002    }
1003
1004    pub fn format_generic_args(&self, args: &[ty::GenericArg<'tcx>]) -> String {
1005        FmtPrinter::print_string(self.tcx, hir::def::Namespace::TypeNS, |p| {
1006            p.print_path_with_generic_args(|_| Ok(()), args)
1007        })
1008        .expect("could not write to `String`.")
1009    }
1010}