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 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 safety1 = safety(fn_def1, sig1);
799 let safety2 = safety(fn_def2, sig2);
800 values.0.push(safety1, safety1 != safety2);
801 values.1.push(safety2, safety1 != safety2);
802
803 if sig1.abi() != ExternAbi::Rust {
806 values.0.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("extern {0} ", sig1.abi()))
})format!("extern {} ", sig1.abi()), sig1.abi() != sig2.abi());
807 }
808 if sig2.abi() != ExternAbi::Rust {
809 values.1.push(::alloc::__export::must_use({
::alloc::fmt::format(format_args!("extern {0} ", sig2.abi()))
})format!("extern {} ", sig2.abi()), sig1.abi() != sig2.abi());
810 }
811
812 let lifetime_diff = lt1 != lt2;
815 values.0.push(lt1, lifetime_diff);
816 values.1.push(lt2, lifetime_diff);
817
818 values.0.push_normal("fn(");
821 values.1.push_normal("fn(");
822
823 let len1 = sig1.inputs().len();
826 let len2 = sig2.inputs().len();
827 if len1 == len2 {
828 for (i, (l, r)) in iter::zip(sig1.inputs(), sig2.inputs()).enumerate() {
829 self.push_comma(&mut values.0, &mut values.1, i);
830 let (x1, x2) = self.cmp(*l, *r);
831 (values.0).0.extend(x1.0);
832 (values.1).0.extend(x2.0);
833 }
834 } else {
835 for (i, l) in sig1.inputs().iter().enumerate() {
836 values.0.push_highlighted(l.to_string());
837 if i != len1 - 1 {
838 values.0.push_highlighted(", ");
839 }
840 }
841 for (i, r) in sig2.inputs().iter().enumerate() {
842 values.1.push_highlighted(r.to_string());
843 if i != len2 - 1 {
844 values.1.push_highlighted(", ");
845 }
846 }
847 }
848
849 if sig1.c_variadic() {
850 if len1 > 0 {
851 values.0.push_normal(", ");
852 }
853 values.0.push("...", !sig2.c_variadic());
854 }
855 if sig2.c_variadic() {
856 if len2 > 0 {
857 values.1.push_normal(", ");
858 }
859 values.1.push("...", !sig1.c_variadic());
860 }
861
862 values.0.push_normal(")");
865 values.1.push_normal(")");
866
867 let output1 = sig1.output();
870 let output2 = sig2.output();
871 let (x1, x2) = self.cmp(output1, output2);
872 let output_diff = x1 != x2;
873 if !output1.is_unit() || output_diff {
874 values.0.push_normal(" -> ");
875 (values.0).0.extend(x1.0);
876 }
877 if !output2.is_unit() || output_diff {
878 values.1.push_normal(" -> ");
879 (values.1).0.extend(x2.0);
880 }
881
882 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));
883
884 match (fn_def1, fn_def2) {
885 (Some((fn_def1, Some(fn_args1))), Some((fn_def2, Some(fn_args2)))) => {
886 let path1 = fmt(fn_def1, fn_args1);
887 let path2 = fmt(fn_def2, fn_args2);
888 let same_path = path1 == path2;
889 values.0.push(path1, !same_path);
890 values.1.push(path2, !same_path);
891 }
892 (Some((fn_def1, Some(fn_args1))), None) => {
893 values.0.push_highlighted(fmt(fn_def1, fn_args1));
894 }
895 (None, Some((fn_def2, Some(fn_args2)))) => {
896 values.1.push_highlighted(fmt(fn_def2, fn_args2));
897 }
898 _ => {}
899 }
900
901 values
902 }
903
904 pub fn cmp_traits(
905 &self,
906 def_id1: DefId,
907 args1: &[ty::GenericArg<'tcx>],
908 def_id2: DefId,
909 args2: &[ty::GenericArg<'tcx>],
910 ) -> (DiagStyledString, DiagStyledString) {
911 let mut values = (DiagStyledString::new(), DiagStyledString::new());
912
913 if def_id1 != def_id2 {
914 values.0.push_highlighted(self.tcx.def_path_str(def_id1).as_str());
915 values.1.push_highlighted(self.tcx.def_path_str(def_id2).as_str());
916 } else {
917 values.0.push_normal(self.tcx.item_name(def_id1).as_str());
918 values.1.push_normal(self.tcx.item_name(def_id2).as_str());
919 }
920
921 if args1.len() != args2.len() {
922 let (pre, post) = if args1.len() > 0 { ("<", ">") } else { ("", "") };
923 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!(
924 "{pre}{}{post}",
925 args1.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
926 ));
927 let (pre, post) = if args2.len() > 0 { ("<", ">") } else { ("", "") };
928 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!(
929 "{pre}{}{post}",
930 args2.iter().map(|a| a.to_string()).collect::<Vec<_>>().join(", ")
931 ));
932 return values;
933 }
934
935 if args1.len() > 0 {
936 values.0.push_normal("<");
937 values.1.push_normal("<");
938 }
939 for (i, (a, b)) in std::iter::zip(args1, args2).enumerate() {
940 let a_str = a.to_string();
941 let b_str = b.to_string();
942 if let (Some(a), Some(b)) = (a.as_type(), b.as_type()) {
943 let (a, b) = self.cmp(a, b);
944 values.0.0.extend(a.0);
945 values.1.0.extend(b.0);
946 } else if a_str != b_str {
947 values.0.push_highlighted(a_str);
948 values.1.push_highlighted(b_str);
949 } else {
950 values.0.push_normal(a_str);
951 values.1.push_normal(b_str);
952 }
953 if i + 1 < args1.len() {
954 values.0.push_normal(", ");
955 values.1.push_normal(", ");
956 }
957 }
958 if args1.len() > 0 {
959 values.0.push_normal(">");
960 values.1.push_normal(">");
961 }
962 values
963 }
964
965 pub fn cmp(&self, t1: Ty<'tcx>, t2: Ty<'tcx>) -> (DiagStyledString, DiagStyledString) {
968 {
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:968",
"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(968u32),
::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());
969
970 let recurse = |t1, t2, values: &mut (DiagStyledString, DiagStyledString)| {
972 let (x1, x2) = self.cmp(t1, t2);
973 (values.0).0.extend(x1.0);
974 (values.1).0.extend(x2.0);
975 };
976
977 fn fmt_region<'tcx>(region: ty::Region<'tcx>) -> String {
978 let mut r = region.to_string();
979 if r == "'_" {
980 r.clear();
981 } else {
982 r.push(' ');
983 }
984 ::alloc::__export::must_use({ ::alloc::fmt::format(format_args!("&{0}", r)) })format!("&{r}")
985 }
986
987 fn push_ref<'tcx>(
988 region: ty::Region<'tcx>,
989 mutbl: hir::Mutability,
990 s: &mut DiagStyledString,
991 ) {
992 s.push_highlighted(fmt_region(region));
993 s.push_highlighted(mutbl.prefix_str());
994 }
995
996 fn maybe_highlight<T: Eq + ToString>(
997 t1: T,
998 t2: T,
999 (buf1, buf2): &mut (DiagStyledString, DiagStyledString),
1000 tcx: TyCtxt<'_>,
1001 ) {
1002 let highlight = t1 != t2;
1003 let (t1, t2) = if highlight || tcx.sess.opts.verbose {
1004 (t1.to_string(), t2.to_string())
1005 } else {
1006 ("_".into(), "_".into())
1008 };
1009 buf1.push(t1, highlight);
1010 buf2.push(t2, highlight);
1011 }
1012
1013 fn cmp_ty_refs<'tcx>(
1014 r1: ty::Region<'tcx>,
1015 mut1: hir::Mutability,
1016 r2: ty::Region<'tcx>,
1017 mut2: hir::Mutability,
1018 ss: &mut (DiagStyledString, DiagStyledString),
1019 ) {
1020 let (r1, r2) = (fmt_region(r1), fmt_region(r2));
1021 if r1 != r2 {
1022 ss.0.push_highlighted(r1);
1023 ss.1.push_highlighted(r2);
1024 } else {
1025 ss.0.push_normal(r1);
1026 ss.1.push_normal(r2);
1027 }
1028
1029 if mut1 != mut2 {
1030 ss.0.push_highlighted(mut1.prefix_str());
1031 ss.1.push_highlighted(mut2.prefix_str());
1032 } else {
1033 ss.0.push_normal(mut1.prefix_str());
1034 ss.1.push_normal(mut2.prefix_str());
1035 }
1036 }
1037
1038 match (t1.kind(), t2.kind()) {
1040 (&ty::Adt(def1, sub1), &ty::Adt(def2, sub2)) => {
1041 let did1 = def1.did();
1042 let did2 = def2.did();
1043
1044 let generics1 = self.tcx.generics_of(did1);
1045 let generics2 = self.tcx.generics_of(did2);
1046
1047 let non_default_after_default = generics1
1048 .check_concrete_type_after_default(self.tcx, sub1)
1049 || generics2.check_concrete_type_after_default(self.tcx, sub2);
1050 let sub_no_defaults_1 = if non_default_after_default {
1051 generics1.own_args(sub1)
1052 } else {
1053 generics1.own_args_no_defaults(self.tcx, sub1)
1054 };
1055 let sub_no_defaults_2 = if non_default_after_default {
1056 generics2.own_args(sub2)
1057 } else {
1058 generics2.own_args_no_defaults(self.tcx, sub2)
1059 };
1060 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1061 let path1 = self.tcx.def_path_str(did1);
1062 let path2 = self.tcx.def_path_str(did2);
1063 if did1 == did2 {
1064 values.0.push_normal(path1);
1073 values.1.push_normal(path2);
1074
1075 let len1 = sub_no_defaults_1.len();
1078 let len2 = sub_no_defaults_2.len();
1079 let common_len = cmp::min(len1, len2);
1080 let remainder1 = &sub1[common_len..];
1081 let remainder2 = &sub2[common_len..];
1082 let common_default_params =
1083 iter::zip(remainder1.iter().rev(), remainder2.iter().rev())
1084 .filter(|(a, b)| a == b)
1085 .count();
1086 let len = sub1.len() - common_default_params;
1087
1088 if len > 0 {
1090 values.0.push_normal("<");
1091 values.1.push_normal("<");
1092 }
1093
1094 fn lifetime_display(lifetime: Region<'_>) -> String {
1095 let s = lifetime.to_string();
1096 if s.is_empty() { "'_".to_string() } else { s }
1097 }
1098
1099 for (i, (arg1, arg2)) in sub1.iter().zip(sub2).enumerate().take(len) {
1100 self.push_comma(&mut values.0, &mut values.1, i);
1101 match arg1.kind() {
1102 ty::GenericArgKind::Lifetime(l1) => {
1119 let l1_str = lifetime_display(l1);
1120 let l2 = arg2.expect_region();
1121 let l2_str = lifetime_display(l2);
1122 if l1 != l2 {
1123 values.0.push_highlighted(l1_str);
1124 values.1.push_highlighted(l2_str);
1125 } else if l1.is_bound() || self.tcx.sess.opts.verbose {
1126 values.0.push_normal(l1_str);
1127 values.1.push_normal(l2_str);
1128 } else {
1129 values.0.push_normal("'_");
1130 values.1.push_normal("'_");
1131 }
1132 }
1133 ty::GenericArgKind::Type(ta1) => {
1134 let ta2 = arg2.expect_ty();
1135 if ta1 == ta2 && !self.tcx.sess.opts.verbose {
1136 values.0.push_normal("_");
1137 values.1.push_normal("_");
1138 } else {
1139 recurse(ta1, ta2, &mut values);
1140 }
1141 }
1142 ty::GenericArgKind::Const(ca1) => {
1152 let ca2 = arg2.expect_const();
1153 maybe_highlight(ca1, ca2, &mut values, self.tcx);
1154 }
1155 }
1156 }
1157
1158 if len > 0 {
1161 values.0.push_normal(">");
1162 values.1.push_normal(">");
1163 }
1164 values
1165 } else {
1166 if self.cmp_type_arg(
1172 &mut values.0,
1173 &mut values.1,
1174 path1.clone(),
1175 sub_no_defaults_1,
1176 path2.clone(),
1177 t2,
1178 ) {
1179 return values;
1180 }
1181 if self.cmp_type_arg(
1187 &mut values.1,
1188 &mut values.0,
1189 path2,
1190 sub_no_defaults_2,
1191 path1,
1192 t1,
1193 ) {
1194 return values;
1195 }
1196
1197 let t1_str = t1.to_string();
1204 let t2_str = t2.to_string();
1205 let min_len = t1_str.len().min(t2_str.len());
1206
1207 const SEPARATOR: &str = "::";
1208 let separator_len = SEPARATOR.len();
1209 let split_idx: usize =
1210 iter::zip(t1_str.split(SEPARATOR), t2_str.split(SEPARATOR))
1211 .take_while(|(mod1_str, mod2_str)| mod1_str == mod2_str)
1212 .map(|(mod_str, _)| mod_str.len() + separator_len)
1213 .sum();
1214
1215 {
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:1215",
"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(1215u32),
::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");
1216
1217 if split_idx >= min_len {
1218 (
1220 DiagStyledString::highlighted(t1_str),
1221 DiagStyledString::highlighted(t2_str),
1222 )
1223 } else {
1224 let (common, uniq1) = t1_str.split_at(split_idx);
1225 let (_, uniq2) = t2_str.split_at(split_idx);
1226 {
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:1226",
"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(1226u32),
::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");
1227
1228 values.0.push_normal(common);
1229 values.0.push_highlighted(uniq1);
1230 values.1.push_normal(common);
1231 values.1.push_highlighted(uniq2);
1232
1233 values
1234 }
1235 }
1236 }
1237
1238 (&ty::Ref(r1, ref_ty1, mutbl1), &ty::Ref(r2, ref_ty2, mutbl2)) => {
1240 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1241 cmp_ty_refs(r1, mutbl1, r2, mutbl2, &mut values);
1242 recurse(ref_ty1, ref_ty2, &mut values);
1243 values
1244 }
1245 (&ty::Ref(r1, ref_ty1, mutbl1), _) => {
1247 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1248 push_ref(r1, mutbl1, &mut values.0);
1249 recurse(ref_ty1, t2, &mut values);
1250 values
1251 }
1252 (_, &ty::Ref(r2, ref_ty2, mutbl2)) => {
1253 let mut values = (DiagStyledString::new(), DiagStyledString::new());
1254 push_ref(r2, mutbl2, &mut values.1);
1255 recurse(t1, ref_ty2, &mut values);
1256 values
1257 }
1258
1259 (&ty::Tuple(args1), &ty::Tuple(args2)) if args1.len() == args2.len() => {
1261 let mut values = (DiagStyledString::normal("("), DiagStyledString::normal("("));
1262 let len = args1.len();
1263 for (i, (left, right)) in args1.iter().zip(args2).enumerate() {
1264 self.push_comma(&mut values.0, &mut values.1, i);
1265 recurse(left, right, &mut values);
1266 }
1267 if len == 1 {
1268 values.0.push_normal(",");
1270 values.1.push_normal(",");
1271 }
1272 values.0.push_normal(")");
1273 values.1.push_normal(")");
1274 values
1275 }
1276
1277 (ty::FnDef(did1, args1), ty::FnDef(did2, args2)) => {
1278 let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1).skip_norm_wip();
1279 let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2).skip_norm_wip();
1280 self.cmp_fn_sig(
1281 &sig1,
1282 Some((*did1, Some(args1))),
1283 &sig2,
1284 Some((*did2, Some(args2))),
1285 )
1286 }
1287
1288 (ty::FnDef(did1, args1), ty::FnPtr(sig_tys2, hdr2)) => {
1289 let sig1 = self.tcx.fn_sig(*did1).instantiate(self.tcx, args1).skip_norm_wip();
1290 self.cmp_fn_sig(&sig1, Some((*did1, Some(args1))), &sig_tys2.with(*hdr2), None)
1291 }
1292
1293 (ty::FnPtr(sig_tys1, hdr1), ty::FnDef(did2, args2)) => {
1294 let sig2 = self.tcx.fn_sig(*did2).instantiate(self.tcx, args2).skip_norm_wip();
1295 self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig2, Some((*did2, Some(args2))))
1296 }
1297
1298 (ty::FnPtr(sig_tys1, hdr1), ty::FnPtr(sig_tys2, hdr2)) => {
1299 self.cmp_fn_sig(&sig_tys1.with(*hdr1), None, &sig_tys2.with(*hdr2), None)
1300 }
1301
1302 _ => {
1303 let mut strs = (DiagStyledString::new(), DiagStyledString::new());
1304 maybe_highlight(t1, t2, &mut strs, self.tcx);
1305 strs
1306 }
1307 }
1308 }
1309
1310 #[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(1319u32),
::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:1430",
"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(1430u32),
::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:1736",
"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(1736u32),
::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:1776",
"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(1776u32),
::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:1829",
"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(1829u32),
::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))]
1320 pub fn note_type_err(
1321 &self,
1322 diag: &mut Diag<'_>,
1323 cause: &ObligationCause<'tcx>,
1324 secondary_span: Option<(Span, Cow<'static, str>, bool)>,
1325 mut values: Option<ty::ParamEnvAnd<'tcx, ValuePairs<'tcx>>>,
1326 terr: TypeError<'tcx>,
1327 prefer_label: bool,
1328 override_span: Option<Span>,
1329 ) {
1330 let span = override_span.unwrap_or(cause.span);
1335 if let TypeError::CyclicTy(_) = terr {
1338 values = None;
1339 }
1340 struct OpaqueTypesVisitor<'tcx> {
1341 types: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1342 expected: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1343 found: FxIndexMap<TyCategory, FxIndexSet<Span>>,
1344 ignore_span: Span,
1345 tcx: TyCtxt<'tcx>,
1346 }
1347
1348 impl<'tcx> OpaqueTypesVisitor<'tcx> {
1349 fn visit_expected_found(
1350 tcx: TyCtxt<'tcx>,
1351 expected: impl TypeVisitable<TyCtxt<'tcx>>,
1352 found: impl TypeVisitable<TyCtxt<'tcx>>,
1353 ignore_span: Span,
1354 ) -> Self {
1355 let mut types_visitor = OpaqueTypesVisitor {
1356 types: Default::default(),
1357 expected: Default::default(),
1358 found: Default::default(),
1359 ignore_span,
1360 tcx,
1361 };
1362 expected.visit_with(&mut types_visitor);
1366 std::mem::swap(&mut types_visitor.expected, &mut types_visitor.types);
1367 found.visit_with(&mut types_visitor);
1368 std::mem::swap(&mut types_visitor.found, &mut types_visitor.types);
1369 types_visitor
1370 }
1371
1372 fn report(&self, err: &mut Diag<'_>) {
1373 self.add_labels_for_types(err, "expected", &self.expected);
1374 self.add_labels_for_types(err, "found", &self.found);
1375 }
1376
1377 fn add_labels_for_types(
1378 &self,
1379 err: &mut Diag<'_>,
1380 target: &str,
1381 types: &FxIndexMap<TyCategory, FxIndexSet<Span>>,
1382 ) {
1383 for (kind, values) in types.iter() {
1384 let count = values.len();
1385 for &sp in values {
1386 err.span_label(
1387 sp,
1388 format!(
1389 "{}{} {:#}{}",
1390 if count == 1 { "the " } else { "one of the " },
1391 target,
1392 kind,
1393 pluralize!(count),
1394 ),
1395 );
1396 }
1397 }
1398 }
1399 }
1400
1401 impl<'tcx> ty::TypeVisitor<TyCtxt<'tcx>> for OpaqueTypesVisitor<'tcx> {
1402 fn visit_ty(&mut self, t: Ty<'tcx>) {
1403 if let Some((kind, def_id)) = TyCategory::from_ty(self.tcx, t) {
1404 let span = self.tcx.def_span(def_id);
1405 if !self.ignore_span.overlaps(span)
1421 && !span.is_desugaring(DesugaringKind::Async)
1422 {
1423 self.types.entry(kind).or_default().insert(span);
1424 }
1425 }
1426 t.super_visit_with(self)
1427 }
1428 }
1429
1430 debug!("note_type_err(diag={:?})", diag);
1431 enum Mismatch<'a> {
1432 Variable(ty::error::ExpectedFound<Ty<'a>>),
1433 Fixed(&'static str),
1434 }
1435 let (expected_found, exp_found, is_simple_error, values, param_env) = match values {
1436 None => (None, Mismatch::Fixed("type"), false, None, None),
1437 Some(ty::ParamEnvAnd { param_env, value: values }) => {
1438 let mut values = self.resolve_vars_if_possible(values);
1439 if self.next_trait_solver() {
1440 values = deeply_normalize_for_diagnostics(self, param_env, values);
1441 }
1442 let (is_simple_error, exp_found) = match values {
1443 ValuePairs::Terms(ExpectedFound { expected, found }) => {
1444 match (expected.kind(), found.kind()) {
1445 (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
1446 let is_simple_err =
1447 expected.is_simple_text() && found.is_simple_text();
1448 OpaqueTypesVisitor::visit_expected_found(
1449 self.tcx, expected, found, span,
1450 )
1451 .report(diag);
1452
1453 (
1454 is_simple_err,
1455 Mismatch::Variable(ExpectedFound { expected, found }),
1456 )
1457 }
1458 (ty::TermKind::Const(_), ty::TermKind::Const(_)) => {
1459 (false, Mismatch::Fixed("constant"))
1460 }
1461 _ => (false, Mismatch::Fixed("type")),
1462 }
1463 }
1464 ValuePairs::PolySigs(ExpectedFound { expected, found }) => {
1465 OpaqueTypesVisitor::visit_expected_found(self.tcx, expected, found, span)
1466 .report(diag);
1467 (false, Mismatch::Fixed("signature"))
1468 }
1469 ValuePairs::TraitRefs(_) => (false, Mismatch::Fixed("trait")),
1470 ValuePairs::Aliases(ExpectedFound { expected, .. }) => {
1471 (false, Mismatch::Fixed(self.tcx.def_descr(expected.def_id())))
1472 }
1473 ValuePairs::Regions(_) => (false, Mismatch::Fixed("lifetime")),
1474 ValuePairs::ExistentialTraitRef(_) => {
1475 (false, Mismatch::Fixed("existential trait ref"))
1476 }
1477 ValuePairs::ExistentialProjection(_) => {
1478 (false, Mismatch::Fixed("existential projection"))
1479 }
1480 };
1481 let Some(vals) = self.values_str(values, cause, diag.long_ty_path()) else {
1482 diag.downgrade_to_delayed_bug();
1486 return;
1487 };
1488 (Some(vals), exp_found, is_simple_error, Some(values), Some(param_env))
1489 }
1490 };
1491
1492 let mut label_or_note = |span: Span, msg: Cow<'static, str>| {
1493 if (prefer_label && is_simple_error) || &[span] == diag.span.primary_spans() {
1494 diag.span_label(span, msg);
1495 } else {
1496 diag.span_note(span, msg);
1497 }
1498 };
1499 if let Some((secondary_span, secondary_msg, swap_secondary_and_primary)) = secondary_span {
1500 if swap_secondary_and_primary {
1501 let terr = if let Some(infer::ValuePairs::Terms(ExpectedFound {
1502 expected, ..
1503 })) = values
1504 {
1505 Cow::from(format!("expected this to be `{expected}`"))
1506 } else {
1507 terr.to_string(self.tcx)
1508 };
1509 label_or_note(secondary_span, terr);
1510 label_or_note(span, secondary_msg);
1511 } else {
1512 label_or_note(span, terr.to_string(self.tcx));
1513 label_or_note(secondary_span, secondary_msg);
1514 }
1515 } else if let Some(values) = values
1516 && let Some((e, f)) = values.ty()
1517 && let TypeError::ArgumentSorts(..) | TypeError::Sorts(_) = terr
1518 {
1519 let e = self.tcx.erase_and_anonymize_regions(e);
1520 let f = self.tcx.erase_and_anonymize_regions(f);
1521 let expected = with_forced_trimmed_paths!(e.sort_string(self.tcx));
1522 let found = with_forced_trimmed_paths!(f.sort_string(self.tcx));
1523 if expected == found {
1524 label_or_note(span, terr.to_string(self.tcx));
1525 } else {
1526 label_or_note(span, Cow::from(format!("expected {expected}, found {found}")));
1527 }
1528 } else {
1529 label_or_note(span, terr.to_string(self.tcx));
1530 }
1531
1532 if let Some(param_env) = param_env {
1533 self.note_field_shadowed_by_private_candidate_in_cause(diag, cause, param_env);
1534 }
1535
1536 if self.check_and_note_conflicting_crates(diag, terr) {
1537 return;
1538 }
1539
1540 if let Some((expected, found)) = expected_found {
1541 let (expected_label, found_label, exp_found) = match exp_found {
1542 Mismatch::Variable(ef) => (
1543 ef.expected.prefix_string(self.tcx),
1544 ef.found.prefix_string(self.tcx),
1545 Some(ef),
1546 ),
1547 Mismatch::Fixed(s) => (s.into(), s.into(), None),
1548 };
1549
1550 enum Similar<'tcx> {
1551 Adts { expected: ty::AdtDef<'tcx>, found: ty::AdtDef<'tcx> },
1552 PrimitiveFound { expected: ty::AdtDef<'tcx>, found: Ty<'tcx> },
1553 PrimitiveExpected { expected: Ty<'tcx>, found: ty::AdtDef<'tcx> },
1554 }
1555
1556 let similarity = |ExpectedFound { expected, found }: ExpectedFound<Ty<'tcx>>| {
1557 if let ty::Adt(expected, _) = expected.kind()
1558 && let Some(primitive) = found.primitive_symbol()
1559 {
1560 let path = self.tcx.def_path(expected.did()).data;
1561 let name = path.last().unwrap().data.get_opt_name();
1562 if name == Some(primitive) {
1563 return Some(Similar::PrimitiveFound { expected: *expected, found });
1564 }
1565 } else if let Some(primitive) = expected.primitive_symbol()
1566 && let ty::Adt(found, _) = found.kind()
1567 {
1568 let path = self.tcx.def_path(found.did()).data;
1569 let name = path.last().unwrap().data.get_opt_name();
1570 if name == Some(primitive) {
1571 return Some(Similar::PrimitiveExpected { expected, found: *found });
1572 }
1573 } else if let ty::Adt(expected, _) = expected.kind()
1574 && let ty::Adt(found, _) = found.kind()
1575 {
1576 if !expected.did().is_local() && expected.did().krate == found.did().krate {
1577 return None;
1581 }
1582 let f_path = self.tcx.def_path(found.did()).data;
1583 let e_path = self.tcx.def_path(expected.did()).data;
1584
1585 if let (Some(e_last), Some(f_last)) = (e_path.last(), f_path.last())
1586 && e_last == f_last
1587 {
1588 return Some(Similar::Adts { expected: *expected, found: *found });
1589 }
1590 }
1591 None
1592 };
1593
1594 match terr {
1595 TypeError::Sorts(values) if let Some(s) = similarity(values) => {
1597 let diagnose_primitive =
1598 |prim: Ty<'tcx>, shadow: Ty<'tcx>, defid: DefId, diag: &mut Diag<'_>| {
1599 let name = shadow.sort_string(self.tcx);
1600 diag.note(format!(
1601 "`{prim}` and {name} have similar names, but are actually distinct types"
1602 ));
1603 diag.note(format!(
1604 "one `{prim}` is a primitive defined by the language",
1605 ));
1606 let def_span = self.tcx.def_span(defid);
1607 let msg = if defid.is_local() {
1608 format!("the other {name} is defined in the current crate")
1609 } else {
1610 let crate_name = self.tcx.crate_name(defid.krate);
1611 format!("the other {name} is defined in crate `{crate_name}`")
1612 };
1613 diag.span_note(def_span, msg);
1614 };
1615
1616 let diagnose_adts =
1617 |expected_adt: ty::AdtDef<'tcx>,
1618 found_adt: ty::AdtDef<'tcx>,
1619 diag: &mut Diag<'_>| {
1620 let found_name = values.found.sort_string(self.tcx);
1621 let expected_name = values.expected.sort_string(self.tcx);
1622
1623 let found_defid = found_adt.did();
1624 let expected_defid = expected_adt.did();
1625
1626 diag.note(format!("{found_name} and {expected_name} have similar names, but are actually distinct types"));
1627 for (defid, name) in
1628 [(found_defid, found_name), (expected_defid, expected_name)]
1629 {
1630 let def_span = self.tcx.def_span(defid);
1631
1632 let msg = if found_defid.is_local() && expected_defid.is_local() {
1633 let module = self
1634 .tcx
1635 .parent_module_from_def_id(defid.expect_local())
1636 .to_def_id();
1637 let module_name =
1638 self.tcx.def_path(module).to_string_no_crate_verbose();
1639 format!(
1640 "{name} is defined in module `crate{module_name}` of the current crate"
1641 )
1642 } else if defid.is_local() {
1643 format!("{name} is defined in the current crate")
1644 } else {
1645 let crate_name = self.tcx.crate_name(defid.krate);
1646 format!("{name} is defined in crate `{crate_name}`")
1647 };
1648 diag.span_note(def_span, msg);
1649 }
1650 };
1651
1652 match s {
1653 Similar::Adts { expected, found } => diagnose_adts(expected, found, diag),
1654 Similar::PrimitiveFound { expected, found: prim } => {
1655 diagnose_primitive(prim, values.expected, expected.did(), diag)
1656 }
1657 Similar::PrimitiveExpected { expected: prim, found } => {
1658 diagnose_primitive(prim, values.found, found.did(), diag)
1659 }
1660 }
1661 }
1662 TypeError::Sorts(values) => {
1663 let extra = expected == found
1664 && values.expected.sort_string(self.tcx)
1668 != values.found.sort_string(self.tcx);
1669 let sort_string = |ty: Ty<'tcx>| match (extra, ty.kind()) {
1670 (true, ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. })) => {
1671 let sm = self.tcx.sess.source_map();
1672 let pos = sm.lookup_char_pos(self.tcx.def_span(*def_id).lo());
1673 DiagStyledString::normal(format!(
1674 " (opaque type at <{}:{}:{}>)",
1675 sm.filename_for_diagnostics(&pos.file.name),
1676 pos.line,
1677 pos.col.to_usize() + 1,
1678 ))
1679 }
1680 (true, &ty::Alias(ty::AliasTy { kind: ty::Projection { def_id }, .. }))
1681 if self.tcx.is_impl_trait_in_trait(def_id) =>
1682 {
1683 let sm = self.tcx.sess.source_map();
1684 let pos = sm.lookup_char_pos(self.tcx.def_span(def_id).lo());
1685 DiagStyledString::normal(format!(
1686 " (trait associated opaque type at <{}:{}:{}>)",
1687 sm.filename_for_diagnostics(&pos.file.name),
1688 pos.line,
1689 pos.col.to_usize() + 1,
1690 ))
1691 }
1692 (true, _) => {
1693 let mut s = DiagStyledString::normal(" (");
1694 s.push_highlighted(ty.sort_string(self.tcx));
1695 s.push_normal(")");
1696 s
1697 }
1698 (false, _) => DiagStyledString::normal(""),
1699 };
1700 if !(values.expected.is_simple_text() && values.found.is_simple_text())
1701 || (exp_found.is_some_and(|ef| {
1702 if !ef.expected.is_ty_or_numeric_infer() {
1707 ef.expected != values.expected
1708 } else if !ef.found.is_ty_or_numeric_infer() {
1709 ef.found != values.found
1710 } else {
1711 false
1712 }
1713 }))
1714 {
1715 if let Some(ExpectedFound { found: found_ty, .. }) = exp_found
1716 && !self.tcx.ty_is_opaque_future(found_ty)
1717 {
1718 diag.note_expected_found_extra(
1725 &expected_label,
1726 expected,
1727 &found_label,
1728 found,
1729 sort_string(values.expected),
1730 sort_string(values.found),
1731 );
1732 }
1733 }
1734 }
1735 _ => {
1736 debug!(
1737 "note_type_err: exp_found={:?}, expected={:?} found={:?}",
1738 exp_found, expected, found
1739 );
1740 if !is_simple_error || terr.must_include_note() {
1741 diag.note_expected_found(&expected_label, expected, &found_label, found);
1742
1743 if let Some(ty::Closure(_, args)) =
1744 exp_found.map(|expected_type_found| expected_type_found.found.kind())
1745 {
1746 diag.highlighted_note(vec![
1747 StringPart::normal("closure has signature: `"),
1748 StringPart::highlighted(
1749 self.tcx
1750 .signature_unclosure(
1751 args.as_closure().sig(),
1752 rustc_hir::Safety::Safe,
1753 )
1754 .to_string(),
1755 ),
1756 StringPart::normal("`"),
1757 ]);
1758 }
1759 }
1760 }
1761 }
1762 }
1763 let exp_found = match exp_found {
1764 Mismatch::Variable(exp_found) => Some(exp_found),
1765 Mismatch::Fixed(_) => None,
1766 };
1767 let exp_found = match terr {
1768 ty::error::TypeError::Sorts(terr)
1770 if exp_found.is_some_and(|ef| terr.found == ef.found) =>
1771 {
1772 Some(terr)
1773 }
1774 _ => exp_found,
1775 };
1776 debug!("exp_found {:?} terr {:?} cause.code {:?}", exp_found, terr, cause.code());
1777 if let Some(exp_found) = exp_found {
1778 let should_suggest_fixes =
1779 if let ObligationCauseCode::Pattern { root_ty, .. } = cause.code() {
1780 self.same_type_modulo_infer(*root_ty, exp_found.expected)
1783 } else {
1784 true
1785 };
1786
1787 if should_suggest_fixes
1791 && !matches!(terr, TypeError::RegionsInsufficientlyPolymorphic(..))
1792 {
1793 self.suggest_tuple_pattern(cause, &exp_found, diag);
1794 self.suggest_accessing_field_where_appropriate(cause, &exp_found, diag);
1795 self.suggest_await_on_expect_found(cause, span, &exp_found, diag);
1796 self.suggest_function_pointers(cause, span, &exp_found, terr, diag);
1797 self.suggest_turning_stmt_into_expr(cause, &exp_found, diag);
1798 }
1799 }
1800
1801 let body_owner_def_id = (cause.body_id != CRATE_DEF_ID).then(|| cause.body_id.to_def_id());
1802 self.note_and_explain_type_err(diag, terr, cause, span, body_owner_def_id);
1803 if let Some(exp_found) = exp_found
1804 && let exp_found = TypeError::Sorts(exp_found)
1805 && exp_found != terr
1806 {
1807 self.note_and_explain_type_err(diag, exp_found, cause, span, body_owner_def_id);
1808 }
1809
1810 if let Some(ValuePairs::TraitRefs(exp_found)) = values
1811 && let ty::Closure(def_id, _) = exp_found.expected.self_ty().kind()
1812 && let Some(def_id) = def_id.as_local()
1813 && terr.involves_regions()
1814 {
1815 let span = self.tcx.def_span(def_id);
1816 diag.span_note(span, "this closure does not fulfill the lifetime requirements");
1817 self.suggest_for_all_lifetime_closure(
1818 span,
1819 self.tcx.hir_node_by_def_id(def_id),
1820 &exp_found,
1821 diag,
1822 );
1823 }
1824
1825 self.note_error_origin(diag, cause, exp_found, terr, param_env);
1828
1829 debug!(?diag);
1830 }
1831
1832 pub(crate) fn type_error_additional_suggestions(
1833 &self,
1834 trace: &TypeTrace<'tcx>,
1835 terr: TypeError<'tcx>,
1836 long_ty_path: &mut Option<PathBuf>,
1837 ) -> Vec<TypeErrorAdditionalDiags> {
1838 let mut suggestions = Vec::new();
1839 let span = trace.cause.span;
1840 let values = self.resolve_vars_if_possible(trace.values);
1841 if let Some((expected, found)) = values.ty() {
1842 match (expected.kind(), found.kind()) {
1843 (ty::Tuple(_), ty::Tuple(_)) => {}
1844 (ty::Tuple(fields), _) => {
1848 suggestions.extend(self.suggest_wrap_to_build_a_tuple(span, found, fields))
1849 }
1850 (ty::Uint(ty::UintTy::U8), ty::Char) => {
1854 if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1855 && let Some(code) = code.strip_circumfix('\'', '\'')
1856 && !code.starts_with("\\u")
1858 && code.chars().next().is_some_and(|c| c.is_ascii())
1860 {
1861 suggestions.push(TypeErrorAdditionalDiags::MeantByteLiteral {
1862 span,
1863 code: escape_literal(code),
1864 })
1865 }
1866 }
1867 (ty::Char, ty::Ref(_, r, _)) if r.is_str() => {
1871 if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1872 && let Some(code) = code.strip_circumfix('"', '"')
1873 && code.chars().count() == 1
1874 {
1875 suggestions.push(TypeErrorAdditionalDiags::MeantCharLiteral {
1876 span,
1877 code: escape_literal(code),
1878 })
1879 }
1880 }
1881 (ty::Ref(_, r, _), ty::Char) if r.is_str() => {
1884 if let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span)
1885 && code.starts_with("'")
1886 && code.ends_with("'")
1887 {
1888 suggestions.push(TypeErrorAdditionalDiags::MeantStrLiteral {
1889 start: span.with_hi(span.lo() + BytePos(1)),
1890 end: span.with_lo(span.hi() - BytePos(1)),
1891 });
1892 }
1893 }
1894 (ty::Bool, ty::Tuple(list)) => {
1897 if list.len() == 0 {
1898 suggestions.extend(self.suggest_let_for_letchains(&trace.cause, span));
1899 }
1900 }
1901 (ty::Array(_, _), ty::Array(_, _)) => {
1902 suggestions.extend(self.suggest_specify_actual_length(terr, trace, span))
1903 }
1904 _ => {}
1905 }
1906 }
1907 let code = trace.cause.code();
1908 if let &(ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
1909 source,
1910 ..
1911 })
1912 | ObligationCauseCode::BlockTailExpression(.., source)) = code
1913 && let hir::MatchSource::TryDesugar(_) = source
1914 && let Some((expected_ty, found_ty)) =
1915 self.values_str(trace.values, &trace.cause, long_ty_path)
1916 {
1917 suggestions.push(TypeErrorAdditionalDiags::TryCannotConvert {
1918 found: found_ty.content(),
1919 expected: expected_ty.content(),
1920 });
1921 }
1922 suggestions
1923 }
1924
1925 fn suggest_specify_actual_length(
1926 &self,
1927 terr: TypeError<'tcx>,
1928 trace: &TypeTrace<'tcx>,
1929 span: Span,
1930 ) -> Option<TypeErrorAdditionalDiags> {
1931 let TypeError::ArraySize(sz) = terr else {
1932 return None;
1933 };
1934 let tykind = match self.tcx.hir_node_by_def_id(trace.cause.body_id) {
1935 hir::Node::Item(hir::Item {
1936 kind: hir::ItemKind::Fn { body: body_id, .. }, ..
1937 }) => {
1938 let body = self.tcx.hir_body(*body_id);
1939 struct LetVisitor {
1940 span: Span,
1941 }
1942 impl<'v> Visitor<'v> for LetVisitor {
1943 type Result = ControlFlow<&'v hir::TyKind<'v>>;
1944 fn visit_stmt(&mut self, s: &'v hir::Stmt<'v>) -> Self::Result {
1945 if let hir::Stmt {
1948 kind:
1949 hir::StmtKind::Let(hir::LetStmt {
1950 init: Some(hir::Expr { span: init_span, .. }),
1951 ty: Some(array_ty),
1952 ..
1953 }),
1954 ..
1955 } = s
1956 && init_span == &self.span
1957 {
1958 ControlFlow::Break(&array_ty.peel_refs().kind)
1959 } else {
1960 ControlFlow::Continue(())
1961 }
1962 }
1963 }
1964 LetVisitor { span }.visit_body(body).break_value()
1965 }
1966 hir::Node::Item(hir::Item { kind: hir::ItemKind::Const(_, _, ty, _), .. }) => {
1967 Some(&ty.peel_refs().kind)
1968 }
1969 _ => None,
1970 };
1971 if let Some(tykind) = tykind
1972 && let hir::TyKind::Array(_, length_arg) = tykind
1973 && let Some(length_val) = sz.found.try_to_target_usize(self.tcx)
1974 {
1975 Some(TypeErrorAdditionalDiags::ConsiderSpecifyingLength {
1976 span: length_arg.span,
1977 length: length_val,
1978 })
1979 } else {
1980 None
1981 }
1982 }
1983
1984 pub fn report_and_explain_type_error(
1985 &self,
1986 trace: TypeTrace<'tcx>,
1987 param_env: ty::ParamEnv<'tcx>,
1988 terr: TypeError<'tcx>,
1989 ) -> Diag<'a> {
1990 {
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:1990",
"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(1990u32),
::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);
1991
1992 let span = trace.cause.span;
1993 let mut path = None;
1994 let failure_code = trace.cause.as_failure_code_diag(
1995 terr,
1996 span,
1997 self.type_error_additional_suggestions(&trace, terr, &mut path),
1998 );
1999 let mut diag = self.dcx().create_err(failure_code);
2000 *diag.long_ty_path() = path;
2001 self.note_type_err(
2002 &mut diag,
2003 &trace.cause,
2004 None,
2005 Some(param_env.and(trace.values)),
2006 terr,
2007 false,
2008 None,
2009 );
2010 diag
2011 }
2012
2013 fn suggest_wrap_to_build_a_tuple(
2014 &self,
2015 span: Span,
2016 found: Ty<'tcx>,
2017 expected_fields: &List<Ty<'tcx>>,
2018 ) -> Option<TypeErrorAdditionalDiags> {
2019 let [expected_tup_elem] = expected_fields[..] else { return None };
2020
2021 if !self.same_type_modulo_infer(expected_tup_elem, found) {
2022 return None;
2023 }
2024
2025 let Ok(code) = self.tcx.sess.source_map().span_to_snippet(span) else { return None };
2026
2027 let sugg = if code.starts_with('(') && code.ends_with(')') {
2028 let before_close = span.hi() - BytePos::from_u32(1);
2029 TypeErrorAdditionalDiags::TupleOnlyComma {
2030 span: span.with_hi(before_close).shrink_to_hi(),
2031 }
2032 } else {
2033 TypeErrorAdditionalDiags::TupleAlsoParentheses {
2034 span_low: span.shrink_to_lo(),
2035 span_high: span.shrink_to_hi(),
2036 }
2037 };
2038 Some(sugg)
2039 }
2040
2041 fn values_str(
2042 &self,
2043 values: ValuePairs<'tcx>,
2044 cause: &ObligationCause<'tcx>,
2045 long_ty_path: &mut Option<PathBuf>,
2046 ) -> Option<(DiagStyledString, DiagStyledString)> {
2047 match values {
2048 ValuePairs::Regions(exp_found) => self.expected_found_str(exp_found),
2049 ValuePairs::Terms(exp_found) => self.expected_found_str_term(exp_found, long_ty_path),
2050 ValuePairs::Aliases(exp_found) => self.expected_found_str(exp_found),
2051 ValuePairs::ExistentialTraitRef(exp_found) => self.expected_found_str(exp_found),
2052 ValuePairs::ExistentialProjection(exp_found) => self.expected_found_str(exp_found),
2053 ValuePairs::TraitRefs(exp_found) => {
2054 let pretty_exp_found = ty::error::ExpectedFound {
2055 expected: exp_found.expected.print_trait_sugared(),
2056 found: exp_found.found.print_trait_sugared(),
2057 };
2058 match self.expected_found_str(pretty_exp_found) {
2059 Some((expected, found)) if expected == found => {
2060 self.expected_found_str(exp_found)
2061 }
2062 ret => ret,
2063 }
2064 }
2065 ValuePairs::PolySigs(exp_found) => {
2066 let exp_found = self.resolve_vars_if_possible(exp_found);
2067 if exp_found.references_error() {
2068 return None;
2069 }
2070 let (fn_def1, fn_def2) = if let ObligationCauseCode::CompareImplItem {
2071 impl_item_def_id,
2072 trait_item_def_id,
2073 ..
2074 } = *cause.code()
2075 {
2076 (Some((trait_item_def_id, None)), Some((impl_item_def_id.to_def_id(), None)))
2077 } else {
2078 (None, None)
2079 };
2080
2081 Some(self.cmp_fn_sig(&exp_found.expected, fn_def1, &exp_found.found, fn_def2))
2082 }
2083 }
2084 }
2085
2086 fn expected_found_str_term(
2087 &self,
2088 exp_found: ty::error::ExpectedFound<ty::Term<'tcx>>,
2089 long_ty_path: &mut Option<PathBuf>,
2090 ) -> Option<(DiagStyledString, DiagStyledString)> {
2091 let exp_found = self.resolve_vars_if_possible(exp_found);
2092 if exp_found.references_error() {
2093 return None;
2094 }
2095
2096 Some(match (exp_found.expected.kind(), exp_found.found.kind()) {
2097 (ty::TermKind::Ty(expected), ty::TermKind::Ty(found)) => {
2098 let (mut exp, mut fnd) = self.cmp(expected, found);
2099 let len = self.tcx.sess.diagnostic_width() + 40;
2103 let exp_s = exp.content();
2104 let fnd_s = fnd.content();
2105 if exp_s.len() > len {
2106 let exp_s = self.tcx.short_string(expected, long_ty_path);
2107 exp = DiagStyledString::highlighted(exp_s);
2108 }
2109 if fnd_s.len() > len {
2110 let fnd_s = self.tcx.short_string(found, long_ty_path);
2111 fnd = DiagStyledString::highlighted(fnd_s);
2112 }
2113 (exp, fnd)
2114 }
2115 _ => (
2116 DiagStyledString::highlighted(exp_found.expected.to_string()),
2117 DiagStyledString::highlighted(exp_found.found.to_string()),
2118 ),
2119 })
2120 }
2121
2122 fn expected_found_str<T: fmt::Display + TypeFoldable<TyCtxt<'tcx>>>(
2124 &self,
2125 exp_found: ty::error::ExpectedFound<T>,
2126 ) -> Option<(DiagStyledString, DiagStyledString)> {
2127 let exp_found = self.resolve_vars_if_possible(exp_found);
2128 if exp_found.references_error() {
2129 return None;
2130 }
2131
2132 Some((
2133 DiagStyledString::highlighted(exp_found.expected.to_string()),
2134 DiagStyledString::highlighted(exp_found.found.to_string()),
2135 ))
2136 }
2137
2138 pub fn is_try_conversion(&self, span: Span, trait_def_id: DefId) -> bool {
2142 span.is_desugaring(DesugaringKind::QuestionMark)
2143 && self.tcx.is_diagnostic_item(sym::From, trait_def_id)
2144 }
2145
2146 pub fn same_type_modulo_infer<T: relate::Relate<TyCtxt<'tcx>>>(&self, a: T, b: T) -> bool {
2153 let (a, b) = self.resolve_vars_if_possible((a, b));
2154 SameTypeModuloInfer(self).relate(a, b).is_ok()
2155 }
2156}
2157
2158struct SameTypeModuloInfer<'a, 'tcx>(&'a InferCtxt<'tcx>);
2159
2160impl<'tcx> TypeRelation<TyCtxt<'tcx>> for SameTypeModuloInfer<'_, 'tcx> {
2161 fn cx(&self) -> TyCtxt<'tcx> {
2162 self.0.tcx
2163 }
2164
2165 fn relate_ty_args(
2166 &mut self,
2167 a_ty: Ty<'tcx>,
2168 _: Ty<'tcx>,
2169 _: DefId,
2170 a_args: ty::GenericArgsRef<'tcx>,
2171 b_args: ty::GenericArgsRef<'tcx>,
2172 _: impl FnOnce(ty::GenericArgsRef<'tcx>) -> Ty<'tcx>,
2173 ) -> RelateResult<'tcx, Ty<'tcx>> {
2174 relate::relate_args_invariantly(self, a_args, b_args)?;
2175 Ok(a_ty)
2176 }
2177
2178 fn relate_with_variance<T: relate::Relate<TyCtxt<'tcx>>>(
2179 &mut self,
2180 _variance: ty::Variance,
2181 _info: ty::VarianceDiagInfo<TyCtxt<'tcx>>,
2182 a: T,
2183 b: T,
2184 ) -> relate::RelateResult<'tcx, T> {
2185 self.relate(a, b)
2186 }
2187
2188 fn tys(&mut self, a: Ty<'tcx>, b: Ty<'tcx>) -> RelateResult<'tcx, Ty<'tcx>> {
2189 match (a.kind(), b.kind()) {
2190 (ty::Int(_) | ty::Uint(_), ty::Infer(ty::InferTy::IntVar(_)))
2191 | (
2192 ty::Infer(ty::InferTy::IntVar(_)),
2193 ty::Int(_) | ty::Uint(_) | ty::Infer(ty::InferTy::IntVar(_)),
2194 )
2195 | (ty::Float(_), ty::Infer(ty::InferTy::FloatVar(_)))
2196 | (
2197 ty::Infer(ty::InferTy::FloatVar(_)),
2198 ty::Float(_) | ty::Infer(ty::InferTy::FloatVar(_)),
2199 )
2200 | (ty::Infer(ty::InferTy::TyVar(_)), _)
2201 | (_, ty::Infer(ty::InferTy::TyVar(_))) => Ok(a),
2202 (ty::Infer(_), _) | (_, ty::Infer(_)) => Err(TypeError::Mismatch),
2203 _ => relate::structurally_relate_tys(self, a, b),
2204 }
2205 }
2206
2207 fn regions(
2208 &mut self,
2209 a: ty::Region<'tcx>,
2210 b: ty::Region<'tcx>,
2211 ) -> RelateResult<'tcx, ty::Region<'tcx>> {
2212 if (a.is_var() && b.is_free())
2213 || (b.is_var() && a.is_free())
2214 || (a.is_var() && b.is_var())
2215 || a == b
2216 {
2217 Ok(a)
2218 } else {
2219 Err(TypeError::Mismatch)
2220 }
2221 }
2222
2223 fn binders<T>(
2224 &mut self,
2225 a: ty::Binder<'tcx, T>,
2226 b: ty::Binder<'tcx, T>,
2227 ) -> relate::RelateResult<'tcx, ty::Binder<'tcx, T>>
2228 where
2229 T: relate::Relate<TyCtxt<'tcx>>,
2230 {
2231 Ok(a.rebind(self.relate(a.skip_binder(), b.skip_binder())?))
2232 }
2233
2234 fn consts(
2235 &mut self,
2236 a: ty::Const<'tcx>,
2237 _b: ty::Const<'tcx>,
2238 ) -> relate::RelateResult<'tcx, ty::Const<'tcx>> {
2239 Ok(a)
2242 }
2243}
2244
2245pub enum FailureCode {
2246 Error0317,
2247 Error0580,
2248 Error0308,
2249 Error0644,
2250}
2251
2252impl<'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>)]
2253impl<'tcx> ObligationCause<'tcx> {
2254 fn as_failure_code(&self, terr: TypeError<'tcx>) -> FailureCode {
2255 match self.code() {
2256 ObligationCauseCode::IfExpressionWithNoElse => FailureCode::Error0317,
2257 ObligationCauseCode::MainFunctionType => FailureCode::Error0580,
2258 ObligationCauseCode::CompareImplItem { .. }
2259 | ObligationCauseCode::MatchExpressionArm(_)
2260 | ObligationCauseCode::IfExpression { .. }
2261 | ObligationCauseCode::LetElse
2262 | ObligationCauseCode::LangFunctionType(_)
2263 | ObligationCauseCode::IntrinsicType
2264 | ObligationCauseCode::MethodReceiver => FailureCode::Error0308,
2265
2266 _ => match terr {
2270 TypeError::CyclicTy(ty)
2271 if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2272 {
2273 FailureCode::Error0644
2274 }
2275 TypeError::IntrinsicCast | TypeError::ForceInlineCast => FailureCode::Error0308,
2276 _ => FailureCode::Error0308,
2277 },
2278 }
2279 }
2280
2281 fn as_failure_code_diag(
2282 &self,
2283 terr: TypeError<'tcx>,
2284 span: Span,
2285 subdiags: Vec<TypeErrorAdditionalDiags>,
2286 ) -> ObligationCauseFailureCode {
2287 match self.code() {
2288 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2289 ObligationCauseFailureCode::MethodCompat { span, subdiags }
2290 }
2291 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2292 ObligationCauseFailureCode::TypeCompat { span, subdiags }
2293 }
2294 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2295 ObligationCauseFailureCode::ConstCompat { span, subdiags }
2296 }
2297 ObligationCauseCode::BlockTailExpression(.., hir::MatchSource::TryDesugar(_)) => {
2298 ObligationCauseFailureCode::TryCompat { span, subdiags }
2299 }
2300 ObligationCauseCode::MatchExpressionArm(box MatchExpressionArmCause {
2301 source, ..
2302 }) => match source {
2303 hir::MatchSource::TryDesugar(_) => {
2304 ObligationCauseFailureCode::TryCompat { span, subdiags }
2305 }
2306 _ => ObligationCauseFailureCode::MatchCompat { span, subdiags },
2307 },
2308 ObligationCauseCode::IfExpression { .. } => {
2309 ObligationCauseFailureCode::IfElseDifferent { span, subdiags }
2310 }
2311 ObligationCauseCode::IfExpressionWithNoElse => {
2312 ObligationCauseFailureCode::NoElse { span }
2313 }
2314 ObligationCauseCode::LetElse => {
2315 ObligationCauseFailureCode::NoDiverge { span, subdiags }
2316 }
2317 ObligationCauseCode::MainFunctionType => {
2318 ObligationCauseFailureCode::FnMainCorrectType { span }
2319 }
2320 &ObligationCauseCode::LangFunctionType(lang_item_name) => {
2321 ObligationCauseFailureCode::FnLangCorrectType { span, subdiags, lang_item_name }
2322 }
2323 ObligationCauseCode::IntrinsicType => {
2324 ObligationCauseFailureCode::IntrinsicCorrectType { span, subdiags }
2325 }
2326 ObligationCauseCode::MethodReceiver => {
2327 ObligationCauseFailureCode::MethodCorrectType { span, subdiags }
2328 }
2329
2330 _ => match terr {
2334 TypeError::CyclicTy(ty)
2335 if ty.is_closure() || ty.is_coroutine() || ty.is_coroutine_closure() =>
2336 {
2337 ObligationCauseFailureCode::ClosureSelfref { span }
2338 }
2339 TypeError::ForceInlineCast => {
2340 ObligationCauseFailureCode::CantCoerceForceInline { span, subdiags }
2341 }
2342 TypeError::IntrinsicCast => {
2343 ObligationCauseFailureCode::CantCoerceIntrinsic { span, subdiags }
2344 }
2345 _ => ObligationCauseFailureCode::Generic { span, subdiags },
2346 },
2347 }
2348 }
2349
2350 fn as_requirement_str(&self) -> &'static str {
2351 match self.code() {
2352 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2353 "method type is compatible with trait"
2354 }
2355 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2356 "associated type is compatible with trait"
2357 }
2358 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2359 "const is compatible with trait"
2360 }
2361 ObligationCauseCode::MainFunctionType => "`main` function has the correct type",
2362 ObligationCauseCode::LangFunctionType(_) => "lang item function has the correct type",
2363 ObligationCauseCode::IntrinsicType => "intrinsic has the correct type",
2364 ObligationCauseCode::MethodReceiver => "method receiver has the correct type",
2365 _ => "types are compatible",
2366 }
2367 }
2368}
2369
2370pub struct ObligationCauseAsDiagArg<'tcx>(pub ObligationCause<'tcx>);
2372
2373impl IntoDiagArg for ObligationCauseAsDiagArg<'_> {
2374 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
2375 let kind = match self.0.code() {
2376 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Fn { .. }, .. } => {
2377 "method_compat"
2378 }
2379 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Type { .. }, .. } => {
2380 "type_compat"
2381 }
2382 ObligationCauseCode::CompareImplItem { kind: ty::AssocKind::Const { .. }, .. } => {
2383 "const_compat"
2384 }
2385 ObligationCauseCode::MainFunctionType => "fn_main_correct_type",
2386 ObligationCauseCode::LangFunctionType(_) => "fn_lang_correct_type",
2387 ObligationCauseCode::IntrinsicType => "intrinsic_correct_type",
2388 ObligationCauseCode::MethodReceiver => "method_correct_type",
2389 _ => "other",
2390 }
2391 .into();
2392 rustc_errors::DiagArgValue::Str(kind)
2393 }
2394}
2395
2396#[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)]
2399pub enum TyCategory {
2400 Closure,
2401 Opaque,
2402 OpaqueFuture,
2403 Coroutine(hir::CoroutineKind),
2404 Foreign,
2405}
2406
2407impl fmt::Display for TyCategory {
2408 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
2409 match self {
2410 Self::Closure => "closure".fmt(f),
2411 Self::Opaque => "opaque type".fmt(f),
2412 Self::OpaqueFuture => "future".fmt(f),
2413 Self::Coroutine(gk) => gk.fmt(f),
2414 Self::Foreign => "foreign type".fmt(f),
2415 }
2416 }
2417}
2418
2419impl TyCategory {
2420 pub fn from_ty(tcx: TyCtxt<'_>, ty: Ty<'_>) -> Option<(Self, DefId)> {
2421 match *ty.kind() {
2422 ty::Closure(def_id, _) => Some((Self::Closure, def_id)),
2423 ty::Alias(ty::AliasTy { kind: ty::Opaque { def_id }, .. }) => {
2424 let kind =
2425 if tcx.ty_is_opaque_future(ty) { Self::OpaqueFuture } else { Self::Opaque };
2426 Some((kind, def_id))
2427 }
2428 ty::Coroutine(def_id, ..) => {
2429 Some((Self::Coroutine(tcx.coroutine_kind(def_id).unwrap()), def_id))
2430 }
2431 ty::Foreign(def_id) => Some((Self::Foreign, def_id)),
2432 _ => None,
2433 }
2434 }
2435}