rustc_trait_selection/error_reporting/infer/
mod.rs

1//! Error Reporting Code for the inference engine
2//!
3//! Because of the way inference, and in particular region inference,
4//! works, it often happens that errors are not detected until far after
5//! the relevant line of code has been type-checked. Therefore, there is
6//! an elaborate system to track why a particular constraint in the
7//! inference graph arose so that we can explain to the user what gave
8//! rise to a particular error.
9//!
10//! The system is based around a set of "origin" types. An "origin" is the
11//! reason that a constraint or inference variable arose. There are
12//! different "origin" enums for different kinds of constraints/variables
13//! (e.g., `TypeOrigin`, `RegionVariableOrigin`). An origin always has
14//! a span, but also more information so that we can generate a meaningful
15//! error message.
16//!
17//! Having a catalog of all the different reasons an error can arise is
18//! also useful for other reasons, like cross-referencing FAQs etc, though
19//! we are not really taking advantage of this yet.
20//!
21//! # Region Inference
22//!
23//! Region inference is particularly tricky because it always succeeds "in
24//! the moment" and simply registers a constraint. Then, at the end, we
25//! can compute the full graph and report errors, so we need to be able to
26//! store and later report what gave rise to the conflicting constraints.
27//!
28//! # Subtype Trace
29//!
30//! Determining whether `T1 <: T2` often involves a number of subtypes and
31//! subconstraints along the way. A "TypeTrace" is an extended version
32//! of an origin that traces the types and other values that were being
33//! compared. It is not necessarily comprehensive (in fact, at the time of
34//! this writing it only tracks the root values being compared) but I'd
35//! like to extend it to include significant "waypoints". For example, if
36//! you are comparing `(T1, T2) <: (T3, T4)`, and the problem is that `T2
37//! <: T4` fails, I'd like the trace to include enough information to say
38//! "in the 2nd element of the tuple". Similarly, failures when comparing
39//! arguments or return types in fn types should be able to cite the
40//! specific position, etc.
41//!
42//! # Reality vs plan
43//!
44//! Of course, there is still a LOT of code in typeck that has yet to be
45//! ported to this system, and which relies on string concatenation at the
46//! time of error detection.
47
48use std::borrow::Cow;
49use std::ops::ControlFlow;
50use std::path::PathBuf;
51use std::{cmp, fmt, iter};
52
53use rustc_abi::ExternAbi;
54use rustc_ast::join_path_syms;
55use rustc_data_structures::fx::{FxIndexMap, FxIndexSet};
56use rustc_errors::{
57    Applicability, Diag, DiagStyledString, IntoDiagArg, MultiSpan, StringPart, pluralize,
58};
59use rustc_hir::def::DefKind;
60use rustc_hir::def_id::DefId;
61use rustc_hir::intravisit::Visitor;
62use rustc_hir::lang_items::LangItem;
63use rustc_hir::{self as hir};
64use rustc_macros::extension;
65use rustc_middle::bug;
66use rustc_middle::dep_graph::DepContext;
67use rustc_middle::traits::PatternOriginExpr;
68use rustc_middle::ty::error::{ExpectedFound, TypeError, TypeErrorToStringExt};
69use rustc_middle::ty::print::{
70    PrintError, PrintTraitRefExt as _, WrapBinderMode, with_forced_trimmed_paths,
71};
72use rustc_middle::ty::{
73    self, List, ParamEnv, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable,
74    TypeVisitableExt,
75};
76use rustc_span::def_id::LOCAL_CRATE;
77use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Pos, Span, Symbol, sym};
78use tracing::{debug, instrument};
79
80use crate::error_reporting::TypeErrCtxt;
81use crate::errors::{ObligationCauseFailureCode, TypeErrorAdditionalDiags};
82use crate::infer;
83use crate::infer::relate::{self, RelateResult, TypeRelation};
84use crate::infer::{InferCtxt, InferCtxtExt as _, TypeTrace, ValuePairs};
85use crate::solve::deeply_normalize_for_diagnostics;
86use crate::traits::{MatchExpressionArmCause, ObligationCause, ObligationCauseCode};
87
88mod note_and_explain;
89mod suggest;
90
91pub mod need_type_info;
92pub mod nice_region_error;
93pub mod region;
94
95/// Makes a valid string literal from a string by escaping special characters (" and \),
96/// unless they are already escaped.
97fn escape_literal(s: &str) -> String {
98    let mut escaped = String::with_capacity(s.len());
99    let mut chrs = s.chars().peekable();
100    while let Some(first) = chrs.next() {
101        match (first, chrs.peek()) {
102            ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
103                escaped.push('\\');
104                escaped.push(delim);
105                chrs.next();
106            }
107            ('"' | '\'', _) => {
108                escaped.push('\\');
109                escaped.push(first)
110            }
111            (c, _) => escaped.push(c),
112        };
113    }
114    escaped
115}
116
117impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
118    // [Note-Type-error-reporting]
119    // An invariant is that anytime the expected or actual type is Error (the special
120    // error type, meaning that an error occurred when typechecking this expression),
121    // this is a derived error. The error cascaded from another error (that was already
122    // reported), so it's not useful to display it to the user.
123    // The following methods implement this logic.
124    // They check if either the actual or expected type is Error, and don't print the error
125    // in this case. The typechecker should only ever report type errors involving mismatched
126    // types using one of these methods, and should not call span_err directly for such
127    // errors.
128    pub fn type_error_struct_with_diag<M>(
129        &self,
130        sp: Span,
131        mk_diag: M,
132        actual_ty: Ty<'tcx>,
133    ) -> Diag<'a>
134    where
135        M: FnOnce(String) -> Diag<'a>,
136    {
137        let actual_ty = self.resolve_vars_if_possible(actual_ty);
138        debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
139
140        let mut err = mk_diag(self.ty_to_string(actual_ty));
141
142        // Don't report an error if actual type is `Error`.
143        if actual_ty.references_error() {
144            err.downgrade_to_delayed_bug();
145        }
146
147        err
148    }
149
150    pub fn report_mismatched_types(
151        &self,
152        cause: &ObligationCause<'tcx>,
153        param_env: ty::ParamEnv<'tcx>,
154        expected: Ty<'tcx>,
155        actual: Ty<'tcx>,
156        err: TypeError<'tcx>,
157    ) -> Diag<'a> {
158        self.report_and_explain_type_error(
159            TypeTrace::types(cause, expected, actual),
160            param_env,
161            err,
162        )
163    }
164
165    pub fn report_mismatched_consts(
166        &self,
167        cause: &ObligationCause<'tcx>,
168        param_env: ty::ParamEnv<'tcx>,
169        expected: ty::Const<'tcx>,
170        actual: ty::Const<'tcx>,
171        err: TypeError<'tcx>,
172    ) -> Diag<'a> {
173        self.report_and_explain_type_error(
174            TypeTrace::consts(cause, expected, actual),
175            param_env,
176            err,
177        )
178    }
179
180    pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
181        let (def_id, args) = match *ty.kind() {
182            ty::Alias(_, ty::AliasTy { def_id, args, .. })
183                if matches!(self.tcx.def_kind(def_id), DefKind::OpaqueTy) =>
184            {
185                (def_id, args)
186            }
187            ty::Alias(_, ty::AliasTy { def_id, args, .. })
188                if self.tcx.is_impl_trait_in_trait(def_id) =>
189            {
190                (def_id, args)
191            }
192            _ => return None,
193        };
194
195        let future_trait = self.tcx.require_lang_item(LangItem::Future, DUMMY_SP);
196        let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
197
198        self.tcx
199            .explicit_item_self_bounds(def_id)
200            .iter_instantiated_copied(self.tcx, args)
201            .find_map(|(predicate, _)| {
202                predicate
203                    .kind()
204                    .map_bound(|kind| match kind {
205                        ty::ClauseKind::Projection(projection_predicate)
206                            if projection_predicate.projection_term.def_id == item_def_id =>
207                        {
208                            projection_predicate.term.as_type()
209                        }
210                        _ => None,
211                    })
212                    .no_bound_vars()
213                    .flatten()
214            })
215    }
216
217    /// Adds a note if the types come from similarly named crates
218    fn check_and_note_conflicting_crates(&self, err: &mut Diag<'_>, terr: TypeError<'tcx>) -> bool {
219        // FIXME(estebank): unify with `report_similar_impl_candidates`. The message is similar,
220        // even if the logic needed to detect the case is very different.
221        use hir::def_id::CrateNum;
222        use rustc_hir::definitions::DisambiguatedDefPathData;
223        use ty::GenericArg;
224        use ty::print::Printer;
225
226        struct ConflictingPathPrinter<'tcx> {
227            tcx: TyCtxt<'tcx>,
228            segments: Vec<Symbol>,
229        }
230
231        impl<'tcx> Printer<'tcx> for ConflictingPathPrinter<'tcx> {
232            fn tcx<'a>(&'a self) -> TyCtxt<'tcx> {
233                self.tcx
234            }
235
236            fn print_region(&mut self, _region: ty::Region<'_>) -> Result<(), PrintError> {
237                unreachable!(); // because `print_path_with_generic_args` ignores the `GenericArgs`
238            }
239
240            fn print_type(&mut self, _ty: Ty<'tcx>) -> Result<(), PrintError> {
241                unreachable!(); // because `print_path_with_generic_args` ignores the `GenericArgs`
242            }
243
244            fn print_dyn_existential(
245                &mut self,
246                _predicates: &'tcx ty::List<ty::PolyExistentialPredicate<'tcx>>,
247            ) -> Result<(), PrintError> {
248                unreachable!(); // because `print_path_with_generic_args` ignores the `GenericArgs`
249            }
250
251            fn print_const(&mut self, _ct: ty::Const<'tcx>) -> Result<(), PrintError> {
252                unreachable!(); // because `print_path_with_generic_args` ignores the `GenericArgs`
253            }
254
255            fn print_crate_name(&mut self, cnum: CrateNum) -> Result<(), PrintError> {
256                self.segments = vec![self.tcx.crate_name(cnum)];
257                Ok(())
258            }
259
260            fn print_path_with_qualified(
261                &mut self,
262                _self_ty: Ty<'tcx>,
263                _trait_ref: Option<ty::TraitRef<'tcx>>,
264            ) -> Result<(), PrintError> {
265                Err(fmt::Error)
266            }
267
268            fn print_path_with_impl(
269                &mut self,
270                _print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
271                _self_ty: Ty<'tcx>,
272                _trait_ref: Option<ty::TraitRef<'tcx>>,
273            ) -> Result<(), PrintError> {
274                Err(fmt::Error)
275            }
276
277            fn print_path_with_simple(
278                &mut self,
279                print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
280                disambiguated_data: &DisambiguatedDefPathData,
281            ) -> Result<(), PrintError> {
282                print_prefix(self)?;
283                self.segments.push(disambiguated_data.as_sym(true));
284                Ok(())
285            }
286
287            fn print_path_with_generic_args(
288                &mut self,
289                print_prefix: impl FnOnce(&mut Self) -> Result<(), PrintError>,
290                _args: &[GenericArg<'tcx>],
291            ) -> Result<(), PrintError> {
292                print_prefix(self)
293            }
294        }
295
296        let report_path_match = |err: &mut Diag<'_>, did1: DefId, did2: DefId, ty: &str| -> bool {
297            // Only report definitions from different crates. If both definitions
298            // are from a local module we could have false positives, e.g.
299            // let _ = [{struct Foo; Foo}, {struct Foo; Foo}];
300            if did1.krate != did2.krate {
301                let abs_path = |def_id| {
302                    let mut p = ConflictingPathPrinter { tcx: self.tcx, segments: vec![] };
303                    p.print_def_path(def_id, &[]).map(|_| p.segments)
304                };
305
306                // We compare strings because DefPath can be different for imported and
307                // non-imported crates.
308                let expected_str = self.tcx.def_path_str(did1);
309                let found_str = self.tcx.def_path_str(did2);
310                let Ok(expected_abs) = abs_path(did1) else { return false };
311                let Ok(found_abs) = abs_path(did2) else { return false };
312                let same_path = expected_str == found_str || expected_abs == found_abs;
313                if same_path {
314                    // We want to use as unique a type path as possible. If both types are "locally
315                    // known" by the same name, we use the "absolute path" which uses the original
316                    // crate name instead.
317                    let (expected, found) = if expected_str == found_str {
318                        (join_path_syms(&expected_abs), join_path_syms(&found_abs))
319                    } else {
320                        (expected_str, found_str)
321                    };
322
323                    // We've displayed "expected `a::b`, found `a::b`". We add context to
324                    // differentiate the different cases where that might happen.
325                    let expected_crate_name = self.tcx.crate_name(did1.krate);
326                    let found_crate_name = self.tcx.crate_name(did2.krate);
327                    let same_crate = expected_crate_name == found_crate_name;
328                    let expected_sp = self.tcx.def_span(did1);
329                    let found_sp = self.tcx.def_span(did2);
330
331                    let both_direct_dependencies = if !did1.is_local()
332                        && !did2.is_local()
333                        && let Some(data1) = self.tcx.extern_crate(did1.krate)
334                        && let Some(data2) = self.tcx.extern_crate(did2.krate)
335                        && data1.dependency_of == LOCAL_CRATE
336                        && data2.dependency_of == LOCAL_CRATE
337                    {
338                        // If both crates are directly depended on, we don't want to mention that
339                        // in the final message, as it is redundant wording.
340                        // We skip the case of semver trick, where one version of the local crate
341                        // depends on another version of itself by checking that both crates at play
342                        // are not the current one.
343                        true
344                    } else {
345                        false
346                    };
347
348                    let mut span: MultiSpan = vec![expected_sp, found_sp].into();
349                    span.push_span_label(
350                        self.tcx.def_span(did1),
351                        format!("this is the expected {ty} `{expected}`"),
352                    );
353                    span.push_span_label(
354                        self.tcx.def_span(did2),
355                        format!("this is the found {ty} `{found}`"),
356                    );
357                    for def_id in [did1, did2] {
358                        let crate_name = self.tcx.crate_name(def_id.krate);
359                        if !def_id.is_local()
360                            && let Some(data) = self.tcx.extern_crate(def_id.krate)
361                        {
362                            let descr = if same_crate {
363                                "one version of".to_string()
364                            } else {
365                                format!("one {ty} comes from")
366                            };
367                            let dependency = if both_direct_dependencies {
368                                if let rustc_session::cstore::ExternCrateSource::Extern(def_id) =
369                                    data.src
370                                    && let Some(name) = self.tcx.opt_item_name(def_id)
371                                {
372                                    format!(", which is renamed locally to `{name}`")
373                                } else {
374                                    String::new()
375                                }
376                            } else if data.dependency_of == LOCAL_CRATE {
377                                ", as a direct dependency of the current crate".to_string()
378                            } else {
379                                let dep = self.tcx.crate_name(data.dependency_of);
380                                format!(", as a dependency of crate `{dep}`")
381                            };
382                            span.push_span_label(
383                                data.span,
384                                format!("{descr} crate `{crate_name}` used here{dependency}"),
385                            );
386                        }
387                    }
388                    let msg = if (did1.is_local() || did2.is_local()) && same_crate {
389                        format!(
390                            "the crate `{expected_crate_name}` is compiled multiple times, \
391                             possibly with different configurations",
392                        )
393                    } else if same_crate {
394                        format!(
395                            "two different versions of crate `{expected_crate_name}` are being \
396                             used; two types coming from two different versions of the same crate \
397                             are different types even if they look the same",
398                        )
399                    } else {
400                        format!(
401                            "two types coming from two different crates are different types even \
402                             if they look the same",
403                        )
404                    };
405                    err.span_note(span, msg);
406                    if same_crate {
407                        err.help("you can use `cargo tree` to explore your dependency tree");
408                    }
409                    return true;
410                }
411            }
412            false
413        };
414        match terr {
415            TypeError::Sorts(ref exp_found) => {
416                // if they are both "path types", there's a chance of ambiguity
417                // due to different versions of the same crate
418                if let (&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _)) =
419                    (exp_found.expected.kind(), exp_found.found.kind())
420                {
421                    return report_path_match(err, exp_adt.did(), found_adt.did(), "type");
422                }
423            }
424            TypeError::Traits(ref exp_found) => {
425                return report_path_match(err, exp_found.expected, exp_found.found, "trait");
426            }
427            _ => (), // FIXME(#22750) handle traits and stuff
428        }
429        false
430    }
431
432    fn note_error_origin(
433        &self,
434        err: &mut Diag<'_>,
435        cause: &ObligationCause<'tcx>,
436        exp_found: Option<ty::error::ExpectedFound<Ty<'tcx>>>,
437        terr: TypeError<'tcx>,
438        param_env: Option<ParamEnv<'tcx>>,
439    ) {
440        match *cause.code() {
441            ObligationCauseCode::Pattern {
442                origin_expr: Some(origin_expr),
443                span: Some(span),
444                root_ty,
445            } => {
446                let expected_ty = self.resolve_vars_if_possible(root_ty);
447                if !matches!(
448                    expected_ty.kind(),
449                    ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_))
450                ) {
451                    // don't show type `_`
452                    if span.desugaring_kind() == Some(DesugaringKind::ForLoop)
453                        && let ty::Adt(def, args) = expected_ty.kind()
454                        && Some(def.did()) == self.tcx.get_diagnostic_item(sym::Option)
455                    {
456                        err.span_label(
457                            span,
458                            format!("this is an iterator with items of type `{}`", args.type_at(0)),
459                        );
460                    } else if !span.overlaps(cause.span) {
461                        let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path());
462                        err.span_label(span, format!("this expression has type `{expected_ty}`"));
463                    }
464                }
465                if let Some(ty::error::ExpectedFound { found, .. }) = exp_found
466                    && let Ok(mut peeled_snippet) =
467                        self.tcx.sess.source_map().span_to_snippet(origin_expr.peeled_span)
468                {
469                    // Parentheses are needed for cases like as casts.
470                    // We use the peeled_span for deref suggestions.
471                    // It's also safe to use for box, since box only triggers if there
472                    // wasn't a reference to begin with.
473                    if origin_expr.peeled_prefix_suggestion_parentheses {
474                        peeled_snippet = format!("({peeled_snippet})");
475                    }
476
477                    // Try giving a box suggestion first, as it is a special case of the
478                    // deref suggestion.
479                    if expected_ty.boxed_ty() == Some(found) {
480                        err.span_suggestion_verbose(
481                            span,
482                            "consider dereferencing the boxed value",
483                            format!("*{peeled_snippet}"),
484                            Applicability::MachineApplicable,
485                        );
486                    } else if let Some(param_env) = param_env
487                        && let Some(prefix) = self.should_deref_suggestion_on_mismatch(
488                            param_env,
489                            found,
490                            expected_ty,
491                            origin_expr,
492                        )
493                    {
494                        err.span_suggestion_verbose(
495                            span,
496                            "consider dereferencing to access the inner value using the Deref trait",
497                            format!("{prefix}{peeled_snippet}"),
498                            Applicability::MaybeIncorrect,
499                        );
500                    }
501                }
502            }
503            ObligationCauseCode::Pattern { origin_expr: None, span: Some(span), .. } => {
504                err.span_label(span, "expected due to this");
505            }
506            ObligationCauseCode::BlockTailExpression(
507                _,
508                hir::MatchSource::TryDesugar(scrut_hir_id),
509            ) => {
510                if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
511                    let scrut_expr = self.tcx.hir_expect_expr(scrut_hir_id);
512                    let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
513                        let arg_expr = args.first().expect("try desugaring call w/out arg");
514                        self.typeck_results
515                            .as_ref()
516                            .and_then(|typeck_results| typeck_results.expr_ty_opt(arg_expr))
517                    } else {
518                        bug!("try desugaring w/out call expr as scrutinee");
519                    };
520
521                    match scrut_ty {
522                        Some(ty) if expected == ty => {
523                            let source_map = self.tcx.sess.source_map();
524                            err.span_suggestion(
525                                source_map.end_point(cause.span),
526                                "try removing this `?`",
527                                "",
528                                Applicability::MachineApplicable,
529                            );
530                        }
531                        _ => {}
532                    }
533                }
534            }
535            ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
536                arm_block_id,
537                arm_span,
538                arm_ty,
539                prior_arm_block_id,
540                prior_arm_span,
541                prior_arm_ty,
542                source,
543                ref prior_non_diverging_arms,
544                scrut_span,
545                expr_span,
546                ..
547            }) => match source {
548                hir::MatchSource::TryDesugar(scrut_hir_id) => {
549                    if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
550                        let scrut_expr = self.tcx.hir_expect_expr(scrut_hir_id);
551                        let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
552                            let arg_expr = args.first().expect("try desugaring call w/out arg");
553                            self.typeck_results
554                                .as_ref()
555                                .and_then(|typeck_results| typeck_results.expr_ty_opt(arg_expr))
556                        } else {
557                            bug!("try desugaring w/out call expr as scrutinee");
558                        };
559
560                        match scrut_ty {
561                            Some(ty) if expected == ty => {
562                                let source_map = self.tcx.sess.source_map();
563                                err.span_suggestion(
564                                    source_map.end_point(cause.span),
565                                    "try removing this `?`",
566                                    "",
567                                    Applicability::MachineApplicable,
568                                );
569                            }
570                            _ => {}
571                        }
572                    }
573                }
574                _ => {
575                    // `prior_arm_ty` can be `!`, `expected` will have better info when present.
576                    let t = self.resolve_vars_if_possible(match exp_found {
577                        Some(ty::error::ExpectedFound { expected, .. }) => expected,
578                        _ => prior_arm_ty,
579                    });
580                    let source_map = self.tcx.sess.source_map();
581                    let mut any_multiline_arm = source_map.is_multiline(arm_span);
582                    if prior_non_diverging_arms.len() <= 4 {
583                        for sp in prior_non_diverging_arms {
584                            any_multiline_arm |= source_map.is_multiline(*sp);
585                            err.span_label(*sp, format!("this is found to be of type `{t}`"));
586                        }
587                    } else if let Some(sp) = prior_non_diverging_arms.last() {
588                        any_multiline_arm |= source_map.is_multiline(*sp);
589                        err.span_label(
590                            *sp,
591                            format!("this and all prior arms are found to be of type `{t}`"),
592                        );
593                    }
594                    let outer = if any_multiline_arm || !source_map.is_multiline(expr_span) {
595                        // Cover just `match` and the scrutinee expression, not
596                        // the entire match body, to reduce diagram noise.
597                        expr_span.shrink_to_lo().to(scrut_span)
598                    } else {
599                        expr_span
600                    };
601                    let msg = "`match` arms have incompatible types";
602                    err.span_label(outer, msg);
603                    if let Some(subdiag) = self.suggest_remove_semi_or_return_binding(
604                        prior_arm_block_id,
605                        prior_arm_ty,
606                        prior_arm_span,
607                        arm_block_id,
608                        arm_ty,
609                        arm_span,
610                    ) {
611                        err.subdiagnostic(subdiag);
612                    }
613                }
614            },
615            ObligationCauseCode::IfExpression { expr_id, .. } => {
616                let hir::Node::Expr(&hir::Expr {
617                    kind: hir::ExprKind::If(cond_expr, then_expr, Some(else_expr)),
618                    span: expr_span,
619                    ..
620                }) = self.tcx.hir_node(expr_id)
621                else {
622                    return;
623                };
624                let then_span = self.find_block_span_from_hir_id(then_expr.hir_id);
625                let then_ty = self
626                    .typeck_results
627                    .as_ref()
628                    .expect("if expression only expected inside FnCtxt")
629                    .expr_ty(then_expr);
630                let else_span = self.find_block_span_from_hir_id(else_expr.hir_id);
631                let else_ty = self
632                    .typeck_results
633                    .as_ref()
634                    .expect("if expression only expected inside FnCtxt")
635                    .expr_ty(else_expr);
636                if let hir::ExprKind::If(_cond, _then, None) = else_expr.kind
637                    && else_ty.is_unit()
638                {
639                    // Account for `let x = if a { 1 } else if b { 2 };`
640                    err.note("`if` expressions without `else` evaluate to `()`");
641                    err.note("consider adding an `else` block that evaluates to the expected type");
642                }
643                err.span_label(then_span, "expected because of this");
644
645                let outer_span = if self.tcx.sess.source_map().is_multiline(expr_span) {
646                    if then_span.hi() == expr_span.hi() || else_span.hi() == expr_span.hi() {
647                        // Point at condition only if either block has the same end point as
648                        // the whole expression, since that'll cause awkward overlapping spans.
649                        Some(expr_span.shrink_to_lo().to(cond_expr.peel_drop_temps().span))
650                    } else {
651                        Some(expr_span)
652                    }
653                } else {
654                    None
655                };
656                if let Some(sp) = outer_span {
657                    err.span_label(sp, "`if` and `else` have incompatible types");
658                }
659
660                let then_id = if let hir::ExprKind::Block(then_blk, _) = then_expr.kind {
661                    then_blk.hir_id
662                } else {
663                    then_expr.hir_id
664                };
665                let else_id = if let hir::ExprKind::Block(else_blk, _) = else_expr.kind {
666                    else_blk.hir_id
667                } else {
668                    else_expr.hir_id
669                };
670                if let Some(subdiag) = self.suggest_remove_semi_or_return_binding(
671                    Some(then_id),
672                    then_ty,
673                    then_span,
674                    Some(else_id),
675                    else_ty,
676                    else_span,
677                ) {
678                    err.subdiagnostic(subdiag);
679                }
680            }
681            ObligationCauseCode::LetElse => {
682                err.help("try adding a diverging expression, such as `return` or `panic!(..)`");
683                err.help("...or use `match` instead of `let...else`");
684            }
685            _ => {
686                if let ObligationCauseCode::WhereClause(_, span)
687                | ObligationCauseCode::WhereClauseInExpr(_, span, ..) =
688                    cause.code().peel_derives()
689                    && !span.is_dummy()
690                    && let TypeError::RegionsPlaceholderMismatch = terr
691                {
692                    err.span_note(*span, "the lifetime requirement is introduced here");
693                }
694            }
695        }
696    }
697
698    /// Determines whether deref_to == <deref_from as Deref>::Target, and if so,
699    /// returns a prefix that should be added to deref_from as a suggestion.
700    fn should_deref_suggestion_on_mismatch(
701        &self,
702        param_env: ParamEnv<'tcx>,
703        deref_to: Ty<'tcx>,
704        deref_from: Ty<'tcx>,
705        origin_expr: PatternOriginExpr,
706    ) -> Option<String> {
707        // origin_expr contains stripped away versions of our expression.
708        // We'll want to use that to avoid suggesting things like *&x.
709        // However, the type that we have access to hasn't been stripped away,
710        // so we need to ignore the first n dereferences, where n is the number
711        // that's been stripped away in origin_expr.
712
713        // Find a way to autoderef from deref_from to deref_to.
714        let Some((num_derefs, (after_deref_ty, _))) = (self.autoderef_steps)(deref_from)
715            .into_iter()
716            .enumerate()
717            .find(|(_, (ty, _))| self.infcx.can_eq(param_env, *ty, deref_to))
718        else {
719            return None;
720        };
721
722        if num_derefs <= origin_expr.peeled_count {
723            return None;
724        }
725
726        let deref_part = "*".repeat(num_derefs - origin_expr.peeled_count);
727
728        // If the user used a reference in the original expression, they probably
729        // want the suggestion to still give a reference.
730        if deref_from.is_ref() && !after_deref_ty.is_ref() {
731            Some(format!("&{deref_part}"))
732        } else {
733            Some(deref_part)
734        }
735    }
736
737    /// Given that `other_ty` is the same as a type argument for `name` in `sub`, populate `value`
738    /// highlighting `name` and every type argument that isn't at `pos` (which is `other_ty`), and
739    /// populate `other_value` with `other_ty`.
740    ///
741    /// ```text
742    /// Foo<Bar<Qux>>
743    /// ^^^^--------^ this is highlighted
744    /// |   |
745    /// |   this type argument is exactly the same as the other type, not highlighted
746    /// this is highlighted
747    /// Bar<Qux>
748    /// -------- this type is the same as a type argument in the other type, not highlighted
749    /// ```
750    fn highlight_outer(
751        &self,
752        value: &mut DiagStyledString,
753        other_value: &mut DiagStyledString,
754        name: String,
755        args: &[ty::GenericArg<'tcx>],
756        pos: usize,
757        other_ty: Ty<'tcx>,
758    ) {
759        // `value` and `other_value` hold two incomplete type representation for display.
760        // `name` is the path of both types being compared. `sub`
761        value.push_highlighted(name);
762
763        if args.is_empty() {
764            return;
765        }
766        value.push_highlighted("<");
767
768        for (i, arg) in args.iter().enumerate() {
769            if i > 0 {
770                value.push_normal(", ");
771            }
772
773            match arg.kind() {
774                ty::GenericArgKind::Lifetime(lt) => {
775                    let s = lt.to_string();
776                    value.push_normal(if s.is_empty() { "'_" } else { &s });
777                }
778                ty::GenericArgKind::Const(ct) => {
779                    value.push_normal(ct.to_string());
780                }
781                // Highlight all the type arguments that aren't at `pos` and compare
782                // the type argument at `pos` and `other_ty`.
783                ty::GenericArgKind::Type(type_arg) => {
784                    if i == pos {
785                        let values = self.cmp(type_arg, other_ty);
786                        value.0.extend((values.0).0);
787                        other_value.0.extend((values.1).0);
788                    } else {
789                        value.push_highlighted(type_arg.to_string());
790                    }
791                }
792            }
793        }
794
795        value.push_highlighted(">");
796    }
797
798    /// If `other_ty` is the same as a type argument present in `sub`, highlight `path` in `t1_out`,
799    /// as that is the difference to the other type.
800    ///
801    /// For the following code:
802    ///
803    /// ```ignore (illustrative)
804    /// let x: Foo<Bar<Qux>> = foo::<Bar<Qux>>();
805    /// ```
806    ///
807    /// The type error output will behave in the following way:
808    ///
809    /// ```text
810    /// Foo<Bar<Qux>>
811    /// ^^^^--------^ this is highlighted
812    /// |   |
813    /// |   this type argument is exactly the same as the other type, not highlighted
814    /// this is highlighted
815    /// Bar<Qux>
816    /// -------- this type is the same as a type argument in the other type, not highlighted
817    /// ```
818    fn cmp_type_arg(
819        &self,
820        t1_out: &mut DiagStyledString,
821        t2_out: &mut DiagStyledString,
822        path: String,
823        args: &'tcx [ty::GenericArg<'tcx>],
824        other_path: String,
825        other_ty: Ty<'tcx>,
826    ) -> bool {
827        for (i, arg) in args.iter().enumerate() {
828            if let Some(ta) = arg.as_type() {
829                if ta == other_ty {
830                    self.highlight_outer(t1_out, t2_out, path, args, i, other_ty);
831                    return true;
832                }
833                if let ty::Adt(def, _) = ta.kind() {
834                    let path_ = self.tcx.def_path_str(def.did());
835                    if path_ == other_path {
836                        self.highlight_outer(t1_out, t2_out, path, args, i, other_ty);
837                        return true;
838                    }
839                }
840            }
841        }
842        false
843    }
844
845    /// Adds a `,` to the type representation only if it is appropriate.
846    fn push_comma(
847        &self,
848        value: &mut DiagStyledString,
849        other_value: &mut DiagStyledString,
850        pos: usize,
851    ) {
852        if pos > 0 {
853            value.push_normal(", ");
854            other_value.push_normal(", ");
855        }
856    }
857
858    /// Given two `fn` signatures highlight only sub-parts that are different.
859    fn cmp_fn_sig(
860        &self,
861        sig1: &ty::PolyFnSig<'tcx>,
862        fn_def1: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
863        sig2: &ty::PolyFnSig<'tcx>,
864        fn_def2: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
865    ) -> (DiagStyledString, DiagStyledString) {
866        let sig1 = &(self.normalize_fn_sig)(*sig1);
867        let sig2 = &(self.normalize_fn_sig)(*sig2);
868
869        let get_lifetimes = |sig| {
870            use rustc_hir::def::Namespace;
871            let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
872                .name_all_regions(sig, WrapBinderMode::ForAll)
873                .unwrap();
874            let lts: Vec<String> =
875                reg.into_items().map(|(_, kind)| kind.to_string()).into_sorted_stable_ord();
876            (if lts.is_empty() { String::new() } else { format!("for<{}> ", lts.join(", ")) }, sig)
877        };
878
879        let (lt1, sig1) = get_lifetimes(sig1);
880        let (lt2, sig2) = get_lifetimes(sig2);
881
882        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
883        let mut values =
884            (DiagStyledString::normal("".to_string()), DiagStyledString::normal("".to_string()));
885
886        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
887        // ^^^^^^
888        let safety = |fn_def, sig: ty::FnSig<'_>| match fn_def {
889            None => sig.safety.prefix_str(),
890            Some((did, _)) => {
891                if self.tcx.codegen_fn_attrs(did).safe_target_features {
892                    "#[target_features] "
893                } else {
894                    sig.safety.prefix_str()
895                }
896            }
897        };
898        let safety1 = safety(fn_def1, sig1);
899        let safety2 = safety(fn_def2, sig2);
900        values.0.push(safety1, safety1 != safety2);
901        values.1.push(safety2, safety1 != safety2);
902
903        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
904        //        ^^^^^^^^^^
905        if sig1.abi != ExternAbi::Rust {
906            values.0.push(format!("extern {} ", sig1.abi), sig1.abi != sig2.abi);
907        }
908        if sig2.abi != ExternAbi::Rust {
909            values.1.push(format!("extern {} ", sig2.abi), sig1.abi != sig2.abi);
910        }
911
912        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
913        //                   ^^^^^^^^
914        let lifetime_diff = lt1 != lt2;
915        values.0.push(lt1, lifetime_diff);
916        values.1.push(lt2, lifetime_diff);
917
918        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
919        //                           ^^^
920        values.0.push_normal("fn(");
921        values.1.push_normal("fn(");
922
923        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
924        //                              ^^^^^
925        let len1 = sig1.inputs().len();
926        let len2 = sig2.inputs().len();
927        if len1 == len2 {
928            for (i, (l, r)) in iter::zip(sig1.inputs(), sig2.inputs()).enumerate() {
929                self.push_comma(&mut values.0, &mut values.1, i);
930                let (x1, x2) = self.cmp(*l, *r);
931                (values.0).0.extend(x1.0);
932                (values.1).0.extend(x2.0);
933            }
934        } else {
935            for (i, l) in sig1.inputs().iter().enumerate() {
936                values.0.push_highlighted(l.to_string());
937                if i != len1 - 1 {
938                    values.0.push_highlighted(", ");
939                }
940            }
941            for (i, r) in sig2.inputs().iter().enumerate() {
942                values.1.push_highlighted(r.to_string());
943                if i != len2 - 1 {
944                    values.1.push_highlighted(", ");
945                }
946            }
947        }
948
949        if sig1.c_variadic {
950            if len1 > 0 {
951                values.0.push_normal(", ");
952            }
953            values.0.push("...", !sig2.c_variadic);
954        }
955        if sig2.c_variadic {
956            if len2 > 0 {
957                values.1.push_normal(", ");
958            }
959            values.1.push("...", !sig1.c_variadic);
960        }
961
962        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
963        //                                   ^
964        values.0.push_normal(")");
965        values.1.push_normal(")");
966
967        // unsafe extern "C" for<'a> fn(&'a T) -> &'a T
968        //                                     ^^^^^^^^
969        let output1 = sig1.output();
970        let output2 = sig2.output();
971        let (x1, x2) = self.cmp(output1, output2);
972        let output_diff = x1 != x2;
973        if !output1.is_unit() || output_diff {
974            values.0.push_normal(" -> ");
975            (values.0).0.extend(x1.0);
976        }
977        if !output2.is_unit() || output_diff {
978            values.1.push_normal(" -> ");
979            (values.1).0.extend(x2.0);
980        }
981
982        let fmt = |did, args| format!(" {{{}}}", self.tcx.def_path_str_with_args(did, args));
983
984        match (fn_def1, fn_def2) {
985            (Some((fn_def1, Some(fn_args1))), Some((fn_def2, Some(fn_args2)))) => {
986                let path1 = fmt(fn_def1, fn_args1);
987                let path2 = fmt(fn_def2, fn_args2);
988                let same_path = path1 == path2;
989                values.0.push(path1, !same_path);
990                values.1.push(path2, !same_path);
991            }
992            (Some((fn_def1, Some(fn_args1))), None) => {
993                values.0.push_highlighted(fmt(fn_def1, fn_args1));
994            }
995            (None, Some((fn_def2, Some(fn_args2)))) => {
996                values.1.push_highlighted(fmt(fn_def2, fn_args2));
997            }
998            _ => {}
999        }
1000
1001        values
1002    }
1003
1004    pub fn cmp_traits(
1005        &self,
1006        def_id1: DefId,
1007        args1: &[ty::GenericArg<'tcx>],
1008        def_id2: DefId,
1009        args2: &[ty::GenericArg<'tcx>],
1010    ) -> (DiagStyledString, DiagStyledString) {
1011        let mut values = (DiagStyledString::new(), DiagStyledString::new());
1012
1013        if def_id1 != def_id2 {
1014            values.0.push_highlighted(self.tcx.def_path_str(def_id1).as_str());
1015            values.1.push_highlighted(self.tcx.def_path_str(def_id2).as_str());
1016        } else {
1017            values.0.push_normal(self.tcx.item_name(def_id1).as_str());
1018            values.1.push_normal(self.tcx.item_name(def_id2).as_str());
1019        }
1020
1021        if args1.len() != args2.len() {
1022            let (pre, post) = if args1.len() > 0 { ("<", ">") } else { ("", "") };
1023            values.0.push_normal(format!(
1024                "{pre}{}{post}",
1025                args1.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
1026            ));
1027            let (pre, post) = if args2.len() > 0 { ("<", ">") } else { ("", "") };
1028            values.1.push_normal(format!(
1029                "{pre}{}{post}",
1030                args2.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
1031            ));
1032            return values;
1033        }
1034
1035        if args1.len() > 0 {
1036            values.0.push_normal("<");
1037            values.1.push_normal("<");
1038        }
1039        for (i, (a, b)) in std::iter::zip(args1, args2).enumerate() {
1040            let a_str = a.to_string();
1041            let b_str = b.to_string();
1042            if let (Some(a), Some(b)) = (a.as_type(), b.as_type()) {
1043                let (a, b) = self.cmp(a, b);
1044                values.0.0.extend(a.0);
1045                values.1.0.extend(b.0);
1046            } else if a_str != b_str {
1047                values.0.push_highlighted(a_str);
1048                values.1.push_highlighted(b_str);
1049            } else {
1050                values.0.push_normal(a_str);
1051                values.1.push_normal(b_str);
1052            }
1053            if i + 1 < args1.len() {
1054                values.0.push_normal(", ");
1055                values.1.push_normal(", ");
1056            }
1057        }
1058        if args1.len() > 0 {
1059            values.0.push_normal(">");
1060            values.1.push_normal(">");
1061        }
1062        values
1063    }
1064
1065    /// Compares two given types, eliding parts that are the same between them and highlighting
1066    /// relevant differences, and return two representation of those types for highlighted printing.
1067    pub fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> (DiagStyledString, DiagStyledString) {
1068        debug!("cmp(t1={}, t1.kind={:?}, t2={}, t2.kind={:?})", t1, t1.kind(), t2, t2.kind());
1069
1070        // helper functions
1071        let recurse = |t1, t2, values: &mut (DiagStyledString, DiagStyledString)| {
1072            let (x1, x2) = self.cmp(t1, t2);
1073            (values.0).0.extend(x1.0);
1074            (values.1).0.extend(x2.0);
1075        };
1076
1077        fn fmt_region<'tcx>(region: ty::Region<'tcx>) -> String {
1078            let mut r = region.to_string();
1079            if r == "'_" {
1080                r.clear();
1081            } else {
1082                r.push(' ');
1083            }
1084            format!("&{r}")
1085        }
1086
1087        fn push_ref<'tcx>(
1088            region: ty::Region<'tcx>,
1089            mutbl: hir::Mutability,
1090            s: &mut DiagStyledString,
1091        ) {
1092            s.push_highlighted(fmt_region(region));
1093            s.push_highlighted(mutbl.prefix_str());
1094        }
1095
1096        fn maybe_highlight<T: Eq + ToString>(
1097            t1: T,
1098            t2: T,
1099            (buf1, buf2): &mut (DiagStyledString, DiagStyledString),
1100            tcx: TyCtxt<'_>,
1101        ) {
1102            let highlight = t1 != t2;
1103            let (t1, t2) = if highlight || tcx.sess.opts.verbose {
1104                (t1.to_string(), t2.to_string())
1105            } else {
1106                // The two types are the same, elide and don't highlight.
1107                ("_".into(), "_".into())
1108            };
1109            buf1.push(t1, highlight);
1110            buf2.push(t2, highlight);
1111        }
1112
1113        fn cmp_ty_refs<'tcx>(
1114            r1: ty::Region<'tcx>,
1115            mut1: hir::Mutability,
1116            r2: ty::Region<'tcx>,
1117            mut2: hir::Mutability,
1118            ss: &mut (DiagStyledString, DiagStyledString),
1119        ) {
1120            let (r1, r2) = (fmt_region(r1), fmt_region(r2));
1121            if r1 != r2 {
1122                ss.0.push_highlighted(r1);
1123                ss.1.push_highlighted(r2);
1124            } else {
1125                ss.0.push_normal(r1);
1126                ss.1.push_normal(r2);
1127            }
1128
1129            if mut1 != mut2 {
1130                ss.0.push_highlighted(mut1.prefix_str());
1131                ss.1.push_highlighted(mut2.prefix_str());
1132            } else {
1133                ss.0.push_normal(mut1.prefix_str());
1134                ss.1.push_normal(mut2.prefix_str());
1135            }
1136        }
1137
1138        // process starts here
1139        match (t1.kind(), t2.kind()) {
1140            (&ty::Adt(def1, sub1), &ty::Adt(def2, sub2)) => {
1141                let did1 = def1.did();
1142                let did2 = def2.did();
1143
1144                let generics1 = self.tcx.generics_of(did1);
1145                let generics2 = self.tcx.generics_of(did2);
1146
1147                let non_default_after_default = generics1
1148                    .check_concrete_type_after_default(self.tcx, sub1)
1149                    || generics2.check_concrete_type_after_default(self.tcx, sub2);
1150                let sub_no_defaults_1 = if non_default_after_default {
1151                    generics1.own_args(sub1)
1152                } else {
1153                    generics1.own_args_no_defaults(self.tcx, sub1)
1154                };
1155                let sub_no_defaults_2 = if non_default_after_default {
1156                    generics2.own_args(sub2)
1157                } else {
1158                    generics2.own_args_no_defaults(self.tcx, sub2)
1159                };
1160                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1161                let path1 = self.tcx.def_path_str(did1);
1162                let path2 = self.tcx.def_path_str(did2);
1163                if did1 == did2 {
1164                    // Easy case. Replace same types with `_` to shorten the output and highlight
1165                    // the differing ones.
1166                    //     let x: Foo<Bar, Qux> = y::<Foo<Quz, Qux>>();
1167                    //     Foo<Bar, _>
1168                    //     Foo<Quz, _>
1169                    //         ---  ^ type argument elided
1170                    //         |
1171                    //         highlighted in output
1172                    values.0.push_normal(path1);
1173                    values.1.push_normal(path2);
1174
1175                    // Avoid printing out default generic parameters that are common to both
1176                    // types.
1177                    let len1 = sub_no_defaults_1.len();
1178                    let len2 = sub_no_defaults_2.len();
1179                    let common_len = cmp::min(len1, len2);
1180                    let remainder1 = &sub1[common_len..];
1181                    let remainder2 = &sub2[common_len..];
1182                    let common_default_params =
1183                        iter::zip(remainder1.iter().rev(), remainder2.iter().rev())
1184                            .filter(|(a, b)| a == b)
1185                            .count();
1186                    let len = sub1.len() - common_default_params;
1187
1188                    // Only draw `<...>` if there are lifetime/type arguments.
1189                    if len > 0 {
1190                        values.0.push_normal("<");
1191                        values.1.push_normal("<");
1192                    }
1193
1194                    fn lifetime_display(lifetime: Region<'_>) -> String {
1195                        let s = lifetime.to_string();
1196                        if s.is_empty() { "'_".to_string() } else { s }
1197                    }
1198
1199                    for (i, (arg1, arg2)) in sub1.iter().zip(sub2).enumerate().take(len) {
1200                        self.push_comma(&mut values.0, &mut values.1, i);
1201                        match arg1.kind() {
1202                            // At one point we'd like to elide all lifetimes here, they are
1203                            // irrelevant for all diagnostics that use this output.
1204                            //
1205                            //     Foo<'x, '_, Bar>
1206                            //     Foo<'y, '_, Qux>
1207                            //         ^^  ^^  --- type arguments are not elided
1208                            //         |   |
1209                            //         |   elided as they were the same
1210                            //         not elided, they were different, but irrelevant
1211                            //
1212                            // For bound lifetimes, keep the names of the lifetimes,
1213                            // even if they are the same so that it's clear what's happening
1214                            // if we have something like
1215                            //
1216                            // for<'r, 's> fn(Inv<'r>, Inv<'s>)
1217                            // for<'r> fn(Inv<'r>, Inv<'r>)
1218                            ty::GenericArgKind::Lifetime(l1) => {
1219                                let l1_str = lifetime_display(l1);
1220                                let l2 = arg2.expect_region();
1221                                let l2_str = lifetime_display(l2);
1222                                if l1 != l2 {
1223                                    values.0.push_highlighted(l1_str);
1224                                    values.1.push_highlighted(l2_str);
1225                                } else if l1.is_bound() || self.tcx.sess.opts.verbose {
1226                                    values.0.push_normal(l1_str);
1227                                    values.1.push_normal(l2_str);
1228                                } else {
1229                                    values.0.push_normal("'_");
1230                                    values.1.push_normal("'_");
1231                                }
1232                            }
1233                            ty::GenericArgKind::Type(ta1) => {
1234                                let ta2 = arg2.expect_ty();
1235                                if ta1 == ta2 && !self.tcx.sess.opts.verbose {
1236                                    values.0.push_normal("_");
1237                                    values.1.push_normal("_");
1238                                } else {
1239                                    recurse(ta1, ta2, &mut values);
1240                                }
1241                            }
1242                            // We're comparing two types with the same path, so we compare the type
1243                            // arguments for both. If they are the same, do not highlight and elide
1244                            // from the output.
1245                            //     Foo<_, Bar>
1246                            //     Foo<_, Qux>
1247                            //         ^ elided type as this type argument was the same in both sides
1248
1249                            // Do the same for const arguments, if they are equal, do not highlight and
1250                            // elide them from the output.
1251                            ty::GenericArgKind::Const(ca1) => {
1252                                let ca2 = arg2.expect_const();
1253                                maybe_highlight(ca1, ca2, &mut values, self.tcx);
1254                            }
1255                        }
1256                    }
1257
1258                    // Close the type argument bracket.
1259                    // Only draw `<...>` if there are arguments.
1260                    if len > 0 {
1261                        values.0.push_normal(">");
1262                        values.1.push_normal(">");
1263                    }
1264                    values
1265                } else {
1266                    // Check for case:
1267                    //     let x: Foo<Bar<Qux> = foo::<Bar<Qux>>();
1268                    //     Foo<Bar<Qux>
1269                    //         ------- this type argument is exactly the same as the other type
1270                    //     Bar<Qux>
1271                    if self.cmp_type_arg(
1272                        &mut values.0,
1273                        &mut values.1,
1274                        path1.clone(),
1275                        sub_no_defaults_1,
1276                        path2.clone(),
1277                        t2,
1278                    ) {
1279                        return values;
1280                    }
1281                    // Check for case:
1282                    //     let x: Bar<Qux> = y:<Foo<Bar<Qux>>>();
1283                    //     Bar<Qux>
1284                    //     Foo<Bar<Qux>>
1285                    //         ------- this type argument is exactly the same as the other type
1286                    if self.cmp_type_arg(
1287                        &mut values.1,
1288                        &mut values.0,
1289                        path2,
1290                        sub_no_defaults_2,
1291                        path1,
1292                        t1,
1293                    ) {
1294                        return values;
1295                    }
1296
1297                    // We can't find anything in common, highlight relevant part of type path.
1298                    //     let x: foo::bar::Baz<Qux> = y:<foo::bar::Bar<Zar>>();
1299                    //     foo::bar::Baz<Qux>
1300                    //     foo::bar::Bar<Zar>
1301                    //               -------- this part of the path is different
1302
1303                    let t1_str = t1.to_string();
1304                    let t2_str = t2.to_string();
1305                    let min_len = t1_str.len().min(t2_str.len());
1306
1307                    const SEPARATOR: &str = "::";
1308                    let separator_len = SEPARATOR.len();
1309                    let split_idx: usize =
1310                        iter::zip(t1_str.split(SEPARATOR), t2_str.split(SEPARATOR))
1311                            .take_while(|(mod1_str, mod2_str)| mod1_str == mod2_str)
1312                            .map(|(mod_str, _)| mod_str.len() + separator_len)
1313                            .sum();
1314
1315                    debug!(?separator_len, ?split_idx, ?min_len, "cmp");
1316
1317                    if split_idx >= min_len {
1318                        // paths are identical, highlight everything
1319                        (
1320                            DiagStyledString::highlighted(t1_str),
1321                            DiagStyledString::highlighted(t2_str),
1322                        )
1323                    } else {
1324                        let (common, uniq1) = t1_str.split_at(split_idx);
1325                        let (_, uniq2) = t2_str.split_at(split_idx);
1326                        debug!(?common, ?uniq1, ?uniq2, "cmp");
1327
1328                        values.0.push_normal(common);
1329                        values.0.push_highlighted(uniq1);
1330                        values.1.push_normal(common);
1331                        values.1.push_highlighted(uniq2);
1332
1333                        values
1334                    }
1335                }
1336            }
1337
1338            // When finding `&T != &T`, compare the references, then recurse into pointee type
1339            (&ty::Ref(r1, ref_ty1, mutbl1), &ty::Ref(r2, ref_ty2, mutbl2)) => {
1340                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1341                cmp_ty_refs(r1, mutbl1, r2, mutbl2, &mut values);
1342                recurse(ref_ty1, ref_ty2, &mut values);
1343                values
1344            }
1345            // When finding T != &T, highlight the borrow
1346            (&ty::Ref(r1, ref_ty1, mutbl1), _) => {
1347                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1348                push_ref(r1, mutbl1, &mut values.0);
1349                recurse(ref_ty1, t2, &mut values);
1350                values
1351            }
1352            (_, &ty::Ref(r2, ref_ty2, mutbl2)) => {
1353                let mut values = (DiagStyledString::new(), DiagStyledString::new());
1354                push_ref(r2, mutbl2, &mut values.1);
1355                recurse(t1, ref_ty2, &mut values);
1356                values
1357            }
1358
1359            // When encountering tuples of the same size, highlight only the differing types
1360            (&ty::Tuple(args1), &ty::Tuple(args2)) if args1.len() == args2.len() => {
1361                let mut values = (DiagStyledString::normal("("), DiagStyledString::normal("("));
1362                let len = args1.len();
1363                for (i, (left, right)) in args1.iter().zip(args2).enumerate() {
1364                    self.push_comma(&mut values.0, &mut values.1, i);
1365                    recurse(left, right, &mut values);
1366                }
1367                if len == 1 {
1368                    // Keep the output for single element tuples as `(ty,)`.
1369                    values.0.push_normal(",");
1370                    values.1.push_normal(",");
1371                }
1372                values.0.push_normal(")");
1373                values.1.push_normal(")");
1374                values
1375            }
1376
1377            (ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
1378                let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
1379                let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
1380                self.cmp_fn_sig(
1381                    &sig1,
1382                    Some((*did1, Some(args1))),
1383                    &sig2,
1384                    Some((*did2, Some(args2))),
1385                )
1386            }
1387
1388            (ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
1389                let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1);
1390                self.cmp_fn_sig(&sig1, Some((*did1, Some(args1))), &sig_tys2.with(*hdr2), None)
1391            }
1392
1393            (ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
1394                let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2);
1395                self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
1396            }
1397
1398            (ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
1399                self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig_tys2.with(*hdr2), None)
1400            }
1401
1402            _ => {
1403                let mut strs = (DiagStyledString::new(), DiagStyledString::new());
1404                maybe_highlight(t1, t2, &mut strs, self.tcx);
1405                strs
1406            }
1407        }
1408    }
1409
1410    /// Extend a type error with extra labels pointing at "non-trivial" types, like closures and
1411    /// the return type of `async fn`s.
1412    ///
1413    /// `secondary_span` gives the caller the opportunity to expand `diag` with a `span_label`.
1414    ///
1415    /// `swap_secondary_and_primary` is used to make projection errors in particular nicer by using
1416    /// the message in `secondary_span` as the primary label, and apply the message that would
1417    /// otherwise be used for the primary label on the `secondary_span` `Span`. This applies on
1418    /// E0271, like `tests/ui/issues/issue-39970.stderr`.
1419    #[instrument(level = "debug", skip(self, diag, secondary_span, prefer_label))]
1420    pub fn note_type_err(
1421        &self,
1422        diag: &mut Diag<'_>,
1423        cause: &ObligationCause<'tcx>,
1424        secondary_span: Option<(Span, Cow<'static, str>, bool)>,
1425        mut values: Option<ty::ParamEnvAnd<'tcx, ValuePairs<'tcx>>>,
1426        terr: TypeError<'tcx>,
1427        prefer_label: bool,
1428        override_span: Option<Span>,
1429    ) {
1430        // We use `override_span` when we want the error to point at a `Span` other than
1431        // `cause.span`. This is used in E0271, when a closure is passed in where the return type
1432        // isn't what was expected. We want to point at the closure's return type (or expression),
1433        // instead of the expression where the closure is passed as call argument.
1434        let span = override_span.unwrap_or(cause.span);
1435        // For some types of errors, expected-found does not make
1436        // sense, so just ignore the values we were given.
1437        if let TypeError::CyclicTy(_) = terr {
1438            values = None;
1439        }
1440        struct OpaqueTypesVisitor<'tcx> {
1441            types: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1442            expected: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1443            found: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1444            ignore_span: Span,
1445            tcx: TyCtxt<'tcx>,
1446        }
1447
1448        impl<'tcx> OpaqueTypesVisitor<'tcx> {
1449            fn visit_expected_found(
1450                tcx: TyCtxt<'tcx>,
1451                expected: impl TypeVisitable<TyCtxt<'tcx>>,
1452                found: impl TypeVisitable<TyCtxt<'tcx>>,
1453                ignore_span: Span,
1454            ) -> Self {
1455                let mut types_visitor = OpaqueTypesVisitor {
1456                    types: Default::default(),
1457                    expected: Default::default(),
1458                    found: Default::default(),
1459                    ignore_span,
1460                    tcx,
1461                };
1462                // The visitor puts all the relevant encountered types in `self.types`, but in
1463                // here we want to visit two separate types with no relation to each other, so we
1464                // move the results from `types` to `expected` or `found` as appropriate.
1465                expected.visit_with(&mut types_visitor);
1466                std::mem::swap(&mut types_visitor.expected, &mut types_visitor.types);
1467                found.visit_with(&mut types_visitor);
1468                std::mem::swap(&mut types_visitor.found, &mut types_visitor.types);
1469                types_visitor
1470            }
1471
1472            fn report(&self, err: &mut Diag<'_>) {
1473                self.add_labels_for_types(err, "expected", &self.expected);
1474                self.add_labels_for_types(err, "found", &self.found);
1475            }
1476
1477            fn add_labels_for_types(
1478                &self,
1479                err: &mut Diag<'_>,
1480                target: &str,
1481                types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
1482            ) {
1483                for (kind, values) in types.iter() {
1484                    let count = values.len();
1485                    for &sp in values {
1486                        err.span_label(
1487                            sp,
1488                            format!(
1489                                "{}{} {:#}{}",
1490                                if count == 1 { "the " } else { "one of the " },
1491                                target,
1492                                kind,
1493                                pluralize!(count),
1494                            ),
1495                        );
1496                    }
1497                }
1498            }
1499        }
1500
1501        impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypesVisitor<'tcx> {
1502            fn visit_ty(&mut self, t: Ty<'tcx>) {
1503                if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
1504                    let span = self.tcx.def_span(def_id);
1505                    // Avoid cluttering the output when the "found" and error span overlap:
1506                    //
1507                    // error[E0308]: mismatched types
1508                    //   --> $DIR/issue-20862.rs:2:5
1509                    //    |
1510                    // LL |     |y| x + y
1511                    //    |     ^^^^^^^^^
1512                    //    |     |
1513                    //    |     the found closure
1514                    //    |     expected `()`, found closure
1515                    //    |
1516                    //    = note: expected unit type `()`
1517                    //                 found closure `{closure@$DIR/issue-20862.rs:2:5: 2:14 x:_}`
1518                    //
1519                    // Also ignore opaque `Future`s that come from async fns.
1520                    if !self.ignore_span.overlaps(span)
1521                        && !span.is_desugaring(DesugaringKind::Async)
1522                    {
1523                        self.types.entry(kind).or_default().insert(span);
1524                    }
1525                }
1526                t.super_visit_with(self)
1527            }
1528        }
1529
1530        debug!("note_type_err(diag={:?})", diag);
1531        enum Mismatch<'a> {
1532            Variable(ty::error::ExpectedFound<Ty<'a>>),
1533            Fixed(&'static str),
1534        }
1535        let (expected_found, exp_found, is_simple_error, values, param_env) = match values {
1536            None => (None, Mismatch::Fixed("type"), false, None, None),
1537            Some(ty::ParamEnvAnd { param_env, value: values }) => {
1538                let mut values = self.resolve_vars_if_possible(values);
1539                if self.next_trait_solver() {
1540                    values = deeply_normalize_for_diagnostics(self, param_env, values);
1541                }
1542                let (is_simple_error, exp_found) = match values {
1543                    ValuePairs::Terms(ExpectedFound { expected, found }) => {
1544                        match (expected.kind(), found.kind()) {
1545                            (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
1546                                let is_simple_err =
1547                                    expected.is_simple_text() && found.is_simple_text();
1548                                OpaqueTypesVisitor::visit_expected_found(
1549                                    self.tcx, expected, found, span,
1550                                )
1551                                .report(diag);
1552
1553                                (
1554                                    is_simple_err,
1555                                    Mismatch::Variable(ExpectedFound { expected, found }),
1556                                )
1557                            }
1558                            (ty::TermKind::Const(_), ty::TermKind::Const(_)) => {
1559                                (false, Mismatch::Fixed("constant"))
1560                            }
1561                            _ => (false, Mismatch::Fixed("type")),
1562                        }
1563                    }
1564                    ValuePairs::PolySigs(ExpectedFound { expected, found }) => {
1565                        OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1566                            .report(diag);
1567                        (false, Mismatch::Fixed("signature"))
1568                    }
1569                    ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
1570                    ValuePairs::Aliases(ExpectedFound { expected, .. }) => {
1571                        (false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id)))
1572                    }
1573                    ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
1574                    ValuePairs::ExistentialTraitRef(_) => {
1575                        (false, Mismatch::Fixed("existential trait ref"))
1576                    }
1577                    ValuePairs::ExistentialProjection(_) => {
1578                        (false, Mismatch::Fixed("existential projection"))
1579                    }
1580                };
1581                let Some(vals) = self.values_str(values, cause, diag.long_ty_path()) else {
1582                    // Derived error. Cancel the emitter.
1583                    // NOTE(eddyb) this was `.cancel()`, but `diag`
1584                    // is borrowed, so we can't fully defuse it.
1585                    diag.downgrade_to_delayed_bug();
1586                    return;
1587                };
1588                (Some(vals), exp_found, is_simple_error, Some(values), Some(param_env))
1589            }
1590        };
1591
1592        let mut label_or_note = |span: Span, msg: Cow<'static, str>| {
1593            if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() {
1594                diag.span_label(span, msg);
1595            } else {
1596                diag.span_note(span, msg);
1597            }
1598        };
1599        if let Some((secondary_span, secondary_msg, swap_secondary_and_primary)) = secondary_span {
1600            if swap_secondary_and_primary {
1601                let terr = if let Some(infer::ValuePairs::Terms(ExpectedFound {
1602                    expected, ..
1603                })) = values
1604                {
1605                    Cow::from(format!("expected this to be `{expected}`"))
1606                } else {
1607                    terr.to_string(self.tcx)
1608                };
1609                label_or_note(secondary_span, terr);
1610                label_or_note(span, secondary_msg);
1611            } else {
1612                label_or_note(span, terr.to_string(self.tcx));
1613                label_or_note(secondary_span, secondary_msg);
1614            }
1615        } else if let Some(values) = values
1616            && let Some((e, f)) = values.ty()
1617            && let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) = terr
1618        {
1619            let e = self.tcx.erase_and_anonymize_regions(e);
1620            let f = self.tcx.erase_and_anonymize_regions(f);
1621            let mut expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1622            let mut found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1623            if let ObligationCauseCode::Pattern { span, .. } = cause.code()
1624                && let Some(span) = span
1625                && !span.from_expansion()
1626                && cause.span.from_expansion()
1627            {
1628                // When the type error comes from a macro like `assert!()`, and we are pointing at
1629                // code the user wrote the cause and effect are reversed as the expected value is
1630                // what the macro expanded to.
1631                (found, expected) = (expected, found);
1632            }
1633            if expected == found {
1634                label_or_note(span, terr.to_string(self.tcx));
1635            } else {
1636                label_or_note(span, Cow::from(format!("expected {expected}, found {found}")));
1637            }
1638        } else {
1639            label_or_note(span, terr.to_string(self.tcx));
1640        }
1641
1642        if self.check_and_note_conflicting_crates(diag, terr) {
1643            return;
1644        }
1645
1646        if let Some((expected, found)) = expected_found {
1647            let (expected_label, found_label, exp_found) = match exp_found {
1648                Mismatch::Variable(ef) => (
1649                    ef.expected.prefix_string(self.tcx),
1650                    ef.found.prefix_string(self.tcx),
1651                    Some(ef),
1652                ),
1653                Mismatch::Fixed(s) => (s.into(), s.into(), None),
1654            };
1655
1656            enum Similar<'tcx> {
1657                Adts { expected: ty::AdtDef<'tcx>, found: ty::AdtDef<'tcx> },
1658                PrimitiveFound { expected: ty::AdtDef<'tcx>, found: Ty<'tcx> },
1659                PrimitiveExpected { expected: Ty<'tcx>, found: ty::AdtDef<'tcx> },
1660            }
1661
1662            let similarity = |ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>| {
1663                if let ty::Adt(expected, _) = expected.kind()
1664                    && let Some(primitive) = found.primitive_symbol()
1665                {
1666                    let path = self.tcx.def_path(expected.did()).data;
1667                    let name = path.last().unwrap().data.get_opt_name();
1668                    if name == Some(primitive) {
1669                        return Some(Similar::PrimitiveFound { expected: *expected, found });
1670                    }
1671                } else if let Some(primitive) = expected.primitive_symbol()
1672                    && let ty::Adt(found, _) = found.kind()
1673                {
1674                    let path = self.tcx.def_path(found.did()).data;
1675                    let name = path.last().unwrap().data.get_opt_name();
1676                    if name == Some(primitive) {
1677                        return Some(Similar::PrimitiveExpected { expected, found: *found });
1678                    }
1679                } else if let ty::Adt(expected, _) = expected.kind()
1680                    && let ty::Adt(found, _) = found.kind()
1681                {
1682                    if !expected.did().is_local() && expected.did().krate == found.did().krate {
1683                        // Most likely types from different versions of the same crate
1684                        // are in play, in which case this message isn't so helpful.
1685                        // A "perhaps two different versions..." error is already emitted for that.
1686                        return None;
1687                    }
1688                    let f_path = self.tcx.def_path(found.did()).data;
1689                    let e_path = self.tcx.def_path(expected.did()).data;
1690
1691                    if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last())
1692                        && e_last == f_last
1693                    {
1694                        return Some(Similar::Adts { expected: *expected, found: *found });
1695                    }
1696                }
1697                None
1698            };
1699
1700            match terr {
1701                // If two types mismatch but have similar names, mention that specifically.
1702                TypeError::Sorts(values) if let Some(s) = similarity(values) => {
1703                    let diagnose_primitive =
1704                        |prim: Ty<'tcx>, shadow: Ty<'tcx>, defid: DefId, diag: &mut Diag<'_>| {
1705                            let name = shadow.sort_string(self.tcx);
1706                            diag.note(format!(
1707                                "`{prim}` and {name} have similar names, but are actually distinct types"
1708                            ));
1709                            diag.note(format!(
1710                                "one `{prim}` is a primitive defined by the language",
1711                            ));
1712                            let def_span = self.tcx.def_span(defid);
1713                            let msg = if defid.is_local() {
1714                                format!("the other {name} is defined in the current crate")
1715                            } else {
1716                                let crate_name = self.tcx.crate_name(defid.krate);
1717                                format!("the other {name} is defined in crate `{crate_name}`")
1718                            };
1719                            diag.span_note(def_span, msg);
1720                        };
1721
1722                    let diagnose_adts =
1723                        |expected_adt: ty::AdtDef<'tcx>,
1724                         found_adt: ty::AdtDef<'tcx>,
1725                         diag: &mut Diag<'_>| {
1726                            let found_name = values.found.sort_string(self.tcx);
1727                            let expected_name = values.expected.sort_string(self.tcx);
1728
1729                            let found_defid = found_adt.did();
1730                            let expected_defid = expected_adt.did();
1731
1732                            diag.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types"));
1733                            for (defid, name) in
1734                                [(found_defid, found_name), (expected_defid, expected_name)]
1735                            {
1736                                let def_span = self.tcx.def_span(defid);
1737
1738                                let msg = if found_defid.is_local() && expected_defid.is_local() {
1739                                    let module = self
1740                                        .tcx
1741                                        .parent_module_from_def_id(defid.expect_local())
1742                                        .to_def_id();
1743                                    let module_name =
1744                                        self.tcx.def_path(module).to_string_no_crate_verbose();
1745                                    format!(
1746                                        "{name} is defined in module `crate{module_name}` of the current crate"
1747                                    )
1748                                } else if defid.is_local() {
1749                                    format!("{name} is defined in the current crate")
1750                                } else {
1751                                    let crate_name = self.tcx.crate_name(defid.krate);
1752                                    format!("{name} is defined in crate `{crate_name}`")
1753                                };
1754                                diag.span_note(def_span, msg);
1755                            }
1756                        };
1757
1758                    match s {
1759                        Similar::Adts { expected, found } => diagnose_adts(expected, found, diag),
1760                        Similar::PrimitiveFound { expected, found: prim } => {
1761                            diagnose_primitive(prim, values.expected, expected.did(), diag)
1762                        }
1763                        Similar::PrimitiveExpected { expected: prim, found } => {
1764                            diagnose_primitive(prim, values.found, found.did(), diag)
1765                        }
1766                    }
1767                }
1768                TypeError::Sorts(values) => {
1769                    let extra = expected == found
1770                        // Ensure that we don't ever say something like
1771                        // expected `impl Trait` (opaque type `impl Trait`)
1772                        //    found `impl Trait` (opaque type `impl Trait`)
1773                        && values.expected.sort_string(self.tcx)
1774                            != values.found.sort_string(self.tcx);
1775                    let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
1776                        (true, ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. })) => {
1777                            let sm = self.tcx.sess.source_map();
1778                            let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1779                            DiagStyledString::normal(format!(
1780                                " (opaque type at <{}:{}:{}>)",
1781                                sm.filename_for_diagnostics(&pos.file.name),
1782                                pos.line,
1783                                pos.col.to_usize() + 1,
1784                            ))
1785                        }
1786                        (true, ty::Alias(ty::Projection, proj))
1787                            if self.tcx.is_impl_trait_in_trait(proj.def_id) =>
1788                        {
1789                            let sm = self.tcx.sess.source_map();
1790                            let pos = sm.lookup_char_pos(self.tcx.def_span(proj.def_id).lo());
1791                            DiagStyledString::normal(format!(
1792                                " (trait associated opaque type at <{}:{}:{}>)",
1793                                sm.filename_for_diagnostics(&pos.file.name),
1794                                pos.line,
1795                                pos.col.to_usize() + 1,
1796                            ))
1797                        }
1798                        (true, _) => {
1799                            let mut s = DiagStyledString::normal(" (");
1800                            s.push_highlighted(ty.sort_string(self.tcx));
1801                            s.push_normal(")");
1802                            s
1803                        }
1804                        (false, _) => DiagStyledString::normal(""),
1805                    };
1806                    if !(values.expected.is_simple_text() && values.found.is_simple_text())
1807                        || (exp_found.is_some_and(|ef| {
1808                            // This happens when the type error is a subset of the expectation,
1809                            // like when you have two references but one is `usize` and the other
1810                            // is `f32`. In those cases we still want to show the `note`. If the
1811                            // value from `ef` is `Infer(_)`, then we ignore it.
1812                            if !ef.expected.is_ty_or_numeric_infer() {
1813                                ef.expected != values.expected
1814                            } else if !ef.found.is_ty_or_numeric_infer() {
1815                                ef.found != values.found
1816                            } else {
1817                                false
1818                            }
1819                        }))
1820                    {
1821                        if let Some(ExpectedFound { found: found_ty, .. }) = exp_found
1822                            && !self.tcx.ty_is_opaque_future(found_ty)
1823                        {
1824                            // `Future` is a special opaque type that the compiler
1825                            // will try to hide in some case such as `async fn`, so
1826                            // to make an error more use friendly we will
1827                            // avoid to suggest a mismatch type with a
1828                            // type that the user usually are not using
1829                            // directly such as `impl Future<Output = u8>`.
1830                            diag.note_expected_found_extra(
1831                                &expected_label,
1832                                expected,
1833                                &found_label,
1834                                found,
1835                                sort_string(values.expected),
1836                                sort_string(values.found),
1837                            );
1838                        }
1839                    }
1840                }
1841                _ => {
1842                    debug!(
1843                        "note_type_err: exp_found={:?}, expected={:?} found={:?}",
1844                        exp_found, expected, found
1845                    );
1846                    if !is_simple_error || terr.must_include_note() {
1847                        diag.note_expected_found(&expected_label, expected, &found_label, found);
1848
1849                        if let Some(ty::Closure(_, args)) =
1850                            exp_found.map(|expected_type_found| expected_type_found.found.kind())
1851                        {
1852                            diag.highlighted_note(vec![
1853                                StringPart::normal("closure has signature: `"),
1854                                StringPart::highlighted(
1855                                    self.tcx
1856                                        .signature_unclosure(
1857                                            args.as_closure().sig(),
1858                                            rustc_hir::Safety::Safe,
1859                                        )
1860                                        .to_string(),
1861                                ),
1862                                StringPart::normal("`"),
1863                            ]);
1864                        }
1865                    }
1866                }
1867            }
1868        }
1869        let exp_found = match exp_found {
1870            Mismatch::Variable(exp_found) => Some(exp_found),
1871            Mismatch::Fixed(_) => None,
1872        };
1873        let exp_found = match terr {
1874            // `terr` has more accurate type information than `exp_found` in match expressions.
1875            ty::error::TypeError::Sorts(terr)
1876                if exp_found.is_some_and(|ef| terr.found == ef.found) =>
1877            {
1878                Some(terr)
1879            }
1880            _ => exp_found,
1881        };
1882        debug!("exp_found {:?} terr {:?} cause.code {:?}", exp_found, terr, cause.code());
1883        if let Some(exp_found) = exp_found {
1884            let should_suggest_fixes =
1885                if let ObligationCauseCode::Pattern { root_ty, .. } = cause.code() {
1886                    // Skip if the root_ty of the pattern is not the same as the expected_ty.
1887                    // If these types aren't equal then we've probably peeled off a layer of arrays.
1888                    self.same_type_modulo_infer(*root_ty, exp_found.expected)
1889                } else {
1890                    true
1891                };
1892
1893            // FIXME(#73154): For now, we do leak check when coercing function
1894            // pointers in typeck, instead of only during borrowck. This can lead
1895            // to these `RegionsInsufficientlyPolymorphic` errors that aren't helpful.
1896            if should_suggest_fixes
1897                && !matches!(terr, TypeError::RegionsInsufficientlyPolymorphic(..))
1898            {
1899                self.suggest_tuple_pattern(cause, &exp_found, diag);
1900                self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1901                self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1902                self.suggest_function_pointers(cause, span, &exp_found, terr, diag);
1903                self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
1904            }
1905        }
1906
1907        self.note_and_explain_type_err(diag, terr, cause, span, cause.body_id.to_def_id());
1908        if let Some(exp_found) = exp_found
1909            && let exp_found = TypeError::Sorts(exp_found)
1910            && exp_found != terr
1911        {
1912            self.note_and_explain_type_err(diag, exp_found, cause, span, cause.body_id.to_def_id());
1913        }
1914
1915        if let Some(ValuePairs::TraitRefs(exp_found)) = values
1916            && let ty::Closure(def_id, _) = exp_found.expected.self_ty().kind()
1917            && let Some(def_id) = def_id.as_local()
1918            && terr.involves_regions()
1919        {
1920            let span = self.tcx.def_span(def_id);
1921            diag.span_note(span, "this closure does not fulfill the lifetime requirements");
1922            self.suggest_for_all_lifetime_closure(
1923                span,
1924                self.tcx.hir_node_by_def_id(def_id),
1925                &exp_found,
1926                diag,
1927            );
1928        }
1929
1930        // It reads better to have the error origin as the final
1931        // thing.
1932        self.note_error_origin(diag, cause, exp_found, terr, param_env);
1933
1934        debug!(?diag);
1935    }
1936
1937    pub fn type_error_additional_suggestions(
1938        &self,
1939        trace: &TypeTrace<'tcx>,
1940        terr: TypeError<'tcx>,
1941        long_ty_path: &mut Option<PathBuf>,
1942    ) -> Vec<TypeErrorAdditionalDiags> {
1943        let mut suggestions = Vec::new();
1944        let span = trace.cause.span;
1945        let values = self.resolve_vars_if_possible(trace.values);
1946        if let Some((expected, found)) = values.ty() {
1947            match (expected.kind(), found.kind()) {
1948                (ty::Tuple(_), ty::Tuple(_)) => {}
1949                // If a tuple of length one was expected and the found expression has
1950                // parentheses around it, perhaps the user meant to write `(expr,)` to
1951                // build a tuple (issue #86100)
1952                (ty::Tuple(fields), _) => {
1953                    suggestions.extend(self.suggest_wrap_to_build_a_tuple(span, found, fields))
1954                }
1955                // If a byte was expected and the found expression is a char literal
1956                // containing a single ASCII character, perhaps the user meant to write `b'c'` to
1957                // specify a byte literal
1958                (ty::Uint(ty::UintTy::U8), ty::Char) => {
1959                    if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
1960                        && let Some(code) =
1961                            code.strip_prefix('\'').and_then(|s| s.strip_suffix('\''))
1962                        // forbid all Unicode escapes
1963                        && !code.starts_with("\\u")
1964                        // forbids literal Unicode characters beyond ASCII
1965                        && code.chars().next().is_some_and(|c| c.is_ascii())
1966                    {
1967                        suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral {
1968                            span,
1969                            code: escape_literal(code),
1970                        })
1971                    }
1972                }
1973                // If a character was expected and the found expression is a string literal
1974                // containing a single character, perhaps the user meant to write `'c'` to
1975                // specify a character literal (issue #92479)
1976                (ty::Char, ty::Ref(_, r, _)) if r.is_str() => {
1977                    if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
1978                        && let Some(code) = code.strip_prefix('"').and_then(|s| s.strip_suffix('"'))
1979                        && code.chars().count() == 1
1980                    {
1981                        suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral {
1982                            span,
1983                            code: escape_literal(code),
1984                        })
1985                    }
1986                }
1987                // If a string was expected and the found expression is a character literal,
1988                // perhaps the user meant to write `"s"` to specify a string literal.
1989                (ty::Ref(_, r, _), ty::Char) if r.is_str() => {
1990                    if let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span)
1991                        && code.starts_with("'")
1992                        && code.ends_with("'")
1993                    {
1994                        suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
1995                            start: span.with_hi(span.lo() + BytePos(1)),
1996                            end: span.with_lo(span.hi() - BytePos(1)),
1997                        });
1998                    }
1999                }
2000                // For code `if Some(..) = expr `, the type mismatch may be expected `bool` but found `()`,
2001                // we try to suggest to add the missing `let` for `if let Some(..) = expr`
2002                (ty::Bool, ty::Tuple(list)) => {
2003                    if list.len() == 0 {
2004                        suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
2005                    }
2006                }
2007                (ty::Array(_, _), ty::Array(_, _)) => {
2008                    suggestions.extend(self.suggest_specify_actual_length(terr, trace, span))
2009                }
2010                _ => {}
2011            }
2012        }
2013        let code = trace.cause.code();
2014        if let &(ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
2015            source,
2016            ..
2017        })
2018        | ObligationCauseCode::BlockTailExpression(.., source)) = code
2019            && let hir::MatchSource::TryDesugar(_) = source
2020            && let Some((expected_ty, found_ty)) =
2021                self.values_str(trace.values, &trace.cause, long_ty_path)
2022        {
2023            suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
2024                found: found_ty.content(),
2025                expected: expected_ty.content(),
2026            });
2027        }
2028        suggestions
2029    }
2030
2031    fn suggest_specify_actual_length(
2032        &self,
2033        terr: TypeError<'tcx>,
2034        trace: &TypeTrace<'tcx>,
2035        span: Span,
2036    ) -> Option<TypeErrorAdditionalDiags> {
2037        let TypeError::ArraySize(sz) = terr else {
2038            return None;
2039        };
2040        let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
2041            hir::Node::Item(hir::Item {
2042                kind: hir::ItemKind::Fn { body: body_id, .. }, ..
2043            }) => {
2044                let body = self.tcx.hir_body(*body_id);
2045                struct LetVisitor {
2046                    span: Span,
2047                }
2048                impl<'v> Visitor<'v> for LetVisitor {
2049                    type Result = ControlFlow<&'v hir::TyKind<'v>>;
2050                    fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) -> Self::Result {
2051                        // Find a local statement where the initializer has
2052                        // the same span as the error and the type is specified.
2053                        if let hir::Stmt {
2054                            kind:
2055                                hir::StmtKind::Let(hir::LetStmt {
2056                                    init: Some(hir::Expr { span: init_span, .. }),
2057                                    ty: Some(array_ty),
2058                                    ..
2059                                }),
2060                            ..
2061                        } = s
2062                            && init_span == &self.span
2063                        {
2064                            ControlFlow::Break(&array_ty.peel_refs().kind)
2065                        } else {
2066                            ControlFlow::Continue(())
2067                        }
2068                    }
2069                }
2070                LetVisitor { span }.visit_body(body).break_value()
2071            }
2072            hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, ty, _), .. }) => {
2073                Some(&ty.peel_refs().kind)
2074            }
2075            _ => None,
2076        };
2077        if let Some(tykind) = tykind
2078            && let hir::TyKind::Array(_, length_arg) = tykind
2079            && let Some(length_val) = sz.found.try_to_target_usize(self.tcx)
2080        {
2081            Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength {
2082                span: length_arg.span(),
2083                length: length_val,
2084            })
2085        } else {
2086            None
2087        }
2088    }
2089
2090    pub fn report_and_explain_type_error(
2091        &self,
2092        trace: TypeTrace<'tcx>,
2093        param_env: ty::ParamEnv<'tcx>,
2094        terr: TypeError<'tcx>,
2095    ) -> Diag<'a> {
2096        debug!("report_and_explain_type_error(trace={:?}, terr={:?})", trace, terr);
2097
2098        let span = trace.cause.span;
2099        let mut path = None;
2100        let failure_code = trace.cause.as_failure_code_diag(
2101            terr,
2102            span,
2103            self.type_error_additional_suggestions(&trace, terr, &mut path),
2104        );
2105        let mut diag = self.dcx().create_err(failure_code);
2106        *diag.long_ty_path() = path;
2107        self.note_type_err(
2108            &mut diag,
2109            &trace.cause,
2110            None,
2111            Some(param_env.and(trace.values)),
2112            terr,
2113            false,
2114            None,
2115        );
2116        diag
2117    }
2118
2119    fn suggest_wrap_to_build_a_tuple(
2120        &self,
2121        span: Span,
2122        found: Ty<'tcx>,
2123        expected_fields: &List<Ty<'tcx>>,
2124    ) -> Option<TypeErrorAdditionalDiags> {
2125        let [expected_tup_elem] = expected_fields[..] else { return None };
2126
2127        if !self.same_type_modulo_infer(expected_tup_elem, found) {
2128            return None;
2129        }
2130
2131        let Ok(code) = self.tcx.sess().source_map().span_to_snippet(span) else { return None };
2132
2133        let sugg = if code.starts_with('(') && code.ends_with(')') {
2134            let before_close = span.hi() - BytePos::from_u32(1);
2135            TypeErrorAdditionalDiags::TupleOnlyComma {
2136                span: span.with_hi(before_close).shrink_to_hi(),
2137            }
2138        } else {
2139            TypeErrorAdditionalDiags::TupleAlsoParentheses {
2140                span_low: span.shrink_to_lo(),
2141                span_high: span.shrink_to_hi(),
2142            }
2143        };
2144        Some(sugg)
2145    }
2146
2147    fn values_str(
2148        &self,
2149        values: ValuePairs<'tcx>,
2150        cause: &ObligationCause<'tcx>,
2151        long_ty_path: &mut Option<PathBuf>,
2152    ) -> Option<(DiagStyledString, DiagStyledString)> {
2153        match values {
2154            ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2155            ValuePairs::Terms(exp_found) => {
2156                self.expected_found_str_term(cause, exp_found, long_ty_path)
2157            }
2158            ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
2159            ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
2160            ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
2161            ValuePairs::TraitRefs(exp_found) => {
2162                let pretty_exp_found = ty::error::ExpectedFound {
2163                    expected: exp_found.expected.print_trait_sugared(),
2164                    found: exp_found.found.print_trait_sugared(),
2165                };
2166                match self.expected_found_str(pretty_exp_found) {
2167                    Some((expected, found)) if expected == found => {
2168                        self.expected_found_str(exp_found)
2169                    }
2170                    ret => ret,
2171                }
2172            }
2173            ValuePairs::PolySigs(exp_found) => {
2174                let exp_found = self.resolve_vars_if_possible(exp_found);
2175                if exp_found.references_error() {
2176                    return None;
2177                }
2178                let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
2179                    impl_item_def_id,
2180                    trait_item_def_id,
2181                    ..
2182                } = *cause.code()
2183                {
2184                    (Some((trait_item_def_id, None)), Some((impl_item_def_id.to_def_id(), None)))
2185                } else {
2186                    (None, None)
2187                };
2188
2189                Some(self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2))
2190            }
2191        }
2192    }
2193
2194    fn expected_found_str_term(
2195        &self,
2196        cause: &ObligationCause<'tcx>,
2197        exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
2198        long_ty_path: &mut Option<PathBuf>,
2199    ) -> Option<(DiagStyledString, DiagStyledString)> {
2200        let exp_found = self.resolve_vars_if_possible(exp_found);
2201        if exp_found.references_error() {
2202            return None;
2203        }
2204        let (mut expected, mut found) = (exp_found.expected, exp_found.found);
2205
2206        if let ObligationCauseCode::Pattern { span, .. } = cause.code()
2207            && let Some(span) = span
2208            && !span.from_expansion()
2209            && cause.span.from_expansion()
2210        {
2211            // When the type error comes from a macro like `assert!()`, and we are pointing at
2212            // code the user wrote, the cause and effect are reversed as the expected value is
2213            // what the macro expanded to. So if the user provided a `Type` when the macro is
2214            // written in such a way that a `bool` was expected, we want to print:
2215            // = note: expected `bool`
2216            //            found `Type`"
2217            // but as far as the compiler is concerned, after expansion what was expected was `Type`
2218            // = note: expected `Type`
2219            //            found `bool`"
2220            // so we reverse them here to match user expectation.
2221            (expected, found) = (found, expected);
2222        }
2223
2224        Some(match (expected.kind(), found.kind()) {
2225            (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
2226                let (mut exp, mut fnd) = self.cmp(expected, found);
2227                // Use the terminal width as the basis to determine when to compress the printed
2228                // out type, but give ourselves some leeway to avoid ending up creating a file for
2229                // a type that is somewhat shorter than the path we'd write to.
2230                let len = self.tcx.sess().diagnostic_width() + 40;
2231                let exp_s = exp.content();
2232                let fnd_s = fnd.content();
2233                if exp_s.len() > len {
2234                    let exp_s = self.tcx.short_string(expected, long_ty_path);
2235                    exp = DiagStyledString::highlighted(exp_s);
2236                }
2237                if fnd_s.len() > len {
2238                    let fnd_s = self.tcx.short_string(found, long_ty_path);
2239                    fnd = DiagStyledString::highlighted(fnd_s);
2240                }
2241                (exp, fnd)
2242            }
2243            _ => (
2244                DiagStyledString::highlighted(exp_found.expected.to_string()),
2245                DiagStyledString::highlighted(exp_found.found.to_string()),
2246            ),
2247        })
2248    }
2249
2250    /// Returns a string of the form "expected `{}`, found `{}`".
2251    fn expected_found_str<T: fmt::Display + TypeFoldable<TyCtxt<'tcx>>>(
2252        &self,
2253        exp_found: ty::error::ExpectedFound<T>,
2254    ) -> Option<(DiagStyledString, DiagStyledString)> {
2255        let exp_found = self.resolve_vars_if_possible(exp_found);
2256        if exp_found.references_error() {
2257            return None;
2258        }
2259
2260        Some((
2261            DiagStyledString::highlighted(exp_found.expected.to_string()),
2262            DiagStyledString::highlighted(exp_found.found.to_string()),
2263        ))
2264    }
2265
2266    /// Determine whether an error associated with the given span and definition
2267    /// should be treated as being caused by the implicit `From` conversion
2268    /// within `?` desugaring.
2269    pub fn is_try_conversion(&self, span: Span, trait_def_id: DefId) -> bool {
2270        span.is_desugaring(DesugaringKind::QuestionMark)
2271            && self.tcx.is_diagnostic_item(sym::From, trait_def_id)
2272    }
2273
2274    /// Structurally compares two types, modulo any inference variables.
2275    ///
2276    /// Returns `true` if two types are equal, or if one type is an inference variable compatible
2277    /// with the other type. A TyVar inference type is compatible with any type, and an IntVar or
2278    /// FloatVar inference type are compatible with themselves or their concrete types (Int and
2279    /// Float types, respectively). When comparing two ADTs, these rules apply recursively.
2280    pub fn same_type_modulo_infer<T: relate::Relate<TyCtxt<'tcx>>>(&self, a: T, b: T) -> bool {
2281        let (a, b) = self.resolve_vars_if_possible((a, b));
2282        SameTypeModuloInfer(self).relate(a, b).is_ok()
2283    }
2284}
2285
2286struct SameTypeModuloInfer<'a, 'tcx>(&'a InferCtxt<'tcx>);
2287
2288impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
2289    fn cx(&self) -> TyCtxt<'tcx> {
2290        self.0.tcx
2291    }
2292
2293    fn relate_with_variance<T: relate::Relate<TyCtxt<'tcx>>>(
2294        &mut self,
2295        _variance: ty::Variance,
2296        _info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
2297        a: T,
2298        b: T,
2299    ) -> relate::RelateResult<'tcx, T> {
2300        self.relate(a, b)
2301    }
2302
2303    fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
2304        match (a.kind(), b.kind()) {
2305            (ty::Int(_) | ty::Uint(_), ty::Infer(ty::InferTy::IntVar(_)))
2306            | (
2307                ty::Infer(ty::InferTy::IntVar(_)),
2308                ty::Int(_) | ty::Uint(_) | ty::Infer(ty::InferTy::IntVar(_)),
2309            )
2310            | (ty::Float(_), ty::Infer(ty::InferTy::FloatVar(_)))
2311            | (
2312                ty::Infer(ty::InferTy::FloatVar(_)),
2313                ty::Float(_) | ty::Infer(ty::InferTy::FloatVar(_)),
2314            )
2315            | (ty::Infer(ty::InferTy::TyVar(_)), _)
2316            | (_, ty::Infer(ty::InferTy::TyVar(_))) => Ok(a),
2317            (ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Mismatch),
2318            _ => relate::structurally_relate_tys(self, a, b),
2319        }
2320    }
2321
2322    fn regions(
2323        &mut self,
2324        a: ty::Region<'tcx>,
2325        b: ty::Region<'tcx>,
2326    ) -> RelateResult<'tcx, ty::Region<'tcx>> {
2327        if (a.is_var() && b.is_free())
2328            || (b.is_var() && a.is_free())
2329            || (a.is_var() && b.is_var())
2330            || a == b
2331        {
2332            Ok(a)
2333        } else {
2334            Err(TypeError::Mismatch)
2335        }
2336    }
2337
2338    fn binders<T>(
2339        &mut self,
2340        a: ty::Binder<'tcx, T>,
2341        b: ty::Binder<'tcx, T>,
2342    ) -> relate::RelateResult<'tcx, ty::Binder<'tcx, T>>
2343    where
2344        T: relate::Relate<TyCtxt<'tcx>>,
2345    {
2346        Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
2347    }
2348
2349    fn consts(
2350        &mut self,
2351        a: ty::Const<'tcx>,
2352        _b: ty::Const<'tcx>,
2353    ) -> relate::RelateResult<'tcx, ty::Const<'tcx>> {
2354        // FIXME(compiler-errors): This could at least do some first-order
2355        // relation
2356        Ok(a)
2357    }
2358}
2359
2360pub enum FailureCode {
2361    Error0317,
2362    Error0580,
2363    Error0308,
2364    Error0644,
2365}
2366
2367#[extension(pub trait ObligationCauseExt<'tcx>)]
2368impl<'tcx> ObligationCause<'tcx> {
2369    fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
2370        match self.code() {
2371            ObligationCauseCode::IfExpressionWithNoElse => FailureCode::Error0317,
2372            ObligationCauseCode::MainFunctionType => FailureCode::Error0580,
2373            ObligationCauseCode::CompareImplItem { .. }
2374            | ObligationCauseCode::MatchExpressionArm(_)
2375            | ObligationCauseCode::IfExpression { .. }
2376            | ObligationCauseCode::LetElse
2377            | ObligationCauseCode::LangFunctionType(_)
2378            | ObligationCauseCode::IntrinsicType
2379            | ObligationCauseCode::MethodReceiver => FailureCode::Error0308,
2380
2381            // In the case where we have no more specific thing to
2382            // say, also take a look at the error code, maybe we can
2383            // tailor to that.
2384            _ => match terr {
2385                TypeError::CyclicTy(ty)
2386                    if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2387                {
2388                    FailureCode::Error0644
2389                }
2390                TypeError::IntrinsicCast | TypeError::ForceInlineCast => FailureCode::Error0308,
2391                _ => FailureCode::Error0308,
2392            },
2393        }
2394    }
2395    fn as_failure_code_diag(
2396        &self,
2397        terr: TypeError<'tcx>,
2398        span: Span,
2399        subdiags: Vec<TypeErrorAdditionalDiags>,
2400    ) -> ObligationCauseFailureCode {
2401        match self.code() {
2402            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2403                ObligationCauseFailureCode::MethodCompat { span, subdiags }
2404            }
2405            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2406                ObligationCauseFailureCode::TypeCompat { span, subdiags }
2407            }
2408            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2409                ObligationCauseFailureCode::ConstCompat { span, subdiags }
2410            }
2411            ObligationCauseCode::BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => {
2412                ObligationCauseFailureCode::TryCompat { span, subdiags }
2413            }
2414            ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
2415                source, ..
2416            }) => match source {
2417                hir::MatchSource::TryDesugar(_) => {
2418                    ObligationCauseFailureCode::TryCompat { span, subdiags }
2419                }
2420                _ => ObligationCauseFailureCode::MatchCompat { span, subdiags },
2421            },
2422            ObligationCauseCode::IfExpression { .. } => {
2423                ObligationCauseFailureCode::IfElseDifferent { span, subdiags }
2424            }
2425            ObligationCauseCode::IfExpressionWithNoElse => {
2426                ObligationCauseFailureCode::NoElse { span }
2427            }
2428            ObligationCauseCode::LetElse => {
2429                ObligationCauseFailureCode::NoDiverge { span, subdiags }
2430            }
2431            ObligationCauseCode::MainFunctionType => {
2432                ObligationCauseFailureCode::FnMainCorrectType { span }
2433            }
2434            &ObligationCauseCode::LangFunctionType(lang_item_name) => {
2435                ObligationCauseFailureCode::FnLangCorrectType { span, subdiags, lang_item_name }
2436            }
2437            ObligationCauseCode::IntrinsicType => {
2438                ObligationCauseFailureCode::IntrinsicCorrectType { span, subdiags }
2439            }
2440            ObligationCauseCode::MethodReceiver => {
2441                ObligationCauseFailureCode::MethodCorrectType { span, subdiags }
2442            }
2443
2444            // In the case where we have no more specific thing to
2445            // say, also take a look at the error code, maybe we can
2446            // tailor to that.
2447            _ => match terr {
2448                TypeError::CyclicTy(ty)
2449                    if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2450                {
2451                    ObligationCauseFailureCode::ClosureSelfref { span }
2452                }
2453                TypeError::ForceInlineCast => {
2454                    ObligationCauseFailureCode::CantCoerceForceInline { span, subdiags }
2455                }
2456                TypeError::IntrinsicCast => {
2457                    ObligationCauseFailureCode::CantCoerceIntrinsic { span, subdiags }
2458                }
2459                _ => ObligationCauseFailureCode::Generic { span, subdiags },
2460            },
2461        }
2462    }
2463
2464    fn as_requirement_str(&self) -> &'static str {
2465        match self.code() {
2466            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2467                "method type is compatible with trait"
2468            }
2469            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2470                "associated type is compatible with trait"
2471            }
2472            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2473                "const is compatible with trait"
2474            }
2475            ObligationCauseCode::MainFunctionType => "`main` function has the correct type",
2476            ObligationCauseCode::LangFunctionType(_) => "lang item function has the correct type",
2477            ObligationCauseCode::IntrinsicType => "intrinsic has the correct type",
2478            ObligationCauseCode::MethodReceiver => "method receiver has the correct type",
2479            _ => "types are compatible",
2480        }
2481    }
2482}
2483
2484/// Newtype to allow implementing IntoDiagArg
2485pub struct ObligationCauseAsDiagArg<'tcx>(pub ObligationCause<'tcx>);
2486
2487impl IntoDiagArg for ObligationCauseAsDiagArg<'_> {
2488    fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
2489        let kind = match self.0.code() {
2490            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2491                "method_compat"
2492            }
2493            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2494                "type_compat"
2495            }
2496            ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2497                "const_compat"
2498            }
2499            ObligationCauseCode::MainFunctionType => "fn_main_correct_type",
2500            ObligationCauseCode::LangFunctionType(_) => "fn_lang_correct_type",
2501            ObligationCauseCode::IntrinsicType => "intrinsic_correct_type",
2502            ObligationCauseCode::MethodReceiver => "method_correct_type",
2503            _ => "other",
2504        }
2505        .into();
2506        rustc_errors::DiagArgValue::Str(kind)
2507    }
2508}
2509
2510/// This is a bare signal of what kind of type we're dealing with. `ty::TyKind` tracks
2511/// extra information about each type, but we only care about the category.
2512#[derive(Clone, Copy, PartialEq, Eq, Hash)]
2513pub enum TyCategory {
2514    Closure,
2515    Opaque,
2516    OpaqueFuture,
2517    Coroutine(hir::CoroutineKind),
2518    Foreign,
2519}
2520
2521impl fmt::Display for TyCategory {
2522    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2523        match self {
2524            Self::Closure => "closure".fmt(f),
2525            Self::Opaque => "opaque type".fmt(f),
2526            Self::OpaqueFuture => "future".fmt(f),
2527            Self::Coroutine(gk) => gk.fmt(f),
2528            Self::Foreign => "foreign type".fmt(f),
2529        }
2530    }
2531}
2532
2533impl TyCategory {
2534    pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
2535        match *ty.kind() {
2536            ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2537            ty::Alias(ty::Opaque, ty::AliasTy { def_id, .. }) => {
2538                let kind =
2539                    if tcx.ty_is_opaque_future(ty) { Self::OpaqueFuture } else { Self::Opaque };
2540                Some((kind, def_id))
2541            }
2542            ty::Coroutine(def_id, ..) => {
2543                Some((Self::Coroutine(tcx.coroutine_kind(def_id).unwrap()), def_id))
2544            }
2545            ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
2546            _ => None,
2547        }
2548    }
2549}