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