rustc_trait_selection/error_reporting/traits/
fulfillment_errors.rs

1use core::ops::ControlFlow;
2use std::borrow::Cow;
3use std::path::PathBuf;
4
5use rustc_abi::ExternAbi;
6use rustc_ast::TraitObjectSyntax;
7use rustc_data_structures::fx::FxHashMap;
8use rustc_data_structures::unord::UnordSet;
9use rustc_errors::codes::*;
10use rustc_errors::{
11    Applicability, Diag, ErrorGuaranteed, Level, MultiSpan, StashKey, StringPart, Suggestions,
12    pluralize, struct_span_code_err,
13};
14use rustc_hir::def_id::{DefId, LOCAL_CRATE, LocalDefId};
15use rustc_hir::intravisit::Visitor;
16use rustc_hir::{self as hir, LangItem, Node};
17use rustc_infer::infer::{InferOk, TypeTrace};
18use rustc_infer::traits::ImplSource;
19use rustc_infer::traits::solve::Goal;
20use rustc_middle::traits::SignatureMismatchData;
21use rustc_middle::traits::select::OverflowError;
22use rustc_middle::ty::abstract_const::NotConstEvaluatable;
23use rustc_middle::ty::error::{ExpectedFound, TypeError};
24use rustc_middle::ty::print::{
25    PrintPolyTraitPredicateExt, PrintTraitPredicateExt as _, PrintTraitRefExt as _,
26    with_forced_trimmed_paths,
27};
28use rustc_middle::ty::{
29    self, TraitRef, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt,
30    Upcast,
31};
32use rustc_middle::{bug, span_bug};
33use rustc_span::{BytePos, DUMMY_SP, STDLIB_STABLE_CRATES, Span, Symbol, sym};
34use tracing::{debug, instrument};
35
36use super::on_unimplemented::{AppendConstMessage, OnUnimplementedNote};
37use super::suggestions::get_explanation_based_on_obligation;
38use super::{
39    ArgKind, CandidateSimilarity, FindExprBySpan, GetSafeTransmuteErrorAndReason, ImplCandidate,
40};
41use crate::error_reporting::TypeErrCtxt;
42use crate::error_reporting::infer::TyCategory;
43use crate::error_reporting::traits::report_dyn_incompatibility;
44use crate::errors::{ClosureFnMutLabel, ClosureFnOnceLabel, ClosureKindMismatch, CoroClosureNotFn};
45use crate::infer::{self, InferCtxt, InferCtxtExt as _};
46use crate::traits::query::evaluate_obligation::InferCtxtExt as _;
47use crate::traits::{
48    MismatchedProjectionTypes, NormalizeExt, Obligation, ObligationCause, ObligationCauseCode,
49    ObligationCtxt, PredicateObligation, SelectionContext, SelectionError, elaborate,
50    specialization_graph,
51};
52
53impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
54    /// The `root_obligation` parameter should be the `root_obligation` field
55    /// from a `FulfillmentError`. If no `FulfillmentError` is available,
56    /// then it should be the same as `obligation`.
57    pub fn report_selection_error(
58        &self,
59        mut obligation: PredicateObligation<'tcx>,
60        root_obligation: &PredicateObligation<'tcx>,
61        error: &SelectionError<'tcx>,
62    ) -> ErrorGuaranteed {
63        let tcx = self.tcx;
64        let mut span = obligation.cause.span;
65        let mut long_ty_file = None;
66
67        let mut err = match *error {
68            SelectionError::Unimplemented => {
69                // If this obligation was generated as a result of well-formedness checking, see if we
70                // can get a better error message by performing HIR-based well-formedness checking.
71                if let ObligationCauseCode::WellFormed(Some(wf_loc)) =
72                    root_obligation.cause.code().peel_derives()
73                    && !obligation.predicate.has_non_region_infer()
74                {
75                    if let Some(cause) = self
76                        .tcx
77                        .diagnostic_hir_wf_check((tcx.erase_regions(obligation.predicate), *wf_loc))
78                    {
79                        obligation.cause = cause.clone();
80                        span = obligation.cause.span;
81                    }
82                }
83
84                if let ObligationCauseCode::CompareImplItem {
85                    impl_item_def_id,
86                    trait_item_def_id,
87                    kind: _,
88                } = *obligation.cause.code()
89                {
90                    debug!("ObligationCauseCode::CompareImplItemObligation");
91                    return self.report_extra_impl_obligation(
92                        span,
93                        impl_item_def_id,
94                        trait_item_def_id,
95                        &format!("`{}`", obligation.predicate),
96                    )
97                    .emit()
98                }
99
100                // Report a const-param specific error
101                if let ObligationCauseCode::ConstParam(ty) = *obligation.cause.code().peel_derives()
102                {
103                    return self.report_const_param_not_wf(ty, &obligation).emit();
104                }
105
106                let bound_predicate = obligation.predicate.kind();
107                match bound_predicate.skip_binder() {
108                    ty::PredicateKind::Clause(ty::ClauseKind::Trait(trait_predicate)) => {
109                        let leaf_trait_predicate =
110                            self.resolve_vars_if_possible(bound_predicate.rebind(trait_predicate));
111
112                        // Let's use the root obligation as the main message, when we care about the
113                        // most general case ("X doesn't implement Pattern<'_>") over the case that
114                        // happened to fail ("char doesn't implement Fn(&mut char)").
115                        //
116                        // We rely on a few heuristics to identify cases where this root
117                        // obligation is more important than the leaf obligation:
118                        let (main_trait_predicate, main_obligation) = if let ty::PredicateKind::Clause(
119                            ty::ClauseKind::Trait(root_pred)
120                        ) = root_obligation.predicate.kind().skip_binder()
121                            && !leaf_trait_predicate.self_ty().skip_binder().has_escaping_bound_vars()
122                            && !root_pred.self_ty().has_escaping_bound_vars()
123                            // The type of the leaf predicate is (roughly) the same as the type
124                            // from the root predicate, as a proxy for "we care about the root"
125                            // FIXME: this doesn't account for trivial derefs, but works as a first
126                            // approximation.
127                            && (
128                                // `T: Trait` && `&&T: OtherTrait`, we want `OtherTrait`
129                                self.can_eq(
130                                    obligation.param_env,
131                                    leaf_trait_predicate.self_ty().skip_binder(),
132                                    root_pred.self_ty().peel_refs(),
133                                )
134                                // `&str: Iterator` && `&str: IntoIterator`, we want `IntoIterator`
135                                || self.can_eq(
136                                    obligation.param_env,
137                                    leaf_trait_predicate.self_ty().skip_binder(),
138                                    root_pred.self_ty(),
139                                )
140                            )
141                            // The leaf trait and the root trait are different, so as to avoid
142                            // talking about `&mut T: Trait` and instead remain talking about
143                            // `T: Trait` instead
144                            && leaf_trait_predicate.def_id() != root_pred.def_id()
145                            // The root trait is not `Unsize`, as to avoid talking about it in
146                            // `tests/ui/coercion/coerce-issue-49593-box-never.rs`.
147                            && !self.tcx.is_lang_item(root_pred.def_id(), LangItem::Unsize)
148                        {
149                            (
150                                self.resolve_vars_if_possible(
151                                    root_obligation.predicate.kind().rebind(root_pred),
152                                ),
153                                root_obligation,
154                            )
155                        } else {
156                            (leaf_trait_predicate, &obligation)
157                        };
158
159                        if let Some(guar) = self.emit_specialized_closure_kind_error(
160                            &obligation,
161                            leaf_trait_predicate,
162                        ) {
163                            return guar;
164                        }
165
166                        if let Err(guar) = leaf_trait_predicate.error_reported()
167                        {
168                            return guar;
169                        }
170                        // Silence redundant errors on binding access that are already
171                        // reported on the binding definition (#56607).
172                        if let Err(guar) = self.fn_arg_obligation(&obligation) {
173                            return guar;
174                        }
175                        let (post_message, pre_message, type_def) = self
176                            .get_parent_trait_ref(obligation.cause.code())
177                            .map(|(t, s)| {
178                                let t = self.tcx.short_string(t, &mut long_ty_file);
179                                (
180                                    format!(" in `{t}`"),
181                                    format!("within `{t}`, "),
182                                    s.map(|s| (format!("within this `{t}`"), s)),
183                                )
184                            })
185                            .unwrap_or_default();
186
187                        let OnUnimplementedNote {
188                            message,
189                            label,
190                            notes,
191                            parent_label,
192                            append_const_msg,
193                        } = self.on_unimplemented_note(main_trait_predicate, main_obligation, &mut long_ty_file);
194
195                        let have_alt_message = message.is_some() || label.is_some();
196                        let is_try_conversion = self.is_try_conversion(span, main_trait_predicate.def_id());
197                        let is_question_mark = matches!(
198                            root_obligation.cause.code().peel_derives(),
199                            ObligationCauseCode::QuestionMark,
200                        ) && !(
201                            self.tcx.is_diagnostic_item(sym::FromResidual, main_trait_predicate.def_id())
202                                || self.tcx.is_lang_item(main_trait_predicate.def_id(), LangItem::Try)
203                        );
204                        let is_unsize =
205                            self.tcx.is_lang_item(leaf_trait_predicate.def_id(), LangItem::Unsize);
206                        let question_mark_message = "the question mark operation (`?`) implicitly \
207                                                     performs a conversion on the error value \
208                                                     using the `From` trait";
209                        let (message, notes, append_const_msg) = if is_try_conversion {
210                            let ty = self.tcx.short_string(
211                                main_trait_predicate.skip_binder().self_ty(),
212                                &mut long_ty_file,
213                            );
214                            // We have a `-> Result<_, E1>` and `gives_E2()?`.
215                            (
216                                Some(format!("`?` couldn't convert the error to `{ty}`")),
217                                vec![question_mark_message.to_owned()],
218                                Some(AppendConstMessage::Default),
219                            )
220                        } else if is_question_mark {
221                            let main_trait_predicate =
222                                self.tcx.short_string(main_trait_predicate, &mut long_ty_file);
223                            // Similar to the case above, but in this case the conversion is for a
224                            // trait object: `-> Result<_, Box<dyn Error>` and `gives_E()?` when
225                            // `E: Error` isn't met.
226                            (
227                                Some(format!(
228                                    "`?` couldn't convert the error: `{main_trait_predicate}` is \
229                                     not satisfied",
230                                )),
231                                vec![question_mark_message.to_owned()],
232                                Some(AppendConstMessage::Default),
233                            )
234                        } else {
235                            (message, notes, append_const_msg)
236                        };
237
238                        let default_err_msg = || self.get_standard_error_message(
239                            main_trait_predicate,
240                            message,
241                            None,
242                            append_const_msg,
243                            post_message,
244                            &mut long_ty_file,
245                        );
246
247                        let (err_msg, safe_transmute_explanation) = if self.tcx.is_lang_item(
248                            main_trait_predicate.def_id(),
249                            LangItem::TransmuteTrait,
250                        ) {
251                            // Recompute the safe transmute reason and use that for the error reporting
252                            match self.get_safe_transmute_error_and_reason(
253                                obligation.clone(),
254                                main_trait_predicate,
255                                span,
256                            ) {
257                                GetSafeTransmuteErrorAndReason::Silent => {
258                                    return self.dcx().span_delayed_bug(
259                                        span, "silent safe transmute error"
260                                    );
261                                }
262                                GetSafeTransmuteErrorAndReason::Default => {
263                                    (default_err_msg(), None)
264                                }
265                                GetSafeTransmuteErrorAndReason::Error {
266                                    err_msg,
267                                    safe_transmute_explanation,
268                                } => (err_msg, safe_transmute_explanation),
269                            }
270                        } else {
271                            (default_err_msg(), None)
272                        };
273
274                        let mut err = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);
275                        *err.long_ty_path() = long_ty_file;
276
277                        let mut suggested = false;
278                        let mut noted_missing_impl = false;
279                        if is_try_conversion || is_question_mark {
280                            (suggested, noted_missing_impl) = self.try_conversion_context(&obligation, main_trait_predicate, &mut err);
281                        }
282
283                        if let Some(ret_span) = self.return_type_span(&obligation) {
284                            if is_try_conversion {
285                                let ty = self.tcx.short_string(
286                                    main_trait_predicate.skip_binder().self_ty(),
287                                    err.long_ty_path(),
288                                );
289                                err.span_label(
290                                    ret_span,
291                                    format!("expected `{ty}` because of this"),
292                                );
293                            } else if is_question_mark {
294                                let main_trait_predicate =
295                                    self.tcx.short_string(main_trait_predicate, err.long_ty_path());
296                                err.span_label(
297                                    ret_span,
298                                    format!("required `{main_trait_predicate}` because of this"),
299                                );
300                            }
301                        }
302
303                        if tcx.is_lang_item(leaf_trait_predicate.def_id(), LangItem::Tuple) {
304                            self.add_tuple_trait_message(
305                                obligation.cause.code().peel_derives(),
306                                &mut err,
307                            );
308                        }
309
310                        let explanation = get_explanation_based_on_obligation(
311                            self.tcx,
312                            &obligation,
313                            leaf_trait_predicate,
314                            pre_message,
315                            err.long_ty_path(),
316                        );
317
318                        self.check_for_binding_assigned_block_without_tail_expression(
319                            &obligation,
320                            &mut err,
321                            leaf_trait_predicate,
322                        );
323                        self.suggest_add_result_as_return_type(
324                            &obligation,
325                            &mut err,
326                            leaf_trait_predicate,
327                        );
328
329                        if self.suggest_add_reference_to_arg(
330                            &obligation,
331                            &mut err,
332                            leaf_trait_predicate,
333                            have_alt_message,
334                        ) {
335                            self.note_obligation_cause(&mut err, &obligation);
336                            return err.emit();
337                        }
338
339                        let ty_span = match leaf_trait_predicate.self_ty().skip_binder().kind() {
340                            ty::Adt(def, _) if def.did().is_local()
341                                && !self.can_suggest_derive(&obligation, leaf_trait_predicate) => self.tcx.def_span(def.did()),
342                            _ => DUMMY_SP,
343                        };
344                        if let Some(s) = label {
345                            // If it has a custom `#[rustc_on_unimplemented]`
346                            // error message, let's display it as the label!
347                            err.span_label(span, s);
348                            if !matches!(leaf_trait_predicate.skip_binder().self_ty().kind(), ty::Param(_))
349                                // When the self type is a type param We don't need to "the trait
350                                // `std::marker::Sized` is not implemented for `T`" as we will point
351                                // at the type param with a label to suggest constraining it.
352                                && !self.tcx.is_diagnostic_item(sym::FromResidual, leaf_trait_predicate.def_id())
353                                    // Don't say "the trait `FromResidual<Option<Infallible>>` is
354                                    // not implemented for `Result<T, E>`".
355                            {
356                                // We do this just so that the JSON output's `help` position is the
357                                // right one and not `file.rs:1:1`. The render is the same.
358                                if ty_span == DUMMY_SP {
359                                    err.help(explanation);
360                                } else {
361                                    err.span_help(ty_span, explanation);
362                                }
363                            }
364                        } else if let Some(custom_explanation) = safe_transmute_explanation {
365                            err.span_label(span, custom_explanation);
366                        } else if (explanation.len() > self.tcx.sess.diagnostic_width() || ty_span != DUMMY_SP) && !noted_missing_impl {
367                            // Really long types don't look good as span labels, instead move it
368                            // to a `help`.
369                            err.span_label(span, "unsatisfied trait bound");
370
371                            // We do this just so that the JSON output's `help` position is the
372                            // right one and not `file.rs:1:1`. The render is the same.
373                            if ty_span == DUMMY_SP {
374                                err.help(explanation);
375                            } else {
376                                err.span_help(ty_span, explanation);
377                            }
378                        } else {
379                            err.span_label(span, explanation);
380                        }
381
382                        if let ObligationCauseCode::Coercion { source, target } =
383                            *obligation.cause.code().peel_derives()
384                        {
385                            if self.tcx.is_lang_item(leaf_trait_predicate.def_id(), LangItem::Sized) {
386                                self.suggest_borrowing_for_object_cast(
387                                    &mut err,
388                                    root_obligation,
389                                    source,
390                                    target,
391                                );
392                            }
393                        }
394
395                        if let Some((msg, span)) = type_def {
396                            err.span_label(span, msg);
397                        }
398                        for note in notes {
399                            // If it has a custom `#[rustc_on_unimplemented]` note, let's display it
400                            err.note(note);
401                        }
402                        if let Some(s) = parent_label {
403                            let body = obligation.cause.body_id;
404                            err.span_label(tcx.def_span(body), s);
405                        }
406
407                        self.suggest_floating_point_literal(&obligation, &mut err, leaf_trait_predicate);
408                        self.suggest_dereferencing_index(&obligation, &mut err, leaf_trait_predicate);
409                        suggested |= self.suggest_dereferences(&obligation, &mut err, leaf_trait_predicate);
410                        suggested |= self.suggest_fn_call(&obligation, &mut err, leaf_trait_predicate);
411                        let impl_candidates = self.find_similar_impl_candidates(leaf_trait_predicate);
412                        suggested = if let &[cand] = &impl_candidates[..] {
413                            let cand = cand.trait_ref;
414                            if let (ty::FnPtr(..), ty::FnDef(..)) =
415                                (cand.self_ty().kind(), main_trait_predicate.self_ty().skip_binder().kind())
416                            {
417                                // Wrap method receivers and `&`-references in parens
418                                let suggestion = if self.tcx.sess.source_map().span_look_ahead(span, ".", Some(50)).is_some() {
419                                    vec![
420                                        (span.shrink_to_lo(), format!("(")),
421                                        (span.shrink_to_hi(), format!(" as {})", cand.self_ty())),
422                                    ]
423                                } else if let Some(body) = self.tcx.hir_maybe_body_owned_by(obligation.cause.body_id) {
424                                    let mut expr_finder = FindExprBySpan::new(span, self.tcx);
425                                    expr_finder.visit_expr(body.value);
426                                    if let Some(expr) = expr_finder.result &&
427                                        let hir::ExprKind::AddrOf(_, _, expr) = expr.kind {
428                                        vec![
429                                            (expr.span.shrink_to_lo(), format!("(")),
430                                            (expr.span.shrink_to_hi(), format!(" as {})", cand.self_ty())),
431                                        ]
432                                    } else {
433                                        vec![(span.shrink_to_hi(), format!(" as {}", cand.self_ty()))]
434                                    }
435                                } else {
436                                    vec![(span.shrink_to_hi(), format!(" as {}", cand.self_ty()))]
437                                };
438                                let trait_ = self.tcx.short_string(cand.print_trait_sugared(), err.long_ty_path());
439                                let ty = self.tcx.short_string(cand.self_ty(), err.long_ty_path());
440                                err.multipart_suggestion(
441                                    format!(
442                                        "the trait `{trait_}` is implemented for fn pointer \
443                                         `{ty}`, try casting using `as`",
444                                    ),
445                                    suggestion,
446                                    Applicability::MaybeIncorrect,
447                                );
448                                true
449                            } else {
450                                false
451                            }
452                        } else {
453                            false
454                        } || suggested;
455                        suggested |=
456                            self.suggest_remove_reference(&obligation, &mut err, leaf_trait_predicate);
457                        suggested |= self.suggest_semicolon_removal(
458                            &obligation,
459                            &mut err,
460                            span,
461                            leaf_trait_predicate,
462                        );
463                        self.note_version_mismatch(&mut err, leaf_trait_predicate);
464                        self.suggest_remove_await(&obligation, &mut err);
465                        self.suggest_derive(&obligation, &mut err, leaf_trait_predicate);
466
467                        if tcx.is_lang_item(leaf_trait_predicate.def_id(), LangItem::Try) {
468                            self.suggest_await_before_try(
469                                &mut err,
470                                &obligation,
471                                leaf_trait_predicate,
472                                span,
473                            );
474                        }
475
476                        if self.suggest_add_clone_to_arg(&obligation, &mut err, leaf_trait_predicate) {
477                            return err.emit();
478                        }
479
480                        if self.suggest_impl_trait(&mut err, &obligation, leaf_trait_predicate) {
481                            return err.emit();
482                        }
483
484                        if is_unsize {
485                            // If the obligation failed due to a missing implementation of the
486                            // `Unsize` trait, give a pointer to why that might be the case
487                            err.note(
488                                "all implementations of `Unsize` are provided \
489                                automatically by the compiler, see \
490                                <https://doc.rust-lang.org/stable/std/marker/trait.Unsize.html> \
491                                for more information",
492                            );
493                        }
494
495                        let is_fn_trait = tcx.is_fn_trait(leaf_trait_predicate.def_id());
496                        let is_target_feature_fn = if let ty::FnDef(def_id, _) =
497                            *leaf_trait_predicate.skip_binder().self_ty().kind()
498                        {
499                            !self.tcx.codegen_fn_attrs(def_id).target_features.is_empty()
500                        } else {
501                            false
502                        };
503                        if is_fn_trait && is_target_feature_fn {
504                            err.note(
505                                "`#[target_feature]` functions do not implement the `Fn` traits",
506                            );
507                            err.note(
508                                "try casting the function to a `fn` pointer or wrapping it in a closure",
509                            );
510                        }
511
512                        self.try_to_add_help_message(
513                            &root_obligation,
514                            &obligation,
515                            leaf_trait_predicate,
516                            &mut err,
517                            span,
518                            is_fn_trait,
519                            suggested,
520                        );
521
522                        // Changing mutability doesn't make a difference to whether we have
523                        // an `Unsize` impl (Fixes ICE in #71036)
524                        if !is_unsize {
525                            self.suggest_change_mut(&obligation, &mut err, leaf_trait_predicate);
526                        }
527
528                        // If this error is due to `!: Trait` not implemented but `(): Trait` is
529                        // implemented, and fallback has occurred, then it could be due to a
530                        // variable that used to fallback to `()` now falling back to `!`. Issue a
531                        // note informing about the change in behaviour.
532                        if leaf_trait_predicate.skip_binder().self_ty().is_never()
533                            && self.fallback_has_occurred
534                        {
535                            let predicate = leaf_trait_predicate.map_bound(|trait_pred| {
536                                trait_pred.with_replaced_self_ty(self.tcx, tcx.types.unit)
537                            });
538                            let unit_obligation = obligation.with(tcx, predicate);
539                            if self.predicate_may_hold(&unit_obligation) {
540                                err.note(
541                                    "this error might have been caused by changes to \
542                                    Rust's type-inference algorithm (see issue #48950 \
543                                    <https://github.com/rust-lang/rust/issues/48950> \
544                                    for more information)",
545                                );
546                                err.help("you might have intended to use the type `()` here instead");
547                            }
548                        }
549
550                        self.explain_hrtb_projection(&mut err, leaf_trait_predicate, obligation.param_env, &obligation.cause);
551                        self.suggest_desugaring_async_fn_in_trait(&mut err, main_trait_predicate);
552
553                        // Return early if the trait is Debug or Display and the invocation
554                        // originates within a standard library macro, because the output
555                        // is otherwise overwhelming and unhelpful (see #85844 for an
556                        // example).
557
558                        let in_std_macro =
559                            match obligation.cause.span.ctxt().outer_expn_data().macro_def_id {
560                                Some(macro_def_id) => {
561                                    let crate_name = tcx.crate_name(macro_def_id.krate);
562                                    STDLIB_STABLE_CRATES.contains(&crate_name)
563                                }
564                                None => false,
565                            };
566
567                        if in_std_macro
568                            && matches!(
569                                self.tcx.get_diagnostic_name(leaf_trait_predicate.def_id()),
570                                Some(sym::Debug | sym::Display)
571                            )
572                        {
573                            return err.emit();
574                        }
575
576                        err
577                    }
578
579                    ty::PredicateKind::Clause(ty::ClauseKind::HostEffect(predicate)) => {
580                        self.report_host_effect_error(bound_predicate.rebind(predicate), obligation.param_env, span)
581                    }
582
583                    ty::PredicateKind::Subtype(predicate) => {
584                        // Errors for Subtype predicates show up as
585                        // `FulfillmentErrorCode::SubtypeError`,
586                        // not selection error.
587                        span_bug!(span, "subtype requirement gave wrong error: `{:?}`", predicate)
588                    }
589
590                    ty::PredicateKind::Coerce(predicate) => {
591                        // Errors for Coerce predicates show up as
592                        // `FulfillmentErrorCode::SubtypeError`,
593                        // not selection error.
594                        span_bug!(span, "coerce requirement gave wrong error: `{:?}`", predicate)
595                    }
596
597                    ty::PredicateKind::Clause(ty::ClauseKind::RegionOutlives(..))
598                    | ty::PredicateKind::Clause(ty::ClauseKind::TypeOutlives(..)) => {
599                        span_bug!(
600                            span,
601                            "outlives clauses should not error outside borrowck. obligation: `{:?}`",
602                            obligation
603                        )
604                    }
605
606                    ty::PredicateKind::Clause(ty::ClauseKind::Projection(..)) => {
607                        span_bug!(
608                            span,
609                            "projection clauses should be implied from elsewhere. obligation: `{:?}`",
610                            obligation
611                        )
612                    }
613
614                    ty::PredicateKind::DynCompatible(trait_def_id) => {
615                        let violations = self.tcx.dyn_compatibility_violations(trait_def_id);
616                        let mut err = report_dyn_incompatibility(
617                            self.tcx,
618                            span,
619                            None,
620                            trait_def_id,
621                            violations,
622                        );
623                        if let hir::Node::Item(item) =
624                            self.tcx.hir_node_by_def_id(obligation.cause.body_id)
625                            && let hir::ItemKind::Impl(impl_) = item.kind
626                            && let None = impl_.of_trait
627                            && let hir::TyKind::TraitObject(_, tagged_ptr) = impl_.self_ty.kind
628                            && let TraitObjectSyntax::None = tagged_ptr.tag()
629                            && impl_.self_ty.span.edition().at_least_rust_2021()
630                        {
631                            // Silence the dyn-compatibility error in favor of the missing dyn on
632                            // self type error. #131051.
633                            err.downgrade_to_delayed_bug();
634                        }
635                        err
636                    }
637
638                    ty::PredicateKind::Clause(ty::ClauseKind::WellFormed(ty)) => {
639                        let ty = self.resolve_vars_if_possible(ty);
640                        if self.next_trait_solver() {
641                            if let Err(guar) = ty.error_reported() {
642                                return guar;
643                            }
644
645                            // FIXME: we'll need a better message which takes into account
646                            // which bounds actually failed to hold.
647                            self.dcx().struct_span_err(
648                                span,
649                                format!("the type `{ty}` is not well-formed"),
650                            )
651                        } else {
652                            // WF predicates cannot themselves make
653                            // errors. They can only block due to
654                            // ambiguity; otherwise, they always
655                            // degenerate into other obligations
656                            // (which may fail).
657                            span_bug!(span, "WF predicate not satisfied for {:?}", ty);
658                        }
659                    }
660
661                    // Errors for `ConstEvaluatable` predicates show up as
662                    // `SelectionError::ConstEvalFailure`,
663                    // not `Unimplemented`.
664                    ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(..))
665                    // Errors for `ConstEquate` predicates show up as
666                    // `SelectionError::ConstEvalFailure`,
667                    // not `Unimplemented`.
668                    | ty::PredicateKind::ConstEquate { .. }
669                    // Ambiguous predicates should never error
670                    | ty::PredicateKind::Ambiguous
671                    // We never return Err when proving UnstableFeature goal.
672                    | ty::PredicateKind::Clause(ty::ClauseKind::UnstableFeature{ .. })
673                    | ty::PredicateKind::NormalizesTo { .. }
674                    | ty::PredicateKind::AliasRelate { .. }
675                    | ty::PredicateKind::Clause(ty::ClauseKind::ConstArgHasType { .. }) => {
676                        span_bug!(
677                            span,
678                            "Unexpected `Predicate` for `SelectionError`: `{:?}`",
679                            obligation
680                        )
681                    }
682                }
683            }
684
685            SelectionError::SignatureMismatch(box SignatureMismatchData {
686                found_trait_ref,
687                expected_trait_ref,
688                terr: terr @ TypeError::CyclicTy(_),
689            }) => self.report_cyclic_signature_error(
690                &obligation,
691                found_trait_ref,
692                expected_trait_ref,
693                terr,
694            ),
695            SelectionError::SignatureMismatch(box SignatureMismatchData {
696                found_trait_ref,
697                expected_trait_ref,
698                terr: _,
699            }) => {
700                match self.report_signature_mismatch_error(
701                    &obligation,
702                    span,
703                    found_trait_ref,
704                    expected_trait_ref,
705                ) {
706                    Ok(err) => err,
707                    Err(guar) => return guar,
708                }
709            }
710
711            SelectionError::OpaqueTypeAutoTraitLeakageUnknown(def_id) => return self.report_opaque_type_auto_trait_leakage(
712                &obligation,
713                def_id,
714            ),
715
716            SelectionError::TraitDynIncompatible(did) => {
717                let violations = self.tcx.dyn_compatibility_violations(did);
718                report_dyn_incompatibility(self.tcx, span, None, did, violations)
719            }
720
721            SelectionError::NotConstEvaluatable(NotConstEvaluatable::MentionsInfer) => {
722                bug!(
723                    "MentionsInfer should have been handled in `traits/fulfill.rs` or `traits/select/mod.rs`"
724                )
725            }
726            SelectionError::NotConstEvaluatable(NotConstEvaluatable::MentionsParam) => {
727                match self.report_not_const_evaluatable_error(&obligation, span) {
728                    Ok(err) => err,
729                    Err(guar) => return guar,
730                }
731            }
732
733            // Already reported in the query.
734            SelectionError::NotConstEvaluatable(NotConstEvaluatable::Error(guar)) |
735            // Already reported.
736            SelectionError::Overflow(OverflowError::Error(guar)) => {
737                self.set_tainted_by_errors(guar);
738                return guar
739            },
740
741            SelectionError::Overflow(_) => {
742                bug!("overflow should be handled before the `report_selection_error` path");
743            }
744
745            SelectionError::ConstArgHasWrongType { ct, ct_ty, expected_ty } => {
746                let expected_ty_str = self.tcx.short_string(expected_ty, &mut long_ty_file);
747                let ct_str = self.tcx.short_string(ct, &mut long_ty_file);
748                let mut diag = self.dcx().struct_span_err(
749                    span,
750                    format!("the constant `{ct_str}` is not of type `{expected_ty_str}`"),
751                );
752                diag.long_ty_path = long_ty_file;
753
754                self.note_type_err(
755                    &mut diag,
756                    &obligation.cause,
757                    None,
758                    None,
759                    TypeError::Sorts(ty::error::ExpectedFound::new(expected_ty, ct_ty)),
760                    false,
761                    None,
762                );
763                diag
764            }
765        };
766
767        self.note_obligation_cause(&mut err, &obligation);
768        err.emit()
769    }
770}
771
772impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
773    pub(super) fn apply_do_not_recommend(
774        &self,
775        obligation: &mut PredicateObligation<'tcx>,
776    ) -> bool {
777        let mut base_cause = obligation.cause.code().clone();
778        let mut applied_do_not_recommend = false;
779        loop {
780            if let ObligationCauseCode::ImplDerived(ref c) = base_cause {
781                if self.tcx.do_not_recommend_impl(c.impl_or_alias_def_id) {
782                    let code = (*c.derived.parent_code).clone();
783                    obligation.cause.map_code(|_| code);
784                    obligation.predicate = c.derived.parent_trait_pred.upcast(self.tcx);
785                    applied_do_not_recommend = true;
786                }
787            }
788            if let Some(parent_cause) = base_cause.parent() {
789                base_cause = parent_cause.clone();
790            } else {
791                break;
792            }
793        }
794
795        applied_do_not_recommend
796    }
797
798    fn report_host_effect_error(
799        &self,
800        predicate: ty::Binder<'tcx, ty::HostEffectPredicate<'tcx>>,
801        param_env: ty::ParamEnv<'tcx>,
802        span: Span,
803    ) -> Diag<'a> {
804        // FIXME(const_trait_impl): We should recompute the predicate with `[const]`
805        // if it's `const`, and if it holds, explain that this bound only
806        // *conditionally* holds. If that fails, we should also do selection
807        // to drill this down to an impl or built-in source, so we can
808        // point at it and explain that while the trait *is* implemented,
809        // that implementation is not const.
810        let trait_ref = predicate.map_bound(|predicate| ty::TraitPredicate {
811            trait_ref: predicate.trait_ref,
812            polarity: ty::PredicatePolarity::Positive,
813        });
814        let mut file = None;
815        let err_msg = self.get_standard_error_message(
816            trait_ref,
817            None,
818            Some(predicate.constness()),
819            None,
820            String::new(),
821            &mut file,
822        );
823        let mut diag = struct_span_code_err!(self.dcx(), span, E0277, "{}", err_msg);
824        *diag.long_ty_path() = file;
825        if !self.predicate_may_hold(&Obligation::new(
826            self.tcx,
827            ObligationCause::dummy(),
828            param_env,
829            trait_ref,
830        )) {
831            diag.downgrade_to_delayed_bug();
832        }
833        diag
834    }
835
836    fn emit_specialized_closure_kind_error(
837        &self,
838        obligation: &PredicateObligation<'tcx>,
839        mut trait_pred: ty::PolyTraitPredicate<'tcx>,
840    ) -> Option<ErrorGuaranteed> {
841        // If we end up on an `AsyncFnKindHelper` goal, try to unwrap the parent
842        // `AsyncFn*` goal.
843        if self.tcx.is_lang_item(trait_pred.def_id(), LangItem::AsyncFnKindHelper) {
844            let mut code = obligation.cause.code();
845            // Unwrap a `FunctionArg` cause, which has been refined from a derived obligation.
846            if let ObligationCauseCode::FunctionArg { parent_code, .. } = code {
847                code = &**parent_code;
848            }
849            // If we have a derived obligation, then the parent will be a `AsyncFn*` goal.
850            if let Some((_, Some(parent))) = code.parent_with_predicate() {
851                trait_pred = parent;
852            }
853        }
854
855        let self_ty = trait_pred.self_ty().skip_binder();
856
857        let (expected_kind, trait_prefix) =
858            if let Some(expected_kind) = self.tcx.fn_trait_kind_from_def_id(trait_pred.def_id()) {
859                (expected_kind, "")
860            } else if let Some(expected_kind) =
861                self.tcx.async_fn_trait_kind_from_def_id(trait_pred.def_id())
862            {
863                (expected_kind, "Async")
864            } else {
865                return None;
866            };
867
868        let (closure_def_id, found_args, has_self_borrows) = match *self_ty.kind() {
869            ty::Closure(def_id, args) => {
870                (def_id, args.as_closure().sig().map_bound(|sig| sig.inputs()[0]), false)
871            }
872            ty::CoroutineClosure(def_id, args) => (
873                def_id,
874                args.as_coroutine_closure()
875                    .coroutine_closure_sig()
876                    .map_bound(|sig| sig.tupled_inputs_ty),
877                !args.as_coroutine_closure().tupled_upvars_ty().is_ty_var()
878                    && args.as_coroutine_closure().has_self_borrows(),
879            ),
880            _ => return None,
881        };
882
883        let expected_args = trait_pred.map_bound(|trait_pred| trait_pred.trait_ref.args.type_at(1));
884
885        // Verify that the arguments are compatible. If the signature is
886        // mismatched, then we have a totally different error to report.
887        if self.enter_forall(found_args, |found_args| {
888            self.enter_forall(expected_args, |expected_args| {
889                !self.can_eq(obligation.param_env, expected_args, found_args)
890            })
891        }) {
892            return None;
893        }
894
895        if let Some(found_kind) = self.closure_kind(self_ty)
896            && !found_kind.extends(expected_kind)
897        {
898            let mut err = self.report_closure_error(
899                &obligation,
900                closure_def_id,
901                found_kind,
902                expected_kind,
903                trait_prefix,
904            );
905            self.note_obligation_cause(&mut err, &obligation);
906            return Some(err.emit());
907        }
908
909        // If the closure has captures, then perhaps the reason that the trait
910        // is unimplemented is because async closures don't implement `Fn`/`FnMut`
911        // if they have captures.
912        if has_self_borrows && expected_kind != ty::ClosureKind::FnOnce {
913            let coro_kind = match self
914                .tcx
915                .coroutine_kind(self.tcx.coroutine_for_closure(closure_def_id))
916                .unwrap()
917            {
918                rustc_hir::CoroutineKind::Desugared(desugaring, _) => desugaring.to_string(),
919                coro => coro.to_string(),
920            };
921            let mut err = self.dcx().create_err(CoroClosureNotFn {
922                span: self.tcx.def_span(closure_def_id),
923                kind: expected_kind.as_str(),
924                coro_kind,
925            });
926            self.note_obligation_cause(&mut err, &obligation);
927            return Some(err.emit());
928        }
929
930        None
931    }
932
933    fn fn_arg_obligation(
934        &self,
935        obligation: &PredicateObligation<'tcx>,
936    ) -> Result<(), ErrorGuaranteed> {
937        if let ObligationCauseCode::FunctionArg { arg_hir_id, .. } = obligation.cause.code()
938            && let Node::Expr(arg) = self.tcx.hir_node(*arg_hir_id)
939            && let arg = arg.peel_borrows()
940            && let hir::ExprKind::Path(hir::QPath::Resolved(
941                None,
942                hir::Path { res: hir::def::Res::Local(hir_id), .. },
943            )) = arg.kind
944            && let Node::Pat(pat) = self.tcx.hir_node(*hir_id)
945            && let Some((preds, guar)) = self.reported_trait_errors.borrow().get(&pat.span)
946            && preds.contains(&obligation.as_goal())
947        {
948            return Err(*guar);
949        }
950        Ok(())
951    }
952
953    /// When the `E` of the resulting `Result<T, E>` in an expression `foo().bar().baz()?`,
954    /// identify those method chain sub-expressions that could or could not have been annotated
955    /// with `?`.
956    fn try_conversion_context(
957        &self,
958        obligation: &PredicateObligation<'tcx>,
959        trait_pred: ty::PolyTraitPredicate<'tcx>,
960        err: &mut Diag<'_>,
961    ) -> (bool, bool) {
962        let span = obligation.cause.span;
963        /// Look for the (direct) sub-expr of `?`, and return it if it's a `.` method call.
964        struct FindMethodSubexprOfTry {
965            search_span: Span,
966        }
967        impl<'v> Visitor<'v> for FindMethodSubexprOfTry {
968            type Result = ControlFlow<&'v hir::Expr<'v>>;
969            fn visit_expr(&mut self, ex: &'v hir::Expr<'v>) -> Self::Result {
970                if let hir::ExprKind::Match(expr, _arms, hir::MatchSource::TryDesugar(_)) = ex.kind
971                    && ex.span.with_lo(ex.span.hi() - BytePos(1)).source_equal(self.search_span)
972                    && let hir::ExprKind::Call(_, [expr, ..]) = expr.kind
973                {
974                    ControlFlow::Break(expr)
975                } else {
976                    hir::intravisit::walk_expr(self, ex)
977                }
978            }
979        }
980        let hir_id = self.tcx.local_def_id_to_hir_id(obligation.cause.body_id);
981        let Some(body_id) = self.tcx.hir_node(hir_id).body_id() else { return (false, false) };
982        let ControlFlow::Break(expr) =
983            (FindMethodSubexprOfTry { search_span: span }).visit_body(self.tcx.hir_body(body_id))
984        else {
985            return (false, false);
986        };
987        let Some(typeck) = &self.typeck_results else {
988            return (false, false);
989        };
990        let ObligationCauseCode::QuestionMark = obligation.cause.code().peel_derives() else {
991            return (false, false);
992        };
993        let self_ty = trait_pred.skip_binder().self_ty();
994        let found_ty = trait_pred.skip_binder().trait_ref.args.get(1).and_then(|a| a.as_type());
995        let noted_missing_impl =
996            self.note_missing_impl_for_question_mark(err, self_ty, found_ty, trait_pred);
997
998        let mut prev_ty = self.resolve_vars_if_possible(
999            typeck.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(self.tcx)),
1000        );
1001
1002        // We always look at the `E` type, because that's the only one affected by `?`. If the
1003        // incorrect `Result<T, E>` is because of the `T`, we'll get an E0308 on the whole
1004        // expression, after the `?` has "unwrapped" the `T`.
1005        let get_e_type = |prev_ty: Ty<'tcx>| -> Option<Ty<'tcx>> {
1006            let ty::Adt(def, args) = prev_ty.kind() else {
1007                return None;
1008            };
1009            let Some(arg) = args.get(1) else {
1010                return None;
1011            };
1012            if !self.tcx.is_diagnostic_item(sym::Result, def.did()) {
1013                return None;
1014            }
1015            arg.as_type()
1016        };
1017
1018        let mut suggested = false;
1019        let mut chain = vec![];
1020
1021        // The following logic is similar to `point_at_chain`, but that's focused on associated types
1022        let mut expr = expr;
1023        while let hir::ExprKind::MethodCall(path_segment, rcvr_expr, args, span) = expr.kind {
1024            // Point at every method call in the chain with the `Result` type.
1025            // let foo = bar.iter().map(mapper)?;
1026            //               ------ -----------
1027            expr = rcvr_expr;
1028            chain.push((span, prev_ty));
1029
1030            let next_ty = self.resolve_vars_if_possible(
1031                typeck.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(self.tcx)),
1032            );
1033
1034            let is_diagnostic_item = |symbol: Symbol, ty: Ty<'tcx>| {
1035                let ty::Adt(def, _) = ty.kind() else {
1036                    return false;
1037                };
1038                self.tcx.is_diagnostic_item(symbol, def.did())
1039            };
1040            // For each method in the chain, see if this is `Result::map_err` or
1041            // `Option::ok_or_else` and if it is, see if the closure passed to it has an incorrect
1042            // trailing `;`.
1043            if let Some(ty) = get_e_type(prev_ty)
1044                && let Some(found_ty) = found_ty
1045                // Ideally we would instead use `FnCtxt::lookup_method_for_diagnostic` for 100%
1046                // accurate check, but we are in the wrong stage to do that and looking for
1047                // `Result::map_err` by checking the Self type and the path segment is enough.
1048                // sym::ok_or_else
1049                && (
1050                    ( // Result::map_err
1051                        path_segment.ident.name == sym::map_err
1052                            && is_diagnostic_item(sym::Result, next_ty)
1053                    ) || ( // Option::ok_or_else
1054                        path_segment.ident.name == sym::ok_or_else
1055                            && is_diagnostic_item(sym::Option, next_ty)
1056                    )
1057                )
1058                // Found `Result<_, ()>?`
1059                && let ty::Tuple(tys) = found_ty.kind()
1060                && tys.is_empty()
1061                // The current method call returns `Result<_, ()>`
1062                && self.can_eq(obligation.param_env, ty, found_ty)
1063                // There's a single argument in the method call and it is a closure
1064                && let [arg] = args
1065                && let hir::ExprKind::Closure(closure) = arg.kind
1066                // The closure has a block for its body with no tail expression
1067                && let body = self.tcx.hir_body(closure.body)
1068                && let hir::ExprKind::Block(block, _) = body.value.kind
1069                && let None = block.expr
1070                // The last statement is of a type that can be converted to the return error type
1071                && let [.., stmt] = block.stmts
1072                && let hir::StmtKind::Semi(expr) = stmt.kind
1073                && let expr_ty = self.resolve_vars_if_possible(
1074                    typeck.expr_ty_adjusted_opt(expr)
1075                        .unwrap_or(Ty::new_misc_error(self.tcx)),
1076                )
1077                && self
1078                    .infcx
1079                    .type_implements_trait(
1080                        self.tcx.get_diagnostic_item(sym::From).unwrap(),
1081                        [self_ty, expr_ty],
1082                        obligation.param_env,
1083                    )
1084                    .must_apply_modulo_regions()
1085            {
1086                suggested = true;
1087                err.span_suggestion_short(
1088                    stmt.span.with_lo(expr.span.hi()),
1089                    "remove this semicolon",
1090                    String::new(),
1091                    Applicability::MachineApplicable,
1092                );
1093            }
1094
1095            prev_ty = next_ty;
1096
1097            if let hir::ExprKind::Path(hir::QPath::Resolved(None, path)) = expr.kind
1098                && let hir::Path { res: hir::def::Res::Local(hir_id), .. } = path
1099                && let hir::Node::Pat(binding) = self.tcx.hir_node(*hir_id)
1100            {
1101                let parent = self.tcx.parent_hir_node(binding.hir_id);
1102                // We've reached the root of the method call chain...
1103                if let hir::Node::LetStmt(local) = parent
1104                    && let Some(binding_expr) = local.init
1105                {
1106                    // ...and it is a binding. Get the binding creation and continue the chain.
1107                    expr = binding_expr;
1108                }
1109                if let hir::Node::Param(_param) = parent {
1110                    // ...and it is an fn argument.
1111                    break;
1112                }
1113            }
1114        }
1115        // `expr` is now the "root" expression of the method call chain, which can be any
1116        // expression kind, like a method call or a path. If this expression is `Result<T, E>` as
1117        // well, then we also point at it.
1118        prev_ty = self.resolve_vars_if_possible(
1119            typeck.expr_ty_adjusted_opt(expr).unwrap_or(Ty::new_misc_error(self.tcx)),
1120        );
1121        chain.push((expr.span, prev_ty));
1122
1123        let mut prev = None;
1124        for (span, err_ty) in chain.into_iter().rev() {
1125            let err_ty = get_e_type(err_ty);
1126            let err_ty = match (err_ty, prev) {
1127                (Some(err_ty), Some(prev)) if !self.can_eq(obligation.param_env, err_ty, prev) => {
1128                    err_ty
1129                }
1130                (Some(err_ty), None) => err_ty,
1131                _ => {
1132                    prev = err_ty;
1133                    continue;
1134                }
1135            };
1136            if self
1137                .infcx
1138                .type_implements_trait(
1139                    self.tcx.get_diagnostic_item(sym::From).unwrap(),
1140                    [self_ty, err_ty],
1141                    obligation.param_env,
1142                )
1143                .must_apply_modulo_regions()
1144            {
1145                if !suggested {
1146                    let err_ty = self.tcx.short_string(err_ty, err.long_ty_path());
1147                    err.span_label(span, format!("this has type `Result<_, {err_ty}>`"));
1148                }
1149            } else {
1150                let err_ty = self.tcx.short_string(err_ty, err.long_ty_path());
1151                err.span_label(
1152                    span,
1153                    format!(
1154                        "this can't be annotated with `?` because it has type `Result<_, {err_ty}>`",
1155                    ),
1156                );
1157            }
1158            prev = Some(err_ty);
1159        }
1160        (suggested, noted_missing_impl)
1161    }
1162
1163    fn note_missing_impl_for_question_mark(
1164        &self,
1165        err: &mut Diag<'_>,
1166        self_ty: Ty<'_>,
1167        found_ty: Option<Ty<'_>>,
1168        trait_pred: ty::PolyTraitPredicate<'tcx>,
1169    ) -> bool {
1170        match (self_ty.kind(), found_ty) {
1171            (ty::Adt(def, _), Some(ty))
1172                if let ty::Adt(found, _) = ty.kind()
1173                    && def.did().is_local()
1174                    && found.did().is_local() =>
1175            {
1176                err.span_note(
1177                    self.tcx.def_span(def.did()),
1178                    format!("`{self_ty}` needs to implement `From<{ty}>`"),
1179                );
1180                err.span_note(
1181                    self.tcx.def_span(found.did()),
1182                    format!("alternatively, `{ty}` needs to implement `Into<{self_ty}>`"),
1183                );
1184            }
1185            (ty::Adt(def, _), None) if def.did().is_local() => {
1186                let trait_path = self.tcx.short_string(
1187                    trait_pred.skip_binder().trait_ref.print_only_trait_path(),
1188                    err.long_ty_path(),
1189                );
1190                err.span_note(
1191                    self.tcx.def_span(def.did()),
1192                    format!("`{self_ty}` needs to implement `{trait_path}`"),
1193                );
1194            }
1195            (ty::Adt(def, _), Some(ty)) if def.did().is_local() => {
1196                err.span_note(
1197                    self.tcx.def_span(def.did()),
1198                    format!("`{self_ty}` needs to implement `From<{ty}>`"),
1199                );
1200            }
1201            (_, Some(ty))
1202                if let ty::Adt(def, _) = ty.kind()
1203                    && def.did().is_local() =>
1204            {
1205                err.span_note(
1206                    self.tcx.def_span(def.did()),
1207                    format!("`{ty}` needs to implement `Into<{self_ty}>`"),
1208                );
1209            }
1210            _ => return false,
1211        }
1212        true
1213    }
1214
1215    fn report_const_param_not_wf(
1216        &self,
1217        ty: Ty<'tcx>,
1218        obligation: &PredicateObligation<'tcx>,
1219    ) -> Diag<'a> {
1220        let param = obligation.cause.body_id;
1221        let hir::GenericParamKind::Const { ty: &hir::Ty { span, .. }, .. } =
1222            self.tcx.hir_node_by_def_id(param).expect_generic_param().kind
1223        else {
1224            bug!()
1225        };
1226
1227        let mut file = None;
1228        let ty_str = self.tcx.short_string(ty, &mut file);
1229        let mut diag = match ty.kind() {
1230            ty::Float(_) => {
1231                struct_span_code_err!(
1232                    self.dcx(),
1233                    span,
1234                    E0741,
1235                    "`{ty_str}` is forbidden as the type of a const generic parameter",
1236                )
1237            }
1238            ty::FnPtr(..) => {
1239                struct_span_code_err!(
1240                    self.dcx(),
1241                    span,
1242                    E0741,
1243                    "using function pointers as const generic parameters is forbidden",
1244                )
1245            }
1246            ty::RawPtr(_, _) => {
1247                struct_span_code_err!(
1248                    self.dcx(),
1249                    span,
1250                    E0741,
1251                    "using raw pointers as const generic parameters is forbidden",
1252                )
1253            }
1254            ty::Adt(def, _) => {
1255                // We should probably see if we're *allowed* to derive `ConstParamTy` on the type...
1256                let mut diag = struct_span_code_err!(
1257                    self.dcx(),
1258                    span,
1259                    E0741,
1260                    "`{ty_str}` must implement `ConstParamTy` to be used as the type of a const generic parameter",
1261                );
1262                // Only suggest derive if this isn't a derived obligation,
1263                // and the struct is local.
1264                if let Some(span) = self.tcx.hir_span_if_local(def.did())
1265                    && obligation.cause.code().parent().is_none()
1266                {
1267                    if ty.is_structural_eq_shallow(self.tcx) {
1268                        diag.span_suggestion(
1269                            span,
1270                            "add `#[derive(ConstParamTy)]` to the struct",
1271                            "#[derive(ConstParamTy)]\n",
1272                            Applicability::MachineApplicable,
1273                        );
1274                    } else {
1275                        // FIXME(adt_const_params): We should check there's not already an
1276                        // overlapping `Eq`/`PartialEq` impl.
1277                        diag.span_suggestion(
1278                            span,
1279                            "add `#[derive(ConstParamTy, PartialEq, Eq)]` to the struct",
1280                            "#[derive(ConstParamTy, PartialEq, Eq)]\n",
1281                            Applicability::MachineApplicable,
1282                        );
1283                    }
1284                }
1285                diag
1286            }
1287            _ => {
1288                struct_span_code_err!(
1289                    self.dcx(),
1290                    span,
1291                    E0741,
1292                    "`{ty_str}` can't be used as a const parameter type",
1293                )
1294            }
1295        };
1296        diag.long_ty_path = file;
1297
1298        let mut code = obligation.cause.code();
1299        let mut pred = obligation.predicate.as_trait_clause();
1300        while let Some((next_code, next_pred)) = code.parent_with_predicate() {
1301            if let Some(pred) = pred {
1302                self.enter_forall(pred, |pred| {
1303                    let ty = self.tcx.short_string(pred.self_ty(), diag.long_ty_path());
1304                    let trait_path = self
1305                        .tcx
1306                        .short_string(pred.print_modifiers_and_trait_path(), diag.long_ty_path());
1307                    diag.note(format!("`{ty}` must implement `{trait_path}`, but it does not"));
1308                })
1309            }
1310            code = next_code;
1311            pred = next_pred;
1312        }
1313
1314        diag
1315    }
1316}
1317
1318impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
1319    fn can_match_trait(
1320        &self,
1321        param_env: ty::ParamEnv<'tcx>,
1322        goal: ty::TraitPredicate<'tcx>,
1323        assumption: ty::PolyTraitPredicate<'tcx>,
1324    ) -> bool {
1325        // Fast path
1326        if goal.polarity != assumption.polarity() {
1327            return false;
1328        }
1329
1330        let trait_assumption = self.instantiate_binder_with_fresh_vars(
1331            DUMMY_SP,
1332            infer::BoundRegionConversionTime::HigherRankedType,
1333            assumption,
1334        );
1335
1336        self.can_eq(param_env, goal.trait_ref, trait_assumption.trait_ref)
1337    }
1338
1339    fn can_match_projection(
1340        &self,
1341        param_env: ty::ParamEnv<'tcx>,
1342        goal: ty::ProjectionPredicate<'tcx>,
1343        assumption: ty::PolyProjectionPredicate<'tcx>,
1344    ) -> bool {
1345        let assumption = self.instantiate_binder_with_fresh_vars(
1346            DUMMY_SP,
1347            infer::BoundRegionConversionTime::HigherRankedType,
1348            assumption,
1349        );
1350
1351        self.can_eq(param_env, goal.projection_term, assumption.projection_term)
1352            && self.can_eq(param_env, goal.term, assumption.term)
1353    }
1354
1355    // returns if `cond` not occurring implies that `error` does not occur - i.e., that
1356    // `error` occurring implies that `cond` occurs.
1357    #[instrument(level = "debug", skip(self), ret)]
1358    pub(super) fn error_implies(
1359        &self,
1360        cond: Goal<'tcx, ty::Predicate<'tcx>>,
1361        error: Goal<'tcx, ty::Predicate<'tcx>>,
1362    ) -> bool {
1363        if cond == error {
1364            return true;
1365        }
1366
1367        // FIXME: We could be smarter about this, i.e. if cond's param-env is a
1368        // subset of error's param-env. This only matters when binders will carry
1369        // predicates though, and obviously only matters for error reporting.
1370        if cond.param_env != error.param_env {
1371            return false;
1372        }
1373        let param_env = error.param_env;
1374
1375        if let Some(error) = error.predicate.as_trait_clause() {
1376            self.enter_forall(error, |error| {
1377                elaborate(self.tcx, std::iter::once(cond.predicate))
1378                    .filter_map(|implied| implied.as_trait_clause())
1379                    .any(|implied| self.can_match_trait(param_env, error, implied))
1380            })
1381        } else if let Some(error) = error.predicate.as_projection_clause() {
1382            self.enter_forall(error, |error| {
1383                elaborate(self.tcx, std::iter::once(cond.predicate))
1384                    .filter_map(|implied| implied.as_projection_clause())
1385                    .any(|implied| self.can_match_projection(param_env, error, implied))
1386            })
1387        } else {
1388            false
1389        }
1390    }
1391
1392    #[instrument(level = "debug", skip_all)]
1393    pub(super) fn report_projection_error(
1394        &self,
1395        obligation: &PredicateObligation<'tcx>,
1396        error: &MismatchedProjectionTypes<'tcx>,
1397    ) -> ErrorGuaranteed {
1398        let predicate = self.resolve_vars_if_possible(obligation.predicate);
1399
1400        if let Err(e) = predicate.error_reported() {
1401            return e;
1402        }
1403
1404        self.probe(|_| {
1405            // try to find the mismatched types to report the error with.
1406            //
1407            // this can fail if the problem was higher-ranked, in which
1408            // cause I have no idea for a good error message.
1409            let bound_predicate = predicate.kind();
1410            let (values, err) = match bound_predicate.skip_binder() {
1411                ty::PredicateKind::Clause(ty::ClauseKind::Projection(data)) => {
1412                    let ocx = ObligationCtxt::new(self);
1413
1414                    let data = self.instantiate_binder_with_fresh_vars(
1415                        obligation.cause.span,
1416                        infer::BoundRegionConversionTime::HigherRankedType,
1417                        bound_predicate.rebind(data),
1418                    );
1419                    let unnormalized_term = data.projection_term.to_term(self.tcx);
1420                    // FIXME(-Znext-solver): For diagnostic purposes, it would be nice
1421                    // to deeply normalize this type.
1422                    let normalized_term =
1423                        ocx.normalize(&obligation.cause, obligation.param_env, unnormalized_term);
1424
1425                    // constrain inference variables a bit more to nested obligations from normalize so
1426                    // we can have more helpful errors.
1427                    //
1428                    // we intentionally drop errors from normalization here,
1429                    // since the normalization is just done to improve the error message.
1430                    let _ = ocx.select_where_possible();
1431
1432                    if let Err(new_err) =
1433                        ocx.eq(&obligation.cause, obligation.param_env, data.term, normalized_term)
1434                    {
1435                        (
1436                            Some((
1437                                data.projection_term,
1438                                self.resolve_vars_if_possible(normalized_term),
1439                                data.term,
1440                            )),
1441                            new_err,
1442                        )
1443                    } else {
1444                        (None, error.err)
1445                    }
1446                }
1447                ty::PredicateKind::AliasRelate(lhs, rhs, _) => {
1448                    let derive_better_type_error =
1449                        |alias_term: ty::AliasTerm<'tcx>, expected_term: ty::Term<'tcx>| {
1450                            let ocx = ObligationCtxt::new(self);
1451
1452                            let Ok(normalized_term) = ocx.structurally_normalize_term(
1453                                &ObligationCause::dummy(),
1454                                obligation.param_env,
1455                                alias_term.to_term(self.tcx),
1456                            ) else {
1457                                return None;
1458                            };
1459
1460                            if let Err(terr) = ocx.eq(
1461                                &ObligationCause::dummy(),
1462                                obligation.param_env,
1463                                expected_term,
1464                                normalized_term,
1465                            ) {
1466                                Some((terr, self.resolve_vars_if_possible(normalized_term)))
1467                            } else {
1468                                None
1469                            }
1470                        };
1471
1472                    if let Some(lhs) = lhs.to_alias_term()
1473                        && let Some((better_type_err, expected_term)) =
1474                            derive_better_type_error(lhs, rhs)
1475                    {
1476                        (
1477                            Some((lhs, self.resolve_vars_if_possible(expected_term), rhs)),
1478                            better_type_err,
1479                        )
1480                    } else if let Some(rhs) = rhs.to_alias_term()
1481                        && let Some((better_type_err, expected_term)) =
1482                            derive_better_type_error(rhs, lhs)
1483                    {
1484                        (
1485                            Some((rhs, self.resolve_vars_if_possible(expected_term), lhs)),
1486                            better_type_err,
1487                        )
1488                    } else {
1489                        (None, error.err)
1490                    }
1491                }
1492                _ => (None, error.err),
1493            };
1494
1495            let mut file = None;
1496            let (msg, span, closure_span) = values
1497                .and_then(|(predicate, normalized_term, expected_term)| {
1498                    self.maybe_detailed_projection_msg(
1499                        obligation.cause.span,
1500                        predicate,
1501                        normalized_term,
1502                        expected_term,
1503                        &mut file,
1504                    )
1505                })
1506                .unwrap_or_else(|| {
1507                    (
1508                        with_forced_trimmed_paths!(format!(
1509                            "type mismatch resolving `{}`",
1510                            self.tcx
1511                                .short_string(self.resolve_vars_if_possible(predicate), &mut file),
1512                        )),
1513                        obligation.cause.span,
1514                        None,
1515                    )
1516                });
1517            let mut diag = struct_span_code_err!(self.dcx(), span, E0271, "{msg}");
1518            *diag.long_ty_path() = file;
1519            if let Some(span) = closure_span {
1520                // Mark the closure decl so that it is seen even if we are pointing at the return
1521                // type or expression.
1522                //
1523                // error[E0271]: expected `{closure@foo.rs:41:16}` to be a closure that returns
1524                //               `Unit3`, but it returns `Unit4`
1525                //   --> $DIR/foo.rs:43:17
1526                //    |
1527                // LL |     let v = Unit2.m(
1528                //    |                   - required by a bound introduced by this call
1529                // ...
1530                // LL |             f: |x| {
1531                //    |                --- /* this span */
1532                // LL |                 drop(x);
1533                // LL |                 Unit4
1534                //    |                 ^^^^^ expected `Unit3`, found `Unit4`
1535                //    |
1536                diag.span_label(span, "this closure");
1537                if !span.overlaps(obligation.cause.span) {
1538                    // Point at the binding corresponding to the closure where it is used.
1539                    diag.span_label(obligation.cause.span, "closure used here");
1540                }
1541            }
1542
1543            let secondary_span = self.probe(|_| {
1544                let ty::PredicateKind::Clause(ty::ClauseKind::Projection(proj)) =
1545                    predicate.kind().skip_binder()
1546                else {
1547                    return None;
1548                };
1549
1550                let trait_ref = self.enter_forall_and_leak_universe(
1551                    predicate.kind().rebind(proj.projection_term.trait_ref(self.tcx)),
1552                );
1553                let Ok(Some(ImplSource::UserDefined(impl_data))) =
1554                    SelectionContext::new(self).select(&obligation.with(self.tcx, trait_ref))
1555                else {
1556                    return None;
1557                };
1558
1559                let Ok(node) =
1560                    specialization_graph::assoc_def(self.tcx, impl_data.impl_def_id, proj.def_id())
1561                else {
1562                    return None;
1563                };
1564
1565                if !node.is_final() {
1566                    return None;
1567                }
1568
1569                match self.tcx.hir_get_if_local(node.item.def_id) {
1570                    Some(
1571                        hir::Node::TraitItem(hir::TraitItem {
1572                            kind: hir::TraitItemKind::Type(_, Some(ty)),
1573                            ..
1574                        })
1575                        | hir::Node::ImplItem(hir::ImplItem {
1576                            kind: hir::ImplItemKind::Type(ty),
1577                            ..
1578                        }),
1579                    ) => Some((
1580                        ty.span,
1581                        with_forced_trimmed_paths!(Cow::from(format!(
1582                            "type mismatch resolving `{}`",
1583                            self.tcx.short_string(
1584                                self.resolve_vars_if_possible(predicate),
1585                                diag.long_ty_path()
1586                            ),
1587                        ))),
1588                        true,
1589                    )),
1590                    _ => None,
1591                }
1592            });
1593
1594            self.note_type_err(
1595                &mut diag,
1596                &obligation.cause,
1597                secondary_span,
1598                values.map(|(_, normalized_ty, expected_ty)| {
1599                    obligation.param_env.and(infer::ValuePairs::Terms(ExpectedFound::new(
1600                        expected_ty,
1601                        normalized_ty,
1602                    )))
1603                }),
1604                err,
1605                false,
1606                Some(span),
1607            );
1608            self.note_obligation_cause(&mut diag, obligation);
1609            diag.emit()
1610        })
1611    }
1612
1613    fn maybe_detailed_projection_msg(
1614        &self,
1615        mut span: Span,
1616        projection_term: ty::AliasTerm<'tcx>,
1617        normalized_ty: ty::Term<'tcx>,
1618        expected_ty: ty::Term<'tcx>,
1619        long_ty_path: &mut Option<PathBuf>,
1620    ) -> Option<(String, Span, Option<Span>)> {
1621        let trait_def_id = projection_term.trait_def_id(self.tcx);
1622        let self_ty = projection_term.self_ty();
1623
1624        with_forced_trimmed_paths! {
1625            if self.tcx.is_lang_item(projection_term.def_id, LangItem::FnOnceOutput) {
1626                let (span, closure_span) = if let ty::Closure(def_id, _) = self_ty.kind() {
1627                    let def_span = self.tcx.def_span(def_id);
1628                    if let Some(local_def_id) = def_id.as_local()
1629                        && let node = self.tcx.hir_node_by_def_id(local_def_id)
1630                        && let Some(fn_decl) = node.fn_decl()
1631                        && let Some(id) = node.body_id()
1632                    {
1633                        span = match fn_decl.output {
1634                            hir::FnRetTy::Return(ty) => ty.span,
1635                            hir::FnRetTy::DefaultReturn(_) => {
1636                                let body = self.tcx.hir_body(id);
1637                                match body.value.kind {
1638                                    hir::ExprKind::Block(
1639                                        hir::Block { expr: Some(expr), .. },
1640                                        _,
1641                                    ) => expr.span,
1642                                    hir::ExprKind::Block(
1643                                        hir::Block {
1644                                            expr: None, stmts: [.., last], ..
1645                                        },
1646                                        _,
1647                                    ) => last.span,
1648                                    _ => body.value.span,
1649                                }
1650                            }
1651                        };
1652                    }
1653                    (span, Some(def_span))
1654                } else {
1655                    (span, None)
1656                };
1657                let item = match self_ty.kind() {
1658                    ty::FnDef(def, _) => self.tcx.item_name(*def).to_string(),
1659                    _ => self.tcx.short_string(self_ty, long_ty_path),
1660                };
1661                let expected_ty = self.tcx.short_string(expected_ty, long_ty_path);
1662                let normalized_ty = self.tcx.short_string(normalized_ty, long_ty_path);
1663                Some((format!(
1664                    "expected `{item}` to return `{expected_ty}`, but it returns `{normalized_ty}`",
1665                ), span, closure_span))
1666            } else if self.tcx.is_lang_item(trait_def_id, LangItem::Future) {
1667                let self_ty = self.tcx.short_string(self_ty, long_ty_path);
1668                let expected_ty = self.tcx.short_string(expected_ty, long_ty_path);
1669                let normalized_ty = self.tcx.short_string(normalized_ty, long_ty_path);
1670                Some((format!(
1671                    "expected `{self_ty}` to be a future that resolves to `{expected_ty}`, but it \
1672                     resolves to `{normalized_ty}`"
1673                ), span, None))
1674            } else if Some(trait_def_id) == self.tcx.get_diagnostic_item(sym::Iterator) {
1675                let self_ty = self.tcx.short_string(self_ty, long_ty_path);
1676                let expected_ty = self.tcx.short_string(expected_ty, long_ty_path);
1677                let normalized_ty = self.tcx.short_string(normalized_ty, long_ty_path);
1678                Some((format!(
1679                    "expected `{self_ty}` to be an iterator that yields `{expected_ty}`, but it \
1680                     yields `{normalized_ty}`"
1681                ), span, None))
1682            } else {
1683                None
1684            }
1685        }
1686    }
1687
1688    pub fn fuzzy_match_tys(
1689        &self,
1690        mut a: Ty<'tcx>,
1691        mut b: Ty<'tcx>,
1692        ignoring_lifetimes: bool,
1693    ) -> Option<CandidateSimilarity> {
1694        /// returns the fuzzy category of a given type, or None
1695        /// if the type can be equated to any type.
1696        fn type_category(tcx: TyCtxt<'_>, t: Ty<'_>) -> Option<u32> {
1697            match t.kind() {
1698                ty::Bool => Some(0),
1699                ty::Char => Some(1),
1700                ty::Str => Some(2),
1701                ty::Adt(def, _) if tcx.is_lang_item(def.did(), LangItem::String) => Some(2),
1702                ty::Int(..)
1703                | ty::Uint(..)
1704                | ty::Float(..)
1705                | ty::Infer(ty::IntVar(..) | ty::FloatVar(..)) => Some(4),
1706                ty::Ref(..) | ty::RawPtr(..) => Some(5),
1707                ty::Array(..) | ty::Slice(..) => Some(6),
1708                ty::FnDef(..) | ty::FnPtr(..) => Some(7),
1709                ty::Dynamic(..) => Some(8),
1710                ty::Closure(..) => Some(9),
1711                ty::Tuple(..) => Some(10),
1712                ty::Param(..) => Some(11),
1713                ty::Alias(ty::Projection, ..) => Some(12),
1714                ty::Alias(ty::Inherent, ..) => Some(13),
1715                ty::Alias(ty::Opaque, ..) => Some(14),
1716                ty::Alias(ty::Free, ..) => Some(15),
1717                ty::Never => Some(16),
1718                ty::Adt(..) => Some(17),
1719                ty::Coroutine(..) => Some(18),
1720                ty::Foreign(..) => Some(19),
1721                ty::CoroutineWitness(..) => Some(20),
1722                ty::CoroutineClosure(..) => Some(21),
1723                ty::Pat(..) => Some(22),
1724                ty::UnsafeBinder(..) => Some(23),
1725                ty::Placeholder(..) | ty::Bound(..) | ty::Infer(..) | ty::Error(_) => None,
1726            }
1727        }
1728
1729        let strip_references = |mut t: Ty<'tcx>| -> Ty<'tcx> {
1730            loop {
1731                match t.kind() {
1732                    ty::Ref(_, inner, _) | ty::RawPtr(inner, _) => t = *inner,
1733                    _ => break t,
1734                }
1735            }
1736        };
1737
1738        if !ignoring_lifetimes {
1739            a = strip_references(a);
1740            b = strip_references(b);
1741        }
1742
1743        let cat_a = type_category(self.tcx, a)?;
1744        let cat_b = type_category(self.tcx, b)?;
1745        if a == b {
1746            Some(CandidateSimilarity::Exact { ignoring_lifetimes })
1747        } else if cat_a == cat_b {
1748            match (a.kind(), b.kind()) {
1749                (ty::Adt(def_a, _), ty::Adt(def_b, _)) => def_a == def_b,
1750                (ty::Foreign(def_a), ty::Foreign(def_b)) => def_a == def_b,
1751                // Matching on references results in a lot of unhelpful
1752                // suggestions, so let's just not do that for now.
1753                //
1754                // We still upgrade successful matches to `ignoring_lifetimes: true`
1755                // to prioritize that impl.
1756                (ty::Ref(..) | ty::RawPtr(..), ty::Ref(..) | ty::RawPtr(..)) => {
1757                    self.fuzzy_match_tys(a, b, true).is_some()
1758                }
1759                _ => true,
1760            }
1761            .then_some(CandidateSimilarity::Fuzzy { ignoring_lifetimes })
1762        } else if ignoring_lifetimes {
1763            None
1764        } else {
1765            self.fuzzy_match_tys(a, b, true)
1766        }
1767    }
1768
1769    pub(super) fn describe_closure(&self, kind: hir::ClosureKind) -> &'static str {
1770        match kind {
1771            hir::ClosureKind::Closure => "a closure",
1772            hir::ClosureKind::Coroutine(hir::CoroutineKind::Coroutine(_)) => "a coroutine",
1773            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1774                hir::CoroutineDesugaring::Async,
1775                hir::CoroutineSource::Block,
1776            )) => "an async block",
1777            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1778                hir::CoroutineDesugaring::Async,
1779                hir::CoroutineSource::Fn,
1780            )) => "an async function",
1781            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1782                hir::CoroutineDesugaring::Async,
1783                hir::CoroutineSource::Closure,
1784            ))
1785            | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Async) => {
1786                "an async closure"
1787            }
1788            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1789                hir::CoroutineDesugaring::AsyncGen,
1790                hir::CoroutineSource::Block,
1791            )) => "an async gen block",
1792            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1793                hir::CoroutineDesugaring::AsyncGen,
1794                hir::CoroutineSource::Fn,
1795            )) => "an async gen function",
1796            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1797                hir::CoroutineDesugaring::AsyncGen,
1798                hir::CoroutineSource::Closure,
1799            ))
1800            | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::AsyncGen) => {
1801                "an async gen closure"
1802            }
1803            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1804                hir::CoroutineDesugaring::Gen,
1805                hir::CoroutineSource::Block,
1806            )) => "a gen block",
1807            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1808                hir::CoroutineDesugaring::Gen,
1809                hir::CoroutineSource::Fn,
1810            )) => "a gen function",
1811            hir::ClosureKind::Coroutine(hir::CoroutineKind::Desugared(
1812                hir::CoroutineDesugaring::Gen,
1813                hir::CoroutineSource::Closure,
1814            ))
1815            | hir::ClosureKind::CoroutineClosure(hir::CoroutineDesugaring::Gen) => "a gen closure",
1816        }
1817    }
1818
1819    pub(super) fn find_similar_impl_candidates(
1820        &self,
1821        trait_pred: ty::PolyTraitPredicate<'tcx>,
1822    ) -> Vec<ImplCandidate<'tcx>> {
1823        let mut candidates: Vec<_> = self
1824            .tcx
1825            .all_impls(trait_pred.def_id())
1826            .filter_map(|def_id| {
1827                let imp = self.tcx.impl_trait_header(def_id).unwrap();
1828                if imp.polarity != ty::ImplPolarity::Positive
1829                    || !self.tcx.is_user_visible_dep(def_id.krate)
1830                {
1831                    return None;
1832                }
1833                let imp = imp.trait_ref.skip_binder();
1834
1835                self.fuzzy_match_tys(trait_pred.skip_binder().self_ty(), imp.self_ty(), false).map(
1836                    |similarity| ImplCandidate { trait_ref: imp, similarity, impl_def_id: def_id },
1837                )
1838            })
1839            .collect();
1840        if candidates.iter().any(|c| matches!(c.similarity, CandidateSimilarity::Exact { .. })) {
1841            // If any of the candidates is a perfect match, we don't want to show all of them.
1842            // This is particularly relevant for the case of numeric types (as they all have the
1843            // same category).
1844            candidates.retain(|c| matches!(c.similarity, CandidateSimilarity::Exact { .. }));
1845        }
1846        candidates
1847    }
1848
1849    pub(super) fn report_similar_impl_candidates(
1850        &self,
1851        impl_candidates: &[ImplCandidate<'tcx>],
1852        trait_pred: ty::PolyTraitPredicate<'tcx>,
1853        body_def_id: LocalDefId,
1854        err: &mut Diag<'_>,
1855        other: bool,
1856        param_env: ty::ParamEnv<'tcx>,
1857    ) -> bool {
1858        let alternative_candidates = |def_id: DefId| {
1859            let mut impl_candidates: Vec<_> = self
1860                .tcx
1861                .all_impls(def_id)
1862                // ignore `do_not_recommend` items
1863                .filter(|def_id| !self.tcx.do_not_recommend_impl(*def_id))
1864                // Ignore automatically derived impls and `!Trait` impls.
1865                .filter_map(|def_id| self.tcx.impl_trait_header(def_id))
1866                .filter_map(|header| {
1867                    (header.polarity != ty::ImplPolarity::Negative
1868                        || self.tcx.is_automatically_derived(def_id))
1869                    .then(|| header.trait_ref.instantiate_identity())
1870                })
1871                .filter(|trait_ref| {
1872                    let self_ty = trait_ref.self_ty();
1873                    // Avoid mentioning type parameters.
1874                    if let ty::Param(_) = self_ty.kind() {
1875                        false
1876                    }
1877                    // Avoid mentioning types that are private to another crate
1878                    else if let ty::Adt(def, _) = self_ty.peel_refs().kind() {
1879                        // FIXME(compiler-errors): This could be generalized, both to
1880                        // be more granular, and probably look past other `#[fundamental]`
1881                        // types, too.
1882                        self.tcx.visibility(def.did()).is_accessible_from(body_def_id, self.tcx)
1883                    } else {
1884                        true
1885                    }
1886                })
1887                .collect();
1888
1889            impl_candidates.sort_by_key(|tr| tr.to_string());
1890            impl_candidates.dedup();
1891            impl_candidates
1892        };
1893
1894        // We'll check for the case where the reason for the mismatch is that the trait comes from
1895        // one crate version and the type comes from another crate version, even though they both
1896        // are from the same crate.
1897        let trait_def_id = trait_pred.def_id();
1898        let trait_name = self.tcx.item_name(trait_def_id);
1899        let crate_name = self.tcx.crate_name(trait_def_id.krate);
1900        if let Some(other_trait_def_id) = self.tcx.all_traits_including_private().find(|def_id| {
1901            trait_name == self.tcx.item_name(trait_def_id)
1902                && trait_def_id.krate != def_id.krate
1903                && crate_name == self.tcx.crate_name(def_id.krate)
1904        }) {
1905            // We've found two different traits with the same name, same crate name, but
1906            // different crate `DefId`. We highlight the traits.
1907
1908            let found_type =
1909                if let ty::Adt(def, _) = trait_pred.self_ty().skip_binder().peel_refs().kind() {
1910                    Some(def.did())
1911                } else {
1912                    None
1913                };
1914            let candidates = if impl_candidates.is_empty() {
1915                alternative_candidates(trait_def_id)
1916            } else {
1917                impl_candidates.into_iter().map(|cand| cand.trait_ref).collect()
1918            };
1919            let mut span: MultiSpan = self.tcx.def_span(trait_def_id).into();
1920            span.push_span_label(self.tcx.def_span(trait_def_id), "this is the required trait");
1921            for (sp, label) in [trait_def_id, other_trait_def_id]
1922                .iter()
1923                // The current crate-version might depend on another version of the same crate
1924                // (Think "semver-trick"). Do not call `extern_crate` in that case for the local
1925                // crate as that doesn't make sense and ICEs (#133563).
1926                .filter(|def_id| !def_id.is_local())
1927                .filter_map(|def_id| self.tcx.extern_crate(def_id.krate))
1928                .map(|data| {
1929                    let dependency = if data.dependency_of == LOCAL_CRATE {
1930                        "direct dependency of the current crate".to_string()
1931                    } else {
1932                        let dep = self.tcx.crate_name(data.dependency_of);
1933                        format!("dependency of crate `{dep}`")
1934                    };
1935                    (
1936                        data.span,
1937                        format!("one version of crate `{crate_name}` used here, as a {dependency}"),
1938                    )
1939                })
1940            {
1941                span.push_span_label(sp, label);
1942            }
1943            let mut points_at_type = false;
1944            if let Some(found_type) = found_type {
1945                span.push_span_label(
1946                    self.tcx.def_span(found_type),
1947                    "this type doesn't implement the required trait",
1948                );
1949                for trait_ref in candidates {
1950                    if let ty::Adt(def, _) = trait_ref.self_ty().peel_refs().kind()
1951                        && let candidate_def_id = def.did()
1952                        && let Some(name) = self.tcx.opt_item_name(candidate_def_id)
1953                        && let Some(found) = self.tcx.opt_item_name(found_type)
1954                        && name == found
1955                        && candidate_def_id.krate != found_type.krate
1956                        && self.tcx.crate_name(candidate_def_id.krate)
1957                            == self.tcx.crate_name(found_type.krate)
1958                    {
1959                        // A candidate was found of an item with the same name, from two separate
1960                        // versions of the same crate, let's clarify.
1961                        let candidate_span = self.tcx.def_span(candidate_def_id);
1962                        span.push_span_label(
1963                            candidate_span,
1964                            "this type implements the required trait",
1965                        );
1966                        points_at_type = true;
1967                    }
1968                }
1969            }
1970            span.push_span_label(self.tcx.def_span(other_trait_def_id), "this is the found trait");
1971            err.highlighted_span_note(
1972                span,
1973                vec![
1974                    StringPart::normal("there are ".to_string()),
1975                    StringPart::highlighted("multiple different versions".to_string()),
1976                    StringPart::normal(" of crate `".to_string()),
1977                    StringPart::highlighted(format!("{crate_name}")),
1978                    StringPart::normal("` in the dependency graph".to_string()),
1979                ],
1980            );
1981            if points_at_type {
1982                // We only clarify that the same type from different crate versions are not the
1983                // same when we *find* the same type coming from different crate versions, otherwise
1984                // it could be that it was a type provided by a different crate than the one that
1985                // provides the trait, and mentioning this adds verbosity without clarification.
1986                err.highlighted_note(vec![
1987                    StringPart::normal(
1988                        "two types coming from two different versions of the same crate are \
1989                         different types "
1990                            .to_string(),
1991                    ),
1992                    StringPart::highlighted("even if they look the same".to_string()),
1993                ]);
1994            }
1995            err.highlighted_help(vec![
1996                StringPart::normal("you can use `".to_string()),
1997                StringPart::highlighted("cargo tree".to_string()),
1998                StringPart::normal("` to explore your dependency tree".to_string()),
1999            ]);
2000            return true;
2001        }
2002
2003        if let [single] = &impl_candidates {
2004            // If we have a single implementation, try to unify it with the trait ref
2005            // that failed. This should uncover a better hint for what *is* implemented.
2006            if self.probe(|_| {
2007                let ocx = ObligationCtxt::new(self);
2008
2009                self.enter_forall(trait_pred, |obligation_trait_ref| {
2010                    let impl_args = self.fresh_args_for_item(DUMMY_SP, single.impl_def_id);
2011                    let impl_trait_ref = ocx.normalize(
2012                        &ObligationCause::dummy(),
2013                        param_env,
2014                        ty::EarlyBinder::bind(single.trait_ref).instantiate(self.tcx, impl_args),
2015                    );
2016
2017                    ocx.register_obligations(
2018                        self.tcx
2019                            .predicates_of(single.impl_def_id)
2020                            .instantiate(self.tcx, impl_args)
2021                            .into_iter()
2022                            .map(|(clause, _)| {
2023                                Obligation::new(
2024                                    self.tcx,
2025                                    ObligationCause::dummy(),
2026                                    param_env,
2027                                    clause,
2028                                )
2029                            }),
2030                    );
2031                    if !ocx.select_where_possible().is_empty() {
2032                        return false;
2033                    }
2034
2035                    let mut terrs = vec![];
2036                    for (obligation_arg, impl_arg) in
2037                        std::iter::zip(obligation_trait_ref.trait_ref.args, impl_trait_ref.args)
2038                    {
2039                        if (obligation_arg, impl_arg).references_error() {
2040                            return false;
2041                        }
2042                        if let Err(terr) =
2043                            ocx.eq(&ObligationCause::dummy(), param_env, impl_arg, obligation_arg)
2044                        {
2045                            terrs.push(terr);
2046                        }
2047                        if !ocx.select_where_possible().is_empty() {
2048                            return false;
2049                        }
2050                    }
2051
2052                    // Literally nothing unified, just give up.
2053                    if terrs.len() == impl_trait_ref.args.len() {
2054                        return false;
2055                    }
2056
2057                    let impl_trait_ref = self.resolve_vars_if_possible(impl_trait_ref);
2058                    if impl_trait_ref.references_error() {
2059                        return false;
2060                    }
2061
2062                    if let [child, ..] = &err.children[..]
2063                        && child.level == Level::Help
2064                        && let Some(line) = child.messages.get(0)
2065                        && let Some(line) = line.0.as_str()
2066                        && line.starts_with("the trait")
2067                        && line.contains("is not implemented for")
2068                    {
2069                        // HACK(estebank): we remove the pre-existing
2070                        // "the trait `X` is not implemented for" note, which only happens if there
2071                        // was a custom label. We do this because we want that note to always be the
2072                        // first, and making this logic run earlier will get tricky. For now, we
2073                        // instead keep the logic the same and modify the already constructed error
2074                        // to avoid the wording duplication.
2075                        err.children.remove(0);
2076                    }
2077
2078                    let traits = self.cmp_traits(
2079                        obligation_trait_ref.def_id(),
2080                        &obligation_trait_ref.trait_ref.args[1..],
2081                        impl_trait_ref.def_id,
2082                        &impl_trait_ref.args[1..],
2083                    );
2084                    let traits_content = (traits.0.content(), traits.1.content());
2085                    let types = self.cmp(obligation_trait_ref.self_ty(), impl_trait_ref.self_ty());
2086                    let types_content = (types.0.content(), types.1.content());
2087                    let mut msg = vec![StringPart::normal("the trait `")];
2088                    if traits_content.0 == traits_content.1 {
2089                        msg.push(StringPart::normal(
2090                            impl_trait_ref.print_trait_sugared().to_string(),
2091                        ));
2092                    } else {
2093                        msg.extend(traits.0.0);
2094                    }
2095                    msg.extend([
2096                        StringPart::normal("` "),
2097                        StringPart::highlighted("is not"),
2098                        StringPart::normal(" implemented for `"),
2099                    ]);
2100                    if types_content.0 == types_content.1 {
2101                        let ty = self
2102                            .tcx
2103                            .short_string(obligation_trait_ref.self_ty(), err.long_ty_path());
2104                        msg.push(StringPart::normal(ty));
2105                    } else {
2106                        msg.extend(types.0.0);
2107                    }
2108                    msg.push(StringPart::normal("`"));
2109                    if types_content.0 == types_content.1 {
2110                        msg.push(StringPart::normal("\nbut trait `"));
2111                        msg.extend(traits.1.0);
2112                        msg.extend([
2113                            StringPart::normal("` "),
2114                            StringPart::highlighted("is"),
2115                            StringPart::normal(" implemented for it"),
2116                        ]);
2117                    } else if traits_content.0 == traits_content.1 {
2118                        msg.extend([
2119                            StringPart::normal("\nbut it "),
2120                            StringPart::highlighted("is"),
2121                            StringPart::normal(" implemented for `"),
2122                        ]);
2123                        msg.extend(types.1.0);
2124                        msg.push(StringPart::normal("`"));
2125                    } else {
2126                        msg.push(StringPart::normal("\nbut trait `"));
2127                        msg.extend(traits.1.0);
2128                        msg.extend([
2129                            StringPart::normal("` "),
2130                            StringPart::highlighted("is"),
2131                            StringPart::normal(" implemented for `"),
2132                        ]);
2133                        msg.extend(types.1.0);
2134                        msg.push(StringPart::normal("`"));
2135                    }
2136                    err.highlighted_help(msg);
2137
2138                    if let [TypeError::Sorts(exp_found)] = &terrs[..] {
2139                        let exp_found = self.resolve_vars_if_possible(*exp_found);
2140                        let expected =
2141                            self.tcx.short_string(exp_found.expected, err.long_ty_path());
2142                        let found = self.tcx.short_string(exp_found.found, err.long_ty_path());
2143                        err.highlighted_help(vec![
2144                            StringPart::normal("for that trait implementation, "),
2145                            StringPart::normal("expected `"),
2146                            StringPart::highlighted(expected),
2147                            StringPart::normal("`, found `"),
2148                            StringPart::highlighted(found),
2149                            StringPart::normal("`"),
2150                        ]);
2151                        self.suggest_function_pointers_impl(None, &exp_found, err);
2152                    }
2153
2154                    true
2155                })
2156            }) {
2157                return true;
2158            }
2159        }
2160
2161        let other = if other { "other " } else { "" };
2162        let report = |mut candidates: Vec<TraitRef<'tcx>>, err: &mut Diag<'_>| {
2163            candidates.retain(|tr| !tr.references_error());
2164            if candidates.is_empty() {
2165                return false;
2166            }
2167            if let &[cand] = &candidates[..] {
2168                if self.tcx.is_diagnostic_item(sym::FromResidual, cand.def_id)
2169                    && !self.tcx.features().enabled(sym::try_trait_v2)
2170                {
2171                    return false;
2172                }
2173                let (desc, mention_castable) =
2174                    match (cand.self_ty().kind(), trait_pred.self_ty().skip_binder().kind()) {
2175                        (ty::FnPtr(..), ty::FnDef(..)) => {
2176                            (" implemented for fn pointer `", ", cast using `as`")
2177                        }
2178                        (ty::FnPtr(..), _) => (" implemented for fn pointer `", ""),
2179                        _ => (" implemented for `", ""),
2180                    };
2181                let trait_ = self.tcx.short_string(cand.print_trait_sugared(), err.long_ty_path());
2182                let self_ty = self.tcx.short_string(cand.self_ty(), err.long_ty_path());
2183                err.highlighted_help(vec![
2184                    StringPart::normal(format!("the trait `{trait_}` ",)),
2185                    StringPart::highlighted("is"),
2186                    StringPart::normal(desc),
2187                    StringPart::highlighted(self_ty),
2188                    StringPart::normal("`"),
2189                    StringPart::normal(mention_castable),
2190                ]);
2191                return true;
2192            }
2193            let trait_ref = TraitRef::identity(self.tcx, candidates[0].def_id);
2194            // Check if the trait is the same in all cases. If so, we'll only show the type.
2195            let mut traits: Vec<_> =
2196                candidates.iter().map(|c| c.print_only_trait_path().to_string()).collect();
2197            traits.sort();
2198            traits.dedup();
2199            // FIXME: this could use a better heuristic, like just checking
2200            // that args[1..] is the same.
2201            let all_traits_equal = traits.len() == 1;
2202
2203            let candidates: Vec<String> = candidates
2204                .into_iter()
2205                .map(|c| {
2206                    if all_traits_equal {
2207                        format!("\n  {}", self.tcx.short_string(c.self_ty(), err.long_ty_path()))
2208                    } else {
2209                        format!(
2210                            "\n  `{}` implements `{}`",
2211                            self.tcx.short_string(c.self_ty(), err.long_ty_path()),
2212                            self.tcx.short_string(c.print_only_trait_path(), err.long_ty_path()),
2213                        )
2214                    }
2215                })
2216                .collect();
2217
2218            let end = if candidates.len() <= 9 || self.tcx.sess.opts.verbose {
2219                candidates.len()
2220            } else {
2221                8
2222            };
2223            err.help(format!(
2224                "the following {other}types implement trait `{}`:{}{}",
2225                trait_ref.print_trait_sugared(),
2226                candidates[..end].join(""),
2227                if candidates.len() > 9 && !self.tcx.sess.opts.verbose {
2228                    format!("\nand {} others", candidates.len() - 8)
2229                } else {
2230                    String::new()
2231                }
2232            ));
2233            true
2234        };
2235
2236        // we filter before checking if `impl_candidates` is empty
2237        // to get the fallback solution if we filtered out any impls
2238        let impl_candidates = impl_candidates
2239            .into_iter()
2240            .cloned()
2241            .filter(|cand| !self.tcx.do_not_recommend_impl(cand.impl_def_id))
2242            .collect::<Vec<_>>();
2243
2244        let def_id = trait_pred.def_id();
2245        if impl_candidates.is_empty() {
2246            if self.tcx.trait_is_auto(def_id)
2247                || self.tcx.lang_items().iter().any(|(_, id)| id == def_id)
2248                || self.tcx.get_diagnostic_name(def_id).is_some()
2249            {
2250                // Mentioning implementers of `Copy`, `Debug` and friends is not useful.
2251                return false;
2252            }
2253            return report(alternative_candidates(def_id), err);
2254        }
2255
2256        // Sort impl candidates so that ordering is consistent for UI tests.
2257        // because the ordering of `impl_candidates` may not be deterministic:
2258        // https://github.com/rust-lang/rust/pull/57475#issuecomment-455519507
2259        //
2260        // Prefer more similar candidates first, then sort lexicographically
2261        // by their normalized string representation.
2262        let mut impl_candidates: Vec<_> = impl_candidates
2263            .iter()
2264            .cloned()
2265            .filter(|cand| !cand.trait_ref.references_error())
2266            .map(|mut cand| {
2267                // Normalize the trait ref in its *own* param-env so
2268                // that consts are folded and any trivial projections
2269                // are normalized.
2270                cand.trait_ref = self
2271                    .tcx
2272                    .try_normalize_erasing_regions(
2273                        ty::TypingEnv::non_body_analysis(self.tcx, cand.impl_def_id),
2274                        cand.trait_ref,
2275                    )
2276                    .unwrap_or(cand.trait_ref);
2277                cand
2278            })
2279            .collect();
2280        impl_candidates.sort_by_key(|cand| (cand.similarity, cand.trait_ref.to_string()));
2281        let mut impl_candidates: Vec<_> =
2282            impl_candidates.into_iter().map(|cand| cand.trait_ref).collect();
2283        impl_candidates.dedup();
2284
2285        report(impl_candidates, err)
2286    }
2287
2288    fn report_similar_impl_candidates_for_root_obligation(
2289        &self,
2290        obligation: &PredicateObligation<'tcx>,
2291        trait_predicate: ty::Binder<'tcx, ty::TraitPredicate<'tcx>>,
2292        body_def_id: LocalDefId,
2293        err: &mut Diag<'_>,
2294    ) {
2295        // This is *almost* equivalent to
2296        // `obligation.cause.code().peel_derives()`, but it gives us the
2297        // trait predicate for that corresponding root obligation. This
2298        // lets us get a derived obligation from a type parameter, like
2299        // when calling `string.strip_suffix(p)` where `p` is *not* an
2300        // implementer of `Pattern<'_>`.
2301        let mut code = obligation.cause.code();
2302        let mut trait_pred = trait_predicate;
2303        let mut peeled = false;
2304        while let Some((parent_code, parent_trait_pred)) = code.parent_with_predicate() {
2305            code = parent_code;
2306            if let Some(parent_trait_pred) = parent_trait_pred {
2307                trait_pred = parent_trait_pred;
2308                peeled = true;
2309            }
2310        }
2311        let def_id = trait_pred.def_id();
2312        // Mention *all* the `impl`s for the *top most* obligation, the
2313        // user might have meant to use one of them, if any found. We skip
2314        // auto-traits or fundamental traits that might not be exactly what
2315        // the user might expect to be presented with. Instead this is
2316        // useful for less general traits.
2317        if peeled && !self.tcx.trait_is_auto(def_id) && self.tcx.as_lang_item(def_id).is_none() {
2318            let impl_candidates = self.find_similar_impl_candidates(trait_pred);
2319            self.report_similar_impl_candidates(
2320                &impl_candidates,
2321                trait_pred,
2322                body_def_id,
2323                err,
2324                true,
2325                obligation.param_env,
2326            );
2327        }
2328    }
2329
2330    /// Gets the parent trait chain start
2331    fn get_parent_trait_ref(
2332        &self,
2333        code: &ObligationCauseCode<'tcx>,
2334    ) -> Option<(Ty<'tcx>, Option<Span>)> {
2335        match code {
2336            ObligationCauseCode::BuiltinDerived(data) => {
2337                let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
2338                match self.get_parent_trait_ref(&data.parent_code) {
2339                    Some(t) => Some(t),
2340                    None => {
2341                        let ty = parent_trait_ref.skip_binder().self_ty();
2342                        let span = TyCategory::from_ty(self.tcx, ty)
2343                            .map(|(_, def_id)| self.tcx.def_span(def_id));
2344                        Some((ty, span))
2345                    }
2346                }
2347            }
2348            ObligationCauseCode::FunctionArg { parent_code, .. } => {
2349                self.get_parent_trait_ref(parent_code)
2350            }
2351            _ => None,
2352        }
2353    }
2354
2355    /// If the `Self` type of the unsatisfied trait `trait_ref` implements a trait
2356    /// with the same path as `trait_ref`, a help message about
2357    /// a probable version mismatch is added to `err`
2358    fn note_version_mismatch(
2359        &self,
2360        err: &mut Diag<'_>,
2361        trait_pred: ty::PolyTraitPredicate<'tcx>,
2362    ) -> bool {
2363        let get_trait_impls = |trait_def_id| {
2364            let mut trait_impls = vec![];
2365            self.tcx.for_each_relevant_impl(
2366                trait_def_id,
2367                trait_pred.skip_binder().self_ty(),
2368                |impl_def_id| {
2369                    trait_impls.push(impl_def_id);
2370                },
2371            );
2372            trait_impls
2373        };
2374
2375        let required_trait_path = self.tcx.def_path_str(trait_pred.def_id());
2376        let traits_with_same_path: UnordSet<_> = self
2377            .tcx
2378            .visible_traits()
2379            .filter(|trait_def_id| *trait_def_id != trait_pred.def_id())
2380            .map(|trait_def_id| (self.tcx.def_path_str(trait_def_id), trait_def_id))
2381            .filter(|(p, _)| *p == required_trait_path)
2382            .collect();
2383
2384        let traits_with_same_path =
2385            traits_with_same_path.into_items().into_sorted_stable_ord_by_key(|(p, _)| p);
2386        let mut suggested = false;
2387        for (_, trait_with_same_path) in traits_with_same_path {
2388            let trait_impls = get_trait_impls(trait_with_same_path);
2389            if trait_impls.is_empty() {
2390                continue;
2391            }
2392            let impl_spans: Vec<_> =
2393                trait_impls.iter().map(|impl_def_id| self.tcx.def_span(*impl_def_id)).collect();
2394            err.span_help(
2395                impl_spans,
2396                format!("trait impl{} with same name found", pluralize!(trait_impls.len())),
2397            );
2398            let trait_crate = self.tcx.crate_name(trait_with_same_path.krate);
2399            let crate_msg =
2400                format!("perhaps two different versions of crate `{trait_crate}` are being used?");
2401            err.note(crate_msg);
2402            suggested = true;
2403        }
2404        suggested
2405    }
2406
2407    /// Creates a `PredicateObligation` with `new_self_ty` replacing the existing type in the
2408    /// `trait_ref`.
2409    ///
2410    /// For this to work, `new_self_ty` must have no escaping bound variables.
2411    pub(super) fn mk_trait_obligation_with_new_self_ty(
2412        &self,
2413        param_env: ty::ParamEnv<'tcx>,
2414        trait_ref_and_ty: ty::Binder<'tcx, (ty::TraitPredicate<'tcx>, Ty<'tcx>)>,
2415    ) -> PredicateObligation<'tcx> {
2416        let trait_pred = trait_ref_and_ty
2417            .map_bound(|(tr, new_self_ty)| tr.with_replaced_self_ty(self.tcx, new_self_ty));
2418
2419        Obligation::new(self.tcx, ObligationCause::dummy(), param_env, trait_pred)
2420    }
2421
2422    /// Returns `true` if the trait predicate may apply for *some* assignment
2423    /// to the type parameters.
2424    fn predicate_can_apply(
2425        &self,
2426        param_env: ty::ParamEnv<'tcx>,
2427        pred: ty::PolyTraitPredicate<'tcx>,
2428    ) -> bool {
2429        struct ParamToVarFolder<'a, 'tcx> {
2430            infcx: &'a InferCtxt<'tcx>,
2431            var_map: FxHashMap<Ty<'tcx>, Ty<'tcx>>,
2432        }
2433
2434        impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ParamToVarFolder<'a, 'tcx> {
2435            fn cx(&self) -> TyCtxt<'tcx> {
2436                self.infcx.tcx
2437            }
2438
2439            fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
2440                if let ty::Param(_) = *ty.kind() {
2441                    let infcx = self.infcx;
2442                    *self.var_map.entry(ty).or_insert_with(|| infcx.next_ty_var(DUMMY_SP))
2443                } else {
2444                    ty.super_fold_with(self)
2445                }
2446            }
2447        }
2448
2449        self.probe(|_| {
2450            let cleaned_pred =
2451                pred.fold_with(&mut ParamToVarFolder { infcx: self, var_map: Default::default() });
2452
2453            let InferOk { value: cleaned_pred, .. } =
2454                self.infcx.at(&ObligationCause::dummy(), param_env).normalize(cleaned_pred);
2455
2456            let obligation =
2457                Obligation::new(self.tcx, ObligationCause::dummy(), param_env, cleaned_pred);
2458
2459            self.predicate_may_hold(&obligation)
2460        })
2461    }
2462
2463    pub fn note_obligation_cause(
2464        &self,
2465        err: &mut Diag<'_>,
2466        obligation: &PredicateObligation<'tcx>,
2467    ) {
2468        // First, attempt to add note to this error with an async-await-specific
2469        // message, and fall back to regular note otherwise.
2470        if !self.maybe_note_obligation_cause_for_async_await(err, obligation) {
2471            self.note_obligation_cause_code(
2472                obligation.cause.body_id,
2473                err,
2474                obligation.predicate,
2475                obligation.param_env,
2476                obligation.cause.code(),
2477                &mut vec![],
2478                &mut Default::default(),
2479            );
2480            self.suggest_swapping_lhs_and_rhs(
2481                err,
2482                obligation.predicate,
2483                obligation.param_env,
2484                obligation.cause.code(),
2485            );
2486            self.suggest_unsized_bound_if_applicable(err, obligation);
2487            if let Some(span) = err.span.primary_span()
2488                && let Some(mut diag) =
2489                    self.dcx().steal_non_err(span, StashKey::AssociatedTypeSuggestion)
2490                && let Suggestions::Enabled(ref mut s1) = err.suggestions
2491                && let Suggestions::Enabled(ref mut s2) = diag.suggestions
2492            {
2493                s1.append(s2);
2494                diag.cancel()
2495            }
2496        }
2497    }
2498
2499    pub(super) fn is_recursive_obligation(
2500        &self,
2501        obligated_types: &mut Vec<Ty<'tcx>>,
2502        cause_code: &ObligationCauseCode<'tcx>,
2503    ) -> bool {
2504        if let ObligationCauseCode::BuiltinDerived(data) = cause_code {
2505            let parent_trait_ref = self.resolve_vars_if_possible(data.parent_trait_pred);
2506            let self_ty = parent_trait_ref.skip_binder().self_ty();
2507            if obligated_types.iter().any(|ot| ot == &self_ty) {
2508                return true;
2509            }
2510            if let ty::Adt(def, args) = self_ty.kind()
2511                && let [arg] = &args[..]
2512                && let ty::GenericArgKind::Type(ty) = arg.kind()
2513                && let ty::Adt(inner_def, _) = ty.kind()
2514                && inner_def == def
2515            {
2516                return true;
2517            }
2518        }
2519        false
2520    }
2521
2522    fn get_standard_error_message(
2523        &self,
2524        trait_predicate: ty::PolyTraitPredicate<'tcx>,
2525        message: Option<String>,
2526        predicate_constness: Option<ty::BoundConstness>,
2527        append_const_msg: Option<AppendConstMessage>,
2528        post_message: String,
2529        long_ty_path: &mut Option<PathBuf>,
2530    ) -> String {
2531        message
2532            .and_then(|cannot_do_this| {
2533                match (predicate_constness, append_const_msg) {
2534                    // do nothing if predicate is not const
2535                    (None, _) => Some(cannot_do_this),
2536                    // suggested using default post message
2537                    (
2538                        Some(ty::BoundConstness::Const | ty::BoundConstness::Maybe),
2539                        Some(AppendConstMessage::Default),
2540                    ) => Some(format!("{cannot_do_this} in const contexts")),
2541                    // overridden post message
2542                    (
2543                        Some(ty::BoundConstness::Const | ty::BoundConstness::Maybe),
2544                        Some(AppendConstMessage::Custom(custom_msg, _)),
2545                    ) => Some(format!("{cannot_do_this}{custom_msg}")),
2546                    // fallback to generic message
2547                    (Some(ty::BoundConstness::Const | ty::BoundConstness::Maybe), None) => None,
2548                }
2549            })
2550            .unwrap_or_else(|| {
2551                format!(
2552                    "the trait bound `{}` is not satisfied{post_message}",
2553                    self.tcx.short_string(
2554                        trait_predicate.print_with_bound_constness(predicate_constness),
2555                        long_ty_path,
2556                    ),
2557                )
2558            })
2559    }
2560
2561    fn get_safe_transmute_error_and_reason(
2562        &self,
2563        obligation: PredicateObligation<'tcx>,
2564        trait_pred: ty::PolyTraitPredicate<'tcx>,
2565        span: Span,
2566    ) -> GetSafeTransmuteErrorAndReason {
2567        use rustc_transmute::Answer;
2568        self.probe(|_| {
2569            // We don't assemble a transmutability candidate for types that are generic
2570            // and we should have ambiguity for types that still have non-region infer.
2571            if obligation.predicate.has_non_region_param() || obligation.has_non_region_infer() {
2572                return GetSafeTransmuteErrorAndReason::Default;
2573            }
2574
2575            // Erase regions because layout code doesn't particularly care about regions.
2576            let trait_pred =
2577                self.tcx.erase_regions(self.tcx.instantiate_bound_regions_with_erased(trait_pred));
2578
2579            let src_and_dst = rustc_transmute::Types {
2580                dst: trait_pred.trait_ref.args.type_at(0),
2581                src: trait_pred.trait_ref.args.type_at(1),
2582            };
2583
2584            let ocx = ObligationCtxt::new(self);
2585            let Ok(assume) = ocx.structurally_normalize_const(
2586                &obligation.cause,
2587                obligation.param_env,
2588                trait_pred.trait_ref.args.const_at(2),
2589            ) else {
2590                self.dcx().span_delayed_bug(
2591                    span,
2592                    "Unable to construct rustc_transmute::Assume where it was previously possible",
2593                );
2594                return GetSafeTransmuteErrorAndReason::Silent;
2595            };
2596
2597            let Some(assume) = rustc_transmute::Assume::from_const(self.infcx.tcx, assume) else {
2598                self.dcx().span_delayed_bug(
2599                    span,
2600                    "Unable to construct rustc_transmute::Assume where it was previously possible",
2601                );
2602                return GetSafeTransmuteErrorAndReason::Silent;
2603            };
2604
2605            let dst = trait_pred.trait_ref.args.type_at(0);
2606            let src = trait_pred.trait_ref.args.type_at(1);
2607            let err_msg = format!("`{src}` cannot be safely transmuted into `{dst}`");
2608
2609            match rustc_transmute::TransmuteTypeEnv::new(self.infcx.tcx)
2610                .is_transmutable(src_and_dst, assume)
2611            {
2612                Answer::No(reason) => {
2613                    let safe_transmute_explanation = match reason {
2614                        rustc_transmute::Reason::SrcIsNotYetSupported => {
2615                            format!("analyzing the transmutability of `{src}` is not yet supported")
2616                        }
2617                        rustc_transmute::Reason::DstIsNotYetSupported => {
2618                            format!("analyzing the transmutability of `{dst}` is not yet supported")
2619                        }
2620                        rustc_transmute::Reason::DstIsBitIncompatible => {
2621                            format!(
2622                                "at least one value of `{src}` isn't a bit-valid value of `{dst}`"
2623                            )
2624                        }
2625                        rustc_transmute::Reason::DstUninhabited => {
2626                            format!("`{dst}` is uninhabited")
2627                        }
2628                        rustc_transmute::Reason::DstMayHaveSafetyInvariants => {
2629                            format!("`{dst}` may carry safety invariants")
2630                        }
2631                        rustc_transmute::Reason::DstIsTooBig => {
2632                            format!("the size of `{src}` is smaller than the size of `{dst}`")
2633                        }
2634                        rustc_transmute::Reason::DstRefIsTooBig {
2635                            src,
2636                            src_size,
2637                            dst,
2638                            dst_size,
2639                        } => {
2640                            format!(
2641                                "the size of `{src}` ({src_size} bytes) \
2642                        is smaller than that of `{dst}` ({dst_size} bytes)"
2643                            )
2644                        }
2645                        rustc_transmute::Reason::SrcSizeOverflow => {
2646                            format!(
2647                                "values of the type `{src}` are too big for the target architecture"
2648                            )
2649                        }
2650                        rustc_transmute::Reason::DstSizeOverflow => {
2651                            format!(
2652                                "values of the type `{dst}` are too big for the target architecture"
2653                            )
2654                        }
2655                        rustc_transmute::Reason::DstHasStricterAlignment {
2656                            src_min_align,
2657                            dst_min_align,
2658                        } => {
2659                            format!(
2660                                "the minimum alignment of `{src}` ({src_min_align}) should be \
2661                                 greater than that of `{dst}` ({dst_min_align})"
2662                            )
2663                        }
2664                        rustc_transmute::Reason::DstIsMoreUnique => {
2665                            format!(
2666                                "`{src}` is a shared reference, but `{dst}` is a unique reference"
2667                            )
2668                        }
2669                        // Already reported by rustc
2670                        rustc_transmute::Reason::TypeError => {
2671                            return GetSafeTransmuteErrorAndReason::Silent;
2672                        }
2673                        rustc_transmute::Reason::SrcLayoutUnknown => {
2674                            format!("`{src}` has an unknown layout")
2675                        }
2676                        rustc_transmute::Reason::DstLayoutUnknown => {
2677                            format!("`{dst}` has an unknown layout")
2678                        }
2679                    };
2680                    GetSafeTransmuteErrorAndReason::Error {
2681                        err_msg,
2682                        safe_transmute_explanation: Some(safe_transmute_explanation),
2683                    }
2684                }
2685                // Should never get a Yes at this point! We already ran it before, and did not get a Yes.
2686                Answer::Yes => span_bug!(
2687                    span,
2688                    "Inconsistent rustc_transmute::is_transmutable(...) result, got Yes",
2689                ),
2690                // Reached when a different obligation (namely `Freeze`) causes the
2691                // transmutability analysis to fail. In this case, silence the
2692                // transmutability error message in favor of that more specific
2693                // error.
2694                Answer::If(_) => GetSafeTransmuteErrorAndReason::Error {
2695                    err_msg,
2696                    safe_transmute_explanation: None,
2697                },
2698            }
2699        })
2700    }
2701
2702    fn add_tuple_trait_message(
2703        &self,
2704        obligation_cause_code: &ObligationCauseCode<'tcx>,
2705        err: &mut Diag<'_>,
2706    ) {
2707        match obligation_cause_code {
2708            ObligationCauseCode::RustCall => {
2709                err.primary_message("functions with the \"rust-call\" ABI must take a single non-self tuple argument");
2710            }
2711            ObligationCauseCode::WhereClause(def_id, _) if self.tcx.is_fn_trait(*def_id) => {
2712                err.code(E0059);
2713                err.primary_message(format!(
2714                    "type parameter to bare `{}` trait must be a tuple",
2715                    self.tcx.def_path_str(*def_id)
2716                ));
2717            }
2718            _ => {}
2719        }
2720    }
2721
2722    fn try_to_add_help_message(
2723        &self,
2724        root_obligation: &PredicateObligation<'tcx>,
2725        obligation: &PredicateObligation<'tcx>,
2726        trait_predicate: ty::PolyTraitPredicate<'tcx>,
2727        err: &mut Diag<'_>,
2728        span: Span,
2729        is_fn_trait: bool,
2730        suggested: bool,
2731    ) {
2732        let body_def_id = obligation.cause.body_id;
2733        let span = if let ObligationCauseCode::BinOp { rhs_span: Some(rhs_span), .. } =
2734            obligation.cause.code()
2735        {
2736            *rhs_span
2737        } else {
2738            span
2739        };
2740
2741        // Try to report a help message
2742        let trait_def_id = trait_predicate.def_id();
2743        if is_fn_trait
2744            && let Ok((implemented_kind, params)) = self.type_implements_fn_trait(
2745                obligation.param_env,
2746                trait_predicate.self_ty(),
2747                trait_predicate.skip_binder().polarity,
2748            )
2749        {
2750            self.add_help_message_for_fn_trait(trait_predicate, err, implemented_kind, params);
2751        } else if !trait_predicate.has_non_region_infer()
2752            && self.predicate_can_apply(obligation.param_env, trait_predicate)
2753        {
2754            // If a where-clause may be useful, remind the
2755            // user that they can add it.
2756            //
2757            // don't display an on-unimplemented note, as
2758            // these notes will often be of the form
2759            //     "the type `T` can't be frobnicated"
2760            // which is somewhat confusing.
2761            self.suggest_restricting_param_bound(
2762                err,
2763                trait_predicate,
2764                None,
2765                obligation.cause.body_id,
2766            );
2767        } else if trait_def_id.is_local()
2768            && self.tcx.trait_impls_of(trait_def_id).is_empty()
2769            && !self.tcx.trait_is_auto(trait_def_id)
2770            && !self.tcx.trait_is_alias(trait_def_id)
2771            && trait_predicate.polarity() == ty::PredicatePolarity::Positive
2772        {
2773            err.span_help(
2774                self.tcx.def_span(trait_def_id),
2775                crate::fluent_generated::trait_selection_trait_has_no_impls,
2776            );
2777        } else if !suggested && trait_predicate.polarity() == ty::PredicatePolarity::Positive {
2778            // Can't show anything else useful, try to find similar impls.
2779            let impl_candidates = self.find_similar_impl_candidates(trait_predicate);
2780            if !self.report_similar_impl_candidates(
2781                &impl_candidates,
2782                trait_predicate,
2783                body_def_id,
2784                err,
2785                true,
2786                obligation.param_env,
2787            ) {
2788                self.report_similar_impl_candidates_for_root_obligation(
2789                    obligation,
2790                    trait_predicate,
2791                    body_def_id,
2792                    err,
2793                );
2794            }
2795
2796            self.suggest_convert_to_slice(
2797                err,
2798                obligation,
2799                trait_predicate,
2800                impl_candidates.as_slice(),
2801                span,
2802            );
2803
2804            self.suggest_tuple_wrapping(err, root_obligation, obligation);
2805        }
2806    }
2807
2808    fn add_help_message_for_fn_trait(
2809        &self,
2810        trait_pred: ty::PolyTraitPredicate<'tcx>,
2811        err: &mut Diag<'_>,
2812        implemented_kind: ty::ClosureKind,
2813        params: ty::Binder<'tcx, Ty<'tcx>>,
2814    ) {
2815        // If the type implements `Fn`, `FnMut`, or `FnOnce`, suppress the following
2816        // suggestion to add trait bounds for the type, since we only typically implement
2817        // these traits once.
2818
2819        // Note if the `FnMut` or `FnOnce` is less general than the trait we're trying
2820        // to implement.
2821        let selected_kind = self
2822            .tcx
2823            .fn_trait_kind_from_def_id(trait_pred.def_id())
2824            .expect("expected to map DefId to ClosureKind");
2825        if !implemented_kind.extends(selected_kind) {
2826            err.note(format!(
2827                "`{}` implements `{}`, but it must implement `{}`, which is more general",
2828                trait_pred.skip_binder().self_ty(),
2829                implemented_kind,
2830                selected_kind
2831            ));
2832        }
2833
2834        // Note any argument mismatches
2835        let ty::Tuple(given) = *params.skip_binder().kind() else {
2836            return;
2837        };
2838
2839        let expected_ty = trait_pred.skip_binder().trait_ref.args.type_at(1);
2840        let ty::Tuple(expected) = *expected_ty.kind() else {
2841            return;
2842        };
2843
2844        if expected.len() != given.len() {
2845            // Note number of types that were expected and given
2846            err.note(format!(
2847                "expected a closure taking {} argument{}, but one taking {} argument{} was given",
2848                given.len(),
2849                pluralize!(given.len()),
2850                expected.len(),
2851                pluralize!(expected.len()),
2852            ));
2853            return;
2854        }
2855
2856        let given_ty = Ty::new_fn_ptr(
2857            self.tcx,
2858            params.rebind(self.tcx.mk_fn_sig(
2859                given,
2860                self.tcx.types.unit,
2861                false,
2862                hir::Safety::Safe,
2863                ExternAbi::Rust,
2864            )),
2865        );
2866        let expected_ty = Ty::new_fn_ptr(
2867            self.tcx,
2868            trait_pred.rebind(self.tcx.mk_fn_sig(
2869                expected,
2870                self.tcx.types.unit,
2871                false,
2872                hir::Safety::Safe,
2873                ExternAbi::Rust,
2874            )),
2875        );
2876
2877        if !self.same_type_modulo_infer(given_ty, expected_ty) {
2878            // Print type mismatch
2879            let (expected_args, given_args) = self.cmp(expected_ty, given_ty);
2880            err.note_expected_found(
2881                "a closure with signature",
2882                expected_args,
2883                "a closure with signature",
2884                given_args,
2885            );
2886        }
2887    }
2888
2889    fn report_closure_error(
2890        &self,
2891        obligation: &PredicateObligation<'tcx>,
2892        closure_def_id: DefId,
2893        found_kind: ty::ClosureKind,
2894        kind: ty::ClosureKind,
2895        trait_prefix: &'static str,
2896    ) -> Diag<'a> {
2897        let closure_span = self.tcx.def_span(closure_def_id);
2898
2899        let mut err = ClosureKindMismatch {
2900            closure_span,
2901            expected: kind,
2902            found: found_kind,
2903            cause_span: obligation.cause.span,
2904            trait_prefix,
2905            fn_once_label: None,
2906            fn_mut_label: None,
2907        };
2908
2909        // Additional context information explaining why the closure only implements
2910        // a particular trait.
2911        if let Some(typeck_results) = &self.typeck_results {
2912            let hir_id = self.tcx.local_def_id_to_hir_id(closure_def_id.expect_local());
2913            match (found_kind, typeck_results.closure_kind_origins().get(hir_id)) {
2914                (ty::ClosureKind::FnOnce, Some((span, place))) => {
2915                    err.fn_once_label = Some(ClosureFnOnceLabel {
2916                        span: *span,
2917                        place: ty::place_to_string_for_capture(self.tcx, place),
2918                    })
2919                }
2920                (ty::ClosureKind::FnMut, Some((span, place))) => {
2921                    err.fn_mut_label = Some(ClosureFnMutLabel {
2922                        span: *span,
2923                        place: ty::place_to_string_for_capture(self.tcx, place),
2924                    })
2925                }
2926                _ => {}
2927            }
2928        }
2929
2930        self.dcx().create_err(err)
2931    }
2932
2933    fn report_cyclic_signature_error(
2934        &self,
2935        obligation: &PredicateObligation<'tcx>,
2936        found_trait_ref: ty::TraitRef<'tcx>,
2937        expected_trait_ref: ty::TraitRef<'tcx>,
2938        terr: TypeError<'tcx>,
2939    ) -> Diag<'a> {
2940        let self_ty = found_trait_ref.self_ty();
2941        let (cause, terr) = if let ty::Closure(def_id, _) = self_ty.kind() {
2942            (
2943                ObligationCause::dummy_with_span(self.tcx.def_span(def_id)),
2944                TypeError::CyclicTy(self_ty),
2945            )
2946        } else {
2947            (obligation.cause.clone(), terr)
2948        };
2949        self.report_and_explain_type_error(
2950            TypeTrace::trait_refs(&cause, expected_trait_ref, found_trait_ref),
2951            obligation.param_env,
2952            terr,
2953        )
2954    }
2955
2956    fn report_opaque_type_auto_trait_leakage(
2957        &self,
2958        obligation: &PredicateObligation<'tcx>,
2959        def_id: DefId,
2960    ) -> ErrorGuaranteed {
2961        let name = match self.tcx.local_opaque_ty_origin(def_id.expect_local()) {
2962            hir::OpaqueTyOrigin::FnReturn { .. } | hir::OpaqueTyOrigin::AsyncFn { .. } => {
2963                "opaque type".to_string()
2964            }
2965            hir::OpaqueTyOrigin::TyAlias { .. } => {
2966                format!("`{}`", self.tcx.def_path_debug_str(def_id))
2967            }
2968        };
2969        let mut err = self.dcx().struct_span_err(
2970            obligation.cause.span,
2971            format!("cannot check whether the hidden type of {name} satisfies auto traits"),
2972        );
2973
2974        err.note(
2975            "fetching the hidden types of an opaque inside of the defining scope is not supported. \
2976            You can try moving the opaque type and the item that actually registers a hidden type into a new submodule",
2977        );
2978        err.span_note(self.tcx.def_span(def_id), "opaque type is declared here");
2979
2980        self.note_obligation_cause(&mut err, &obligation);
2981        self.dcx().try_steal_replace_and_emit_err(self.tcx.def_span(def_id), StashKey::Cycle, err)
2982    }
2983
2984    fn report_signature_mismatch_error(
2985        &self,
2986        obligation: &PredicateObligation<'tcx>,
2987        span: Span,
2988        found_trait_ref: ty::TraitRef<'tcx>,
2989        expected_trait_ref: ty::TraitRef<'tcx>,
2990    ) -> Result<Diag<'a>, ErrorGuaranteed> {
2991        let found_trait_ref = self.resolve_vars_if_possible(found_trait_ref);
2992        let expected_trait_ref = self.resolve_vars_if_possible(expected_trait_ref);
2993
2994        expected_trait_ref.self_ty().error_reported()?;
2995        let found_trait_ty = found_trait_ref.self_ty();
2996
2997        let found_did = match *found_trait_ty.kind() {
2998            ty::Closure(did, _) | ty::FnDef(did, _) | ty::Coroutine(did, ..) => Some(did),
2999            _ => None,
3000        };
3001
3002        let found_node = found_did.and_then(|did| self.tcx.hir_get_if_local(did));
3003        let found_span = found_did.and_then(|did| self.tcx.hir_span_if_local(did));
3004
3005        if !self.reported_signature_mismatch.borrow_mut().insert((span, found_span)) {
3006            // We check closures twice, with obligations flowing in different directions,
3007            // but we want to complain about them only once.
3008            return Err(self.dcx().span_delayed_bug(span, "already_reported"));
3009        }
3010
3011        let mut not_tupled = false;
3012
3013        let found = match found_trait_ref.args.type_at(1).kind() {
3014            ty::Tuple(tys) => vec![ArgKind::empty(); tys.len()],
3015            _ => {
3016                not_tupled = true;
3017                vec![ArgKind::empty()]
3018            }
3019        };
3020
3021        let expected_ty = expected_trait_ref.args.type_at(1);
3022        let expected = match expected_ty.kind() {
3023            ty::Tuple(tys) => {
3024                tys.iter().map(|t| ArgKind::from_expected_ty(t, Some(span))).collect()
3025            }
3026            _ => {
3027                not_tupled = true;
3028                vec![ArgKind::Arg("_".to_owned(), expected_ty.to_string())]
3029            }
3030        };
3031
3032        // If this is a `Fn` family trait and either the expected or found
3033        // is not tupled, then fall back to just a regular mismatch error.
3034        // This shouldn't be common unless manually implementing one of the
3035        // traits manually, but don't make it more confusing when it does
3036        // happen.
3037        if !self.tcx.is_lang_item(expected_trait_ref.def_id, LangItem::Coroutine) && not_tupled {
3038            return Ok(self.report_and_explain_type_error(
3039                TypeTrace::trait_refs(&obligation.cause, expected_trait_ref, found_trait_ref),
3040                obligation.param_env,
3041                ty::error::TypeError::Mismatch,
3042            ));
3043        }
3044        if found.len() != expected.len() {
3045            let (closure_span, closure_arg_span, found) = found_did
3046                .and_then(|did| {
3047                    let node = self.tcx.hir_get_if_local(did)?;
3048                    let (found_span, closure_arg_span, found) = self.get_fn_like_arguments(node)?;
3049                    Some((Some(found_span), closure_arg_span, found))
3050                })
3051                .unwrap_or((found_span, None, found));
3052
3053            // If the coroutine take a single () as its argument,
3054            // the trait argument would found the coroutine take 0 arguments,
3055            // but get_fn_like_arguments would give 1 argument.
3056            // This would result in "Expected to take 1 argument, but it takes 1 argument".
3057            // Check again to avoid this.
3058            if found.len() != expected.len() {
3059                return Ok(self.report_arg_count_mismatch(
3060                    span,
3061                    closure_span,
3062                    expected,
3063                    found,
3064                    found_trait_ty.is_closure(),
3065                    closure_arg_span,
3066                ));
3067            }
3068        }
3069        Ok(self.report_closure_arg_mismatch(
3070            span,
3071            found_span,
3072            found_trait_ref,
3073            expected_trait_ref,
3074            obligation.cause.code(),
3075            found_node,
3076            obligation.param_env,
3077        ))
3078    }
3079
3080    /// Given some node representing a fn-like thing in the HIR map,
3081    /// returns a span and `ArgKind` information that describes the
3082    /// arguments it expects. This can be supplied to
3083    /// `report_arg_count_mismatch`.
3084    pub fn get_fn_like_arguments(
3085        &self,
3086        node: Node<'_>,
3087    ) -> Option<(Span, Option<Span>, Vec<ArgKind>)> {
3088        let sm = self.tcx.sess.source_map();
3089        Some(match node {
3090            Node::Expr(&hir::Expr {
3091                kind: hir::ExprKind::Closure(&hir::Closure { body, fn_decl_span, fn_arg_span, .. }),
3092                ..
3093            }) => (
3094                fn_decl_span,
3095                fn_arg_span,
3096                self.tcx
3097                    .hir_body(body)
3098                    .params
3099                    .iter()
3100                    .map(|arg| {
3101                        if let hir::Pat { kind: hir::PatKind::Tuple(args, _), span, .. } = *arg.pat
3102                        {
3103                            Some(ArgKind::Tuple(
3104                                Some(span),
3105                                args.iter()
3106                                    .map(|pat| {
3107                                        sm.span_to_snippet(pat.span)
3108                                            .ok()
3109                                            .map(|snippet| (snippet, "_".to_owned()))
3110                                    })
3111                                    .collect::<Option<Vec<_>>>()?,
3112                            ))
3113                        } else {
3114                            let name = sm.span_to_snippet(arg.pat.span).ok()?;
3115                            Some(ArgKind::Arg(name, "_".to_owned()))
3116                        }
3117                    })
3118                    .collect::<Option<Vec<ArgKind>>>()?,
3119            ),
3120            Node::Item(&hir::Item { kind: hir::ItemKind::Fn { ref sig, .. }, .. })
3121            | Node::ImplItem(&hir::ImplItem { kind: hir::ImplItemKind::Fn(ref sig, _), .. })
3122            | Node::TraitItem(&hir::TraitItem {
3123                kind: hir::TraitItemKind::Fn(ref sig, _), ..
3124            })
3125            | Node::ForeignItem(&hir::ForeignItem {
3126                kind: hir::ForeignItemKind::Fn(ref sig, _, _),
3127                ..
3128            }) => (
3129                sig.span,
3130                None,
3131                sig.decl
3132                    .inputs
3133                    .iter()
3134                    .map(|arg| match arg.kind {
3135                        hir::TyKind::Tup(tys) => ArgKind::Tuple(
3136                            Some(arg.span),
3137                            vec![("_".to_owned(), "_".to_owned()); tys.len()],
3138                        ),
3139                        _ => ArgKind::empty(),
3140                    })
3141                    .collect::<Vec<ArgKind>>(),
3142            ),
3143            Node::Ctor(variant_data) => {
3144                let span = variant_data.ctor_hir_id().map_or(DUMMY_SP, |id| self.tcx.hir_span(id));
3145                (span, None, vec![ArgKind::empty(); variant_data.fields().len()])
3146            }
3147            _ => panic!("non-FnLike node found: {node:?}"),
3148        })
3149    }
3150
3151    /// Reports an error when the number of arguments needed by a
3152    /// trait match doesn't match the number that the expression
3153    /// provides.
3154    pub fn report_arg_count_mismatch(
3155        &self,
3156        span: Span,
3157        found_span: Option<Span>,
3158        expected_args: Vec<ArgKind>,
3159        found_args: Vec<ArgKind>,
3160        is_closure: bool,
3161        closure_arg_span: Option<Span>,
3162    ) -> Diag<'a> {
3163        let kind = if is_closure { "closure" } else { "function" };
3164
3165        let args_str = |arguments: &[ArgKind], other: &[ArgKind]| {
3166            let arg_length = arguments.len();
3167            let distinct = matches!(other, &[ArgKind::Tuple(..)]);
3168            match (arg_length, arguments.get(0)) {
3169                (1, Some(ArgKind::Tuple(_, fields))) => {
3170                    format!("a single {}-tuple as argument", fields.len())
3171                }
3172                _ => format!(
3173                    "{} {}argument{}",
3174                    arg_length,
3175                    if distinct && arg_length > 1 { "distinct " } else { "" },
3176                    pluralize!(arg_length)
3177                ),
3178            }
3179        };
3180
3181        let expected_str = args_str(&expected_args, &found_args);
3182        let found_str = args_str(&found_args, &expected_args);
3183
3184        let mut err = struct_span_code_err!(
3185            self.dcx(),
3186            span,
3187            E0593,
3188            "{} is expected to take {}, but it takes {}",
3189            kind,
3190            expected_str,
3191            found_str,
3192        );
3193
3194        err.span_label(span, format!("expected {kind} that takes {expected_str}"));
3195
3196        if let Some(found_span) = found_span {
3197            err.span_label(found_span, format!("takes {found_str}"));
3198
3199            // Suggest to take and ignore the arguments with expected_args_length `_`s if
3200            // found arguments is empty (assume the user just wants to ignore args in this case).
3201            // For example, if `expected_args_length` is 2, suggest `|_, _|`.
3202            if found_args.is_empty() && is_closure {
3203                let underscores = vec!["_"; expected_args.len()].join(", ");
3204                err.span_suggestion_verbose(
3205                    closure_arg_span.unwrap_or(found_span),
3206                    format!(
3207                        "consider changing the closure to take and ignore the expected argument{}",
3208                        pluralize!(expected_args.len())
3209                    ),
3210                    format!("|{underscores}|"),
3211                    Applicability::MachineApplicable,
3212                );
3213            }
3214
3215            if let &[ArgKind::Tuple(_, ref fields)] = &found_args[..] {
3216                if fields.len() == expected_args.len() {
3217                    let sugg = fields
3218                        .iter()
3219                        .map(|(name, _)| name.to_owned())
3220                        .collect::<Vec<String>>()
3221                        .join(", ");
3222                    err.span_suggestion_verbose(
3223                        found_span,
3224                        "change the closure to take multiple arguments instead of a single tuple",
3225                        format!("|{sugg}|"),
3226                        Applicability::MachineApplicable,
3227                    );
3228                }
3229            }
3230            if let &[ArgKind::Tuple(_, ref fields)] = &expected_args[..]
3231                && fields.len() == found_args.len()
3232                && is_closure
3233            {
3234                let sugg = format!(
3235                    "|({}){}|",
3236                    found_args
3237                        .iter()
3238                        .map(|arg| match arg {
3239                            ArgKind::Arg(name, _) => name.to_owned(),
3240                            _ => "_".to_owned(),
3241                        })
3242                        .collect::<Vec<String>>()
3243                        .join(", "),
3244                    // add type annotations if available
3245                    if found_args.iter().any(|arg| match arg {
3246                        ArgKind::Arg(_, ty) => ty != "_",
3247                        _ => false,
3248                    }) {
3249                        format!(
3250                            ": ({})",
3251                            fields
3252                                .iter()
3253                                .map(|(_, ty)| ty.to_owned())
3254                                .collect::<Vec<String>>()
3255                                .join(", ")
3256                        )
3257                    } else {
3258                        String::new()
3259                    },
3260                );
3261                err.span_suggestion_verbose(
3262                    found_span,
3263                    "change the closure to accept a tuple instead of individual arguments",
3264                    sugg,
3265                    Applicability::MachineApplicable,
3266                );
3267            }
3268        }
3269
3270        err
3271    }
3272
3273    /// Checks if the type implements one of `Fn`, `FnMut`, or `FnOnce`
3274    /// in that order, and returns the generic type corresponding to the
3275    /// argument of that trait (corresponding to the closure arguments).
3276    pub fn type_implements_fn_trait(
3277        &self,
3278        param_env: ty::ParamEnv<'tcx>,
3279        ty: ty::Binder<'tcx, Ty<'tcx>>,
3280        polarity: ty::PredicatePolarity,
3281    ) -> Result<(ty::ClosureKind, ty::Binder<'tcx, Ty<'tcx>>), ()> {
3282        self.commit_if_ok(|_| {
3283            for trait_def_id in [
3284                self.tcx.lang_items().fn_trait(),
3285                self.tcx.lang_items().fn_mut_trait(),
3286                self.tcx.lang_items().fn_once_trait(),
3287            ] {
3288                let Some(trait_def_id) = trait_def_id else { continue };
3289                // Make a fresh inference variable so we can determine what the generic parameters
3290                // of the trait are.
3291                let var = self.next_ty_var(DUMMY_SP);
3292                // FIXME(const_trait_impl)
3293                let trait_ref = ty::TraitRef::new(self.tcx, trait_def_id, [ty.skip_binder(), var]);
3294                let obligation = Obligation::new(
3295                    self.tcx,
3296                    ObligationCause::dummy(),
3297                    param_env,
3298                    ty.rebind(ty::TraitPredicate { trait_ref, polarity }),
3299                );
3300                let ocx = ObligationCtxt::new(self);
3301                ocx.register_obligation(obligation);
3302                if ocx.select_all_or_error().is_empty() {
3303                    return Ok((
3304                        self.tcx
3305                            .fn_trait_kind_from_def_id(trait_def_id)
3306                            .expect("expected to map DefId to ClosureKind"),
3307                        ty.rebind(self.resolve_vars_if_possible(var)),
3308                    ));
3309                }
3310            }
3311
3312            Err(())
3313        })
3314    }
3315
3316    fn report_not_const_evaluatable_error(
3317        &self,
3318        obligation: &PredicateObligation<'tcx>,
3319        span: Span,
3320    ) -> Result<Diag<'a>, ErrorGuaranteed> {
3321        if !self.tcx.features().generic_const_exprs()
3322            && !self.tcx.features().min_generic_const_args()
3323        {
3324            let guar = self
3325                .dcx()
3326                .struct_span_err(span, "constant expression depends on a generic parameter")
3327                // FIXME(const_generics): we should suggest to the user how they can resolve this
3328                // issue. However, this is currently not actually possible
3329                // (see https://github.com/rust-lang/rust/issues/66962#issuecomment-575907083).
3330                //
3331                // Note that with `feature(generic_const_exprs)` this case should not
3332                // be reachable.
3333                .with_note("this may fail depending on what value the parameter takes")
3334                .emit();
3335            return Err(guar);
3336        }
3337
3338        match obligation.predicate.kind().skip_binder() {
3339            ty::PredicateKind::Clause(ty::ClauseKind::ConstEvaluatable(ct)) => match ct.kind() {
3340                ty::ConstKind::Unevaluated(uv) => {
3341                    let mut err =
3342                        self.dcx().struct_span_err(span, "unconstrained generic constant");
3343                    let const_span = self.tcx.def_span(uv.def);
3344
3345                    let const_ty = self.tcx.type_of(uv.def).instantiate(self.tcx, uv.args);
3346                    let cast = if const_ty != self.tcx.types.usize { " as usize" } else { "" };
3347                    let msg = "try adding a `where` bound";
3348                    match self.tcx.sess.source_map().span_to_snippet(const_span) {
3349                        Ok(snippet) => {
3350                            let code = format!("[(); {snippet}{cast}]:");
3351                            let def_id = if let ObligationCauseCode::CompareImplItem {
3352                                trait_item_def_id,
3353                                ..
3354                            } = obligation.cause.code()
3355                            {
3356                                trait_item_def_id.as_local()
3357                            } else {
3358                                Some(obligation.cause.body_id)
3359                            };
3360                            if let Some(def_id) = def_id
3361                                && let Some(generics) = self.tcx.hir_get_generics(def_id)
3362                            {
3363                                err.span_suggestion_verbose(
3364                                    generics.tail_span_for_predicate_suggestion(),
3365                                    msg,
3366                                    format!("{} {code}", generics.add_where_or_trailing_comma()),
3367                                    Applicability::MaybeIncorrect,
3368                                );
3369                            } else {
3370                                err.help(format!("{msg}: where {code}"));
3371                            };
3372                        }
3373                        _ => {
3374                            err.help(msg);
3375                        }
3376                    };
3377                    Ok(err)
3378                }
3379                ty::ConstKind::Expr(_) => {
3380                    let err = self
3381                        .dcx()
3382                        .struct_span_err(span, format!("unconstrained generic constant `{ct}`"));
3383                    Ok(err)
3384                }
3385                _ => {
3386                    bug!("const evaluatable failed for non-unevaluated const `{ct:?}`");
3387                }
3388            },
3389            _ => {
3390                span_bug!(
3391                    span,
3392                    "unexpected non-ConstEvaluatable predicate, this should not be reachable"
3393                )
3394            }
3395        }
3396    }
3397}