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