1use 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 as hir;
57use rustc_hir::def_id::{CRATE_DEF_ID, DefId};
58use rustc_hir::intravisit::Visitor;
59use rustc_hir::lang_items::LangItem;
60use rustc_infer::infer::DefineOpaqueTypes;
61use rustc_macros::extension;
62use rustc_middle::bug;
63use rustc_middle::traits::PatternOriginExpr;
64use rustc_middle::ty::error::{ExpectedFound, TypeError, TypeErrorToStringExt};
65use rustc_middle::ty::print::{PrintTraitRefExt as _, WrapBinderMode, with_forced_trimmed_paths};
66use rustc_middle::ty::{
67 self, List, ParamEnv, Region, Ty, TyCtxt, TypeFoldable, TypeSuperVisitable, TypeVisitable,
68 TypeVisitableExt, Unnormalized,
69};
70use rustc_span::{BytePos, DUMMY_SP, DesugaringKind, Pos, Span, sym};
71use tracing::{debug, instrument};
72
73use crate::error_reporting::TypeErrCtxt;
74use crate::error_reporting::traits::ambiguity::{
75 CandidateSource, compute_applicable_impls_for_diagnostics,
76};
77use crate::errors::{ObligationCauseFailureCode, TypeErrorAdditionalDiags};
78use crate::infer;
79use crate::infer::relate::{self, RelateResult, TypeRelation};
80use crate::infer::{InferCtxt, InferCtxtExt as _, TypeTrace, ValuePairs};
81use crate::solve::deeply_normalize_for_diagnostics;
82use crate::traits::{
83 MatchExpressionArmCause, Obligation, ObligationCause, ObligationCauseCode, specialization_graph,
84};
85
86mod note_and_explain;
87mod suggest;
88
89pub mod need_type_info;
90pub mod nice_region_error;
91pub mod region;
92
93fn escape_literal(s: &str) -> String {
96 let mut escaped = String::with_capacity(s.len());
97 let mut chrs = s.chars().peekable();
98 while let Some(first) = chrs.next() {
99 match (first, chrs.peek()) {
100 ('\\', Some(&delim @ '"') | Some(&delim @ '\'')) => {
101 escaped.push('\\');
102 escaped.push(delim);
103 chrs.next();
104 }
105 ('"' | '\'', _) => {
106 escaped.push('\\');
107 escaped.push(first)
108 }
109 (c, _) => escaped.push(c),
110 };
111 }
112 escaped
113}
114
115impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
116 pub fn type_error_struct_with_diag<M>(
127 &self,
128 sp: Span,
129 mk_diag: M,
130 actual_ty: Ty<'tcx>,
131 ) -> Diag<'a>
132 where
133 M: FnOnce(String) -> Diag<'a>,
134 {
135 let actual_ty = self.resolve_vars_if_possible(actual_ty);
136 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:136",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(136u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("type_error_struct_with_diag({0:?}, {1:?})",
sp, actual_ty) as &dyn Value))])
});
} else { ; }
};debug!("type_error_struct_with_diag({:?}, {:?})", sp, actual_ty);
137
138 let mut err = mk_diag(self.ty_to_string(actual_ty));
139
140 if actual_ty.references_error() {
142 err.downgrade_to_delayed_bug();
143 }
144
145 err
146 }
147
148 pub fn report_mismatched_types(
149 &self,
150 cause: &ObligationCause<'tcx>,
151 param_env: ty::ParamEnv<'tcx>,
152 expected: Ty<'tcx>,
153 actual: Ty<'tcx>,
154 err: TypeError<'tcx>,
155 ) -> Diag<'a> {
156 let mut diag = self.report_and_explain_type_error(
157 TypeTrace::types(cause, expected, actual),
158 param_env,
159 err,
160 );
161
162 self.suggest_param_env_shadowing(&mut diag, expected, actual, param_env);
163
164 diag
165 }
166
167 pub fn report_mismatched_consts(
168 &self,
169 cause: &ObligationCause<'tcx>,
170 param_env: ty::ParamEnv<'tcx>,
171 expected: ty::Const<'tcx>,
172 actual: ty::Const<'tcx>,
173 err: TypeError<'tcx>,
174 ) -> Diag<'a> {
175 self.report_and_explain_type_error(
176 TypeTrace::consts(cause, expected, actual),
177 param_env,
178 err,
179 )
180 }
181
182 pub fn get_impl_future_output_ty(&self, ty: Ty<'tcx>) -> Option<Ty<'tcx>> {
183 let (def_id, args) = match *ty.kind() {
184 ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, args, .. }) => (def_id, args),
185 ty::Alias(ty::AliasTy { kind, args, .. })
186 if self.tcx.is_impl_trait_in_trait(kind.def_id()) =>
187 {
188 (kind.def_id(), args)
189 }
190 _ => return None,
191 };
192
193 let future_trait = self.tcx.require_lang_item(LangItem::Future, DUMMY_SP);
194 let item_def_id = self.tcx.associated_item_def_ids(future_trait)[0];
195
196 self.tcx
197 .explicit_item_self_bounds(def_id)
198 .iter_instantiated_copied(self.tcx, args)
199 .map(Unnormalized::skip_norm_wip)
200 .find_map(|(predicate, _)| {
201 predicate
202 .kind()
203 .map_bound(|kind| match kind {
204 ty::ClauseKind::Projection(projection_predicate)
205 if projection_predicate.projection_term.def_id() == item_def_id =>
206 {
207 projection_predicate.term.as_type()
208 }
209 _ => None,
210 })
211 .no_bound_vars()
212 .flatten()
213 })
214 }
215
216 fn check_and_note_conflicting_crates(&self, err: &mut Diag<'_>, terr: TypeError<'tcx>) -> bool {
218 match terr {
219 TypeError::Sorts(ref exp_found) => {
220 if let (&ty::Adt(exp_adt, _), &ty::Adt(found_adt, _)) =
223 (exp_found.expected.kind(), exp_found.found.kind())
224 {
225 return self.check_same_definition_different_crate(
226 err,
227 exp_adt.did(),
228 [found_adt.did()].into_iter(),
229 |did| ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[self.tcx.def_span(did)]))vec![self.tcx.def_span(did)],
230 "type",
231 );
232 }
233 }
234 TypeError::Traits(ref exp_found) => {
235 return self.check_same_definition_different_crate(
236 err,
237 exp_found.expected,
238 [exp_found.found].into_iter(),
239 |did| ::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[self.tcx.def_span(did)]))vec![self.tcx.def_span(did)],
240 "trait",
241 );
242 }
243 _ => (), }
245 false
246 }
247
248 fn suggest_param_env_shadowing(
249 &self,
250 diag: &mut Diag<'_>,
251 expected: Ty<'tcx>,
252 found: Ty<'tcx>,
253 param_env: ty::ParamEnv<'tcx>,
254 ) {
255 let (alias, &def_id, concrete) = match (expected.kind(), found.kind()) {
256 (ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. }), _) => {
257 (proj, def_id, found)
258 }
259 (_, ty::Alias(proj @ ty::AliasTy { kind: ty::Projection { def_id }, .. })) => {
260 (proj, def_id, expected)
261 }
262 _ => return,
263 };
264
265 let tcx = self.tcx;
266
267 let trait_ref = alias.trait_ref(tcx);
268 let obligation =
269 Obligation::new(tcx, ObligationCause::dummy(), param_env, ty::Binder::dummy(trait_ref));
270
271 let applicable_impls = compute_applicable_impls_for_diagnostics(self.infcx, &obligation);
272
273 for candidate in applicable_impls {
274 let impl_def_id = match candidate {
275 CandidateSource::DefId(did) => did,
276 CandidateSource::ParamEnv(_) => continue,
277 };
278
279 let is_shadowed = self.infcx.probe(|_| {
280 let impl_substs = self.infcx.fresh_args_for_item(DUMMY_SP, impl_def_id);
281 let impl_trait_ref =
282 tcx.impl_trait_ref(impl_def_id).instantiate(tcx, impl_substs).skip_norm_wip();
283
284 let expected_trait_ref = alias.trait_ref(tcx);
285
286 if let Err(_) = self.infcx.at(&ObligationCause::dummy(), param_env).eq(
287 DefineOpaqueTypes::No,
288 expected_trait_ref,
289 impl_trait_ref,
290 ) {
291 return false;
292 }
293
294 let leaf_def = match specialization_graph::assoc_def(tcx, impl_def_id, def_id) {
295 Ok(leaf) => leaf,
296 Err(_) => return false,
297 };
298
299 let trait_def_id = alias.trait_def_id(tcx);
300 let rebased_args = alias.args.rebase_onto(tcx, trait_def_id, impl_substs);
301
302 if !leaf_def.item.defaultness(tcx).has_value() {
305 return false;
306 }
307
308 let impl_item_def_id = leaf_def.item.def_id;
309 if !tcx.check_args_compatible(impl_item_def_id, rebased_args) {
310 return false;
311 }
312 let impl_assoc_ty =
313 tcx.type_of(impl_item_def_id).instantiate(tcx, rebased_args).skip_norm_wip();
314
315 self.infcx.can_eq(param_env, impl_assoc_ty, concrete)
316 });
317
318 if is_shadowed {
319 diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the associated type `{0}` is defined as `{1}` in the implementation, but the where-bound `{2}` shadows this definition\nsee issue #152409 <https://github.com/rust-lang/rust/issues/152409> for more information",
self.ty_to_string(tcx.mk_ty_from_kind(ty::Alias(*alias))),
self.ty_to_string(concrete),
self.ty_to_string(alias.self_ty())))
})format!(
320 "the associated type `{}` is defined as `{}` in the implementation, \
321 but the where-bound `{}` shadows this definition\n\
322 see issue #152409 <https://github.com/rust-lang/rust/issues/152409> for more information",
323 self.ty_to_string(tcx.mk_ty_from_kind(ty::Alias(*alias))),
324 self.ty_to_string(concrete),
325 self.ty_to_string(alias.self_ty())
326 ));
327 return;
328 }
329 }
330 }
331
332 fn note_error_origin(
333 &self,
334 err: &mut Diag<'_>,
335 cause: &ObligationCause<'tcx>,
336 exp_found: Option<ty::error::ExpectedFound<Ty<'tcx>>>,
337 terr: TypeError<'tcx>,
338 param_env: Option<ParamEnv<'tcx>>,
339 ) {
340 match *cause.code() {
341 ObligationCauseCode::Pattern {
342 origin_expr: Some(origin_expr),
343 span: Some(span),
344 root_ty,
345 } => {
346 let expected_ty = self.resolve_vars_if_possible(root_ty);
347 if !#[allow(non_exhaustive_omitted_patterns)] match expected_ty.kind() {
ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_)) => true,
_ => false,
}matches!(
348 expected_ty.kind(),
349 ty::Infer(ty::InferTy::TyVar(_) | ty::InferTy::FreshTy(_))
350 ) {
351 if span.desugaring_kind() == Some(DesugaringKind::ForLoop)
353 && let ty::Adt(def, args) = expected_ty.kind()
354 && Some(def.did()) == self.tcx.get_diagnostic_item(sym::Option)
355 {
356 err.span_label(
357 span,
358 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this is an iterator with items of type `{0}`",
args.type_at(0)))
})format!("this is an iterator with items of type `{}`", args.type_at(0)),
359 );
360 } else if !span.overlaps(cause.span) {
361 let expected_ty = self.tcx.short_string(expected_ty, err.long_ty_path());
362 err.span_label(span, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this expression has type `{0}`",
expected_ty))
})format!("this expression has type `{expected_ty}`"));
363 }
364 }
365 if let Some(ty::error::ExpectedFound { found, .. }) = exp_found
366 && let Ok(mut peeled_snippet) =
367 self.tcx.sess.source_map().span_to_snippet(origin_expr.peeled_span)
368 {
369 if origin_expr.peeled_prefix_suggestion_parentheses {
374 peeled_snippet = ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("({0})", peeled_snippet))
})format!("({peeled_snippet})");
375 }
376
377 if expected_ty.boxed_ty() == Some(found) {
380 err.span_suggestion_verbose(
381 span,
382 "consider dereferencing the boxed value",
383 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("*{0}", peeled_snippet))
})format!("*{peeled_snippet}"),
384 Applicability::MachineApplicable,
385 );
386 } else if let Some(param_env) = param_env
387 && let Some(prefix) = self.should_deref_suggestion_on_mismatch(
388 param_env,
389 found,
390 expected_ty,
391 origin_expr,
392 )
393 {
394 err.span_suggestion_verbose(
395 span,
396 "consider dereferencing to access the inner value using the `Deref` trait",
397 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1}", prefix, peeled_snippet))
})format!("{prefix}{peeled_snippet}"),
398 Applicability::MaybeIncorrect,
399 );
400 }
401 }
402 }
403 ObligationCauseCode::Pattern { origin_expr: None, span: Some(span), .. } => {
404 err.span_label(span, "expected due to this");
405 }
406 ObligationCauseCode::BlockTailExpression(
407 _,
408 hir::MatchSource::TryDesugar(scrut_hir_id),
409 ) => {
410 if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
411 let scrut_expr = self.tcx.hir_expect_expr(scrut_hir_id);
412 let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
413 let arg_expr = args.first().expect("try desugaring call w/out arg");
414 self.typeck_results
415 .as_ref()
416 .and_then(|typeck_results| typeck_results.expr_ty_opt(arg_expr))
417 } else {
418 ::rustc_middle::util::bug::bug_fmt(format_args!("try desugaring w/out call expr as scrutinee"));bug!("try desugaring w/out call expr as scrutinee");
419 };
420
421 match scrut_ty {
422 Some(ty) if expected == ty => {
423 let source_map = self.tcx.sess.source_map();
424 err.span_suggestion(
425 source_map.end_point(cause.span),
426 "try removing this `?`",
427 "",
428 Applicability::MachineApplicable,
429 );
430 }
431 _ => {}
432 }
433 }
434 }
435 ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
436 arm_block_id,
437 arm_span,
438 arm_ty,
439 prior_arm_block_id,
440 prior_arm_span,
441 prior_arm_ty,
442 source,
443 ref prior_non_diverging_arms,
444 scrut_span,
445 expr_span,
446 ..
447 }) => match source {
448 hir::MatchSource::TryDesugar(scrut_hir_id) => {
449 if let Some(ty::error::ExpectedFound { expected, .. }) = exp_found {
450 let scrut_expr = self.tcx.hir_expect_expr(scrut_hir_id);
451 let scrut_ty = if let hir::ExprKind::Call(_, args) = &scrut_expr.kind {
452 let arg_expr = args.first().expect("try desugaring call w/out arg");
453 self.typeck_results
454 .as_ref()
455 .and_then(|typeck_results| typeck_results.expr_ty_opt(arg_expr))
456 } else {
457 ::rustc_middle::util::bug::bug_fmt(format_args!("try desugaring w/out call expr as scrutinee"));bug!("try desugaring w/out call expr as scrutinee");
458 };
459
460 match scrut_ty {
461 Some(ty) if expected == ty => {
462 let source_map = self.tcx.sess.source_map();
463 err.span_suggestion(
464 source_map.end_point(cause.span),
465 "try removing this `?`",
466 "",
467 Applicability::MachineApplicable,
468 );
469 }
470 _ => {}
471 }
472 }
473 }
474 _ => {
475 let t = self.resolve_vars_if_possible(match exp_found {
477 Some(ty::error::ExpectedFound { expected, .. }) => expected,
478 _ => prior_arm_ty,
479 });
480 let source_map = self.tcx.sess.source_map();
481 let mut any_multiline_arm = source_map.is_multiline(arm_span);
482 if prior_non_diverging_arms.len() <= 4 {
483 for sp in prior_non_diverging_arms {
484 any_multiline_arm |= source_map.is_multiline(*sp);
485 err.span_label(*sp, ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this is found to be of type `{0}`",
t))
})format!("this is found to be of type `{t}`"));
486 }
487 } else if let Some(sp) = prior_non_diverging_arms.last() {
488 any_multiline_arm |= source_map.is_multiline(*sp);
489 err.span_label(
490 *sp,
491 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("this and all prior arms are found to be of type `{0}`",
t))
})format!("this and all prior arms are found to be of type `{t}`"),
492 );
493 }
494 let outer = if any_multiline_arm || !source_map.is_multiline(expr_span) {
495 expr_span.shrink_to_lo().to(scrut_span)
498 } else {
499 expr_span
500 };
501 let msg = "`match` arms have incompatible types";
502 err.span_label(outer, msg);
503 if let Some(subdiag) = self.suggest_remove_semi_or_return_binding(
504 prior_arm_block_id,
505 prior_arm_ty,
506 prior_arm_span,
507 arm_block_id,
508 arm_ty,
509 arm_span,
510 ) {
511 err.subdiagnostic(subdiag);
512 }
513 }
514 },
515 ObligationCauseCode::IfExpression { expr_id, .. } => {
516 let hir::Node::Expr(&hir::Expr {
517 kind: hir::ExprKind::If(cond_expr, then_expr, Some(else_expr)),
518 span: expr_span,
519 ..
520 }) = self.tcx.hir_node(expr_id)
521 else {
522 return;
523 };
524 let then_span = self.find_block_span_from_hir_id(then_expr.hir_id);
525 let then_ty = self
526 .typeck_results
527 .as_ref()
528 .expect("if expression only expected inside FnCtxt")
529 .expr_ty(then_expr);
530 let else_span = self.find_block_span_from_hir_id(else_expr.hir_id);
531 let else_ty = self
532 .typeck_results
533 .as_ref()
534 .expect("if expression only expected inside FnCtxt")
535 .expr_ty(else_expr);
536 if let hir::ExprKind::If(_cond, _then, None) = else_expr.kind
537 && else_ty.is_unit()
538 {
539 err.note("`if` expressions without `else` evaluate to `()`");
541 err.note("consider adding an `else` block that evaluates to the expected type");
542 }
543 err.span_label(then_span, "expected because of this");
544
545 let outer_span = if self.tcx.sess.source_map().is_multiline(expr_span) {
546 if then_span.hi() == expr_span.hi() || else_span.hi() == expr_span.hi() {
547 Some(expr_span.shrink_to_lo().to(cond_expr.peel_drop_temps().span))
550 } else {
551 Some(expr_span)
552 }
553 } else {
554 None
555 };
556 if let Some(sp) = outer_span {
557 err.span_label(sp, "`if` and `else` have incompatible types");
558 }
559
560 let then_id = if let hir::ExprKind::Block(then_blk, _) = then_expr.kind {
561 then_blk.hir_id
562 } else {
563 then_expr.hir_id
564 };
565 let else_id = if let hir::ExprKind::Block(else_blk, _) = else_expr.kind {
566 else_blk.hir_id
567 } else {
568 else_expr.hir_id
569 };
570 if let Some(subdiag) = self.suggest_remove_semi_or_return_binding(
571 Some(then_id),
572 then_ty,
573 then_span,
574 Some(else_id),
575 else_ty,
576 else_span,
577 ) {
578 err.subdiagnostic(subdiag);
579 }
580 }
581 ObligationCauseCode::LetElse => {
582 err.help("try adding a diverging expression, such as `return` or `panic!(..)`");
583 err.help("...or use `match` instead of `let...else`");
584 }
585 _ => {
586 if let ObligationCauseCode::WhereClause(_, span)
587 | ObligationCauseCode::WhereClauseInExpr(_, span, ..) =
588 cause.code().peel_derives()
589 && !span.is_dummy()
590 && let TypeError::RegionsPlaceholderMismatch = terr
591 {
592 err.span_note(*span, "the lifetime requirement is introduced here");
593 }
594 }
595 }
596 }
597
598 fn should_deref_suggestion_on_mismatch(
601 &self,
602 param_env: ParamEnv<'tcx>,
603 deref_to: Ty<'tcx>,
604 deref_from: Ty<'tcx>,
605 origin_expr: PatternOriginExpr,
606 ) -> Option<String> {
607 let Some((num_derefs, (after_deref_ty, _))) = (self.autoderef_steps)(deref_from)
615 .into_iter()
616 .enumerate()
617 .find(|(_, (ty, _))| self.infcx.can_eq(param_env, *ty, deref_to))
618 else {
619 return None;
620 };
621
622 if num_derefs <= origin_expr.peeled_count {
623 return None;
624 }
625
626 let deref_part = "*".repeat(num_derefs - origin_expr.peeled_count);
627
628 if deref_from.is_ref() && !after_deref_ty.is_ref() {
631 Some(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("&{0}", deref_part))
})format!("&{deref_part}"))
632 } else {
633 Some(deref_part)
634 }
635 }
636
637 fn highlight_outer(
651 &self,
652 value: &mut DiagStyledString,
653 other_value: &mut DiagStyledString,
654 name: String,
655 args: &[ty::GenericArg<'tcx>],
656 pos: usize,
657 other_ty: Ty<'tcx>,
658 ) {
659 value.push_highlighted(name);
662
663 if args.is_empty() {
664 return;
665 }
666 value.push_highlighted("<");
667
668 for (i, arg) in args.iter().enumerate() {
669 if i > 0 {
670 value.push_normal(", ");
671 }
672
673 match arg.kind() {
674 ty::GenericArgKind::Lifetime(lt) => {
675 let s = lt.to_string();
676 value.push_normal(if s.is_empty() { "'_" } else { &s });
677 }
678 ty::GenericArgKind::Const(ct) => {
679 value.push_normal(ct.to_string());
680 }
681 ty::GenericArgKind::Type(type_arg) => {
684 if i == pos {
685 let values = self.cmp(type_arg, other_ty);
686 value.0.extend((values.0).0);
687 other_value.0.extend((values.1).0);
688 } else {
689 value.push_highlighted(type_arg.to_string());
690 }
691 }
692 }
693 }
694
695 value.push_highlighted(">");
696 }
697
698 fn cmp_type_arg(
719 &self,
720 t1_out: &mut DiagStyledString,
721 t2_out: &mut DiagStyledString,
722 path: String,
723 args: &'tcx [ty::GenericArg<'tcx>],
724 other_path: String,
725 other_ty: Ty<'tcx>,
726 ) -> bool {
727 for (i, arg) in args.iter().enumerate() {
728 if let Some(ta) = arg.as_type() {
729 if ta == other_ty {
730 self.highlight_outer(t1_out, t2_out, path, args, i, other_ty);
731 return true;
732 }
733 if let ty::Adt(def, _) = ta.kind() {
734 let path_ = self.tcx.def_path_str(def.did());
735 if path_ == other_path {
736 self.highlight_outer(t1_out, t2_out, path, args, i, other_ty);
737 return true;
738 }
739 }
740 }
741 }
742 false
743 }
744
745 fn push_comma(
747 &self,
748 value: &mut DiagStyledString,
749 other_value: &mut DiagStyledString,
750 pos: usize,
751 ) {
752 if pos > 0 {
753 value.push_normal(", ");
754 other_value.push_normal(", ");
755 }
756 }
757
758 fn cmp_fn_sig(
760 &self,
761 sig1: &ty::PolyFnSig<'tcx>,
762 fn_def1: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
763 sig2: &ty::PolyFnSig<'tcx>,
764 fn_def2: Option<(DefId, Option<&'tcx [ty::GenericArg<'tcx>]>)>,
765 ) -> (DiagStyledString, DiagStyledString) {
766 let sig1 = &(self.normalize_fn_sig)(Unnormalized::new_wip(*sig1));
767 let sig2 = &(self.normalize_fn_sig)(Unnormalized::new_wip(*sig2));
768
769 let get_lifetimes = |sig| {
770 use rustc_hir::def::Namespace;
771 let (sig, reg) = ty::print::FmtPrinter::new(self.tcx, Namespace::TypeNS)
772 .name_all_regions(sig, WrapBinderMode::ForAll)
773 .unwrap();
774 let lts: Vec<String> =
775 reg.into_items().map(|(_, kind)| kind.to_string()).into_sorted_stable_ord();
776 (if lts.is_empty() { String::new() } else { ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("for<{0}> ", lts.join(", ")))
})format!("for<{}> ", lts.join(", ")) }, sig)
777 };
778
779 let (lt1, sig1) = get_lifetimes(sig1);
780 let (lt2, sig2) = get_lifetimes(sig2);
781
782 let mut values =
784 (DiagStyledString::normal("".to_string()), DiagStyledString::normal("".to_string()));
785
786 let fn_item_prefix_and_safety = |fn_def, sig: ty::FnSig<'_>| match fn_def {
789 None => ("", sig.safety().prefix_str()),
790 Some((did, _)) => {
791 if self.tcx.codegen_fn_attrs(did).safe_target_features {
792 ("#[target_features] ", "")
793 } else {
794 ("", sig.safety().prefix_str())
795 }
796 }
797 };
798 let (prefix1, safety1) = fn_item_prefix_and_safety(fn_def1, sig1);
799 let (prefix2, safety2) = fn_item_prefix_and_safety(fn_def2, sig2);
800 values.0.push(prefix1, prefix1 != prefix2);
801 values.1.push(prefix2, prefix1 != prefix2);
802
803 let lifetime_diff = lt1 != lt2;
806 values.0.push(lt1, lifetime_diff);
807 values.1.push(lt2, lifetime_diff);
808
809 values.0.push(safety1, safety1 != safety2);
812 values.1.push(safety2, safety1 != safety2);
813
814 if sig1.abi() != ExternAbi::Rust {
817 values.0.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("extern {0} ", sig1.abi()))
})format!("extern {} ", sig1.abi()), sig1.abi() != sig2.abi());
818 }
819 if sig2.abi() != ExternAbi::Rust {
820 values.1.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("extern {0} ", sig2.abi()))
})format!("extern {} ", sig2.abi()), sig1.abi() != sig2.abi());
821 }
822
823 values.0.push_normal("fn(");
826 values.1.push_normal("fn(");
827
828 let len1 = sig1.inputs().len();
831 let len2 = sig2.inputs().len();
832 if len1 == len2 {
833 for (i, (l, r)) in iter::zip(sig1.inputs(), sig2.inputs()).enumerate() {
834 self.push_comma(&mut values.0, &mut values.1, i);
835 let (x1, x2) = self.cmp(*l, *r);
836 (values.0).0.extend(x1.0);
837 (values.1).0.extend(x2.0);
838 }
839 } else {
840 for (i, l) in sig1.inputs().iter().enumerate() {
841 values.0.push_highlighted(l.to_string());
842 if i != len1 - 1 {
843 values.0.push_highlighted(", ");
844 }
845 }
846 for (i, r) in sig2.inputs().iter().enumerate() {
847 values.1.push_highlighted(r.to_string());
848 if i != len2 - 1 {
849 values.1.push_highlighted(", ");
850 }
851 }
852 }
853
854 if sig1.c_variadic() {
855 if len1 > 0 {
856 values.0.push_normal(", ");
857 }
858 values.0.push("...", !sig2.c_variadic());
859 }
860 if sig2.c_variadic() {
861 if len2 > 0 {
862 values.1.push_normal(", ");
863 }
864 values.1.push("...", !sig1.c_variadic());
865 }
866
867 values.0.push_normal(")");
870 values.1.push_normal(")");
871
872 let output1 = sig1.output();
875 let output2 = sig2.output();
876 let (x1, x2) = self.cmp(output1, output2);
877 let output_diff = x1 != x2;
878 if !output1.is_unit() || output_diff {
879 values.0.push_normal(" -> ");
880 (values.0).0.extend(x1.0);
881 }
882 if !output2.is_unit() || output_diff {
883 values.1.push_normal(" -> ");
884 (values.1).0.extend(x2.0);
885 }
886
887 let fmt = |did, args| ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" {{{0}}}",
self.tcx.def_path_str_with_args(did, args)))
})format!(" {{{}}}", self.tcx.def_path_str_with_args(did, args));
888
889 match (fn_def1, fn_def2) {
890 (Some((fn_def1, Some(fn_args1))), Some((fn_def2, Some(fn_args2)))) => {
891 let path1 = fmt(fn_def1, fn_args1);
892 let path2 = fmt(fn_def2, fn_args2);
893 let same_path = path1 == path2;
894 values.0.push(path1, !same_path);
895 values.1.push(path2, !same_path);
896 }
897 (Some((fn_def1, Some(fn_args1))), None) => {
898 values.0.push_highlighted(fmt(fn_def1, fn_args1));
899 }
900 (None, Some((fn_def2, Some(fn_args2)))) => {
901 values.1.push_highlighted(fmt(fn_def2, fn_args2));
902 }
903 _ => {}
904 }
905
906 values
907 }
908
909 pub fn cmp_traits(
910 &self,
911 def_id1: DefId,
912 args1: &[ty::GenericArg<'tcx>],
913 def_id2: DefId,
914 args2: &[ty::GenericArg<'tcx>],
915 ) -> (DiagStyledString, DiagStyledString) {
916 let mut values = (DiagStyledString::new(), DiagStyledString::new());
917
918 if def_id1 != def_id2 {
919 values.0.push_highlighted(self.tcx.def_path_str(def_id1).as_str());
920 values.1.push_highlighted(self.tcx.def_path_str(def_id2).as_str());
921 } else {
922 values.0.push_normal(self.tcx.item_name(def_id1).as_str());
923 values.1.push_normal(self.tcx.item_name(def_id2).as_str());
924 }
925
926 if args1.len() != args2.len() {
927 let (pre, post) = if args1.len() > 0 { ("<", ">") } else { ("", "") };
928 values.0.push_normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}{2}",
args1.iter().map(|a|
a.to_string()).collect::<Vec<_>>().join(", "), pre, post))
})format!(
929 "{pre}{}{post}",
930 args1.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
931 ));
932 let (pre, post) = if args2.len() > 0 { ("<", ">") } else { ("", "") };
933 values.1.push_normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{1}{0}{2}",
args2.iter().map(|a|
a.to_string()).collect::<Vec<_>>().join(", "), pre, post))
})format!(
934 "{pre}{}{post}",
935 args2.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
936 ));
937 return values;
938 }
939
940 if args1.len() > 0 {
941 values.0.push_normal("<");
942 values.1.push_normal("<");
943 }
944 for (i, (a, b)) in std::iter::zip(args1, args2).enumerate() {
945 let a_str = a.to_string();
946 let b_str = b.to_string();
947 if let (Some(a), Some(b)) = (a.as_type(), b.as_type()) {
948 let (a, b) = self.cmp(a, b);
949 values.0.0.extend(a.0);
950 values.1.0.extend(b.0);
951 } else if a_str != b_str {
952 values.0.push_highlighted(a_str);
953 values.1.push_highlighted(b_str);
954 } else {
955 values.0.push_normal(a_str);
956 values.1.push_normal(b_str);
957 }
958 if i + 1 < args1.len() {
959 values.0.push_normal(", ");
960 values.1.push_normal(", ");
961 }
962 }
963 if args1.len() > 0 {
964 values.0.push_normal(">");
965 values.1.push_normal(">");
966 }
967 values
968 }
969
970 pub fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> (DiagStyledString, DiagStyledString) {
973 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:973",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(973u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("cmp(t1={0}, t1.kind={1:?}, t2={2}, t2.kind={3:?})",
t1, t1.kind(), t2, t2.kind()) as &dyn Value))])
});
} else { ; }
};debug!("cmp(t1={}, t1.kind={:?}, t2={}, t2.kind={:?})", t1, t1.kind(), t2, t2.kind());
974
975 let recurse = |t1, t2, values: &mut (DiagStyledString, DiagStyledString)| {
977 let (x1, x2) = self.cmp(t1, t2);
978 (values.0).0.extend(x1.0);
979 (values.1).0.extend(x2.0);
980 };
981
982 fn fmt_region<'tcx>(region: ty::Region<'tcx>) -> String {
983 let mut r = region.to_string();
984 if r == "'_" {
985 r.clear();
986 } else {
987 r.push(' ');
988 }
989 ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("&{0}", r)) })format!("&{r}")
990 }
991
992 fn push_ref<'tcx>(
993 region: ty::Region<'tcx>,
994 mutbl: hir::Mutability,
995 s: &mut DiagStyledString,
996 ) {
997 s.push_highlighted(fmt_region(region));
998 s.push_highlighted(mutbl.prefix_str());
999 }
1000
1001 fn maybe_highlight<T: Eq + ToString>(
1002 t1: T,
1003 t2: T,
1004 (buf1, buf2): &mut (DiagStyledString, DiagStyledString),
1005 tcx: TyCtxt<'_>,
1006 ) {
1007 let highlight = t1 != t2;
1008 let (t1, t2) = if highlight || tcx.sess.opts.verbose {
1009 (t1.to_string(), t2.to_string())
1010 } else {
1011 ("_".into(), "_".into())
1013 };
1014 buf1.push(t1, highlight);
1015 buf2.push(t2, highlight);
1016 }
1017
1018 fn cmp_ty_refs<'tcx>(
1019 r1: ty::Region<'tcx>,
1020 mut1: hir::Mutability,
1021 r2: ty::Region<'tcx>,
1022 mut2: hir::Mutability,
1023 ss: &mut (DiagStyledString, DiagStyledString),
1024 ) {
1025 let (r1, r2) = (fmt_region(r1), fmt_region(r2));
1026 if r1 != r2 {
1027 ss.0.push_highlighted(r1);
1028 ss.1.push_highlighted(r2);
1029 } else {
1030 ss.0.push_normal(r1);
1031 ss.1.push_normal(r2);
1032 }
1033
1034 if mut1 != mut2 {
1035 ss.0.push_highlighted(mut1.prefix_str());
1036 ss.1.push_highlighted(mut2.prefix_str());
1037 } else {
1038 ss.0.push_normal(mut1.prefix_str());
1039 ss.1.push_normal(mut2.prefix_str());
1040 }
1041 }
1042
1043 match (t1.kind(), t2.kind()) {
1045 (&ty::Adt(def1, sub1), &ty::Adt(def2, sub2)) => {
1046 let did1 = def1.did();
1047 let did2 = def2.did();
1048
1049 let generics1 = self.tcx.generics_of(did1);
1050 let generics2 = self.tcx.generics_of(did2);
1051
1052 let non_default_after_default = generics1
1053 .check_concrete_type_after_default(self.tcx, sub1)
1054 || generics2.check_concrete_type_after_default(self.tcx, sub2);
1055 let sub_no_defaults_1 = if non_default_after_default {
1056 generics1.own_args(sub1)
1057 } else {
1058 generics1.own_args_no_defaults(self.tcx, sub1)
1059 };
1060 let sub_no_defaults_2 = if non_default_after_default {
1061 generics2.own_args(sub2)
1062 } else {
1063 generics2.own_args_no_defaults(self.tcx, sub2)
1064 };
1065 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1066 let path1 = self.tcx.def_path_str(did1);
1067 let path2 = self.tcx.def_path_str(did2);
1068 if did1 == did2 {
1069 values.0.push_normal(path1);
1078 values.1.push_normal(path2);
1079
1080 let len1 = sub_no_defaults_1.len();
1083 let len2 = sub_no_defaults_2.len();
1084 let common_len = cmp::min(len1, len2);
1085 let remainder1 = &sub1[common_len..];
1086 let remainder2 = &sub2[common_len..];
1087 let common_default_params =
1088 iter::zip(remainder1.iter().rev(), remainder2.iter().rev())
1089 .filter(|(a, b)| a == b)
1090 .count();
1091 let len = sub1.len() - common_default_params;
1092
1093 if len > 0 {
1095 values.0.push_normal("<");
1096 values.1.push_normal("<");
1097 }
1098
1099 fn lifetime_display(lifetime: Region<'_>) -> String {
1100 let s = lifetime.to_string();
1101 if s.is_empty() { "'_".to_string() } else { s }
1102 }
1103
1104 for (i, (arg1, arg2)) in sub1.iter().zip(sub2).enumerate().take(len) {
1105 self.push_comma(&mut values.0, &mut values.1, i);
1106 match arg1.kind() {
1107 ty::GenericArgKind::Lifetime(l1) => {
1124 let l1_str = lifetime_display(l1);
1125 let l2 = arg2.expect_region();
1126 let l2_str = lifetime_display(l2);
1127 if l1 != l2 {
1128 values.0.push_highlighted(l1_str);
1129 values.1.push_highlighted(l2_str);
1130 } else if l1.is_bound() || self.tcx.sess.opts.verbose {
1131 values.0.push_normal(l1_str);
1132 values.1.push_normal(l2_str);
1133 } else {
1134 values.0.push_normal("'_");
1135 values.1.push_normal("'_");
1136 }
1137 }
1138 ty::GenericArgKind::Type(ta1) => {
1139 let ta2 = arg2.expect_ty();
1140 if ta1 == ta2 && !self.tcx.sess.opts.verbose {
1141 values.0.push_normal("_");
1142 values.1.push_normal("_");
1143 } else {
1144 recurse(ta1, ta2, &mut values);
1145 }
1146 }
1147 ty::GenericArgKind::Const(ca1) => {
1157 let ca2 = arg2.expect_const();
1158 maybe_highlight(ca1, ca2, &mut values, self.tcx);
1159 }
1160 }
1161 }
1162
1163 if len > 0 {
1166 values.0.push_normal(">");
1167 values.1.push_normal(">");
1168 }
1169 values
1170 } else {
1171 if self.cmp_type_arg(
1177 &mut values.0,
1178 &mut values.1,
1179 path1.clone(),
1180 sub_no_defaults_1,
1181 path2.clone(),
1182 t2,
1183 ) {
1184 return values;
1185 }
1186 if self.cmp_type_arg(
1192 &mut values.1,
1193 &mut values.0,
1194 path2,
1195 sub_no_defaults_2,
1196 path1,
1197 t1,
1198 ) {
1199 return values;
1200 }
1201
1202 let t1_str = t1.to_string();
1209 let t2_str = t2.to_string();
1210 let min_len = t1_str.len().min(t2_str.len());
1211
1212 const SEPARATOR: &str = "::";
1213 let separator_len = SEPARATOR.len();
1214 let split_idx: usize =
1215 iter::zip(t1_str.split(SEPARATOR), t2_str.split(SEPARATOR))
1216 .take_while(|(mod1_str, mod2_str)| mod1_str == mod2_str)
1217 .map(|(mod_str, _)| mod_str.len() + separator_len)
1218 .sum();
1219
1220 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1220",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1220u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message",
"separator_len", "split_idx", "min_len"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("cmp")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&separator_len)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&split_idx)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&min_len) as
&dyn Value))])
});
} else { ; }
};debug!(?separator_len, ?split_idx, ?min_len, "cmp");
1221
1222 if split_idx >= min_len {
1223 (
1225 DiagStyledString::highlighted(t1_str),
1226 DiagStyledString::highlighted(t2_str),
1227 )
1228 } else {
1229 let (common, uniq1) = t1_str.split_at(split_idx);
1230 let (_, uniq2) = t2_str.split_at(split_idx);
1231 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1231",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1231u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message", "common",
"uniq1", "uniq2"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("cmp")
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&common) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&uniq1) as
&dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&uniq2) as
&dyn Value))])
});
} else { ; }
};debug!(?common, ?uniq1, ?uniq2, "cmp");
1232
1233 values.0.push_normal(common);
1234 values.0.push_highlighted(uniq1);
1235 values.1.push_normal(common);
1236 values.1.push_highlighted(uniq2);
1237
1238 values
1239 }
1240 }
1241 }
1242
1243 (&ty::Ref(r1, ref_ty1, mutbl1), &ty::Ref(r2, ref_ty2, mutbl2)) => {
1245 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1246 cmp_ty_refs(r1, mutbl1, r2, mutbl2, &mut values);
1247 recurse(ref_ty1, ref_ty2, &mut values);
1248 values
1249 }
1250 (&ty::Ref(r1, ref_ty1, mutbl1), _) => {
1252 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1253 push_ref(r1, mutbl1, &mut values.0);
1254 recurse(ref_ty1, t2, &mut values);
1255 values
1256 }
1257 (_, &ty::Ref(r2, ref_ty2, mutbl2)) => {
1258 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1259 push_ref(r2, mutbl2, &mut values.1);
1260 recurse(t1, ref_ty2, &mut values);
1261 values
1262 }
1263
1264 (&ty::Tuple(args1), &ty::Tuple(args2)) if args1.len() == args2.len() => {
1266 let mut values = (DiagStyledString::normal("("), DiagStyledString::normal("("));
1267 let len = args1.len();
1268 for (i, (left, right)) in args1.iter().zip(args2).enumerate() {
1269 self.push_comma(&mut values.0, &mut values.1, i);
1270 recurse(left, right, &mut values);
1271 }
1272 if len == 1 {
1273 values.0.push_normal(",");
1275 values.1.push_normal(",");
1276 }
1277 values.0.push_normal(")");
1278 values.1.push_normal(")");
1279 values
1280 }
1281
1282 (ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
1283 let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1).skip_norm_wip();
1284 let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2).skip_norm_wip();
1285 self.cmp_fn_sig(
1286 &sig1,
1287 Some((*did1, Some(args1))),
1288 &sig2,
1289 Some((*did2, Some(args2))),
1290 )
1291 }
1292
1293 (ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
1294 let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1).skip_norm_wip();
1295 self.cmp_fn_sig(&sig1, Some((*did1, Some(args1))), &sig_tys2.with(*hdr2), None)
1296 }
1297
1298 (ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
1299 let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2).skip_norm_wip();
1300 self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
1301 }
1302
1303 (ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
1304 self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig_tys2.with(*hdr2), None)
1305 }
1306
1307 _ => {
1308 let mut strs = (DiagStyledString::new(), DiagStyledString::new());
1309 maybe_highlight(t1, t2, &mut strs, self.tcx);
1310 strs
1311 }
1312 }
1313 }
1314
1315 #[allow(clippy :: suspicious_else_formatting)]
{
let __tracing_attr_span;
let __tracing_attr_guard;
if ::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() ||
{ false } {
__tracing_attr_span =
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("note_type_err",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1324u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["cause", "values",
"terr", "override_span"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::SPAN)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let mut interest = ::tracing::subscriber::Interest::never();
if ::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{ interest = __CALLSITE.interest(); !interest.is_never() }
&&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest) {
let meta = __CALLSITE.metadata();
::tracing::Span::new(meta,
&{
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = meta.fields().iter();
meta.fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&cause)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&values)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&terr)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&::tracing::field::debug(&override_span)
as &dyn Value))])
})
} else {
let span =
::tracing::__macro_support::__disabled_span(__CALLSITE.metadata());
{};
span
}
};
__tracing_attr_guard = __tracing_attr_span.enter();
}
#[warn(clippy :: suspicious_else_formatting)]
{
#[allow(unknown_lints, unreachable_code, clippy ::
diverging_sub_expression, clippy :: empty_loop, clippy ::
let_unit_value, clippy :: let_with_type_underscore, clippy ::
needless_return, clippy :: unreachable)]
if false {
let __tracing_attr_fake_return: () = loop {};
return __tracing_attr_fake_return;
}
{
let span = override_span.unwrap_or(cause.span);
if let TypeError::CyclicTy(_) = terr { values = None; }
struct OpaqueTypesVisitor<'tcx> {
types: FxIndexMap<TyCategory, FxIndexSet<Span>>,
expected: FxIndexMap<TyCategory, FxIndexSet<Span>>,
found: FxIndexMap<TyCategory, FxIndexSet<Span>>,
ignore_span: Span,
tcx: TyCtxt<'tcx>,
}
impl<'tcx> OpaqueTypesVisitor<'tcx> {
fn visit_expected_found(tcx: TyCtxt<'tcx>,
expected: impl TypeVisitable<TyCtxt<'tcx>>,
found: impl TypeVisitable<TyCtxt<'tcx>>, ignore_span: Span)
-> Self {
let mut types_visitor =
OpaqueTypesVisitor {
types: Default::default(),
expected: Default::default(),
found: Default::default(),
ignore_span,
tcx,
};
expected.visit_with(&mut types_visitor);
std::mem::swap(&mut types_visitor.expected,
&mut types_visitor.types);
found.visit_with(&mut types_visitor);
std::mem::swap(&mut types_visitor.found,
&mut types_visitor.types);
types_visitor
}
fn report(&self, err: &mut Diag<'_>) {
self.add_labels_for_types(err, "expected", &self.expected);
self.add_labels_for_types(err, "found", &self.found);
}
fn add_labels_for_types(&self, err: &mut Diag<'_>,
target: &str,
types: &FxIndexMap<TyCategory, FxIndexSet<Span>>) {
for (kind, values) in types.iter() {
let count = values.len();
for &sp in values {
err.span_label(sp,
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0}{1} {2:#}{3}",
if count == 1 { "the " } else { "one of the " }, target,
kind, if count == 1 { "" } else { "s" }))
}));
}
}
}
}
impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for
OpaqueTypesVisitor<'tcx> {
fn visit_ty(&mut self, t: Ty<'tcx>) {
if let Some((kind, def_id)) =
TyCategory::from_ty(self.tcx, t) {
let span = self.tcx.def_span(def_id);
if !self.ignore_span.overlaps(span) &&
!span.is_desugaring(DesugaringKind::Async) {
self.types.entry(kind).or_default().insert(span);
}
}
t.super_visit_with(self)
}
}
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1435",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1435u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("note_type_err(diag={0:?})",
diag) as &dyn Value))])
});
} else { ; }
};
enum Mismatch<'a> {
Variable(ty::error::ExpectedFound<Ty<'a>>),
Fixed(&'static str),
}
let (expected_found, exp_found, is_simple_error, values,
param_env) =
match values {
None => (None, Mismatch::Fixed("type"), false, None, None),
Some(ty::ParamEnvAnd { param_env, value: values }) => {
let mut values = self.resolve_vars_if_possible(values);
if self.next_trait_solver() {
values =
deeply_normalize_for_diagnostics(self, param_env, values);
}
let (is_simple_error, exp_found) =
match values {
ValuePairs::Terms(ExpectedFound { expected, found }) => {
match (expected.kind(), found.kind()) {
(ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
let is_simple_err =
expected.is_simple_text() && found.is_simple_text();
OpaqueTypesVisitor::visit_expected_found(self.tcx, expected,
found, span).report(diag);
(is_simple_err,
Mismatch::Variable(ExpectedFound { expected, found }))
}
(ty::TermKind::Const(_), ty::TermKind::Const(_)) => {
(false, Mismatch::Fixed("constant"))
}
_ => (false, Mismatch::Fixed("type")),
}
}
ValuePairs::PolySigs(ExpectedFound { expected, found }) => {
OpaqueTypesVisitor::visit_expected_found(self.tcx, expected,
found, span).report(diag);
(false, Mismatch::Fixed("signature"))
}
ValuePairs::TraitRefs(_) =>
(false, Mismatch::Fixed("trait")),
ValuePairs::Aliases(ExpectedFound { expected, .. }) => {
(false,
Mismatch::Fixed(self.tcx.def_descr(expected.def_id())))
}
ValuePairs::Regions(_) =>
(false, Mismatch::Fixed("lifetime")),
ValuePairs::ExistentialTraitRef(_) => {
(false, Mismatch::Fixed("existential trait ref"))
}
ValuePairs::ExistentialProjection(_) => {
(false, Mismatch::Fixed("existential projection"))
}
};
let Some(vals) =
self.values_str(values, cause,
diag.long_ty_path()) else {
diag.downgrade_to_delayed_bug();
return;
};
(Some(vals), exp_found, is_simple_error, Some(values),
Some(param_env))
}
};
let mut label_or_note =
|span: Span, msg: Cow<'static, str>|
{
if (prefer_label && is_simple_error) ||
&[span] == diag.span.primary_spans() {
diag.span_label(span, msg);
} else { diag.span_note(span, msg); }
};
if let Some((secondary_span, secondary_msg,
swap_secondary_and_primary)) = secondary_span {
if swap_secondary_and_primary {
let terr =
if let Some(infer::ValuePairs::Terms(ExpectedFound {
expected, .. })) = values {
Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected this to be `{0}`",
expected))
}))
} else { terr.to_string(self.tcx) };
label_or_note(secondary_span, terr);
label_or_note(span, secondary_msg);
} else {
label_or_note(span, terr.to_string(self.tcx));
label_or_note(secondary_span, secondary_msg);
}
} else if let Some(values) = values &&
let Some((e, f)) = values.ty() &&
let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) =
terr {
let e = self.tcx.erase_and_anonymize_regions(e);
let f = self.tcx.erase_and_anonymize_regions(f);
let expected =
{
let _guard = ForceTrimmedGuard::new();
e.sort_string(self.tcx)
};
let found =
{
let _guard = ForceTrimmedGuard::new();
f.sort_string(self.tcx)
};
if expected == found {
label_or_note(span, terr.to_string(self.tcx));
} else {
label_or_note(span,
Cow::from(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("expected {0}, found {1}",
expected, found))
})));
}
} else { label_or_note(span, terr.to_string(self.tcx)); }
if let Some(param_env) = param_env {
self.note_field_shadowed_by_private_candidate_in_cause(diag,
cause, param_env);
}
if self.check_and_note_conflicting_crates(diag, terr) { return; }
if let Some((expected, found)) = expected_found {
let (expected_label, found_label, exp_found) =
match exp_found {
Mismatch::Variable(ef) =>
(ef.expected.prefix_string(self.tcx),
ef.found.prefix_string(self.tcx), Some(ef)),
Mismatch::Fixed(s) => (s.into(), s.into(), None),
};
enum Similar<'tcx> {
Adts {
expected: ty::AdtDef<'tcx>,
found: ty::AdtDef<'tcx>,
},
PrimitiveFound {
expected: ty::AdtDef<'tcx>,
found: Ty<'tcx>,
},
PrimitiveExpected {
expected: Ty<'tcx>,
found: ty::AdtDef<'tcx>,
},
}
let similarity =
|ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>|
{
if let ty::Adt(expected, _) = expected.kind() &&
let Some(primitive) = found.primitive_symbol() {
let path = self.tcx.def_path(expected.did()).data;
let name = path.last().unwrap().data.get_opt_name();
if name == Some(primitive) {
return Some(Similar::PrimitiveFound {
expected: *expected,
found,
});
}
} else if let Some(primitive) = expected.primitive_symbol()
&& let ty::Adt(found, _) = found.kind() {
let path = self.tcx.def_path(found.did()).data;
let name = path.last().unwrap().data.get_opt_name();
if name == Some(primitive) {
return Some(Similar::PrimitiveExpected {
expected,
found: *found,
});
}
} else if let ty::Adt(expected, _) = expected.kind() &&
let ty::Adt(found, _) = found.kind() {
if !expected.did().is_local() &&
expected.did().krate == found.did().krate {
return None;
}
let f_path = self.tcx.def_path(found.did()).data;
let e_path = self.tcx.def_path(expected.did()).data;
if let (Some(e_last), Some(f_last)) =
(e_path.last(), f_path.last()) && e_last == f_last {
return Some(Similar::Adts {
expected: *expected,
found: *found,
});
}
}
None
};
match terr {
TypeError::Sorts(values) if let Some(s) = similarity(values)
=> {
let diagnose_primitive =
|prim: Ty<'tcx>, shadow: Ty<'tcx>, defid: DefId,
diag: &mut Diag<'_>|
{
let name = shadow.sort_string(self.tcx);
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("`{0}` and {1} have similar names, but are actually distinct types",
prim, name))
}));
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("one `{0}` is a primitive defined by the language",
prim))
}));
let def_span = self.tcx.def_span(defid);
let msg =
if defid.is_local() {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the other {0} is defined in the current crate",
name))
})
} else {
let crate_name = self.tcx.crate_name(defid.krate);
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("the other {0} is defined in crate `{1}`",
name, crate_name))
})
};
diag.span_note(def_span, msg);
};
let diagnose_adts =
|expected_adt: ty::AdtDef<'tcx>,
found_adt: ty::AdtDef<'tcx>, diag: &mut Diag<'_>|
{
let found_name = values.found.sort_string(self.tcx);
let expected_name = values.expected.sort_string(self.tcx);
let found_defid = found_adt.did();
let expected_defid = expected_adt.did();
diag.note(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} and {1} have similar names, but are actually distinct types",
found_name, expected_name))
}));
for (defid, name) in
[(found_defid, found_name), (expected_defid, expected_name)]
{
let def_span = self.tcx.def_span(defid);
let msg =
if found_defid.is_local() && expected_defid.is_local() {
let module =
self.tcx.parent_module_from_def_id(defid.expect_local()).to_def_id();
let module_name =
self.tcx.def_path(module).to_string_no_crate_verbose();
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is defined in module `crate{1}` of the current crate",
name, module_name))
})
} else if defid.is_local() {
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is defined in the current crate",
name))
})
} else {
let crate_name = self.tcx.crate_name(defid.krate);
::alloc::__export::must_use({
::alloc::fmt::format(format_args!("{0} is defined in crate `{1}`",
name, crate_name))
})
};
diag.span_note(def_span, msg);
}
};
match s {
Similar::Adts { expected, found } =>
diagnose_adts(expected, found, diag),
Similar::PrimitiveFound { expected, found: prim } => {
diagnose_primitive(prim, values.expected, expected.did(),
diag)
}
Similar::PrimitiveExpected { expected: prim, found } => {
diagnose_primitive(prim, values.found, found.did(), diag)
}
}
}
TypeError::Sorts(values) => {
let extra =
expected == found &&
values.expected.sort_string(self.tcx) !=
values.found.sort_string(self.tcx);
let sort_string =
|ty: Ty<'tcx>|
match (extra, ty.kind()) {
(true,
ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }))
=> {
let sm = self.tcx.sess.source_map();
let pos =
sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
DiagStyledString::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" (opaque type at <{0}:{1}:{2}>)",
sm.filename_for_diagnostics(&pos.file.name), pos.line,
pos.col.to_usize() + 1))
}))
}
(true,
&ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, ..
})) if self.tcx.is_impl_trait_in_trait(def_id) => {
let sm = self.tcx.sess.source_map();
let pos =
sm.lookup_char_pos(self.tcx.def_span(def_id).lo());
DiagStyledString::normal(::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" (trait associated opaque type at <{0}:{1}:{2}>)",
sm.filename_for_diagnostics(&pos.file.name), pos.line,
pos.col.to_usize() + 1))
}))
}
(true, _) => {
let mut s = DiagStyledString::normal(" (");
s.push_highlighted(ty.sort_string(self.tcx));
s.push_normal(")");
s
}
(false, _) => DiagStyledString::normal(""),
};
if !(values.expected.is_simple_text() &&
values.found.is_simple_text()) ||
(exp_found.is_some_and(|ef|
{
if !ef.expected.is_ty_or_numeric_infer() {
ef.expected != values.expected
} else if !ef.found.is_ty_or_numeric_infer() {
ef.found != values.found
} else { false }
})) {
if let Some(ExpectedFound { found: found_ty, .. }) =
exp_found && !self.tcx.ty_is_opaque_future(found_ty) {
diag.note_expected_found_extra(&expected_label, expected,
&found_label, found, sort_string(values.expected),
sort_string(values.found));
}
}
}
_ => {
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1741",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1741u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("note_type_err: exp_found={0:?}, expected={1:?} found={2:?}",
exp_found, expected, found) as &dyn Value))])
});
} else { ; }
};
if !is_simple_error || terr.must_include_note() {
diag.note_expected_found(&expected_label, expected,
&found_label, found);
if let Some(ty::Closure(_, args)) =
exp_found.map(|expected_type_found|
expected_type_found.found.kind()) {
diag.highlighted_note(::alloc::boxed::box_assume_init_into_vec_unsafe(::alloc::intrinsics::write_box_via_move(::alloc::boxed::Box::new_uninit(),
[StringPart::normal("closure has signature: `"),
StringPart::highlighted(self.tcx.signature_unclosure(args.as_closure().sig(),
rustc_hir::Safety::Safe).to_string()),
StringPart::normal("`")])));
}
}
}
}
}
let exp_found =
match exp_found {
Mismatch::Variable(exp_found) => Some(exp_found),
Mismatch::Fixed(_) => None,
};
let exp_found =
match terr {
ty::error::TypeError::Sorts(terr) if
exp_found.is_some_and(|ef| terr.found == ef.found) => {
Some(terr)
}
_ => exp_found,
};
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1781",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1781u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("exp_found {0:?} terr {1:?} cause.code {2:?}",
exp_found, terr, cause.code()) as &dyn Value))])
});
} else { ; }
};
if let Some(exp_found) = exp_found {
let should_suggest_fixes =
if let ObligationCauseCode::Pattern { root_ty, .. } =
cause.code() {
self.same_type_modulo_infer(*root_ty, exp_found.expected)
} else { true };
if should_suggest_fixes &&
!#[allow(non_exhaustive_omitted_patterns)] match terr {
TypeError::RegionsInsufficientlyPolymorphic(..) => true,
_ => false,
} {
self.suggest_tuple_pattern(cause, &exp_found, diag);
self.suggest_accessing_field_where_appropriate(cause,
&exp_found, diag);
self.suggest_await_on_expect_found(cause, span, &exp_found,
diag);
self.suggest_function_pointers(cause, span, &exp_found,
terr, diag);
self.suggest_turning_stmt_into_expr(cause, &exp_found,
diag);
}
}
let body_owner_def_id =
(cause.body_id !=
CRATE_DEF_ID).then(|| cause.body_id.to_def_id());
self.note_and_explain_type_err(diag, terr, cause, span,
body_owner_def_id);
if let Some(exp_found) = exp_found &&
let exp_found = TypeError::Sorts(exp_found) &&
exp_found != terr {
self.note_and_explain_type_err(diag, exp_found, cause, span,
body_owner_def_id);
}
if let Some(ValuePairs::TraitRefs(exp_found)) = values &&
let ty::Closure(def_id, _) =
exp_found.expected.self_ty().kind() &&
let Some(def_id) = def_id.as_local() &&
terr.involves_regions() {
let span = self.tcx.def_span(def_id);
diag.span_note(span,
"this closure does not fulfill the lifetime requirements");
self.suggest_for_all_lifetime_closure(span,
self.tcx.hir_node_by_def_id(def_id), &exp_found, diag);
}
self.note_error_origin(diag, cause, exp_found, terr, param_env);
{
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1834",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1834u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["diag"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <=
::tracing::level_filters::STATIC_MAX_LEVEL &&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&debug(&diag) as
&dyn Value))])
});
} else { ; }
};
}
}
}#[instrument(level = "debug", skip(self, diag, secondary_span, prefer_label))]
1325 pub fn note_type_err(
1326 &self,
1327 diag: &mut Diag<'_>,
1328 cause: &ObligationCause<'tcx>,
1329 secondary_span: Option<(Span, Cow<'static, str>, bool)>,
1330 mut values: Option<ty::ParamEnvAnd<'tcx, ValuePairs<'tcx>>>,
1331 terr: TypeError<'tcx>,
1332 prefer_label: bool,
1333 override_span: Option<Span>,
1334 ) {
1335 let span = override_span.unwrap_or(cause.span);
1340 if let TypeError::CyclicTy(_) = terr {
1343 values = None;
1344 }
1345 struct OpaqueTypesVisitor<'tcx> {
1346 types: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1347 expected: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1348 found: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1349 ignore_span: Span,
1350 tcx: TyCtxt<'tcx>,
1351 }
1352
1353 impl<'tcx> OpaqueTypesVisitor<'tcx> {
1354 fn visit_expected_found(
1355 tcx: TyCtxt<'tcx>,
1356 expected: impl TypeVisitable<TyCtxt<'tcx>>,
1357 found: impl TypeVisitable<TyCtxt<'tcx>>,
1358 ignore_span: Span,
1359 ) -> Self {
1360 let mut types_visitor = OpaqueTypesVisitor {
1361 types: Default::default(),
1362 expected: Default::default(),
1363 found: Default::default(),
1364 ignore_span,
1365 tcx,
1366 };
1367 expected.visit_with(&mut types_visitor);
1371 std::mem::swap(&mut types_visitor.expected, &mut types_visitor.types);
1372 found.visit_with(&mut types_visitor);
1373 std::mem::swap(&mut types_visitor.found, &mut types_visitor.types);
1374 types_visitor
1375 }
1376
1377 fn report(&self, err: &mut Diag<'_>) {
1378 self.add_labels_for_types(err, "expected", &self.expected);
1379 self.add_labels_for_types(err, "found", &self.found);
1380 }
1381
1382 fn add_labels_for_types(
1383 &self,
1384 err: &mut Diag<'_>,
1385 target: &str,
1386 types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
1387 ) {
1388 for (kind, values) in types.iter() {
1389 let count = values.len();
1390 for &sp in values {
1391 err.span_label(
1392 sp,
1393 format!(
1394 "{}{} {:#}{}",
1395 if count == 1 { "the " } else { "one of the " },
1396 target,
1397 kind,
1398 pluralize!(count),
1399 ),
1400 );
1401 }
1402 }
1403 }
1404 }
1405
1406 impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypesVisitor<'tcx> {
1407 fn visit_ty(&mut self, t: Ty<'tcx>) {
1408 if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
1409 let span = self.tcx.def_span(def_id);
1410 if !self.ignore_span.overlaps(span)
1426 && !span.is_desugaring(DesugaringKind::Async)
1427 {
1428 self.types.entry(kind).or_default().insert(span);
1429 }
1430 }
1431 t.super_visit_with(self)
1432 }
1433 }
1434
1435 debug!("note_type_err(diag={:?})", diag);
1436 enum Mismatch<'a> {
1437 Variable(ty::error::ExpectedFound<Ty<'a>>),
1438 Fixed(&'static str),
1439 }
1440 let (expected_found, exp_found, is_simple_error, values, param_env) = match values {
1441 None => (None, Mismatch::Fixed("type"), false, None, None),
1442 Some(ty::ParamEnvAnd { param_env, value: values }) => {
1443 let mut values = self.resolve_vars_if_possible(values);
1444 if self.next_trait_solver() {
1445 values = deeply_normalize_for_diagnostics(self, param_env, values);
1446 }
1447 let (is_simple_error, exp_found) = match values {
1448 ValuePairs::Terms(ExpectedFound { expected, found }) => {
1449 match (expected.kind(), found.kind()) {
1450 (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
1451 let is_simple_err =
1452 expected.is_simple_text() && found.is_simple_text();
1453 OpaqueTypesVisitor::visit_expected_found(
1454 self.tcx, expected, found, span,
1455 )
1456 .report(diag);
1457
1458 (
1459 is_simple_err,
1460 Mismatch::Variable(ExpectedFound { expected, found }),
1461 )
1462 }
1463 (ty::TermKind::Const(_), ty::TermKind::Const(_)) => {
1464 (false, Mismatch::Fixed("constant"))
1465 }
1466 _ => (false, Mismatch::Fixed("type")),
1467 }
1468 }
1469 ValuePairs::PolySigs(ExpectedFound { expected, found }) => {
1470 OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1471 .report(diag);
1472 (false, Mismatch::Fixed("signature"))
1473 }
1474 ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
1475 ValuePairs::Aliases(ExpectedFound { expected, .. }) => {
1476 (false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id())))
1477 }
1478 ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
1479 ValuePairs::ExistentialTraitRef(_) => {
1480 (false, Mismatch::Fixed("existential trait ref"))
1481 }
1482 ValuePairs::ExistentialProjection(_) => {
1483 (false, Mismatch::Fixed("existential projection"))
1484 }
1485 };
1486 let Some(vals) = self.values_str(values, cause, diag.long_ty_path()) else {
1487 diag.downgrade_to_delayed_bug();
1491 return;
1492 };
1493 (Some(vals), exp_found, is_simple_error, Some(values), Some(param_env))
1494 }
1495 };
1496
1497 let mut label_or_note = |span: Span, msg: Cow<'static, str>| {
1498 if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() {
1499 diag.span_label(span, msg);
1500 } else {
1501 diag.span_note(span, msg);
1502 }
1503 };
1504 if let Some((secondary_span, secondary_msg, swap_secondary_and_primary)) = secondary_span {
1505 if swap_secondary_and_primary {
1506 let terr = if let Some(infer::ValuePairs::Terms(ExpectedFound {
1507 expected, ..
1508 })) = values
1509 {
1510 Cow::from(format!("expected this to be `{expected}`"))
1511 } else {
1512 terr.to_string(self.tcx)
1513 };
1514 label_or_note(secondary_span, terr);
1515 label_or_note(span, secondary_msg);
1516 } else {
1517 label_or_note(span, terr.to_string(self.tcx));
1518 label_or_note(secondary_span, secondary_msg);
1519 }
1520 } else if let Some(values) = values
1521 && let Some((e, f)) = values.ty()
1522 && let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) = terr
1523 {
1524 let e = self.tcx.erase_and_anonymize_regions(e);
1525 let f = self.tcx.erase_and_anonymize_regions(f);
1526 let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1527 let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1528 if expected == found {
1529 label_or_note(span, terr.to_string(self.tcx));
1530 } else {
1531 label_or_note(span, Cow::from(format!("expected {expected}, found {found}")));
1532 }
1533 } else {
1534 label_or_note(span, terr.to_string(self.tcx));
1535 }
1536
1537 if let Some(param_env) = param_env {
1538 self.note_field_shadowed_by_private_candidate_in_cause(diag, cause, param_env);
1539 }
1540
1541 if self.check_and_note_conflicting_crates(diag, terr) {
1542 return;
1543 }
1544
1545 if let Some((expected, found)) = expected_found {
1546 let (expected_label, found_label, exp_found) = match exp_found {
1547 Mismatch::Variable(ef) => (
1548 ef.expected.prefix_string(self.tcx),
1549 ef.found.prefix_string(self.tcx),
1550 Some(ef),
1551 ),
1552 Mismatch::Fixed(s) => (s.into(), s.into(), None),
1553 };
1554
1555 enum Similar<'tcx> {
1556 Adts { expected: ty::AdtDef<'tcx>, found: ty::AdtDef<'tcx> },
1557 PrimitiveFound { expected: ty::AdtDef<'tcx>, found: Ty<'tcx> },
1558 PrimitiveExpected { expected: Ty<'tcx>, found: ty::AdtDef<'tcx> },
1559 }
1560
1561 let similarity = |ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>| {
1562 if let ty::Adt(expected, _) = expected.kind()
1563 && let Some(primitive) = found.primitive_symbol()
1564 {
1565 let path = self.tcx.def_path(expected.did()).data;
1566 let name = path.last().unwrap().data.get_opt_name();
1567 if name == Some(primitive) {
1568 return Some(Similar::PrimitiveFound { expected: *expected, found });
1569 }
1570 } else if let Some(primitive) = expected.primitive_symbol()
1571 && let ty::Adt(found, _) = found.kind()
1572 {
1573 let path = self.tcx.def_path(found.did()).data;
1574 let name = path.last().unwrap().data.get_opt_name();
1575 if name == Some(primitive) {
1576 return Some(Similar::PrimitiveExpected { expected, found: *found });
1577 }
1578 } else if let ty::Adt(expected, _) = expected.kind()
1579 && let ty::Adt(found, _) = found.kind()
1580 {
1581 if !expected.did().is_local() && expected.did().krate == found.did().krate {
1582 return None;
1586 }
1587 let f_path = self.tcx.def_path(found.did()).data;
1588 let e_path = self.tcx.def_path(expected.did()).data;
1589
1590 if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last())
1591 && e_last == f_last
1592 {
1593 return Some(Similar::Adts { expected: *expected, found: *found });
1594 }
1595 }
1596 None
1597 };
1598
1599 match terr {
1600 TypeError::Sorts(values) if let Some(s) = similarity(values) => {
1602 let diagnose_primitive =
1603 |prim: Ty<'tcx>, shadow: Ty<'tcx>, defid: DefId, diag: &mut Diag<'_>| {
1604 let name = shadow.sort_string(self.tcx);
1605 diag.note(format!(
1606 "`{prim}` and {name} have similar names, but are actually distinct types"
1607 ));
1608 diag.note(format!(
1609 "one `{prim}` is a primitive defined by the language",
1610 ));
1611 let def_span = self.tcx.def_span(defid);
1612 let msg = if defid.is_local() {
1613 format!("the other {name} is defined in the current crate")
1614 } else {
1615 let crate_name = self.tcx.crate_name(defid.krate);
1616 format!("the other {name} is defined in crate `{crate_name}`")
1617 };
1618 diag.span_note(def_span, msg);
1619 };
1620
1621 let diagnose_adts =
1622 |expected_adt: ty::AdtDef<'tcx>,
1623 found_adt: ty::AdtDef<'tcx>,
1624 diag: &mut Diag<'_>| {
1625 let found_name = values.found.sort_string(self.tcx);
1626 let expected_name = values.expected.sort_string(self.tcx);
1627
1628 let found_defid = found_adt.did();
1629 let expected_defid = expected_adt.did();
1630
1631 diag.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types"));
1632 for (defid, name) in
1633 [(found_defid, found_name), (expected_defid, expected_name)]
1634 {
1635 let def_span = self.tcx.def_span(defid);
1636
1637 let msg = if found_defid.is_local() && expected_defid.is_local() {
1638 let module = self
1639 .tcx
1640 .parent_module_from_def_id(defid.expect_local())
1641 .to_def_id();
1642 let module_name =
1643 self.tcx.def_path(module).to_string_no_crate_verbose();
1644 format!(
1645 "{name} is defined in module `crate{module_name}` of the current crate"
1646 )
1647 } else if defid.is_local() {
1648 format!("{name} is defined in the current crate")
1649 } else {
1650 let crate_name = self.tcx.crate_name(defid.krate);
1651 format!("{name} is defined in crate `{crate_name}`")
1652 };
1653 diag.span_note(def_span, msg);
1654 }
1655 };
1656
1657 match s {
1658 Similar::Adts { expected, found } => diagnose_adts(expected, found, diag),
1659 Similar::PrimitiveFound { expected, found: prim } => {
1660 diagnose_primitive(prim, values.expected, expected.did(), diag)
1661 }
1662 Similar::PrimitiveExpected { expected: prim, found } => {
1663 diagnose_primitive(prim, values.found, found.did(), diag)
1664 }
1665 }
1666 }
1667 TypeError::Sorts(values) => {
1668 let extra = expected == found
1669 && values.expected.sort_string(self.tcx)
1673 != values.found.sort_string(self.tcx);
1674 let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
1675 (true, ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. })) => {
1676 let sm = self.tcx.sess.source_map();
1677 let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1678 DiagStyledString::normal(format!(
1679 " (opaque type at <{}:{}:{}>)",
1680 sm.filename_for_diagnostics(&pos.file.name),
1681 pos.line,
1682 pos.col.to_usize() + 1,
1683 ))
1684 }
1685 (true, &ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, .. }))
1686 if self.tcx.is_impl_trait_in_trait(def_id) =>
1687 {
1688 let sm = self.tcx.sess.source_map();
1689 let pos = sm.lookup_char_pos(self.tcx.def_span(def_id).lo());
1690 DiagStyledString::normal(format!(
1691 " (trait associated opaque type at <{}:{}:{}>)",
1692 sm.filename_for_diagnostics(&pos.file.name),
1693 pos.line,
1694 pos.col.to_usize() + 1,
1695 ))
1696 }
1697 (true, _) => {
1698 let mut s = DiagStyledString::normal(" (");
1699 s.push_highlighted(ty.sort_string(self.tcx));
1700 s.push_normal(")");
1701 s
1702 }
1703 (false, _) => DiagStyledString::normal(""),
1704 };
1705 if !(values.expected.is_simple_text() && values.found.is_simple_text())
1706 || (exp_found.is_some_and(|ef| {
1707 if !ef.expected.is_ty_or_numeric_infer() {
1712 ef.expected != values.expected
1713 } else if !ef.found.is_ty_or_numeric_infer() {
1714 ef.found != values.found
1715 } else {
1716 false
1717 }
1718 }))
1719 {
1720 if let Some(ExpectedFound { found: found_ty, .. }) = exp_found
1721 && !self.tcx.ty_is_opaque_future(found_ty)
1722 {
1723 diag.note_expected_found_extra(
1730 &expected_label,
1731 expected,
1732 &found_label,
1733 found,
1734 sort_string(values.expected),
1735 sort_string(values.found),
1736 );
1737 }
1738 }
1739 }
1740 _ => {
1741 debug!(
1742 "note_type_err: exp_found={:?}, expected={:?} found={:?}",
1743 exp_found, expected, found
1744 );
1745 if !is_simple_error || terr.must_include_note() {
1746 diag.note_expected_found(&expected_label, expected, &found_label, found);
1747
1748 if let Some(ty::Closure(_, args)) =
1749 exp_found.map(|expected_type_found| expected_type_found.found.kind())
1750 {
1751 diag.highlighted_note(vec![
1752 StringPart::normal("closure has signature: `"),
1753 StringPart::highlighted(
1754 self.tcx
1755 .signature_unclosure(
1756 args.as_closure().sig(),
1757 rustc_hir::Safety::Safe,
1758 )
1759 .to_string(),
1760 ),
1761 StringPart::normal("`"),
1762 ]);
1763 }
1764 }
1765 }
1766 }
1767 }
1768 let exp_found = match exp_found {
1769 Mismatch::Variable(exp_found) => Some(exp_found),
1770 Mismatch::Fixed(_) => None,
1771 };
1772 let exp_found = match terr {
1773 ty::error::TypeError::Sorts(terr)
1775 if exp_found.is_some_and(|ef| terr.found == ef.found) =>
1776 {
1777 Some(terr)
1778 }
1779 _ => exp_found,
1780 };
1781 debug!("exp_found {:?} terr {:?} cause.code {:?}", exp_found, terr, cause.code());
1782 if let Some(exp_found) = exp_found {
1783 let should_suggest_fixes =
1784 if let ObligationCauseCode::Pattern { root_ty, .. } = cause.code() {
1785 self.same_type_modulo_infer(*root_ty, exp_found.expected)
1788 } else {
1789 true
1790 };
1791
1792 if should_suggest_fixes
1796 && !matches!(terr, TypeError::RegionsInsufficientlyPolymorphic(..))
1797 {
1798 self.suggest_tuple_pattern(cause, &exp_found, diag);
1799 self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1800 self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1801 self.suggest_function_pointers(cause, span, &exp_found, terr, diag);
1802 self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
1803 }
1804 }
1805
1806 let body_owner_def_id = (cause.body_id != CRATE_DEF_ID).then(|| cause.body_id.to_def_id());
1807 self.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id);
1808 if let Some(exp_found) = exp_found
1809 && let exp_found = TypeError::Sorts(exp_found)
1810 && exp_found != terr
1811 {
1812 self.note_and_explain_type_err(diag, exp_found, cause, span, body_owner_def_id);
1813 }
1814
1815 if let Some(ValuePairs::TraitRefs(exp_found)) = values
1816 && let ty::Closure(def_id, _) = exp_found.expected.self_ty().kind()
1817 && let Some(def_id) = def_id.as_local()
1818 && terr.involves_regions()
1819 {
1820 let span = self.tcx.def_span(def_id);
1821 diag.span_note(span, "this closure does not fulfill the lifetime requirements");
1822 self.suggest_for_all_lifetime_closure(
1823 span,
1824 self.tcx.hir_node_by_def_id(def_id),
1825 &exp_found,
1826 diag,
1827 );
1828 }
1829
1830 self.note_error_origin(diag, cause, exp_found, terr, param_env);
1833
1834 debug!(?diag);
1835 }
1836
1837 pub(crate) fn type_error_additional_suggestions(
1838 &self,
1839 trace: &TypeTrace<'tcx>,
1840 terr: TypeError<'tcx>,
1841 long_ty_path: &mut Option<PathBuf>,
1842 ) -> Vec<TypeErrorAdditionalDiags> {
1843 let mut suggestions = Vec::new();
1844 let span = trace.cause.span;
1845 let values = self.resolve_vars_if_possible(trace.values);
1846 if let Some((expected, found)) = values.ty() {
1847 match (expected.kind(), found.kind()) {
1848 (ty::Tuple(_), ty::Tuple(_)) => {}
1849 (ty::Tuple(fields), _) => {
1853 suggestions.extend(self.suggest_wrap_to_build_a_tuple(span, found, fields))
1854 }
1855 (ty::Uint(ty::UintTy::U8), ty::Char) => {
1859 if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1860 && let Some(code) = code.strip_circumfix('\'', '\'')
1861 && !code.starts_with("\\u")
1863 && code.chars().next().is_some_and(|c| c.is_ascii())
1865 {
1866 suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral {
1867 span,
1868 code: escape_literal(code),
1869 })
1870 }
1871 }
1872 (ty::Char, ty::Ref(_, r, _)) if r.is_str() => {
1876 if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1877 && let Some(code) = code.strip_circumfix('"', '"')
1878 && code.chars().count() == 1
1879 {
1880 suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral {
1881 span,
1882 code: escape_literal(code),
1883 })
1884 }
1885 }
1886 (ty::Ref(_, r, _), ty::Char) if r.is_str() => {
1889 if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1890 && code.starts_with("'")
1891 && code.ends_with("'")
1892 {
1893 suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
1894 start: span.with_hi(span.lo() + BytePos(1)),
1895 end: span.with_lo(span.hi() - BytePos(1)),
1896 });
1897 }
1898 }
1899 (ty::Bool, ty::Tuple(list)) => {
1902 if list.len() == 0 {
1903 suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
1904 }
1905 }
1906 (ty::Array(_, _), ty::Array(_, _)) => {
1907 suggestions.extend(self.suggest_specify_actual_length(terr, trace, span))
1908 }
1909 _ => {}
1910 }
1911 }
1912 let code = trace.cause.code();
1913 if let &(ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
1914 source,
1915 ..
1916 })
1917 | ObligationCauseCode::BlockTailExpression(.., source)) = code
1918 && let hir::MatchSource::TryDesugar(_) = source
1919 && let Some((expected_ty, found_ty)) =
1920 self.values_str(trace.values, &trace.cause, long_ty_path)
1921 {
1922 suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
1923 found: found_ty.content(),
1924 expected: expected_ty.content(),
1925 });
1926 }
1927 suggestions
1928 }
1929
1930 fn suggest_specify_actual_length(
1931 &self,
1932 terr: TypeError<'tcx>,
1933 trace: &TypeTrace<'tcx>,
1934 span: Span,
1935 ) -> Option<TypeErrorAdditionalDiags> {
1936 let TypeError::ArraySize(sz) = terr else {
1937 return None;
1938 };
1939 let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
1940 hir::Node::Item(hir::Item {
1941 kind: hir::ItemKind::Fn { body: body_id, .. }, ..
1942 }) => {
1943 let body = self.tcx.hir_body(*body_id);
1944 struct LetVisitor {
1945 span: Span,
1946 }
1947 impl<'v> Visitor<'v> for LetVisitor {
1948 type Result = ControlFlow<&'v hir::TyKind<'v>>;
1949 fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) -> Self::Result {
1950 if let hir::Stmt {
1953 kind:
1954 hir::StmtKind::Let(hir::LetStmt {
1955 init: Some(hir::Expr { span: init_span, .. }),
1956 ty: Some(array_ty),
1957 ..
1958 }),
1959 ..
1960 } = s
1961 && init_span == &self.span
1962 {
1963 ControlFlow::Break(&array_ty.peel_refs().kind)
1964 } else {
1965 ControlFlow::Continue(())
1966 }
1967 }
1968 }
1969 LetVisitor { span }.visit_body(body).break_value()
1970 }
1971 hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, ty, _), .. }) => {
1972 Some(&ty.peel_refs().kind)
1973 }
1974 _ => None,
1975 };
1976 if let Some(tykind) = tykind
1977 && let hir::TyKind::Array(_, length_arg) = tykind
1978 && let Some(length_val) = sz.found.try_to_target_usize(self.tcx)
1979 {
1980 Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength {
1981 span: length_arg.span,
1982 length: length_val,
1983 })
1984 } else {
1985 None
1986 }
1987 }
1988
1989 pub fn report_and_explain_type_error(
1990 &self,
1991 trace: TypeTrace<'tcx>,
1992 param_env: ty::ParamEnv<'tcx>,
1993 terr: TypeError<'tcx>,
1994 ) -> Diag<'a> {
1995 {
use ::tracing::__macro_support::Callsite as _;
static __CALLSITE: ::tracing::callsite::DefaultCallsite =
{
static META: ::tracing::Metadata<'static> =
{
::tracing_core::metadata::Metadata::new("event compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs:1995",
"rustc_trait_selection::error_reporting::infer",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/mod.rs"),
::tracing_core::__macro_support::Option::Some(1995u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::DEBUG <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::DEBUG <=
::tracing::level_filters::LevelFilter::current() &&
{
let interest = __CALLSITE.interest();
!interest.is_never() &&
::tracing::__macro_support::__is_enabled(__CALLSITE.metadata(),
interest)
};
if enabled {
(|value_set: ::tracing::field::ValueSet|
{
let meta = __CALLSITE.metadata();
::tracing::Event::dispatch(meta, &value_set);
;
})({
#[allow(unused_imports)]
use ::tracing::field::{debug, display, Value};
let mut iter = __CALLSITE.metadata().fields().iter();
__CALLSITE.metadata().fields().value_set(&[(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&format_args!("report_and_explain_type_error(trace={0:?}, terr={1:?})",
trace, terr) as &dyn Value))])
});
} else { ; }
};debug!("report_and_explain_type_error(trace={:?}, terr={:?})", trace, terr);
1996
1997 let span = trace.cause.span;
1998 let mut path = None;
1999 let failure_code = trace.cause.as_failure_code_diag(
2000 terr,
2001 span,
2002 self.type_error_additional_suggestions(&trace, terr, &mut path),
2003 );
2004 let mut diag = self.dcx().create_err(failure_code);
2005 *diag.long_ty_path() = path;
2006 self.note_type_err(
2007 &mut diag,
2008 &trace.cause,
2009 None,
2010 Some(param_env.and(trace.values)),
2011 terr,
2012 false,
2013 None,
2014 );
2015 diag
2016 }
2017
2018 fn suggest_wrap_to_build_a_tuple(
2019 &self,
2020 span: Span,
2021 found: Ty<'tcx>,
2022 expected_fields: &List<Ty<'tcx>>,
2023 ) -> Option<TypeErrorAdditionalDiags> {
2024 let [expected_tup_elem] = expected_fields[..] else { return None };
2025
2026 if !self.same_type_modulo_infer(expected_tup_elem, found) {
2027 return None;
2028 }
2029
2030 let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span) else { return None };
2031
2032 let sugg = if code.starts_with('(') && code.ends_with(')') {
2033 let before_close = span.hi() - BytePos::from_u32(1);
2034 TypeErrorAdditionalDiags::TupleOnlyComma {
2035 span: span.with_hi(before_close).shrink_to_hi(),
2036 }
2037 } else {
2038 TypeErrorAdditionalDiags::TupleAlsoParentheses {
2039 span_low: span.shrink_to_lo(),
2040 span_high: span.shrink_to_hi(),
2041 }
2042 };
2043 Some(sugg)
2044 }
2045
2046 fn values_str(
2047 &self,
2048 values: ValuePairs<'tcx>,
2049 cause: &ObligationCause<'tcx>,
2050 long_ty_path: &mut Option<PathBuf>,
2051 ) -> Option<(DiagStyledString, DiagStyledString)> {
2052 match values {
2053 ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2054 ValuePairs::Terms(exp_found) => self.expected_found_str_term(exp_found, long_ty_path),
2055 ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
2056 ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
2057 ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
2058 ValuePairs::TraitRefs(exp_found) => {
2059 let pretty_exp_found = ty::error::ExpectedFound {
2060 expected: exp_found.expected.print_trait_sugared(),
2061 found: exp_found.found.print_trait_sugared(),
2062 };
2063 match self.expected_found_str(pretty_exp_found) {
2064 Some((expected, found)) if expected == found => {
2065 self.expected_found_str(exp_found)
2066 }
2067 ret => ret,
2068 }
2069 }
2070 ValuePairs::PolySigs(exp_found) => {
2071 let exp_found = self.resolve_vars_if_possible(exp_found);
2072 if exp_found.references_error() {
2073 return None;
2074 }
2075 let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
2076 impl_item_def_id,
2077 trait_item_def_id,
2078 ..
2079 } = *cause.code()
2080 {
2081 (Some((trait_item_def_id, None)), Some((impl_item_def_id.to_def_id(), None)))
2082 } else {
2083 (None, None)
2084 };
2085
2086 Some(self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2))
2087 }
2088 }
2089 }
2090
2091 fn expected_found_str_term(
2092 &self,
2093 exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
2094 long_ty_path: &mut Option<PathBuf>,
2095 ) -> Option<(DiagStyledString, DiagStyledString)> {
2096 let exp_found = self.resolve_vars_if_possible(exp_found);
2097 if exp_found.references_error() {
2098 return None;
2099 }
2100
2101 Some(match (exp_found.expected.kind(), exp_found.found.kind()) {
2102 (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
2103 let (mut exp, mut fnd) = self.cmp(expected, found);
2104 let len = self.tcx.sess.diagnostic_width() + 40;
2108 let exp_s = exp.content();
2109 let fnd_s = fnd.content();
2110 if exp_s.len() > len {
2111 let exp_s = self.tcx.short_string(expected, long_ty_path);
2112 exp = DiagStyledString::highlighted(exp_s);
2113 }
2114 if fnd_s.len() > len {
2115 let fnd_s = self.tcx.short_string(found, long_ty_path);
2116 fnd = DiagStyledString::highlighted(fnd_s);
2117 }
2118 (exp, fnd)
2119 }
2120 _ => (
2121 DiagStyledString::highlighted(exp_found.expected.to_string()),
2122 DiagStyledString::highlighted(exp_found.found.to_string()),
2123 ),
2124 })
2125 }
2126
2127 fn expected_found_str<T: fmt::Display + TypeFoldable<TyCtxt<'tcx>>>(
2129 &self,
2130 exp_found: ty::error::ExpectedFound<T>,
2131 ) -> Option<(DiagStyledString, DiagStyledString)> {
2132 let exp_found = self.resolve_vars_if_possible(exp_found);
2133 if exp_found.references_error() {
2134 return None;
2135 }
2136
2137 Some((
2138 DiagStyledString::highlighted(exp_found.expected.to_string()),
2139 DiagStyledString::highlighted(exp_found.found.to_string()),
2140 ))
2141 }
2142
2143 pub fn is_try_conversion(&self, span: Span, trait_def_id: DefId) -> bool {
2147 span.is_desugaring(DesugaringKind::QuestionMark)
2148 && self.tcx.is_diagnostic_item(sym::From, trait_def_id)
2149 }
2150
2151 pub fn same_type_modulo_infer<T: relate::Relate<TyCtxt<'tcx>>>(&self, a: T, b: T) -> bool {
2158 let (a, b) = self.resolve_vars_if_possible((a, b));
2159 SameTypeModuloInfer(self).relate(a, b).is_ok()
2160 }
2161}
2162
2163struct SameTypeModuloInfer<'a, 'tcx>(&'a InferCtxt<'tcx>);
2164
2165impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
2166 fn cx(&self) -> TyCtxt<'tcx> {
2167 self.0.tcx
2168 }
2169
2170 fn relate_ty_args(
2171 &mut self,
2172 a_ty: Ty<'tcx>,
2173 _: Ty<'tcx>,
2174 _: DefId,
2175 a_args: ty::GenericArgsRef<'tcx>,
2176 b_args: ty::GenericArgsRef<'tcx>,
2177 _: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
2178 ) -> RelateResult<'tcx, Ty<'tcx>> {
2179 relate::relate_args_invariantly(self, a_args, b_args)?;
2180 Ok(a_ty)
2181 }
2182
2183 fn relate_with_variance<T: relate::Relate<TyCtxt<'tcx>>>(
2184 &mut self,
2185 _variance: ty::Variance,
2186 _info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
2187 a: T,
2188 b: T,
2189 ) -> relate::RelateResult<'tcx, T> {
2190 self.relate(a, b)
2191 }
2192
2193 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
2194 match (a.kind(), b.kind()) {
2195 (ty::Int(_) | ty::Uint(_), ty::Infer(ty::InferTy::IntVar(_)))
2196 | (
2197 ty::Infer(ty::InferTy::IntVar(_)),
2198 ty::Int(_) | ty::Uint(_) | ty::Infer(ty::InferTy::IntVar(_)),
2199 )
2200 | (ty::Float(_), ty::Infer(ty::InferTy::FloatVar(_)))
2201 | (
2202 ty::Infer(ty::InferTy::FloatVar(_)),
2203 ty::Float(_) | ty::Infer(ty::InferTy::FloatVar(_)),
2204 )
2205 | (ty::Infer(ty::InferTy::TyVar(_)), _)
2206 | (_, ty::Infer(ty::InferTy::TyVar(_))) => Ok(a),
2207 (ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Mismatch),
2208 _ => relate::structurally_relate_tys(self, a, b),
2209 }
2210 }
2211
2212 fn regions(
2213 &mut self,
2214 a: ty::Region<'tcx>,
2215 b: ty::Region<'tcx>,
2216 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
2217 if (a.is_var() && b.is_free())
2218 || (b.is_var() && a.is_free())
2219 || (a.is_var() && b.is_var())
2220 || a == b
2221 {
2222 Ok(a)
2223 } else {
2224 Err(TypeError::Mismatch)
2225 }
2226 }
2227
2228 fn binders<T>(
2229 &mut self,
2230 a: ty::Binder<'tcx, T>,
2231 b: ty::Binder<'tcx, T>,
2232 ) -> relate::RelateResult<'tcx, ty::Binder<'tcx, T>>
2233 where
2234 T: relate::Relate<TyCtxt<'tcx>>,
2235 {
2236 Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
2237 }
2238
2239 fn consts(
2240 &mut self,
2241 a: ty::Const<'tcx>,
2242 _b: ty::Const<'tcx>,
2243 ) -> relate::RelateResult<'tcx, ty::Const<'tcx>> {
2244 Ok(a)
2247 }
2248}
2249
2250pub enum FailureCode {
2251 Error0317,
2252 Error0580,
2253 Error0308,
2254 Error0644,
2255}
2256
2257impl<'tcx> ObligationCauseExt<'tcx> for ObligationCause<'tcx> {
fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
match self.code() {
ObligationCauseCode::IfExpressionWithNoElse =>
FailureCode::Error0317,
ObligationCauseCode::MainFunctionType => FailureCode::Error0580,
ObligationCauseCode::CompareImplItem { .. } |
ObligationCauseCode::MatchExpressionArm(_) |
ObligationCauseCode::IfExpression { .. } |
ObligationCauseCode::LetElse |
ObligationCauseCode::LangFunctionType(_) |
ObligationCauseCode::IntrinsicType |
ObligationCauseCode::MethodReceiver => FailureCode::Error0308,
_ =>
match terr {
TypeError::CyclicTy(ty) if
ty.is_closure() || ty.is_coroutine() ||
ty.is_coroutine_closure() => {
FailureCode::Error0644
}
TypeError::IntrinsicCast | TypeError::ForceInlineCast =>
FailureCode::Error0308,
_ => FailureCode::Error0308,
},
}
}
fn as_failure_code_diag(&self, terr: TypeError<'tcx>, span: Span,
subdiags: Vec<TypeErrorAdditionalDiags>)
-> ObligationCauseFailureCode {
match self.code() {
ObligationCauseCode::CompareImplItem {
kind: ty::AssocKind::Fn { .. }, .. } => {
ObligationCauseFailureCode::MethodCompat { span, subdiags }
}
ObligationCauseCode::CompareImplItem {
kind: ty::AssocKind::Type { .. }, .. } => {
ObligationCauseFailureCode::TypeCompat { span, subdiags }
}
ObligationCauseCode::CompareImplItem {
kind: ty::AssocKind::Const { .. }, .. } => {
ObligationCauseFailureCode::ConstCompat { span, subdiags }
}
ObligationCauseCode::BlockTailExpression(..,
hir::MatchSource::TryDesugar(_)) => {
ObligationCauseFailureCode::TryCompat { span, subdiags }
}
ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
source, .. }) =>
match source {
hir::MatchSource::TryDesugar(_) => {
ObligationCauseFailureCode::TryCompat { span, subdiags }
}
_ =>
ObligationCauseFailureCode::MatchCompat { span, subdiags },
},
ObligationCauseCode::IfExpression { .. } => {
ObligationCauseFailureCode::IfElseDifferent { span, subdiags }
}
ObligationCauseCode::IfExpressionWithNoElse => {
ObligationCauseFailureCode::NoElse { span }
}
ObligationCauseCode::LetElse => {
ObligationCauseFailureCode::NoDiverge { span, subdiags }
}
ObligationCauseCode::MainFunctionType => {
ObligationCauseFailureCode::FnMainCorrectType { span }
}
&ObligationCauseCode::LangFunctionType(lang_item_name) => {
ObligationCauseFailureCode::FnLangCorrectType {
span,
subdiags,
lang_item_name,
}
}
ObligationCauseCode::IntrinsicType => {
ObligationCauseFailureCode::IntrinsicCorrectType {
span,
subdiags,
}
}
ObligationCauseCode::MethodReceiver => {
ObligationCauseFailureCode::MethodCorrectType {
span,
subdiags,
}
}
_ =>
match terr {
TypeError::CyclicTy(ty) if
ty.is_closure() || ty.is_coroutine() ||
ty.is_coroutine_closure() => {
ObligationCauseFailureCode::ClosureSelfref { span }
}
TypeError::ForceInlineCast => {
ObligationCauseFailureCode::CantCoerceForceInline {
span,
subdiags,
}
}
TypeError::IntrinsicCast => {
ObligationCauseFailureCode::CantCoerceIntrinsic {
span,
subdiags,
}
}
_ => ObligationCauseFailureCode::Generic { span, subdiags },
},
}
}
fn as_requirement_str(&self) -> &'static str {
match self.code() {
ObligationCauseCode::CompareImplItem {
kind: ty::AssocKind::Fn { .. }, .. } => {
"method type is compatible with trait"
}
ObligationCauseCode::CompareImplItem {
kind: ty::AssocKind::Type { .. }, .. } => {
"associated type is compatible with trait"
}
ObligationCauseCode::CompareImplItem {
kind: ty::AssocKind::Const { .. }, .. } => {
"const is compatible with trait"
}
ObligationCauseCode::MainFunctionType =>
"`main` function has the correct type",
ObligationCauseCode::LangFunctionType(_) =>
"lang item function has the correct type",
ObligationCauseCode::IntrinsicType =>
"intrinsic has the correct type",
ObligationCauseCode::MethodReceiver =>
"method receiver has the correct type",
_ => "types are compatible",
}
}
}#[extension(pub trait ObligationCauseExt<'tcx>)]
2258impl<'tcx> ObligationCause<'tcx> {
2259 fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
2260 match self.code() {
2261 ObligationCauseCode::IfExpressionWithNoElse => FailureCode::Error0317,
2262 ObligationCauseCode::MainFunctionType => FailureCode::Error0580,
2263 ObligationCauseCode::CompareImplItem { .. }
2264 | ObligationCauseCode::MatchExpressionArm(_)
2265 | ObligationCauseCode::IfExpression { .. }
2266 | ObligationCauseCode::LetElse
2267 | ObligationCauseCode::LangFunctionType(_)
2268 | ObligationCauseCode::IntrinsicType
2269 | ObligationCauseCode::MethodReceiver => FailureCode::Error0308,
2270
2271 _ => match terr {
2275 TypeError::CyclicTy(ty)
2276 if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2277 {
2278 FailureCode::Error0644
2279 }
2280 TypeError::IntrinsicCast | TypeError::ForceInlineCast => FailureCode::Error0308,
2281 _ => FailureCode::Error0308,
2282 },
2283 }
2284 }
2285
2286 fn as_failure_code_diag(
2287 &self,
2288 terr: TypeError<'tcx>,
2289 span: Span,
2290 subdiags: Vec<TypeErrorAdditionalDiags>,
2291 ) -> ObligationCauseFailureCode {
2292 match self.code() {
2293 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2294 ObligationCauseFailureCode::MethodCompat { span, subdiags }
2295 }
2296 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2297 ObligationCauseFailureCode::TypeCompat { span, subdiags }
2298 }
2299 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2300 ObligationCauseFailureCode::ConstCompat { span, subdiags }
2301 }
2302 ObligationCauseCode::BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => {
2303 ObligationCauseFailureCode::TryCompat { span, subdiags }
2304 }
2305 ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
2306 source, ..
2307 }) => match source {
2308 hir::MatchSource::TryDesugar(_) => {
2309 ObligationCauseFailureCode::TryCompat { span, subdiags }
2310 }
2311 _ => ObligationCauseFailureCode::MatchCompat { span, subdiags },
2312 },
2313 ObligationCauseCode::IfExpression { .. } => {
2314 ObligationCauseFailureCode::IfElseDifferent { span, subdiags }
2315 }
2316 ObligationCauseCode::IfExpressionWithNoElse => {
2317 ObligationCauseFailureCode::NoElse { span }
2318 }
2319 ObligationCauseCode::LetElse => {
2320 ObligationCauseFailureCode::NoDiverge { span, subdiags }
2321 }
2322 ObligationCauseCode::MainFunctionType => {
2323 ObligationCauseFailureCode::FnMainCorrectType { span }
2324 }
2325 &ObligationCauseCode::LangFunctionType(lang_item_name) => {
2326 ObligationCauseFailureCode::FnLangCorrectType { span, subdiags, lang_item_name }
2327 }
2328 ObligationCauseCode::IntrinsicType => {
2329 ObligationCauseFailureCode::IntrinsicCorrectType { span, subdiags }
2330 }
2331 ObligationCauseCode::MethodReceiver => {
2332 ObligationCauseFailureCode::MethodCorrectType { span, subdiags }
2333 }
2334
2335 _ => match terr {
2339 TypeError::CyclicTy(ty)
2340 if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2341 {
2342 ObligationCauseFailureCode::ClosureSelfref { span }
2343 }
2344 TypeError::ForceInlineCast => {
2345 ObligationCauseFailureCode::CantCoerceForceInline { span, subdiags }
2346 }
2347 TypeError::IntrinsicCast => {
2348 ObligationCauseFailureCode::CantCoerceIntrinsic { span, subdiags }
2349 }
2350 _ => ObligationCauseFailureCode::Generic { span, subdiags },
2351 },
2352 }
2353 }
2354
2355 fn as_requirement_str(&self) -> &'static str {
2356 match self.code() {
2357 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2358 "method type is compatible with trait"
2359 }
2360 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2361 "associated type is compatible with trait"
2362 }
2363 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2364 "const is compatible with trait"
2365 }
2366 ObligationCauseCode::MainFunctionType => "`main` function has the correct type",
2367 ObligationCauseCode::LangFunctionType(_) => "lang item function has the correct type",
2368 ObligationCauseCode::IntrinsicType => "intrinsic has the correct type",
2369 ObligationCauseCode::MethodReceiver => "method receiver has the correct type",
2370 _ => "types are compatible",
2371 }
2372 }
2373}
2374
2375pub struct ObligationCauseAsDiagArg<'tcx>(pub ObligationCause<'tcx>);
2377
2378impl IntoDiagArg for ObligationCauseAsDiagArg<'_> {
2379 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
2380 let kind = match self.0.code() {
2381 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2382 "method_compat"
2383 }
2384 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2385 "type_compat"
2386 }
2387 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2388 "const_compat"
2389 }
2390 ObligationCauseCode::MainFunctionType => "fn_main_correct_type",
2391 ObligationCauseCode::LangFunctionType(_) => "fn_lang_correct_type",
2392 ObligationCauseCode::IntrinsicType => "intrinsic_correct_type",
2393 ObligationCauseCode::MethodReceiver => "method_correct_type",
2394 _ => "other",
2395 }
2396 .into();
2397 rustc_errors::DiagArgValue::Str(kind)
2398 }
2399}
2400
2401#[derive(#[automatically_derived]
impl ::core::clone::Clone for TyCategory {
#[inline]
fn clone(&self) -> TyCategory {
let _: ::core::clone::AssertParamIsClone<hir::CoroutineKind>;
*self
}
}Clone, #[automatically_derived]
impl ::core::marker::Copy for TyCategory { }Copy, #[automatically_derived]
impl ::core::cmp::PartialEq for TyCategory {
#[inline]
fn eq(&self, other: &TyCategory) -> bool {
let __self_discr = ::core::intrinsics::discriminant_value(self);
let __arg1_discr = ::core::intrinsics::discriminant_value(other);
__self_discr == __arg1_discr &&
match (self, other) {
(TyCategory::Coroutine(__self_0),
TyCategory::Coroutine(__arg1_0)) => __self_0 == __arg1_0,
_ => true,
}
}
}PartialEq, #[automatically_derived]
impl ::core::cmp::Eq for TyCategory {
#[inline]
#[doc(hidden)]
#[coverage(off)]
fn assert_fields_are_eq(&self) {
let _: ::core::cmp::AssertParamIsEq<hir::CoroutineKind>;
}
}Eq, #[automatically_derived]
impl ::core::hash::Hash for TyCategory {
#[inline]
fn hash<__H: ::core::hash::Hasher>(&self, state: &mut __H) {
let __self_discr = ::core::intrinsics::discriminant_value(self);
::core::hash::Hash::hash(&__self_discr, state);
match self {
TyCategory::Coroutine(__self_0) =>
::core::hash::Hash::hash(__self_0, state),
_ => {}
}
}
}Hash)]
2404pub enum TyCategory {
2405 Closure,
2406 Opaque,
2407 OpaqueFuture,
2408 Coroutine(hir::CoroutineKind),
2409 Foreign,
2410}
2411
2412impl fmt::Display for TyCategory {
2413 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2414 match self {
2415 Self::Closure => "closure".fmt(f),
2416 Self::Opaque => "opaque type".fmt(f),
2417 Self::OpaqueFuture => "future".fmt(f),
2418 Self::Coroutine(gk) => gk.fmt(f),
2419 Self::Foreign => "foreign type".fmt(f),
2420 }
2421 }
2422}
2423
2424impl TyCategory {
2425 pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
2426 match *ty.kind() {
2427 ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2428 ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) => {
2429 let kind =
2430 if tcx.ty_is_opaque_future(ty) { Self::OpaqueFuture } else { Self::Opaque };
2431 Some((kind, def_id))
2432 }
2433 ty::Coroutine(def_id, ..) => {
2434 Some((Self::Coroutine(tcx.coroutine_kind(def_id).unwrap()), def_id))
2435 }
2436 ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
2437 _ => None,
2438 }
2439 }
2440}