1use std::borrow::Cow;
2use std::iter;
3use std::path::PathBuf;
4
5use rustc_errors::codes::*;
6use rustc_errors::{Diag, IntoDiagArg};
7use rustc_hir as hir;
8use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
9use rustc_hir::def_id::{DefId, LocalDefId};
10use rustc_hir::intravisit::{self, Visitor};
11use rustc_hir::{Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource};
12use rustc_middle::bug;
13use rustc_middle::hir::nested_filter;
14use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow};
15use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
16use rustc_middle::ty::{
17 self, GenericArg, GenericArgKind, GenericArgsRef, InferConst, IsSuggestable, Term, TermKind,
18 Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable, TypeVisitableExt, TypeckResults,
19};
20use rustc_span::{BytePos, DUMMY_SP, Ident, Span, sym};
21use tracing::{debug, instrument, warn};
22
23use super::nice_region_error::placeholder_error::Highlighted;
24use crate::error_reporting::TypeErrCtxt;
25use crate::errors::{
26 AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
27 SourceKindMultiSuggestion, SourceKindSubdiag,
28};
29use crate::infer::InferCtxt;
30
31pub enum TypeAnnotationNeeded {
32 E0282,
36 E0283,
41 E0284,
46}
47
48impl From<TypeAnnotationNeeded> for ErrCode {
49 fn from(val: TypeAnnotationNeeded) -> Self {
50 match val {
51 TypeAnnotationNeeded::E0282 => E0282,
52 TypeAnnotationNeeded::E0283 => E0283,
53 TypeAnnotationNeeded::E0284 => E0284,
54 }
55 }
56}
57
58pub struct InferenceDiagnosticsData {
60 pub name: String,
61 pub span: Option<Span>,
62 pub kind: UnderspecifiedArgKind,
63 pub parent: Option<InferenceDiagnosticsParentData>,
64}
65
66pub struct InferenceDiagnosticsParentData {
68 prefix: &'static str,
69 name: String,
70}
71
72#[derive(Clone)]
73pub enum UnderspecifiedArgKind {
74 Type { prefix: Cow<'static, str> },
75 Const { is_parameter: bool },
76}
77
78impl InferenceDiagnosticsData {
79 fn can_add_more_info(&self) -> bool {
80 !(self.name == "_" && matches!(self.kind, UnderspecifiedArgKind::Type { .. }))
81 }
82
83 fn where_x_is_kind(&self, in_type: Ty<'_>) -> &'static str {
84 if in_type.is_ty_or_numeric_infer() {
85 ""
86 } else if self.name == "_" {
87 "underscore"
90 } else {
91 "has_name"
92 }
93 }
94
95 fn make_bad_error(&self, span: Span) -> InferenceBadError<'_> {
99 let has_parent = self.parent.is_some();
100 let bad_kind = if self.can_add_more_info() { "more_info" } else { "other" };
101 let (parent_prefix, parent_name) = self
102 .parent
103 .as_ref()
104 .map(|parent| (parent.prefix, parent.name.clone()))
105 .unwrap_or_default();
106 InferenceBadError {
107 span,
108 bad_kind,
109 prefix_kind: self.kind.clone(),
110 prefix: self.kind.try_get_prefix().unwrap_or_default(),
111 name: self.name.clone(),
112 has_parent,
113 parent_prefix,
114 parent_name,
115 }
116 }
117}
118
119impl InferenceDiagnosticsParentData {
120 fn for_parent_def_id(
121 tcx: TyCtxt<'_>,
122 parent_def_id: DefId,
123 ) -> Option<InferenceDiagnosticsParentData> {
124 let parent_name =
125 tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
126
127 Some(InferenceDiagnosticsParentData {
128 prefix: tcx.def_descr(parent_def_id),
129 name: parent_name,
130 })
131 }
132
133 fn for_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<InferenceDiagnosticsParentData> {
134 Self::for_parent_def_id(tcx, tcx.parent(def_id))
135 }
136}
137
138impl IntoDiagArg for UnderspecifiedArgKind {
139 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
140 let kind = match self {
141 Self::Type { .. } => "type",
142 Self::Const { is_parameter: true } => "const_with_param",
143 Self::Const { is_parameter: false } => "const",
144 };
145 rustc_errors::DiagArgValue::Str(kind.into())
146 }
147}
148
149impl UnderspecifiedArgKind {
150 fn try_get_prefix(&self) -> Option<&str> {
151 match self {
152 Self::Type { prefix } => Some(prefix.as_ref()),
153 Self::Const { .. } => None,
154 }
155 }
156}
157
158struct ClosureEraser<'a, 'tcx> {
159 infcx: &'a InferCtxt<'tcx>,
160}
161
162impl<'a, 'tcx> ClosureEraser<'a, 'tcx> {
163 fn new_infer(&mut self) -> Ty<'tcx> {
164 self.infcx.next_ty_var(DUMMY_SP)
165 }
166}
167
168impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ClosureEraser<'a, 'tcx> {
169 fn cx(&self) -> TyCtxt<'tcx> {
170 self.infcx.tcx
171 }
172
173 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
174 match ty.kind() {
175 ty::Closure(_, args) => {
176 let closure_sig = args.as_closure().sig();
179 Ty::new_fn_ptr(
180 self.cx(),
181 self.cx().signature_unclosure(closure_sig, hir::Safety::Safe),
182 )
183 }
184 ty::Adt(_, args) if !args.iter().any(|a| a.has_infer()) => {
185 self.new_infer()
190 }
191 ty::Adt(def, args) => {
192 let generics = self.cx().generics_of(def.did());
193 let generics: Vec<bool> = generics
194 .own_params
195 .iter()
196 .map(|param| param.default_value(self.cx()).is_some())
197 .collect();
198 let ty = Ty::new_adt(
199 self.cx(),
200 *def,
201 self.cx().mk_args_from_iter(generics.into_iter().zip(args.iter()).map(
202 |(has_default, arg)| {
203 if arg.has_infer() {
204 arg.fold_with(self)
210 } else if has_default {
211 arg
218 } else if let GenericArgKind::Type(_) = arg.unpack() {
219 self.new_infer().into()
221 } else {
222 arg.fold_with(self)
223 }
224 },
225 )),
226 );
227 ty
228 }
229 _ if ty.has_infer() => {
230 ty.super_fold_with(self)
234 }
235 _ => self.new_infer(),
237 }
238 }
239
240 fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
241 c
243 }
244}
245
246fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinter<'a, 'tcx> {
247 let mut printer = FmtPrinter::new(infcx.tcx, ns);
248 let ty_getter = move |ty_vid| {
249 if infcx.probe_ty_var(ty_vid).is_ok() {
250 warn!("resolved ty var in error message");
251 }
252
253 let var_origin = infcx.type_var_origin(ty_vid);
254 if let Some(def_id) = var_origin.param_def_id
255 && infcx.tcx.def_kind(def_id) == DefKind::TyParam
258 && let name = infcx.tcx.item_name(def_id)
259 && !var_origin.span.from_expansion()
260 {
261 let generics = infcx.tcx.generics_of(infcx.tcx.parent(def_id));
262 let idx = generics.param_def_id_to_index(infcx.tcx, def_id).unwrap();
263 let generic_param_def = generics.param_at(idx as usize, infcx.tcx);
264 if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param_def.kind {
265 None
266 } else {
267 Some(name)
268 }
269 } else {
270 None
271 }
272 };
273 printer.ty_infer_name_resolver = Some(Box::new(ty_getter));
274 let const_getter =
275 move |ct_vid| Some(infcx.tcx.item_name(infcx.const_var_origin(ct_vid)?.param_def_id?));
276 printer.const_infer_name_resolver = Some(Box::new(const_getter));
277 printer
278}
279
280fn ty_to_string<'tcx>(
281 infcx: &InferCtxt<'tcx>,
282 ty: Ty<'tcx>,
283 called_method_def_id: Option<DefId>,
284) -> String {
285 let mut printer = fmt_printer(infcx, Namespace::TypeNS);
286 let ty = infcx.resolve_vars_if_possible(ty);
287 let ty = ty.fold_with(&mut ClosureEraser { infcx });
290
291 match (ty.kind(), called_method_def_id) {
292 (ty::FnDef(..), _) => {
295 ty.fn_sig(infcx.tcx).print(&mut printer).unwrap();
296 printer.into_buffer()
297 }
298 (_, Some(def_id))
299 if ty.is_ty_or_numeric_infer()
300 && infcx.tcx.get_diagnostic_item(sym::iterator_collect_fn) == Some(def_id) =>
301 {
302 "Vec<_>".to_string()
303 }
304 _ if ty.is_ty_or_numeric_infer() => "/* Type */".to_string(),
305 _ => {
306 ty.print(&mut printer).unwrap();
307 printer.into_buffer()
308 }
309 }
310}
311
312fn closure_as_fn_str<'tcx>(infcx: &InferCtxt<'tcx>, ty: Ty<'tcx>) -> String {
316 let ty::Closure(_, args) = ty.kind() else {
317 bug!("cannot convert non-closure to fn str in `closure_as_fn_str`")
318 };
319 let fn_sig = args.as_closure().sig();
320 let args = fn_sig
321 .inputs()
322 .skip_binder()
323 .iter()
324 .next()
325 .map(|args| {
326 args.tuple_fields()
327 .iter()
328 .map(|arg| ty_to_string(infcx, arg, None))
329 .collect::<Vec<_>>()
330 .join(", ")
331 })
332 .unwrap_or_default();
333 let ret = if fn_sig.output().skip_binder().is_unit() {
334 String::new()
335 } else {
336 format!(" -> {}", ty_to_string(infcx, fn_sig.output().skip_binder(), None))
337 };
338 format!("fn({args}){ret}")
339}
340
341impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
342 pub fn extract_inference_diagnostics_data(
345 &self,
346 term: Term<'tcx>,
347 highlight: ty::print::RegionHighlightMode<'tcx>,
348 ) -> InferenceDiagnosticsData {
349 let tcx = self.tcx;
350 match term.unpack() {
351 TermKind::Ty(ty) => {
352 if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() {
353 let var_origin = self.infcx.type_var_origin(ty_vid);
354 if let Some(def_id) = var_origin.param_def_id
355 && self.tcx.def_kind(def_id) == DefKind::TyParam
358 && !var_origin.span.from_expansion()
359 {
360 return InferenceDiagnosticsData {
361 name: self.tcx.item_name(def_id).to_string(),
362 span: Some(var_origin.span),
363 kind: UnderspecifiedArgKind::Type { prefix: "type parameter".into() },
364 parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id),
365 };
366 }
367 }
368
369 InferenceDiagnosticsData {
370 name: Highlighted { highlight, ns: Namespace::TypeNS, tcx, value: ty }
371 .to_string(),
372 span: None,
373 kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
374 parent: None,
375 }
376 }
377 TermKind::Const(ct) => {
378 if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
379 let origin = self.const_var_origin(vid).expect("expected unresolved const var");
380 if let Some(def_id) = origin.param_def_id {
381 return InferenceDiagnosticsData {
382 name: self.tcx.item_name(def_id).to_string(),
383 span: Some(origin.span),
384 kind: UnderspecifiedArgKind::Const { is_parameter: true },
385 parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id),
386 };
387 }
388
389 debug_assert!(!origin.span.is_dummy());
390 InferenceDiagnosticsData {
391 name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
392 .to_string(),
393 span: Some(origin.span),
394 kind: UnderspecifiedArgKind::Const { is_parameter: false },
395 parent: None,
396 }
397 } else {
398 InferenceDiagnosticsData {
405 name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
406 .to_string(),
407 span: None,
408 kind: UnderspecifiedArgKind::Const { is_parameter: false },
409 parent: None,
410 }
411 }
412 }
413 }
414 }
415
416 fn bad_inference_failure_err(
419 &self,
420 span: Span,
421 arg_data: InferenceDiagnosticsData,
422 error_code: TypeAnnotationNeeded,
423 ) -> Diag<'a> {
424 let source_kind = "other";
425 let source_name = "";
426 let failure_span = None;
427 let infer_subdiags = Vec::new();
428 let multi_suggestions = Vec::new();
429 let bad_label = Some(arg_data.make_bad_error(span));
430 match error_code {
431 TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
432 span,
433 source_kind,
434 source_name,
435 failure_span,
436 infer_subdiags,
437 multi_suggestions,
438 bad_label,
439 was_written: false,
440 path: Default::default(),
441 }),
442 TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
443 span,
444 source_kind,
445 source_name,
446 failure_span,
447 infer_subdiags,
448 multi_suggestions,
449 bad_label,
450 was_written: false,
451 path: Default::default(),
452 }),
453 TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
454 span,
455 source_kind,
456 source_name,
457 failure_span,
458 infer_subdiags,
459 multi_suggestions,
460 bad_label,
461 was_written: false,
462 path: Default::default(),
463 }),
464 }
465 }
466
467 #[instrument(level = "debug", skip(self, error_code))]
468 pub fn emit_inference_failure_err(
469 &self,
470 body_def_id: LocalDefId,
471 failure_span: Span,
472 term: Term<'tcx>,
473 error_code: TypeAnnotationNeeded,
474 should_label_span: bool,
475 ) -> Diag<'a> {
476 let term = self.resolve_vars_if_possible(term);
477 let arg_data = self
478 .extract_inference_diagnostics_data(term, ty::print::RegionHighlightMode::default());
479
480 let Some(typeck_results) = &self.typeck_results else {
481 return self.bad_inference_failure_err(failure_span, arg_data, error_code);
485 };
486
487 let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, term);
488 if let Some(body) = self.tcx.hir_maybe_body_owned_by(
489 self.tcx.typeck_root_def_id(body_def_id.to_def_id()).expect_local(),
490 ) {
491 let expr = body.value;
492 local_visitor.visit_expr(expr);
493 }
494
495 let Some(InferSource { span, kind }) = local_visitor.infer_source else {
496 return self.bad_inference_failure_err(failure_span, arg_data, error_code);
497 };
498
499 let (source_kind, name, path) = kind.ty_localized_msg(self);
500 let failure_span = if should_label_span && !failure_span.overlaps(span) {
501 Some(failure_span)
502 } else {
503 None
504 };
505
506 let mut infer_subdiags = Vec::new();
507 let mut multi_suggestions = Vec::new();
508 match kind {
509 InferSourceKind::LetBinding { insert_span, pattern_name, ty, def_id } => {
510 infer_subdiags.push(SourceKindSubdiag::LetLike {
511 span: insert_span,
512 name: pattern_name.map(|name| name.to_string()).unwrap_or_else(String::new),
513 x_kind: arg_data.where_x_is_kind(ty),
514 prefix_kind: arg_data.kind.clone(),
515 prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
516 arg_name: arg_data.name,
517 kind: if pattern_name.is_some() { "with_pattern" } else { "other" },
518 type_name: ty_to_string(self, ty, def_id),
519 });
520 }
521 InferSourceKind::ClosureArg { insert_span, ty } => {
522 infer_subdiags.push(SourceKindSubdiag::LetLike {
523 span: insert_span,
524 name: String::new(),
525 x_kind: arg_data.where_x_is_kind(ty),
526 prefix_kind: arg_data.kind.clone(),
527 prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
528 arg_name: arg_data.name,
529 kind: "closure",
530 type_name: ty_to_string(self, ty, None),
531 });
532 }
533 InferSourceKind::GenericArg {
534 insert_span,
535 argument_index,
536 generics_def_id,
537 def_id: _,
538 generic_args,
539 have_turbofish,
540 } => {
541 let generics = self.tcx.generics_of(generics_def_id);
542 let is_type = term.as_type().is_some();
543
544 let (parent_exists, parent_prefix, parent_name) =
545 InferenceDiagnosticsParentData::for_parent_def_id(self.tcx, generics_def_id)
546 .map_or((false, String::new(), String::new()), |parent| {
547 (true, parent.prefix.to_string(), parent.name)
548 });
549
550 infer_subdiags.push(SourceKindSubdiag::GenericLabel {
551 span,
552 is_type,
553 param_name: generics.own_params[argument_index].name.to_string(),
554 parent_exists,
555 parent_prefix,
556 parent_name,
557 });
558
559 let args = if self.tcx.get_diagnostic_item(sym::iterator_collect_fn)
560 == Some(generics_def_id)
561 {
562 "Vec<_>".to_string()
563 } else {
564 let mut printer = fmt_printer(self, Namespace::TypeNS);
565 printer
566 .comma_sep(generic_args.iter().copied().map(|arg| {
567 if arg.is_suggestable(self.tcx, true) {
568 return arg;
569 }
570
571 match arg.unpack() {
572 GenericArgKind::Lifetime(_) => bug!("unexpected lifetime"),
573 GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
574 GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
575 }
576 }))
577 .unwrap();
578 printer.into_buffer()
579 };
580
581 if !have_turbofish {
582 infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
583 span: insert_span,
584 arg_count: generic_args.len(),
585 args,
586 });
587 }
588 }
589 InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
590 let placeholder = Some(self.next_ty_var(DUMMY_SP));
591 if let Some(args) = args.make_suggestable(self.infcx.tcx, true, placeholder) {
592 let mut printer = fmt_printer(self, Namespace::ValueNS);
593 printer.print_def_path(def_id, args).unwrap();
594 let def_path = printer.into_buffer();
595
596 let adjustment = match typeck_results.expr_adjustments(receiver) {
600 [
601 Adjustment { kind: Adjust::Deref(None), target: _ },
602 ..,
603 Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), target: _ },
604 ] => "",
605 [
606 ..,
607 Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mut_)), target: _ },
608 ] => hir::Mutability::from(*mut_).ref_prefix_str(),
609 _ => "",
610 };
611
612 multi_suggestions.push(SourceKindMultiSuggestion::new_fully_qualified(
613 receiver.span,
614 def_path,
615 adjustment,
616 successor,
617 ));
618 }
619 }
620 InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
621 let placeholder = Some(self.next_ty_var(DUMMY_SP));
622 if let Some(ty) = ty.make_suggestable(self.infcx.tcx, true, placeholder) {
623 let ty_info = ty_to_string(self, ty, None);
624 multi_suggestions.push(SourceKindMultiSuggestion::new_closure_return(
625 ty_info,
626 data,
627 should_wrap_expr,
628 ));
629 }
630 }
631 }
632 match error_code {
633 TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
634 span,
635 source_kind,
636 source_name: &name,
637 failure_span,
638 infer_subdiags,
639 multi_suggestions,
640 bad_label: None,
641 was_written: path.is_some(),
642 path: path.unwrap_or_default(),
643 }),
644 TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
645 span,
646 source_kind,
647 source_name: &name,
648 failure_span,
649 infer_subdiags,
650 multi_suggestions,
651 bad_label: None,
652 was_written: path.is_some(),
653 path: path.unwrap_or_default(),
654 }),
655 TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
656 span,
657 source_kind,
658 source_name: &name,
659 failure_span,
660 infer_subdiags,
661 multi_suggestions,
662 bad_label: None,
663 was_written: path.is_some(),
664 path: path.unwrap_or_default(),
665 }),
666 }
667 }
668}
669
670#[derive(Debug)]
671struct InferSource<'tcx> {
672 span: Span,
673 kind: InferSourceKind<'tcx>,
674}
675
676#[derive(Debug)]
677enum InferSourceKind<'tcx> {
678 LetBinding {
679 insert_span: Span,
680 pattern_name: Option<Ident>,
681 ty: Ty<'tcx>,
682 def_id: Option<DefId>,
683 },
684 ClosureArg {
685 insert_span: Span,
686 ty: Ty<'tcx>,
687 },
688 GenericArg {
689 insert_span: Span,
690 argument_index: usize,
691 generics_def_id: DefId,
692 def_id: DefId,
693 generic_args: &'tcx [GenericArg<'tcx>],
694 have_turbofish: bool,
695 },
696 FullyQualifiedMethodCall {
697 receiver: &'tcx Expr<'tcx>,
698 successor: (&'static str, BytePos),
701 args: GenericArgsRef<'tcx>,
702 def_id: DefId,
703 },
704 ClosureReturn {
705 ty: Ty<'tcx>,
706 data: &'tcx FnRetTy<'tcx>,
707 should_wrap_expr: Option<Span>,
708 },
709}
710
711impl<'tcx> InferSource<'tcx> {
712 fn from_expansion(&self) -> bool {
713 let source_from_expansion = match self.kind {
714 InferSourceKind::LetBinding { insert_span, .. }
715 | InferSourceKind::ClosureArg { insert_span, .. }
716 | InferSourceKind::GenericArg { insert_span, .. } => insert_span.from_expansion(),
717 InferSourceKind::FullyQualifiedMethodCall { receiver, .. } => {
718 receiver.span.from_expansion()
719 }
720 InferSourceKind::ClosureReturn { data, should_wrap_expr, .. } => {
721 data.span().from_expansion() || should_wrap_expr.is_some_and(Span::from_expansion)
722 }
723 };
724 source_from_expansion || self.span.from_expansion()
725 }
726}
727
728impl<'tcx> InferSourceKind<'tcx> {
729 fn ty_localized_msg(&self, infcx: &InferCtxt<'tcx>) -> (&'static str, String, Option<PathBuf>) {
730 let mut path = None;
731 match *self {
732 InferSourceKind::LetBinding { ty, .. }
733 | InferSourceKind::ClosureArg { ty, .. }
734 | InferSourceKind::ClosureReturn { ty, .. } => {
735 if ty.is_closure() {
736 ("closure", closure_as_fn_str(infcx, ty), path)
737 } else if !ty.is_ty_or_numeric_infer() {
738 ("normal", infcx.tcx.short_string(ty, &mut path), path)
739 } else {
740 ("other", String::new(), path)
741 }
742 }
743 InferSourceKind::GenericArg { .. }
745 | InferSourceKind::FullyQualifiedMethodCall { .. } => ("other", String::new(), path),
746 }
747 }
748}
749
750#[derive(Debug)]
751struct InsertableGenericArgs<'tcx> {
752 insert_span: Span,
753 args: GenericArgsRef<'tcx>,
754 generics_def_id: DefId,
755 def_id: DefId,
756 have_turbofish: bool,
757}
758
759struct FindInferSourceVisitor<'a, 'tcx> {
767 tecx: &'a TypeErrCtxt<'a, 'tcx>,
768 typeck_results: &'a TypeckResults<'tcx>,
769
770 target: Term<'tcx>,
771
772 attempt: usize,
773 infer_source_cost: usize,
774 infer_source: Option<InferSource<'tcx>>,
775}
776
777impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
778 fn new(
779 tecx: &'a TypeErrCtxt<'a, 'tcx>,
780 typeck_results: &'a TypeckResults<'tcx>,
781 target: Term<'tcx>,
782 ) -> Self {
783 FindInferSourceVisitor {
784 tecx,
785 typeck_results,
786
787 target,
788
789 attempt: 0,
790 infer_source_cost: usize::MAX,
791 infer_source: None,
792 }
793 }
794
795 fn source_cost(&self, source: &InferSource<'tcx>) -> usize {
800 #[derive(Clone, Copy)]
801 struct CostCtxt<'tcx> {
802 tcx: TyCtxt<'tcx>,
803 }
804 impl<'tcx> CostCtxt<'tcx> {
805 fn arg_cost(self, arg: GenericArg<'tcx>) -> usize {
806 match arg.unpack() {
807 GenericArgKind::Lifetime(_) => 0, GenericArgKind::Type(ty) => self.ty_cost(ty),
809 GenericArgKind::Const(_) => 3, }
811 }
812 fn ty_cost(self, ty: Ty<'tcx>) -> usize {
813 match *ty.kind() {
814 ty::Closure(..) => 1000,
815 ty::FnDef(..) => 150,
816 ty::FnPtr(..) => 30,
817 ty::Adt(def, args) => {
818 5 + self
819 .tcx
820 .generics_of(def.did())
821 .own_args_no_defaults(self.tcx, args)
822 .iter()
823 .map(|&arg| self.arg_cost(arg))
824 .sum::<usize>()
825 }
826 ty::Tuple(args) => 5 + args.iter().map(|arg| self.ty_cost(arg)).sum::<usize>(),
827 ty::Ref(_, ty, _) => 2 + self.ty_cost(ty),
828 ty::Infer(..) => 0,
829 _ => 1,
830 }
831 }
832 }
833
834 let tcx = self.tecx.tcx;
836 let ctx = CostCtxt { tcx };
837 match source.kind {
838 InferSourceKind::LetBinding { ty, .. } => ctx.ty_cost(ty),
839 InferSourceKind::ClosureArg { ty, .. } => ctx.ty_cost(ty),
840 InferSourceKind::GenericArg { def_id, generic_args, .. } => {
841 let variant_cost = match tcx.def_kind(def_id) {
842 DefKind::Variant | DefKind::Ctor(CtorOf::Variant, _) => 15,
844 _ => 10,
845 };
846 variant_cost + generic_args.iter().map(|&arg| ctx.arg_cost(arg)).sum::<usize>()
847 }
848 InferSourceKind::FullyQualifiedMethodCall { args, .. } => {
849 20 + args.iter().map(|arg| ctx.arg_cost(arg)).sum::<usize>()
850 }
851 InferSourceKind::ClosureReturn { ty, should_wrap_expr, .. } => {
852 30 + ctx.ty_cost(ty) + if should_wrap_expr.is_some() { 10 } else { 0 }
853 }
854 }
855 }
856
857 #[instrument(level = "debug", skip(self))]
860 fn update_infer_source(&mut self, mut new_source: InferSource<'tcx>) {
861 if new_source.from_expansion() {
862 return;
863 }
864
865 let cost = self.source_cost(&new_source) + self.attempt;
866 debug!(?cost);
867 self.attempt += 1;
868 if let Some(InferSource { kind: InferSourceKind::GenericArg { def_id: did, .. }, .. }) =
869 self.infer_source
870 && let InferSourceKind::LetBinding { ref ty, ref mut def_id, .. } = new_source.kind
871 && ty.is_ty_or_numeric_infer()
872 {
873 *def_id = Some(did);
876 }
877
878 if cost < self.infer_source_cost {
879 self.infer_source_cost = cost;
880 self.infer_source = Some(new_source);
881 }
882 }
883
884 fn node_args_opt(&self, hir_id: HirId) -> Option<GenericArgsRef<'tcx>> {
885 let args = self.typeck_results.node_args_opt(hir_id);
886 self.tecx.resolve_vars_if_possible(args)
887 }
888
889 fn opt_node_type(&self, hir_id: HirId) -> Option<Ty<'tcx>> {
890 let ty = self.typeck_results.node_type_opt(hir_id);
891 self.tecx.resolve_vars_if_possible(ty)
892 }
893
894 fn generic_arg_is_target(&self, arg: GenericArg<'tcx>) -> bool {
897 if arg == self.target.into() {
898 return true;
899 }
900
901 match (arg.unpack(), self.target.unpack()) {
902 (GenericArgKind::Type(inner_ty), TermKind::Ty(target_ty)) => {
903 use ty::{Infer, TyVar};
904 match (inner_ty.kind(), target_ty.kind()) {
905 (&Infer(TyVar(a_vid)), &Infer(TyVar(b_vid))) => {
906 self.tecx.sub_relations.borrow_mut().unified(self.tecx, a_vid, b_vid)
907 }
908 _ => false,
909 }
910 }
911 (GenericArgKind::Const(inner_ct), TermKind::Const(target_ct)) => {
912 use ty::InferConst::*;
913 match (inner_ct.kind(), target_ct.kind()) {
914 (ty::ConstKind::Infer(Var(a_vid)), ty::ConstKind::Infer(Var(b_vid))) => {
915 self.tecx.root_const_var(a_vid) == self.tecx.root_const_var(b_vid)
916 }
917 _ => false,
918 }
919 }
920 _ => false,
921 }
922 }
923
924 fn generic_arg_contains_target(&self, arg: GenericArg<'tcx>) -> bool {
927 let mut walker = arg.walk();
928 while let Some(inner) = walker.next() {
929 if self.generic_arg_is_target(inner) {
930 return true;
931 }
932 match inner.unpack() {
933 GenericArgKind::Lifetime(_) => {}
934 GenericArgKind::Type(ty) => {
935 if matches!(
936 ty.kind(),
937 ty::Alias(ty::Opaque, ..)
938 | ty::Closure(..)
939 | ty::CoroutineClosure(..)
940 | ty::Coroutine(..)
941 ) {
942 walker.skip_current_subtree();
953 }
954 }
955 GenericArgKind::Const(ct) => {
956 if matches!(ct.kind(), ty::ConstKind::Unevaluated(..)) {
957 walker.skip_current_subtree();
960 }
961 }
962 }
963 }
964 false
965 }
966
967 fn expr_inferred_arg_iter(
968 &self,
969 expr: &'tcx hir::Expr<'tcx>,
970 ) -> Box<dyn Iterator<Item = InsertableGenericArgs<'tcx>> + 'a> {
971 let tcx = self.tecx.tcx;
972 match expr.kind {
973 hir::ExprKind::Path(ref path) => {
974 if let Some(args) = self.node_args_opt(expr.hir_id) {
975 return self.path_inferred_arg_iter(expr.hir_id, args, path);
976 }
977 }
978 hir::ExprKind::Struct(&hir::QPath::Resolved(_self_ty, path), _, _)
989 if matches!(path.res, Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) => {
996 if let Some(ty) = self.opt_node_type(expr.hir_id)
997 && let ty::Adt(_, args) = ty.kind()
998 {
999 return Box::new(self.resolved_path_inferred_arg_iter(path, args));
1000 }
1001 }
1002 hir::ExprKind::MethodCall(segment, ..) => {
1003 if let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id) {
1004 let generics = tcx.generics_of(def_id);
1005 let insertable: Option<_> = try {
1006 if generics.has_impl_trait() {
1007 None?
1008 }
1009 let args = self.node_args_opt(expr.hir_id)?;
1010 let span = tcx.hir_span(segment.hir_id);
1011 let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1012 InsertableGenericArgs {
1013 insert_span,
1014 args,
1015 generics_def_id: def_id,
1016 def_id,
1017 have_turbofish: false,
1018 }
1019 };
1020 return Box::new(insertable.into_iter());
1021 }
1022 }
1023 _ => {}
1024 }
1025
1026 Box::new(iter::empty())
1027 }
1028
1029 fn resolved_path_inferred_arg_iter(
1030 &self,
1031 path: &'tcx hir::Path<'tcx>,
1032 args: GenericArgsRef<'tcx>,
1033 ) -> impl Iterator<Item = InsertableGenericArgs<'tcx>> + 'tcx {
1034 let tcx = self.tecx.tcx;
1035 let have_turbofish = path.segments.iter().any(|segment| {
1036 segment.args.is_some_and(|args| args.args.iter().any(|arg| arg.is_ty_or_const()))
1037 });
1038 let last_segment_using_path_data: Option<_> = try {
1044 let generics_def_id = tcx.res_generics_def_id(path.res)?;
1045 let generics = tcx.generics_of(generics_def_id);
1046 if generics.has_impl_trait() {
1047 do yeet ();
1048 }
1049 let insert_span =
1050 path.segments.last().unwrap().ident.span.shrink_to_hi().with_hi(path.span.hi());
1051 InsertableGenericArgs {
1052 insert_span,
1053 args,
1054 generics_def_id,
1055 def_id: path.res.def_id(),
1056 have_turbofish,
1057 }
1058 };
1059
1060 path.segments
1061 .iter()
1062 .filter_map(move |segment| {
1063 let res = segment.res;
1064 let generics_def_id = tcx.res_generics_def_id(res)?;
1065 let generics = tcx.generics_of(generics_def_id);
1066 if generics.has_impl_trait() {
1067 return None;
1068 }
1069 let span = tcx.hir_span(segment.hir_id);
1070 let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1071 Some(InsertableGenericArgs {
1072 insert_span,
1073 args,
1074 generics_def_id,
1075 def_id: res.def_id(),
1076 have_turbofish,
1077 })
1078 })
1079 .chain(last_segment_using_path_data)
1080 }
1081
1082 fn path_inferred_arg_iter(
1083 &self,
1084 hir_id: HirId,
1085 args: GenericArgsRef<'tcx>,
1086 qpath: &'tcx hir::QPath<'tcx>,
1087 ) -> Box<dyn Iterator<Item = InsertableGenericArgs<'tcx>> + 'a> {
1088 let tcx = self.tecx.tcx;
1089 match qpath {
1090 hir::QPath::Resolved(_self_ty, path) => {
1091 Box::new(self.resolved_path_inferred_arg_iter(path, args))
1092 }
1093 hir::QPath::TypeRelative(ty, segment) => {
1094 let Some(def_id) = self.typeck_results.type_dependent_def_id(hir_id) else {
1095 return Box::new(iter::empty());
1096 };
1097
1098 let generics = tcx.generics_of(def_id);
1099 let segment: Option<_> = try {
1100 if !segment.infer_args || generics.has_impl_trait() {
1101 do yeet ();
1102 }
1103 let span = tcx.hir_span(segment.hir_id);
1104 let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1105 InsertableGenericArgs {
1106 insert_span,
1107 args,
1108 generics_def_id: def_id,
1109 def_id,
1110 have_turbofish: false,
1111 }
1112 };
1113
1114 let parent_def_id = generics.parent.unwrap();
1115 if let DefKind::Impl { .. } = tcx.def_kind(parent_def_id) {
1116 let parent_ty = tcx.type_of(parent_def_id).instantiate(tcx, args);
1117 match (parent_ty.kind(), &ty.kind) {
1118 (
1119 ty::Adt(def, args),
1120 hir::TyKind::Path(hir::QPath::Resolved(_self_ty, path)),
1121 ) => {
1122 if tcx.res_generics_def_id(path.res) != Some(def.did()) {
1123 match path.res {
1124 Res::Def(DefKind::TyAlias, _) => {
1125 }
1132 Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {}
1135 _ => warn!(
1136 "unexpected path: def={:?} args={:?} path={:?}",
1137 def, args, path,
1138 ),
1139 }
1140 } else {
1141 return Box::new(
1142 self.resolved_path_inferred_arg_iter(path, args).chain(segment),
1143 );
1144 }
1145 }
1146 _ => (),
1147 }
1148 }
1149
1150 Box::new(segment.into_iter())
1151 }
1152 hir::QPath::LangItem(_, _) => Box::new(iter::empty()),
1153 }
1154 }
1155}
1156
1157impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
1158 type NestedFilter = nested_filter::OnlyBodies;
1159
1160 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
1161 self.tecx.tcx
1162 }
1163
1164 fn visit_local(&mut self, local: &'tcx LetStmt<'tcx>) {
1165 intravisit::walk_local(self, local);
1166
1167 if let Some(ty) = self.opt_node_type(local.hir_id) {
1168 if self.generic_arg_contains_target(ty.into()) {
1169 match local.source {
1170 LocalSource::Normal if local.ty.is_none() => {
1171 self.update_infer_source(InferSource {
1172 span: local.pat.span,
1173 kind: InferSourceKind::LetBinding {
1174 insert_span: local.pat.span.shrink_to_hi(),
1175 pattern_name: local.pat.simple_ident(),
1176 ty,
1177 def_id: None,
1178 },
1179 })
1180 }
1181 _ => {}
1182 }
1183 }
1184 }
1185 }
1186
1187 fn visit_body(&mut self, body: &Body<'tcx>) {
1190 for param in body.params {
1191 debug!(
1192 "param: span {:?}, ty_span {:?}, pat.span {:?}",
1193 param.span, param.ty_span, param.pat.span
1194 );
1195 if param.ty_span != param.pat.span {
1196 debug!("skipping param: has explicit type");
1197 continue;
1198 }
1199
1200 let Some(param_ty) = self.opt_node_type(param.hir_id) else { continue };
1201
1202 if self.generic_arg_contains_target(param_ty.into()) {
1203 self.update_infer_source(InferSource {
1204 span: param.pat.span,
1205 kind: InferSourceKind::ClosureArg {
1206 insert_span: param.pat.span.shrink_to_hi(),
1207 ty: param_ty,
1208 },
1209 })
1210 }
1211 }
1212 intravisit::walk_body(self, body);
1213 }
1214
1215 #[instrument(level = "debug", skip(self))]
1216 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
1217 let tcx = self.tecx.tcx;
1218 match expr.kind {
1219 ExprKind::Call(func, args) => {
1222 for arg in args {
1223 self.visit_expr(arg);
1224 }
1225 self.visit_expr(func);
1226 }
1227 _ => intravisit::walk_expr(self, expr),
1228 }
1229
1230 for args in self.expr_inferred_arg_iter(expr) {
1231 debug!(?args);
1232 let InsertableGenericArgs {
1233 insert_span,
1234 args,
1235 generics_def_id,
1236 def_id,
1237 have_turbofish,
1238 } = args;
1239 let generics = tcx.generics_of(generics_def_id);
1240 if let Some(mut argument_index) = generics
1241 .own_args(args)
1242 .iter()
1243 .position(|&arg| self.generic_arg_contains_target(arg))
1244 {
1245 if generics.parent.is_none() && generics.has_self {
1246 argument_index += 1;
1247 }
1248 let args = self.tecx.resolve_vars_if_possible(args);
1249 let generic_args =
1250 &generics.own_args_no_defaults(tcx, args)[generics.own_counts().lifetimes..];
1251 let span = match expr.kind {
1252 ExprKind::MethodCall(path, ..) => path.ident.span,
1253 _ => expr.span,
1254 };
1255
1256 self.update_infer_source(InferSource {
1257 span,
1258 kind: InferSourceKind::GenericArg {
1259 insert_span,
1260 argument_index,
1261 generics_def_id,
1262 def_id,
1263 generic_args,
1264 have_turbofish,
1265 },
1266 });
1267 }
1268 }
1269
1270 if let Some(node_ty) = self.opt_node_type(expr.hir_id) {
1271 if let (
1272 &ExprKind::Closure(&Closure { fn_decl, body, fn_decl_span, .. }),
1273 ty::Closure(_, args),
1274 ) = (&expr.kind, node_ty.kind())
1275 {
1276 let output = args.as_closure().sig().output().skip_binder();
1277 if self.generic_arg_contains_target(output.into()) {
1278 let body = self.tecx.tcx.hir_body(body);
1279 let should_wrap_expr = if matches!(body.value.kind, ExprKind::Block(..)) {
1280 None
1281 } else {
1282 Some(body.value.span.shrink_to_hi())
1283 };
1284 self.update_infer_source(InferSource {
1285 span: fn_decl_span,
1286 kind: InferSourceKind::ClosureReturn {
1287 ty: output,
1288 data: &fn_decl.output,
1289 should_wrap_expr,
1290 },
1291 })
1292 }
1293 }
1294 }
1295
1296 let has_impl_trait = |def_id| {
1297 iter::successors(Some(tcx.generics_of(def_id)), |generics| {
1298 generics.parent.map(|def_id| tcx.generics_of(def_id))
1299 })
1300 .any(|generics| generics.has_impl_trait())
1301 };
1302 if let ExprKind::MethodCall(path, receiver, method_args, span) = expr.kind
1303 && let Some(args) = self.node_args_opt(expr.hir_id)
1304 && args.iter().any(|arg| self.generic_arg_contains_target(arg))
1305 && let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id)
1306 && self.tecx.tcx.trait_of_item(def_id).is_some()
1307 && !has_impl_trait(def_id)
1308 && tcx.hir_opt_delegation_sig_id(expr.hir_id.owner.def_id).is_none()
1311 {
1312 let successor =
1313 method_args.get(0).map_or_else(|| (")", span.hi()), |arg| (", ", arg.span.lo()));
1314 let args = self.tecx.resolve_vars_if_possible(args);
1315 self.update_infer_source(InferSource {
1316 span: path.ident.span,
1317 kind: InferSourceKind::FullyQualifiedMethodCall {
1318 receiver,
1319 successor,
1320 args,
1321 def_id,
1322 },
1323 })
1324 }
1325 }
1326}