1use std::borrow::Cow;
2use std::iter;
3use std::path::PathBuf;
4
5use rustc_errors::codes::*;
6use rustc_errors::{Diag, IntoDiagArg};
7use rustc_hir::def::{CtorOf, DefKind, Namespace, Res};
8use rustc_hir::def_id::{DefId, LocalDefId};
9use rustc_hir::intravisit::{self, Visitor};
10use rustc_hir::{
11 self as hir, Body, Closure, Expr, ExprKind, FnRetTy, HirId, LetStmt, LocalSource, PatKind,
12};
13use rustc_middle::bug;
14use rustc_middle::hir::nested_filter;
15use rustc_middle::ty::adjustment::{Adjust, Adjustment, AutoBorrow, DerefAdjustKind};
16use rustc_middle::ty::print::{FmtPrinter, PrettyPrinter, Print, Printer};
17use rustc_middle::ty::{
18 self, GenericArg, GenericArgKind, GenericArgsRef, GenericParamDefKind, InferConst,
19 IsSuggestable, Term, TermKind, Ty, TyCtxt, TypeFoldable, TypeFolder, TypeSuperFoldable,
20 TypeVisitableExt, TypeckResults,
21};
22use rustc_span::{BytePos, DUMMY_SP, Ident, Span, sym};
23use tracing::{debug, instrument, warn};
24
25use super::nice_region_error::placeholder_error::Highlighted;
26use crate::error_reporting::TypeErrCtxt;
27use crate::errors::{
28 AmbiguousImpl, AmbiguousReturn, AnnotationRequired, InferenceBadError,
29 SourceKindMultiSuggestion, SourceKindSubdiag,
30};
31use crate::infer::{InferCtxt, TyOrConstInferVar};
32
33pub enum TypeAnnotationNeeded {
34 E0282,
38 E0283,
43 E0284,
48}
49
50impl From<TypeAnnotationNeeded> for ErrCode {
51 fn from(val: TypeAnnotationNeeded) -> Self {
52 match val {
53 TypeAnnotationNeeded::E0282 => E0282,
54 TypeAnnotationNeeded::E0283 => E0283,
55 TypeAnnotationNeeded::E0284 => E0284,
56 }
57 }
58}
59
60pub struct InferenceDiagnosticsData {
62 pub name: String,
63 pub span: Option<Span>,
64 pub kind: UnderspecifiedArgKind,
65 pub parent: Option<InferenceDiagnosticsParentData>,
66}
67
68pub struct InferenceDiagnosticsParentData {
70 prefix: &'static str,
71 name: String,
72}
73
74#[derive(#[automatically_derived]
impl ::core::clone::Clone for UnderspecifiedArgKind {
#[inline]
fn clone(&self) -> UnderspecifiedArgKind {
match self {
UnderspecifiedArgKind::Type { prefix: __self_0 } =>
UnderspecifiedArgKind::Type {
prefix: ::core::clone::Clone::clone(__self_0),
},
UnderspecifiedArgKind::Const { is_parameter: __self_0 } =>
UnderspecifiedArgKind::Const {
is_parameter: ::core::clone::Clone::clone(__self_0),
},
}
}
}Clone)]
75pub enum UnderspecifiedArgKind {
76 Type { prefix: Cow<'static, str> },
77 Const { is_parameter: bool },
78}
79
80impl InferenceDiagnosticsData {
81 fn can_add_more_info(&self) -> bool {
82 !(self.name == "_" && #[allow(non_exhaustive_omitted_patterns)] match self.kind {
UnderspecifiedArgKind::Type { .. } => true,
_ => false,
}matches!(self.kind, UnderspecifiedArgKind::Type { .. }))
83 }
84
85 fn where_x_is_kind<'tcx>(&self, infcx: &InferCtxt<'tcx>, in_type: Ty<'tcx>) -> &'static str {
86 if in_type.is_ty_or_numeric_infer() {
87 ""
88 } else if self.name == "_" {
89 let displayed_ty = infcx
90 .resolve_vars_if_possible(in_type)
91 .fold_with(&mut ClosureEraser { infcx, depth: 0 });
92 if displayed_ty.is_ty_or_numeric_infer() {
93 ""
94 } else {
95 match displayed_ty
96 .walk()
97 .filter_map(TyOrConstInferVar::maybe_from_generic_arg)
98 .take(2)
99 .count()
100 {
101 0 => "",
102 1 => "underscore_single",
103 _ => "underscore_multiple",
104 }
105 }
106 } else {
107 "has_name"
108 }
109 }
110
111 fn make_bad_error(&self, span: Span) -> InferenceBadError<'_> {
115 let has_parent = self.parent.is_some();
116 let bad_kind = if self.can_add_more_info() { "more_info" } else { "other" };
117 let (parent_prefix, parent_name) = self
118 .parent
119 .as_ref()
120 .map(|parent| (parent.prefix, parent.name.clone()))
121 .unwrap_or_default();
122 InferenceBadError {
123 span,
124 bad_kind,
125 prefix_kind: self.kind.clone(),
126 prefix: self.kind.try_get_prefix().unwrap_or_default(),
127 name: self.name.clone(),
128 has_parent,
129 parent_prefix,
130 parent_name,
131 }
132 }
133}
134
135impl InferenceDiagnosticsParentData {
136 fn for_parent_def_id(
137 tcx: TyCtxt<'_>,
138 parent_def_id: DefId,
139 ) -> Option<InferenceDiagnosticsParentData> {
140 let parent_name =
141 tcx.def_key(parent_def_id).disambiguated_data.data.get_opt_name()?.to_string();
142
143 Some(InferenceDiagnosticsParentData {
144 prefix: tcx.def_descr(parent_def_id),
145 name: parent_name,
146 })
147 }
148
149 fn for_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<InferenceDiagnosticsParentData> {
150 Self::for_parent_def_id(tcx, tcx.parent(def_id))
151 }
152}
153
154impl IntoDiagArg for UnderspecifiedArgKind {
155 fn into_diag_arg(self, _: &mut Option<std::path::PathBuf>) -> rustc_errors::DiagArgValue {
156 let kind = match self {
157 Self::Type { .. } => "type",
158 Self::Const { is_parameter: true } => "const_with_param",
159 Self::Const { is_parameter: false } => "const",
160 };
161 rustc_errors::DiagArgValue::Str(kind.into())
162 }
163}
164
165impl UnderspecifiedArgKind {
166 fn try_get_prefix(&self) -> Option<&str> {
167 match self {
168 Self::Type { prefix } => Some(prefix.as_ref()),
169 Self::Const { .. } => None,
170 }
171 }
172}
173
174struct ClosureEraser<'a, 'tcx> {
175 infcx: &'a InferCtxt<'tcx>,
176 depth: usize,
177}
178
179impl<'a, 'tcx> ClosureEraser<'a, 'tcx> {
180 fn new_infer(&mut self) -> Ty<'tcx> {
181 self.infcx.next_ty_var(DUMMY_SP)
182 }
183}
184
185impl<'a, 'tcx> TypeFolder<TyCtxt<'tcx>> for ClosureEraser<'a, 'tcx> {
186 fn cx(&self) -> TyCtxt<'tcx> {
187 self.infcx.tcx
188 }
189
190 fn fold_ty(&mut self, ty: Ty<'tcx>) -> Ty<'tcx> {
191 self.depth += 1;
192 let ty = match ty.kind() {
193 ty::Closure(_, args) => {
194 let closure_sig = args.as_closure().sig();
197 Ty::new_fn_ptr(
198 self.cx(),
199 self.cx().signature_unclosure(closure_sig, hir::Safety::Safe),
200 )
201 }
202 ty::Adt(_, args) if !args.iter().any(|a| a.has_infer()) => {
203 self.new_infer()
208 }
209 ty::Adt(def, args) => {
210 let generics = self.cx().generics_of(def.did());
211 let generics: Vec<bool> = generics
212 .own_params
213 .iter()
214 .map(|param| param.default_value(self.cx()).is_some())
215 .collect();
216 let ty = Ty::new_adt(
217 self.cx(),
218 *def,
219 self.cx().mk_args_from_iter(generics.into_iter().zip(args.iter()).map(
220 |(has_default, arg)| {
221 if arg.has_infer() {
222 arg.fold_with(self)
228 } else if has_default {
229 arg
236 } else if let GenericArgKind::Type(_) = arg.kind() {
237 self.new_infer().into()
239 } else {
240 arg.fold_with(self)
241 }
242 },
243 )),
244 );
245 ty
246 }
247 _ if ty.has_infer() => {
248 ty.super_fold_with(self)
252 }
253 _ if self.depth == 1 => ty.super_fold_with(self),
256 _ => self.new_infer(),
259 };
260 self.depth -= 1;
261 ty
262 }
263
264 fn fold_const(&mut self, c: ty::Const<'tcx>) -> ty::Const<'tcx> {
265 c
267 }
268}
269
270fn fmt_printer<'a, 'tcx>(infcx: &'a InferCtxt<'tcx>, ns: Namespace) -> FmtPrinter<'a, 'tcx> {
271 let mut p = FmtPrinter::new(infcx.tcx, ns);
272 let ty_getter = move |ty_vid| {
273 if infcx.try_resolve_ty_var(ty_vid).is_ok() {
274 {
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/need_type_info.rs:274",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::WARN,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(274u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::WARN <=
::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!("resolved ty var in error message")
as &dyn Value))])
});
} else { ; }
};warn!("resolved ty var in error message");
275 }
276
277 let var_origin = infcx.type_var_origin(ty_vid);
278 if let Some(def_id) = var_origin.param_def_id
279 && infcx.tcx.def_kind(def_id) == DefKind::TyParam
282 && let name = infcx.tcx.item_name(def_id)
283 && !var_origin.span.from_expansion()
284 {
285 let generics = infcx.tcx.generics_of(infcx.tcx.parent(def_id));
286 let idx = generics.param_def_id_to_index(infcx.tcx, def_id).unwrap();
287 let generic_param_def = generics.param_at(idx as usize, infcx.tcx);
288 if let ty::GenericParamDefKind::Type { synthetic: true, .. } = generic_param_def.kind {
289 None
290 } else {
291 Some(name)
292 }
293 } else {
294 None
295 }
296 };
297 p.ty_infer_name_resolver = Some(Box::new(ty_getter));
298 let const_getter =
299 move |ct_vid| Some(infcx.tcx.item_name(infcx.const_var_origin(ct_vid)?.param_def_id?));
300 p.const_infer_name_resolver = Some(Box::new(const_getter));
301 p
302}
303
304fn ty_to_string<'tcx>(
305 infcx: &InferCtxt<'tcx>,
306 ty: Ty<'tcx>,
307 called_method_def_id: Option<DefId>,
308) -> String {
309 let mut p = fmt_printer(infcx, Namespace::TypeNS);
310 let ty = infcx.resolve_vars_if_possible(ty);
311 let ty = ty.fold_with(&mut ClosureEraser { infcx, depth: 0 });
314
315 match (ty.kind(), called_method_def_id) {
316 (ty::FnDef(..), _) => {
319 ty.fn_sig(infcx.tcx).print(&mut p).unwrap();
320 p.into_buffer()
321 }
322 (_, Some(def_id))
323 if ty.is_ty_or_numeric_infer()
324 && infcx.tcx.get_diagnostic_item(sym::iterator_collect_fn) == Some(def_id) =>
325 {
326 "Vec<_>".to_string()
327 }
328 _ if ty.is_ty_or_numeric_infer() => "/* Type */".to_string(),
329 _ => {
330 ty.print(&mut p).unwrap();
331 p.into_buffer()
332 }
333 }
334}
335
336fn closure_as_fn_str<'tcx>(infcx: &InferCtxt<'tcx>, ty: Ty<'tcx>) -> String {
340 let ty::Closure(_, args) = ty.kind() else {
341 ::rustc_middle::util::bug::bug_fmt(format_args!("cannot convert non-closure to fn str in `closure_as_fn_str`"))bug!("cannot convert non-closure to fn str in `closure_as_fn_str`")
342 };
343 let fn_sig = args.as_closure().sig();
344 let args = fn_sig
345 .inputs()
346 .skip_binder()
347 .iter()
348 .next()
349 .map(|args| {
350 args.tuple_fields()
351 .iter()
352 .map(|arg| ty_to_string(infcx, arg, None))
353 .collect::<Vec<_>>()
354 .join(", ")
355 })
356 .unwrap_or_default();
357 let ret = if fn_sig.output().skip_binder().is_unit() {
358 String::new()
359 } else {
360 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!(" -> {0}",
ty_to_string(infcx, fn_sig.output().skip_binder(), None)))
})format!(" -> {}", ty_to_string(infcx, fn_sig.output().skip_binder(), None))
361 };
362 ::alloc::__export::must_use({
::alloc::fmt::format(format_args!("fn({0}){1}", args, ret))
})format!("fn({args}){ret}")
363}
364
365impl<'a, 'tcx> TypeErrCtxt<'a, 'tcx> {
366 pub fn extract_inference_diagnostics_data(
369 &self,
370 term: Term<'tcx>,
371 highlight: ty::print::RegionHighlightMode<'tcx>,
372 ) -> InferenceDiagnosticsData {
373 let tcx = self.tcx;
374 match term.kind() {
375 TermKind::Ty(ty) => {
376 if let ty::Infer(ty::TyVar(ty_vid)) = *ty.kind() {
377 let var_origin = self.infcx.type_var_origin(ty_vid);
378 if let Some(def_id) = var_origin.param_def_id
379 && self.tcx.def_kind(def_id) == DefKind::TyParam
382 && !var_origin.span.from_expansion()
383 {
384 return InferenceDiagnosticsData {
385 name: self.tcx.item_name(def_id).to_string(),
386 span: Some(var_origin.span),
387 kind: UnderspecifiedArgKind::Type { prefix: "type parameter".into() },
388 parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id),
389 };
390 }
391 }
392
393 InferenceDiagnosticsData {
394 name: Highlighted { highlight, ns: Namespace::TypeNS, tcx, value: ty }
395 .to_string(),
396 span: None,
397 kind: UnderspecifiedArgKind::Type { prefix: ty.prefix_string(self.tcx) },
398 parent: None,
399 }
400 }
401 TermKind::Const(ct) => {
402 if let ty::ConstKind::Infer(InferConst::Var(vid)) = ct.kind() {
403 let origin = self.const_var_origin(vid).expect("expected unresolved const var");
404 if let Some(def_id) = origin.param_def_id {
405 return InferenceDiagnosticsData {
406 name: self.tcx.item_name(def_id).to_string(),
407 span: Some(origin.span),
408 kind: UnderspecifiedArgKind::Const { is_parameter: true },
409 parent: InferenceDiagnosticsParentData::for_def_id(self.tcx, def_id),
410 };
411 }
412
413 if true {
if !!origin.span.is_dummy() {
::core::panicking::panic("assertion failed: !origin.span.is_dummy()")
};
};debug_assert!(!origin.span.is_dummy());
414 InferenceDiagnosticsData {
415 name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
416 .to_string(),
417 span: Some(origin.span),
418 kind: UnderspecifiedArgKind::Const { is_parameter: false },
419 parent: None,
420 }
421 } else {
422 InferenceDiagnosticsData {
429 name: Highlighted { highlight, ns: Namespace::ValueNS, tcx, value: ct }
430 .to_string(),
431 span: None,
432 kind: UnderspecifiedArgKind::Const { is_parameter: false },
433 parent: None,
434 }
435 }
436 }
437 }
438 }
439
440 fn bad_inference_failure_err(
443 &self,
444 span: Span,
445 arg_data: InferenceDiagnosticsData,
446 error_code: TypeAnnotationNeeded,
447 ) -> Diag<'a> {
448 let source_kind = "other";
449 let source_name = "";
450 let failure_span = None;
451 let infer_subdiags = Vec::new();
452 let multi_suggestions = Vec::new();
453 let bad_label = Some(arg_data.make_bad_error(span));
454 match error_code {
455 TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
456 span,
457 source_kind,
458 source_name,
459 failure_span,
460 infer_subdiags,
461 multi_suggestions,
462 bad_label,
463 }),
464 TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
465 span,
466 source_kind,
467 source_name,
468 failure_span,
469 infer_subdiags,
470 multi_suggestions,
471 bad_label,
472 }),
473 TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
474 span,
475 source_kind,
476 source_name,
477 failure_span,
478 infer_subdiags,
479 multi_suggestions,
480 bad_label,
481 }),
482 }
483 }
484
485 #[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("emit_inference_failure_err",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(485u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["body_def_id",
"failure_span", "term", "should_label_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(&body_def_id)
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(&failure_span)
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(&term)
as &dyn Value)),
(&::tracing::__macro_support::Iterator::next(&mut iter).expect("FieldSet corrupted (this is a bug)"),
::tracing::__macro_support::Option::Some(&should_label_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: Diag<'a> = loop {};
return __tracing_attr_fake_return;
}
{
self.emit_inference_failure_err_with_type_hint(body_def_id,
failure_span, term, error_code, should_label_span, None)
}
}
}#[instrument(level = "debug", skip(self, error_code))]
486 pub fn emit_inference_failure_err(
487 &self,
488 body_def_id: LocalDefId,
489 failure_span: Span,
490 term: Term<'tcx>,
491 error_code: TypeAnnotationNeeded,
492 should_label_span: bool,
493 ) -> Diag<'a> {
494 self.emit_inference_failure_err_with_type_hint(
495 body_def_id,
496 failure_span,
497 term,
498 error_code,
499 should_label_span,
500 None,
501 )
502 }
503
504 pub fn emit_inference_failure_err_with_type_hint(
505 &self,
506 body_def_id: LocalDefId,
507 failure_span: Span,
508 term: Term<'tcx>,
509 error_code: TypeAnnotationNeeded,
510 should_label_span: bool,
511 ty: Option<Ty<'tcx>>,
512 ) -> Diag<'a> {
513 let term = self.resolve_vars_if_possible(term);
514 let arg_data = self
515 .extract_inference_diagnostics_data(term, ty::print::RegionHighlightMode::default());
516
517 let Some(typeck_results) = &self.typeck_results else {
518 return self.bad_inference_failure_err(failure_span, arg_data, error_code);
522 };
523
524 let mut local_visitor = FindInferSourceVisitor::new(self, typeck_results, term, ty);
525 if let Some(body) =
526 self.tcx.hir_maybe_body_owned_by(self.tcx.typeck_root_def_id_local(body_def_id))
527 {
528 let expr = body.value;
529 local_visitor.visit_expr(expr);
530 }
531
532 let Some(InferSource { span, kind }) = local_visitor.infer_source else {
533 let silence = if let DefKind::AssocFn = self.tcx.def_kind(body_def_id)
534 && let parent = self.tcx.local_parent(body_def_id)
535 && self.tcx.is_automatically_derived(parent.to_def_id())
536 && let hir::Node::Item(item) = self.tcx.hir_node_by_def_id(parent)
537 && let hir::ItemKind::Impl(imp) = item.kind
538 && let hir::TyKind::Path(hir::QPath::Resolved(_, path)) = imp.self_ty.kind
539 && let Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, def_id) = path.res
540 && let Some(def_id) = def_id.as_local()
541 && let hir::Node::Item(item) = self.tcx.hir_node_by_def_id(def_id)
542 {
543 item.kind.recovered()
548 } else {
549 false
550 };
551 let mut err = self.bad_inference_failure_err(failure_span, arg_data, error_code);
552 if silence {
553 err.downgrade_to_delayed_bug();
554 }
555 return err;
556 };
557
558 let (source_kind, name, long_ty_path) = kind.ty_localized_msg(self);
559 let failure_span = if should_label_span && !failure_span.overlaps(span) {
560 Some(failure_span)
561 } else {
562 None
563 };
564
565 let mut infer_subdiags = Vec::new();
566 let mut multi_suggestions = Vec::new();
567 match kind {
568 InferSourceKind::LetBinding { insert_span, pattern_name, ty, def_id } => {
569 infer_subdiags.push(SourceKindSubdiag::LetLike {
570 span: insert_span,
571 name: pattern_name.map(|name| name.to_string()).unwrap_or_else(String::new),
572 x_kind: arg_data.where_x_is_kind(self.infcx, ty),
573 prefix_kind: arg_data.kind.clone(),
574 prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
575 arg_name: arg_data.name,
576 kind: if pattern_name.is_some() { "with_pattern" } else { "other" },
577 type_name: ty_to_string(self, ty, def_id),
578 });
579 }
580 InferSourceKind::ClosureArg { insert_span, ty, .. } => {
581 infer_subdiags.push(SourceKindSubdiag::LetLike {
582 span: insert_span,
583 name: String::new(),
584 x_kind: arg_data.where_x_is_kind(self.infcx, ty),
585 prefix_kind: arg_data.kind.clone(),
586 prefix: arg_data.kind.try_get_prefix().unwrap_or_default(),
587 arg_name: arg_data.name,
588 kind: "closure",
589 type_name: ty_to_string(self, ty, None),
590 });
591 }
592 InferSourceKind::GenericArg {
593 insert_span,
594 argument_index,
595 generics_def_id,
596 def_id: _,
597 generic_args,
598 have_turbofish,
599 hir_id,
600 } => {
601 let generics = self.tcx.generics_of(generics_def_id);
602 let is_type = term.as_type().is_some();
603
604 let (parent_exists, parent_prefix, parent_name) =
605 InferenceDiagnosticsParentData::for_parent_def_id(self.tcx, generics_def_id)
606 .map_or((false, String::new(), String::new()), |parent| {
607 (true, parent.prefix.to_string(), parent.name)
608 });
609
610 let param = &generics.own_params[argument_index];
611 let param_name = param.name.to_string();
612
613 infer_subdiags.push(SourceKindSubdiag::GenericLabel {
614 span,
615 is_type,
616 param_name: param_name.clone(),
617 parent_exists,
618 parent_prefix,
619 parent_name,
620 });
621
622 let mut used_fallback = false;
623 let args = if self.tcx.get_diagnostic_item(sym::iterator_collect_fn)
624 == Some(generics_def_id)
625 {
626 if let hir::Node::Expr(expr) = self.tcx.parent_hir_node(hir_id)
627 && let hir::ExprKind::Call(expr, _args) = expr.kind
628 && let hir::ExprKind::Path(hir::QPath::Resolved(_, path)) = expr.kind
629 && let Res::Def(DefKind::AssocFn, def_id) = path.res
630 && let Some(try_trait) = self.tcx.lang_items().try_trait()
631 && try_trait == self.tcx.parent(def_id)
632 && let DefKind::Fn | DefKind::AssocFn =
633 self.tcx.def_kind(body_def_id.to_def_id())
634 && let ret = self
635 .tcx
636 .fn_sig(body_def_id.to_def_id())
637 .instantiate_identity()
638 .skip_binder()
639 .output()
640 && let ty::Adt(adt, _args) = ret.kind()
641 && let Some(sym::Option | sym::Result) =
642 self.tcx.get_diagnostic_name(adt.did())
643 {
644 if let Some(sym::Option) = self.tcx.get_diagnostic_name(adt.did()) {
645 "Option<_>".to_string()
646 } else {
647 "Result<_, _>".to_string()
648 }
649 } else {
650 "Vec<_>".to_string()
651 }
652 } else {
653 let mut p = fmt_printer(self, Namespace::TypeNS);
654 p.comma_sep(generic_args.iter().copied().map(|arg| {
655 if arg.is_suggestable(self.tcx, true) {
656 used_fallback = true;
657 return arg;
658 }
659 match arg.kind() {
660 GenericArgKind::Lifetime(_) => ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected lifetime"))bug!("unexpected lifetime"),
661 GenericArgKind::Type(_) => self.next_ty_var(DUMMY_SP).into(),
662 GenericArgKind::Const(_) => self.next_const_var(DUMMY_SP).into(),
663 }
664 }))
665 .unwrap();
666 p.into_buffer()
667 };
668
669 if !have_turbofish {
670 if generic_args.len() == 1 && used_fallback {
671 match param.kind {
672 GenericParamDefKind::Type { .. } => {
673 infer_subdiags.push(SourceKindSubdiag::GenericTypeSuggestion {
674 span: insert_span,
675 param: param_name,
676 });
677 }
678 GenericParamDefKind::Const { .. } => {
679 infer_subdiags.push(SourceKindSubdiag::ConstGenericSuggestion {
680 span: insert_span,
681 param: param_name,
682 });
683 }
684 GenericParamDefKind::Lifetime => {
685 ::rustc_middle::util::bug::bug_fmt(format_args!("unexpected lifetime"))bug!("unexpected lifetime")
686 }
687 }
688 } else {
689 infer_subdiags.push(SourceKindSubdiag::GenericSuggestion {
690 span: insert_span,
691 arg_count: generic_args.len(),
692 args,
693 });
694 }
695 }
696 }
697 InferSourceKind::FullyQualifiedMethodCall { receiver, successor, args, def_id } => {
698 let placeholder = Some(self.next_ty_var(DUMMY_SP));
699 if let Some(args) = args.make_suggestable(self.infcx.tcx, true, placeholder) {
700 let mut p = fmt_printer(self, Namespace::ValueNS);
701 p.print_def_path(def_id, args).unwrap();
702 let def_path = p.into_buffer();
703
704 let adjustment = match typeck_results.expr_adjustments(receiver) {
708 [
709 Adjustment { kind: Adjust::Deref(DerefAdjustKind::Builtin), target: _ },
710 ..,
711 Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(..)), target: _ },
712 ] => "",
713 [
714 ..,
715 Adjustment { kind: Adjust::Borrow(AutoBorrow::Ref(mut_)), target: _ },
716 ] => hir::Mutability::from(*mut_).ref_prefix_str(),
717 _ => "",
718 };
719
720 multi_suggestions.push(SourceKindMultiSuggestion::new_fully_qualified(
721 receiver.span,
722 def_path,
723 adjustment,
724 successor,
725 ));
726 }
727 }
728 InferSourceKind::ClosureReturn { ty, data, should_wrap_expr } => {
729 let placeholder = Some(self.next_ty_var(DUMMY_SP));
730 if let Some(ty) = ty.make_suggestable(self.infcx.tcx, true, placeholder) {
731 let ty_info = ty_to_string(self, ty, None);
732 multi_suggestions.push(SourceKindMultiSuggestion::new_closure_return(
733 ty_info,
734 data,
735 should_wrap_expr,
736 ));
737 }
738 }
739 }
740 let mut err = match error_code {
741 TypeAnnotationNeeded::E0282 => self.dcx().create_err(AnnotationRequired {
742 span,
743 source_kind,
744 source_name: &name,
745 failure_span,
746 infer_subdiags,
747 multi_suggestions,
748 bad_label: None,
749 }),
750 TypeAnnotationNeeded::E0283 => self.dcx().create_err(AmbiguousImpl {
751 span,
752 source_kind,
753 source_name: &name,
754 failure_span,
755 infer_subdiags,
756 multi_suggestions,
757 bad_label: None,
758 }),
759 TypeAnnotationNeeded::E0284 => self.dcx().create_err(AmbiguousReturn {
760 span,
761 source_kind,
762 source_name: &name,
763 failure_span,
764 infer_subdiags,
765 multi_suggestions,
766 bad_label: None,
767 }),
768 };
769 *err.long_ty_path() = long_ty_path;
770 if let InferSourceKind::ClosureArg { kind: PatKind::Err(_), .. } = kind {
771 err.downgrade_to_delayed_bug();
773 }
774 err
775 }
776}
777
778#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InferSource<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field2_finish(f, "InferSource",
"span", &self.span, "kind", &&self.kind)
}
}Debug)]
779struct InferSource<'tcx> {
780 span: Span,
781 kind: InferSourceKind<'tcx>,
782}
783
784#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InferSourceKind<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
match self {
InferSourceKind::LetBinding {
insert_span: __self_0,
pattern_name: __self_1,
ty: __self_2,
def_id: __self_3 } =>
::core::fmt::Formatter::debug_struct_field4_finish(f,
"LetBinding", "insert_span", __self_0, "pattern_name",
__self_1, "ty", __self_2, "def_id", &__self_3),
InferSourceKind::ClosureArg {
insert_span: __self_0, ty: __self_1, kind: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"ClosureArg", "insert_span", __self_0, "ty", __self_1,
"kind", &__self_2),
InferSourceKind::GenericArg {
insert_span: __self_0,
argument_index: __self_1,
generics_def_id: __self_2,
def_id: __self_3,
generic_args: __self_4,
have_turbofish: __self_5,
hir_id: __self_6 } => {
let names: &'static _ =
&["insert_span", "argument_index", "generics_def_id",
"def_id", "generic_args", "have_turbofish", "hir_id"];
let values: &[&dyn ::core::fmt::Debug] =
&[__self_0, __self_1, __self_2, __self_3, __self_4,
__self_5, &__self_6];
::core::fmt::Formatter::debug_struct_fields_finish(f,
"GenericArg", names, values)
}
InferSourceKind::FullyQualifiedMethodCall {
receiver: __self_0,
successor: __self_1,
args: __self_2,
def_id: __self_3 } =>
::core::fmt::Formatter::debug_struct_field4_finish(f,
"FullyQualifiedMethodCall", "receiver", __self_0,
"successor", __self_1, "args", __self_2, "def_id",
&__self_3),
InferSourceKind::ClosureReturn {
ty: __self_0, data: __self_1, should_wrap_expr: __self_2 } =>
::core::fmt::Formatter::debug_struct_field3_finish(f,
"ClosureReturn", "ty", __self_0, "data", __self_1,
"should_wrap_expr", &__self_2),
}
}
}Debug)]
785enum InferSourceKind<'tcx> {
786 LetBinding {
787 insert_span: Span,
788 pattern_name: Option<Ident>,
789 ty: Ty<'tcx>,
790 def_id: Option<DefId>,
791 },
792 ClosureArg {
793 insert_span: Span,
794 ty: Ty<'tcx>,
795 kind: PatKind<'tcx>,
796 },
797 GenericArg {
798 insert_span: Span,
799 argument_index: usize,
800 generics_def_id: DefId,
801 def_id: DefId,
802 generic_args: &'tcx [GenericArg<'tcx>],
803 have_turbofish: bool,
804 hir_id: HirId,
805 },
806 FullyQualifiedMethodCall {
807 receiver: &'tcx Expr<'tcx>,
808 successor: (&'static str, BytePos),
811 args: GenericArgsRef<'tcx>,
812 def_id: DefId,
813 },
814 ClosureReturn {
815 ty: Ty<'tcx>,
816 data: &'tcx FnRetTy<'tcx>,
817 should_wrap_expr: Option<Span>,
818 },
819}
820
821impl<'tcx> InferSource<'tcx> {
822 fn from_expansion(&self) -> bool {
823 let source_from_expansion = match self.kind {
824 InferSourceKind::LetBinding { insert_span, .. }
825 | InferSourceKind::ClosureArg { insert_span, .. }
826 | InferSourceKind::GenericArg { insert_span, .. } => insert_span.from_expansion(),
827 InferSourceKind::FullyQualifiedMethodCall { receiver, .. } => {
828 receiver.span.from_expansion()
829 }
830 InferSourceKind::ClosureReturn { data, should_wrap_expr, .. } => {
831 data.span().from_expansion() || should_wrap_expr.is_some_and(Span::from_expansion)
832 }
833 };
834 source_from_expansion || self.span.from_expansion()
835 }
836}
837
838impl<'tcx> InferSourceKind<'tcx> {
839 fn ty_localized_msg(&self, infcx: &InferCtxt<'tcx>) -> (&'static str, String, Option<PathBuf>) {
840 let mut long_ty_path = None;
841 match *self {
842 InferSourceKind::LetBinding { ty, .. }
843 | InferSourceKind::ClosureArg { ty, .. }
844 | InferSourceKind::ClosureReturn { ty, .. } => {
845 if ty.is_closure() {
846 ("closure", closure_as_fn_str(infcx, ty), long_ty_path)
847 } else if ty.is_ty_or_numeric_infer()
848 || ty.is_primitive()
849 || #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Adt(_, args) if
args.types().count() == 0 && args.consts().count() == 0 => true,
_ => false,
}matches!(
850 ty.kind(),
851 ty::Adt(_, args)
852 if args.types().count() == 0 && args.consts().count() == 0
853 )
854 {
855 ("other", String::new(), long_ty_path)
859 } else {
860 ("normal", infcx.tcx.short_string(ty, &mut long_ty_path), long_ty_path)
861 }
862 }
863 InferSourceKind::GenericArg { .. }
865 | InferSourceKind::FullyQualifiedMethodCall { .. } => {
866 ("other", String::new(), long_ty_path)
867 }
868 }
869 }
870}
871
872#[derive(#[automatically_derived]
impl<'tcx> ::core::fmt::Debug for InsertableGenericArgs<'tcx> {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field5_finish(f,
"InsertableGenericArgs", "insert_span", &self.insert_span, "args",
&self.args, "generics_def_id", &self.generics_def_id, "def_id",
&self.def_id, "have_turbofish", &&self.have_turbofish)
}
}Debug)]
873struct InsertableGenericArgs<'tcx> {
874 insert_span: Span,
875 args: GenericArgsRef<'tcx>,
876 generics_def_id: DefId,
877 def_id: DefId,
878 have_turbofish: bool,
879}
880
881struct FindInferSourceVisitor<'a, 'tcx> {
889 tecx: &'a TypeErrCtxt<'a, 'tcx>,
890 typeck_results: &'a TypeckResults<'tcx>,
891
892 target: Term<'tcx>,
893 ty: Option<Ty<'tcx>>,
894
895 attempt: usize,
896 infer_source_cost: usize,
897 infer_source: Option<InferSource<'tcx>>,
898}
899
900impl<'a, 'tcx> FindInferSourceVisitor<'a, 'tcx> {
901 fn new(
902 tecx: &'a TypeErrCtxt<'a, 'tcx>,
903 typeck_results: &'a TypeckResults<'tcx>,
904 target: Term<'tcx>,
905 ty: Option<Ty<'tcx>>,
906 ) -> Self {
907 FindInferSourceVisitor {
908 tecx,
909 typeck_results,
910
911 target,
912 ty,
913
914 attempt: 0,
915 infer_source_cost: usize::MAX,
916 infer_source: None,
917 }
918 }
919
920 fn source_cost(&self, source: &InferSource<'tcx>) -> usize {
925 #[derive(#[automatically_derived]
impl<'tcx> ::core::clone::Clone for CostCtxt<'tcx> {
#[inline]
fn clone(&self) -> CostCtxt<'tcx> {
let _: ::core::clone::AssertParamIsClone<TyCtxt<'tcx>>;
*self
}
}Clone, #[automatically_derived]
impl<'tcx> ::core::marker::Copy for CostCtxt<'tcx> { }Copy)]
926 struct CostCtxt<'tcx> {
927 tcx: TyCtxt<'tcx>,
928 }
929 impl<'tcx> CostCtxt<'tcx> {
930 fn arg_cost(self, arg: GenericArg<'tcx>) -> usize {
931 match arg.kind() {
932 GenericArgKind::Lifetime(_) => 0, GenericArgKind::Type(ty) => self.ty_cost(ty),
934 GenericArgKind::Const(_) => 3, }
936 }
937 fn ty_cost(self, ty: Ty<'tcx>) -> usize {
938 match *ty.kind() {
939 ty::Closure(..) => 1000,
940 ty::FnDef(..) => 150,
941 ty::FnPtr(..) => 30,
942 ty::Adt(def, args) => {
943 5 + self
944 .tcx
945 .generics_of(def.did())
946 .own_args_no_defaults(self.tcx, args)
947 .iter()
948 .map(|&arg| self.arg_cost(arg))
949 .sum::<usize>()
950 }
951 ty::Tuple(args) => 5 + args.iter().map(|arg| self.ty_cost(arg)).sum::<usize>(),
952 ty::Ref(_, ty, _) => 2 + self.ty_cost(ty),
953 ty::Infer(..) => 0,
954 _ => 1,
955 }
956 }
957 }
958
959 let tcx = self.tecx.tcx;
961 let ctx = CostCtxt { tcx };
962 match source.kind {
963 InferSourceKind::LetBinding { ty, .. } => ctx.ty_cost(ty),
964 InferSourceKind::ClosureArg { ty, .. } => ctx.ty_cost(ty),
965 InferSourceKind::GenericArg { def_id, generic_args, .. } => {
966 let variant_cost = match tcx.def_kind(def_id) {
967 DefKind::Variant | DefKind::Ctor(CtorOf::Variant, _) => 15,
969 _ => 10,
970 };
971 variant_cost + generic_args.iter().map(|&arg| ctx.arg_cost(arg)).sum::<usize>()
972 }
973 InferSourceKind::FullyQualifiedMethodCall { args, .. } => {
974 20 + args.iter().map(|arg| ctx.arg_cost(arg)).sum::<usize>()
975 }
976 InferSourceKind::ClosureReturn { ty, should_wrap_expr, .. } => {
977 30 + ctx.ty_cost(ty) + if should_wrap_expr.is_some() { 10 } else { 0 }
978 }
979 }
980 }
981
982 #[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("update_infer_source",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(984u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["new_source"],
::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(&new_source)
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;
}
{
if new_source.from_expansion() { return; }
let cost = self.source_cost(&new_source) + self.attempt;
{
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/need_type_info.rs:991",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(991u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["cost"],
::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(&cost) as
&dyn Value))])
});
} else { ; }
};
self.attempt += 1;
if let Some(InferSource {
kind: InferSourceKind::GenericArg { def_id: did, .. }, .. })
= self.infer_source &&
let InferSourceKind::LetBinding { ref ty, ref mut def_id, ..
} = new_source.kind && ty.is_ty_or_numeric_infer() {
*def_id = Some(did);
}
if cost < self.infer_source_cost {
self.infer_source_cost = cost;
self.infer_source = Some(new_source);
}
}
}
}#[instrument(level = "debug", skip(self))]
985 fn update_infer_source(&mut self, mut new_source: InferSource<'tcx>) {
986 if new_source.from_expansion() {
987 return;
988 }
989
990 let cost = self.source_cost(&new_source) + self.attempt;
991 debug!(?cost);
992 self.attempt += 1;
993 if let Some(InferSource { kind: InferSourceKind::GenericArg { def_id: did, .. }, .. }) =
994 self.infer_source
995 && let InferSourceKind::LetBinding { ref ty, ref mut def_id, .. } = new_source.kind
996 && ty.is_ty_or_numeric_infer()
997 {
998 *def_id = Some(did);
1001 }
1002
1003 if cost < self.infer_source_cost {
1004 self.infer_source_cost = cost;
1005 self.infer_source = Some(new_source);
1006 }
1007 }
1008
1009 fn node_args_opt(&self, hir_id: HirId) -> Option<GenericArgsRef<'tcx>> {
1010 let args = self.typeck_results.node_args_opt(hir_id);
1011 self.tecx.resolve_vars_if_possible(args)
1012 }
1013
1014 fn opt_node_type(&self, hir_id: HirId) -> Option<Ty<'tcx>> {
1015 let ty = self.typeck_results.node_type_opt(hir_id);
1016 self.tecx.resolve_vars_if_possible(ty)
1017 }
1018
1019 fn generic_arg_is_target(&self, arg: GenericArg<'tcx>) -> bool {
1022 if arg == self.target.into() {
1023 return true;
1024 }
1025
1026 match (arg.kind(), self.target.kind()) {
1027 (GenericArgKind::Type(inner_ty), TermKind::Ty(target_ty)) => {
1028 use ty::{Infer, TyVar};
1029 match (inner_ty.kind(), target_ty.kind()) {
1030 (&Infer(TyVar(a_vid)), &Infer(TyVar(b_vid))) => {
1031 self.tecx.sub_unification_table_root_var(a_vid)
1032 == self.tecx.sub_unification_table_root_var(b_vid)
1033 }
1034 _ => false,
1035 }
1036 }
1037 (GenericArgKind::Const(inner_ct), TermKind::Const(target_ct)) => {
1038 match (inner_ct.kind(), target_ct.kind()) {
1039 (
1040 ty::ConstKind::Infer(ty::InferConst::Var(a_vid)),
1041 ty::ConstKind::Infer(ty::InferConst::Var(b_vid)),
1042 ) => self.tecx.root_const_var(a_vid) == self.tecx.root_const_var(b_vid),
1043 _ => false,
1044 }
1045 }
1046 _ => false,
1047 }
1048 }
1049
1050 fn generic_arg_contains_target(&self, arg: GenericArg<'tcx>) -> bool {
1053 let mut walker = arg.walk();
1054 while let Some(inner) = walker.next() {
1055 if self.generic_arg_is_target(inner) {
1056 return true;
1057 }
1058 match inner.kind() {
1059 GenericArgKind::Lifetime(_) => {}
1060 GenericArgKind::Type(ty) => {
1061 if #[allow(non_exhaustive_omitted_patterns)] match ty.kind() {
ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. }) | ty::Closure(..) |
ty::CoroutineClosure(..) | ty::Coroutine(..) => true,
_ => false,
}matches!(
1062 ty.kind(),
1063 ty::Alias(ty::AliasTy { kind: ty::Opaque { .. }, .. })
1064 | ty::Closure(..)
1065 | ty::CoroutineClosure(..)
1066 | ty::Coroutine(..)
1067 ) {
1068 walker.skip_current_subtree();
1079 }
1080 }
1081 GenericArgKind::Const(ct) => {
1082 if #[allow(non_exhaustive_omitted_patterns)] match ct.kind() {
ty::ConstKind::Unevaluated(..) => true,
_ => false,
}matches!(ct.kind(), ty::ConstKind::Unevaluated(..)) {
1083 walker.skip_current_subtree();
1086 }
1087 }
1088 }
1089 }
1090 false
1091 }
1092
1093 fn expr_inferred_arg_iter(
1094 &self,
1095 expr: &'tcx hir::Expr<'tcx>,
1096 ) -> Box<dyn Iterator<Item = InsertableGenericArgs<'tcx>> + 'a> {
1097 let tcx = self.tecx.tcx;
1098 match expr.kind {
1099 hir::ExprKind::Path(ref path) => {
1100 if let Some(args) = self.node_args_opt(expr.hir_id) {
1101 return self.path_inferred_arg_iter(expr.hir_id, args, path);
1102 }
1103 }
1104 hir::ExprKind::Struct(&hir::QPath::Resolved(_self_ty, path), _, _)
1115 if #[allow(non_exhaustive_omitted_patterns)] match path.res {
Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _) => true,
_ => false,
}matches!(path.res, Res::Def(DefKind::Struct | DefKind::Enum | DefKind::Union, _)) => {
1122 if let Some(ty) = self.opt_node_type(expr.hir_id)
1123 && let ty::Adt(_, args) = ty.kind()
1124 {
1125 return Box::new(self.resolved_path_inferred_arg_iter(path, args));
1126 }
1127 }
1128 hir::ExprKind::MethodCall(segment, ..) => {
1129 if let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id) {
1130 let generics = tcx.generics_of(def_id);
1131 let insertable = try {
1132 if generics.has_impl_trait() {
1133 None?
1134 }
1135 let args = self.node_args_opt(expr.hir_id)?;
1136 let span = tcx.hir_span(segment.hir_id);
1137 let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1138 let have_turbofish = segment.args.is_some_and(|args| {
1139 args.args.iter().any(|arg| arg.is_ty_or_const())
1140 });
1141 InsertableGenericArgs {
1142 insert_span,
1143 args,
1144 generics_def_id: def_id,
1145 def_id,
1146 have_turbofish,
1147 }
1148 };
1149 return Box::new(insertable.into_iter());
1150 }
1151 }
1152 _ => {}
1153 }
1154
1155 Box::new(iter::empty())
1156 }
1157
1158 fn resolved_path_inferred_arg_iter(
1159 &self,
1160 path: &'tcx hir::Path<'tcx>,
1161 args: GenericArgsRef<'tcx>,
1162 ) -> impl Iterator<Item = InsertableGenericArgs<'tcx>> + 'tcx {
1163 let tcx = self.tecx.tcx;
1164 let have_turbofish = path.segments.iter().any(|segment| {
1165 segment.args.is_some_and(|args| args.args.iter().any(|arg| arg.is_ty_or_const()))
1166 });
1167 let last_segment_using_path_data = try {
1173 let generics_def_id = tcx.res_generics_def_id(path.res)?;
1174 let generics = tcx.generics_of(generics_def_id);
1175 if generics.has_impl_trait() {
1176 do yeet ();
1177 }
1178 let insert_span =
1179 path.segments.last().unwrap().ident.span.shrink_to_hi().with_hi(path.span.hi());
1180 InsertableGenericArgs {
1181 insert_span,
1182 args,
1183 generics_def_id,
1184 def_id: path.res.def_id(),
1185 have_turbofish,
1186 }
1187 };
1188
1189 path.segments
1190 .iter()
1191 .filter_map(move |segment| {
1192 let res = segment.res;
1193 let generics_def_id = tcx.res_generics_def_id(res)?;
1194 let generics = tcx.generics_of(generics_def_id);
1195 if generics.has_impl_trait() {
1196 return None;
1197 }
1198 let span = tcx.hir_span(segment.hir_id);
1199 let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1200 Some(InsertableGenericArgs {
1201 insert_span,
1202 args,
1203 generics_def_id,
1204 def_id: res.def_id(),
1205 have_turbofish,
1206 })
1207 })
1208 .chain(last_segment_using_path_data)
1209 }
1210
1211 fn path_inferred_arg_iter(
1212 &self,
1213 hir_id: HirId,
1214 args: GenericArgsRef<'tcx>,
1215 qpath: &'tcx hir::QPath<'tcx>,
1216 ) -> Box<dyn Iterator<Item = InsertableGenericArgs<'tcx>> + 'a> {
1217 let tcx = self.tecx.tcx;
1218 match qpath {
1219 hir::QPath::Resolved(_self_ty, path) => {
1220 Box::new(self.resolved_path_inferred_arg_iter(path, args))
1221 }
1222 hir::QPath::TypeRelative(ty, segment) => {
1223 let Some(def_id) = self.typeck_results.type_dependent_def_id(hir_id) else {
1224 return Box::new(iter::empty());
1225 };
1226
1227 let generics = tcx.generics_of(def_id);
1228 let segment = if !segment.infer_args || generics.has_impl_trait() {
1229 None
1230 } else {
1231 let span = tcx.hir_span(segment.hir_id);
1232 let insert_span = segment.ident.span.shrink_to_hi().with_hi(span.hi());
1233 Some(InsertableGenericArgs {
1234 insert_span,
1235 args,
1236 generics_def_id: def_id,
1237 def_id,
1238 have_turbofish: false,
1239 })
1240 };
1241
1242 let parent_def_id = generics.parent.unwrap();
1243 if let DefKind::Impl { .. } = tcx.def_kind(parent_def_id) {
1244 let parent_ty =
1245 tcx.type_of(parent_def_id).instantiate(tcx, args).skip_norm_wip();
1246 match (parent_ty.kind(), &ty.kind) {
1247 (
1248 ty::Adt(def, args),
1249 hir::TyKind::Path(hir::QPath::Resolved(_self_ty, path)),
1250 ) => {
1251 if tcx.res_generics_def_id(path.res) != Some(def.did()) {
1252 match path.res {
1253 Res::Def(DefKind::TyAlias, _) => {
1254 }
1261 Res::SelfTyParam { .. } | Res::SelfTyAlias { .. } => {}
1264 _ => {
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/need_type_info.rs:1264",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::WARN,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(1264u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["message"],
::tracing_core::callsite::Identifier(&__CALLSITE)),
::tracing::metadata::Kind::EVENT)
};
::tracing::callsite::DefaultCallsite::new(&META)
};
let enabled =
::tracing::Level::WARN <= ::tracing::level_filters::STATIC_MAX_LEVEL
&&
::tracing::Level::WARN <=
::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!("unexpected path: def={0:?} args={1:?} path={2:?}",
def, args, path) as &dyn Value))])
});
} else { ; }
}warn!(
1265 "unexpected path: def={:?} args={:?} path={:?}",
1266 def, args, path,
1267 ),
1268 }
1269 } else {
1270 return Box::new(
1271 self.resolved_path_inferred_arg_iter(path, args).chain(segment),
1272 );
1273 }
1274 }
1275 _ => (),
1276 }
1277 }
1278
1279 Box::new(segment.into_iter())
1280 }
1281 }
1282 }
1283}
1284
1285impl<'a, 'tcx> Visitor<'tcx> for FindInferSourceVisitor<'a, 'tcx> {
1286 type NestedFilter = nested_filter::OnlyBodies;
1287
1288 fn maybe_tcx(&mut self) -> Self::MaybeTyCtxt {
1289 self.tecx.tcx
1290 }
1291
1292 fn visit_local(&mut self, local: &'tcx LetStmt<'tcx>) {
1293 intravisit::walk_local(self, local);
1294
1295 if let Some(mut ty) = self.opt_node_type(local.hir_id) {
1296 if self.generic_arg_contains_target(ty.into()) {
1297 fn get_did(
1298 typeck_results: &TypeckResults<'_>,
1299 expr: &hir::Expr<'_>,
1300 ) -> Option<DefId> {
1301 match expr.kind {
1302 hir::ExprKind::Match(expr, _, hir::MatchSource::TryDesugar(_))
1303 if let hir::ExprKind::Call(_, [expr]) = expr.kind =>
1304 {
1305 get_did(typeck_results, expr)
1306 }
1307 hir::ExprKind::Call(base, _args)
1308 if let hir::ExprKind::Path(path) = base.kind
1309 && let hir::QPath::Resolved(_, path) = path
1310 && let Res::Def(_, did) = path.res =>
1311 {
1312 Some(did)
1313 }
1314 hir::ExprKind::MethodCall(..)
1315 if let Some(did) =
1316 typeck_results.type_dependent_def_id(expr.hir_id) =>
1317 {
1318 Some(did)
1319 }
1320 _ => None,
1321 }
1322 }
1323 if let Some(t) = self.ty
1324 && ty.has_infer()
1325 {
1326 ty = t;
1327 }
1328 if let LocalSource::Normal = local.source
1329 && local.ty.is_none()
1330 {
1331 self.update_infer_source(InferSource {
1332 span: local.pat.span,
1333 kind: InferSourceKind::LetBinding {
1334 insert_span: local.pat.span.shrink_to_hi(),
1335 pattern_name: local.pat.simple_ident(),
1336 ty,
1337 def_id: local.init.and_then(|expr| get_did(self.typeck_results, expr)),
1338 },
1339 });
1340 }
1341 }
1342 }
1343 }
1344
1345 fn visit_body(&mut self, body: &Body<'tcx>) {
1348 for param in body.params {
1349 {
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/need_type_info.rs:1349",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(1349u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::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!("param: span {0:?}, ty_span {1:?}, pat.span {2:?}",
param.span, param.ty_span, param.pat.span) as &dyn Value))])
});
} else { ; }
};debug!(
1350 "param: span {:?}, ty_span {:?}, pat.span {:?}",
1351 param.span, param.ty_span, param.pat.span
1352 );
1353 if param.ty_span != param.pat.span {
1354 {
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/need_type_info.rs:1354",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(1354u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::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!("skipping param: has explicit type")
as &dyn Value))])
});
} else { ; }
};debug!("skipping param: has explicit type");
1355 continue;
1356 }
1357
1358 let Some(param_ty) = self.opt_node_type(param.hir_id) else { continue };
1359
1360 if self.generic_arg_contains_target(param_ty.into()) {
1361 self.update_infer_source(InferSource {
1362 span: param.pat.span,
1363 kind: InferSourceKind::ClosureArg {
1364 insert_span: param.pat.span.shrink_to_hi(),
1365 ty: param_ty,
1366 kind: param.pat.kind,
1367 },
1368 })
1369 }
1370 }
1371 intravisit::walk_body(self, body);
1372 }
1373
1374 #[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("visit_expr",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(1374u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["expr"],
::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(&expr)
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 tcx = self.tecx.tcx;
match expr.kind {
ExprKind::Call(func, args) => {
for arg in args { self.visit_expr(arg); }
self.visit_expr(func);
}
_ => intravisit::walk_expr(self, expr),
}
for args in self.expr_inferred_arg_iter(expr) {
{
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/need_type_info.rs:1390",
"rustc_trait_selection::error_reporting::infer::need_type_info",
::tracing::Level::DEBUG,
::tracing_core::__macro_support::Option::Some("compiler/rustc_trait_selection/src/error_reporting/infer/need_type_info.rs"),
::tracing_core::__macro_support::Option::Some(1390u32),
::tracing_core::__macro_support::Option::Some("rustc_trait_selection::error_reporting::infer::need_type_info"),
::tracing_core::field::FieldSet::new(&["args"],
::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(&args) as
&dyn Value))])
});
} else { ; }
};
let InsertableGenericArgs {
insert_span, args, generics_def_id, def_id, have_turbofish
} = args;
let generics = tcx.generics_of(generics_def_id);
if let Some(argument_index) =
generics.own_args(args).iter().position(|&arg|
self.generic_arg_contains_target(arg)) {
let args = self.tecx.resolve_vars_if_possible(args);
let generic_args =
&generics.own_args_no_defaults(tcx,
args)[generics.own_counts().lifetimes..];
let span =
match expr.kind {
ExprKind::MethodCall(segment, ..) if
have_turbofish && let Some(hir_args) = segment.args &&
let Some(idx) =
argument_index.checked_sub(generics.own_counts().lifetimes)
&&
let Some(arg) =
hir_args.args.get(hir_args.num_lifetime_params() + idx) => {
arg.span()
}
ExprKind::MethodCall(segment, ..) => segment.ident.span,
_ => expr.span,
};
let mut argument_index = argument_index;
if generics.has_own_self() { argument_index += 1; }
self.update_infer_source(InferSource {
span,
kind: InferSourceKind::GenericArg {
insert_span,
argument_index,
generics_def_id,
def_id,
generic_args,
have_turbofish,
hir_id: expr.hir_id,
},
});
}
}
if let Some(node_ty) = self.opt_node_type(expr.hir_id) {
if let (&ExprKind::Closure(&Closure {
fn_decl, body, fn_decl_span, .. }), ty::Closure(_, args)) =
(&expr.kind, node_ty.kind()) {
let output = args.as_closure().sig().output().skip_binder();
if self.generic_arg_contains_target(output.into()) {
let body = self.tecx.tcx.hir_body(body);
let should_wrap_expr =
if #[allow(non_exhaustive_omitted_patterns)] match body.value.kind
{
ExprKind::Block(..) => true,
_ => false,
} {
None
} else { Some(body.value.span.shrink_to_hi()) };
self.update_infer_source(InferSource {
span: fn_decl_span,
kind: InferSourceKind::ClosureReturn {
ty: output,
data: &fn_decl.output,
should_wrap_expr,
},
})
}
}
}
let has_impl_trait =
|def_id|
{
iter::successors(Some(tcx.generics_of(def_id)),
|generics|
{
generics.parent.map(|def_id| tcx.generics_of(def_id))
}).any(|generics| generics.has_impl_trait())
};
if let ExprKind::MethodCall(path, receiver, method_args, span) =
expr.kind &&
let Some(args) = self.node_args_opt(expr.hir_id) &&
args.iter().any(|arg| self.generic_arg_contains_target(arg))
&&
let Some(def_id) =
self.typeck_results.type_dependent_def_id(expr.hir_id) &&
self.tecx.tcx.trait_of_assoc(def_id).is_some() &&
!has_impl_trait(def_id) &&
tcx.hir_opt_delegation_sig_id(expr.hir_id.owner.def_id).is_none()
{
let successor =
method_args.get(0).map_or_else(|| (")", span.hi()),
|arg| (", ", arg.span.lo()));
let args = self.tecx.resolve_vars_if_possible(args);
self.update_infer_source(InferSource {
span: path.ident.span,
kind: InferSourceKind::FullyQualifiedMethodCall {
receiver,
successor,
args,
def_id,
},
})
}
}
}
}#[instrument(level = "debug", skip(self))]
1375 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
1376 let tcx = self.tecx.tcx;
1377 match expr.kind {
1378 ExprKind::Call(func, args) => {
1381 for arg in args {
1382 self.visit_expr(arg);
1383 }
1384 self.visit_expr(func);
1385 }
1386 _ => intravisit::walk_expr(self, expr),
1387 }
1388
1389 for args in self.expr_inferred_arg_iter(expr) {
1390 debug!(?args);
1391 let InsertableGenericArgs {
1392 insert_span,
1393 args,
1394 generics_def_id,
1395 def_id,
1396 have_turbofish,
1397 } = args;
1398 let generics = tcx.generics_of(generics_def_id);
1399 if let Some(argument_index) = generics
1400 .own_args(args)
1401 .iter()
1402 .position(|&arg| self.generic_arg_contains_target(arg))
1403 {
1404 let args = self.tecx.resolve_vars_if_possible(args);
1405 let generic_args =
1406 &generics.own_args_no_defaults(tcx, args)[generics.own_counts().lifetimes..];
1407 let span = match expr.kind {
1408 ExprKind::MethodCall(segment, ..)
1409 if have_turbofish
1410 && let Some(hir_args) = segment.args
1411 && let Some(idx) =
1412 argument_index.checked_sub(generics.own_counts().lifetimes)
1413 && let Some(arg) =
1414 hir_args.args.get(hir_args.num_lifetime_params() + idx) =>
1415 {
1416 arg.span()
1417 }
1418 ExprKind::MethodCall(segment, ..) => segment.ident.span,
1419 _ => expr.span,
1420 };
1421 let mut argument_index = argument_index;
1422 if generics.has_own_self() {
1423 argument_index += 1;
1424 }
1425
1426 self.update_infer_source(InferSource {
1427 span,
1428 kind: InferSourceKind::GenericArg {
1429 insert_span,
1430 argument_index,
1431 generics_def_id,
1432 def_id,
1433 generic_args,
1434 have_turbofish,
1435 hir_id: expr.hir_id,
1436 },
1437 });
1438 }
1439 }
1440
1441 if let Some(node_ty) = self.opt_node_type(expr.hir_id) {
1442 if let (
1443 &ExprKind::Closure(&Closure { fn_decl, body, fn_decl_span, .. }),
1444 ty::Closure(_, args),
1445 ) = (&expr.kind, node_ty.kind())
1446 {
1447 let output = args.as_closure().sig().output().skip_binder();
1448 if self.generic_arg_contains_target(output.into()) {
1449 let body = self.tecx.tcx.hir_body(body);
1450 let should_wrap_expr = if matches!(body.value.kind, ExprKind::Block(..)) {
1451 None
1452 } else {
1453 Some(body.value.span.shrink_to_hi())
1454 };
1455 self.update_infer_source(InferSource {
1456 span: fn_decl_span,
1457 kind: InferSourceKind::ClosureReturn {
1458 ty: output,
1459 data: &fn_decl.output,
1460 should_wrap_expr,
1461 },
1462 })
1463 }
1464 }
1465 }
1466
1467 let has_impl_trait = |def_id| {
1468 iter::successors(Some(tcx.generics_of(def_id)), |generics| {
1469 generics.parent.map(|def_id| tcx.generics_of(def_id))
1470 })
1471 .any(|generics| generics.has_impl_trait())
1472 };
1473 if let ExprKind::MethodCall(path, receiver, method_args, span) = expr.kind
1474 && let Some(args) = self.node_args_opt(expr.hir_id)
1475 && args.iter().any(|arg| self.generic_arg_contains_target(arg))
1476 && let Some(def_id) = self.typeck_results.type_dependent_def_id(expr.hir_id)
1477 && self.tecx.tcx.trait_of_assoc(def_id).is_some()
1478 && !has_impl_trait(def_id)
1479 && tcx.hir_opt_delegation_sig_id(expr.hir_id.owner.def_id).is_none()
1482 {
1483 let successor =
1484 method_args.get(0).map_or_else(|| (")", span.hi()), |arg| (", ", arg.span.lo()));
1485 let args = self.tecx.resolve_vars_if_possible(args);
1486 self.update_infer_source(InferSource {
1487 span: path.ident.span,
1488 kind: InferSourceKind::FullyQualifiedMethodCall {
1489 receiver,
1490 successor,
1491 args,
1492 def_id,
1493 },
1494 })
1495 }
1496 }
1497}