1 use crate::infer::type_variable::TypeVariableOriginKind;
2 use crate::infer::InferCtxt;
3 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
5 use rustc_hir::def::{DefKind, Namespace};
6 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
7 use rustc_hir::{Body, Expr, ExprKind, FnRetTy, HirId, Local, Pat};
8 use rustc_middle::hir::map::Map;
9 use rustc_middle::ty::print::Print;
10 use rustc_middle::ty::subst::{GenericArg, GenericArgKind};
11 use rustc_middle::ty::{self, DefIdTree, Ty};
12 use rustc_span::source_map::DesugaringKind;
13 use rustc_span::symbol::kw;
17 struct FindHirNodeVisitor<'a, 'tcx> {
18 infcx: &'a InferCtxt<'a, 'tcx>,
19 target: GenericArg<'tcx>,
21 found_node_ty: Option<Ty<'tcx>>,
22 found_local_pattern: Option<&'tcx Pat<'tcx>>,
23 found_arg_pattern: Option<&'tcx Pat<'tcx>>,
24 found_closure: Option<&'tcx Expr<'tcx>>,
25 found_method_call: Option<&'tcx Expr<'tcx>>,
26 found_exact_method_call: Option<&'tcx Expr<'tcx>>,
29 impl<'a, 'tcx> FindHirNodeVisitor<'a, 'tcx> {
30 fn new(infcx: &'a InferCtxt<'a, 'tcx>, target: GenericArg<'tcx>, target_span: Span) -> Self {
36 found_local_pattern: None,
37 found_arg_pattern: None,
39 found_method_call: None,
40 found_exact_method_call: None,
44 fn node_ty_contains_target(&mut self, hir_id: HirId) -> Option<Ty<'tcx>> {
46 self.infcx.in_progress_tables.and_then(|tables| tables.borrow().node_type_opt(hir_id));
49 let ty = self.infcx.resolve_vars_if_possible(&ty);
50 if ty.walk().any(|inner| {
52 || match (inner.unpack(), self.target.unpack()) {
53 (GenericArgKind::Type(inner_ty), GenericArgKind::Type(target_ty)) => {
54 match (&inner_ty.kind, &target_ty.kind) {
56 &ty::Infer(ty::TyVar(a_vid)),
57 &ty::Infer(ty::TyVar(b_vid)),
63 .sub_unified(a_vid, b_vid),
80 impl<'a, 'tcx> Visitor<'tcx> for FindHirNodeVisitor<'a, 'tcx> {
83 fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
84 NestedVisitorMap::OnlyBodies(self.infcx.tcx.hir())
87 fn visit_local(&mut self, local: &'tcx Local<'tcx>) {
88 if let (None, Some(ty)) =
89 (self.found_local_pattern, self.node_ty_contains_target(local.hir_id))
91 self.found_local_pattern = Some(&*local.pat);
92 self.found_node_ty = Some(ty);
94 intravisit::walk_local(self, local);
97 fn visit_body(&mut self, body: &'tcx Body<'tcx>) {
98 for param in body.params {
99 if let (None, Some(ty)) =
100 (self.found_arg_pattern, self.node_ty_contains_target(param.hir_id))
102 self.found_arg_pattern = Some(&*param.pat);
103 self.found_node_ty = Some(ty);
106 intravisit::walk_body(self, body);
109 fn visit_expr(&mut self, expr: &'tcx Expr<'tcx>) {
110 if let ExprKind::MethodCall(_, call_span, exprs) = expr.kind {
111 if call_span == self.target_span
113 == self.infcx.in_progress_tables.and_then(|tables| {
114 tables.borrow().node_type_opt(exprs.first().unwrap().hir_id).map(Into::into)
117 self.found_exact_method_call = Some(&expr);
121 if self.node_ty_contains_target(expr.hir_id).is_some() {
123 ExprKind::Closure(..) => self.found_closure = Some(&expr),
124 ExprKind::MethodCall(..) => self.found_method_call = Some(&expr),
128 intravisit::walk_expr(self, expr);
132 /// Suggest giving an appropriate return type to a closure expression.
133 fn closure_return_type_suggestion(
135 err: &mut DiagnosticBuilder<'_>,
136 output: &FnRetTy<'_>,
141 parent_name: Option<String>,
142 parent_descr: Option<&str>,
144 let (arrow, post) = match output {
145 FnRetTy::DefaultReturn(_) => ("-> ", " "),
148 let suggestion = match body.value.kind {
149 ExprKind::Block(..) => vec![(output.span(), format!("{}{}{}", arrow, ret, post))],
151 (output.span(), format!("{}{}{}{{ ", arrow, ret, post)),
152 (body.value.span.shrink_to_hi(), " }".to_string()),
155 err.multipart_suggestion(
156 "give this closure an explicit return type without `_` placeholders",
158 Applicability::HasPlaceholders,
160 err.span_label(span, InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr));
163 /// Given a closure signature, return a `String` containing a list of all its argument types.
164 fn closure_args(fn_sig: &ty::PolyFnSig<'_>) -> String {
170 .map(|args| args.tuple_fields().map(|arg| arg.to_string()).collect::<Vec<_>>().join(", "))
174 pub enum TypeAnnotationNeeded {
180 impl Into<rustc_errors::DiagnosticId> for TypeAnnotationNeeded {
181 fn into(self) -> rustc_errors::DiagnosticId {
183 Self::E0282 => rustc_errors::error_code!(E0282),
184 Self::E0283 => rustc_errors::error_code!(E0283),
185 Self::E0284 => rustc_errors::error_code!(E0284),
190 impl<'a, 'tcx> InferCtxt<'a, 'tcx> {
191 pub fn extract_type_name(
194 highlight: Option<ty::print::RegionHighlightMode>,
195 ) -> (String, Option<Span>, Cow<'static, str>, Option<String>, Option<&'static str>) {
196 if let ty::Infer(ty::TyVar(ty_vid)) = ty.kind {
197 let mut inner = self.inner.borrow_mut();
198 let ty_vars = &inner.type_variables();
199 let var_origin = ty_vars.var_origin(ty_vid);
200 if let TypeVariableOriginKind::TypeParameterDefinition(name, def_id) = var_origin.kind {
201 let parent_def_id = def_id.and_then(|def_id| self.tcx.parent(def_id));
202 let (parent_name, parent_desc) = if let Some(parent_def_id) = parent_def_id {
203 let parent_name = self
205 .def_key(parent_def_id)
209 .map(|parent_symbol| parent_symbol.to_string());
211 (parent_name, Some(self.tcx.def_kind(parent_def_id).descr(parent_def_id)))
216 if name != kw::SelfUpper {
219 Some(var_origin.span),
220 "type parameter".into(),
228 let mut s = String::new();
229 let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
230 if let Some(highlight) = highlight {
231 printer.region_highlight_mode = highlight;
233 let _ = ty.print(printer);
234 (s, None, ty.prefix_string(), None, None)
237 // FIXME(eddyb) generalize all of this to handle `ty::Const` inference variables as well.
238 pub fn need_type_info_err(
240 body_id: Option<hir::BodyId>,
243 error_code: TypeAnnotationNeeded,
244 ) -> DiagnosticBuilder<'tcx> {
245 let ty = self.resolve_vars_if_possible(&ty);
246 let (name, name_sp, descr, parent_name, parent_descr) = self.extract_type_name(&ty, None);
248 let mut local_visitor = FindHirNodeVisitor::new(&self, ty.into(), span);
249 let ty_to_string = |ty: Ty<'tcx>| -> String {
250 let mut s = String::new();
251 let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
252 let mut inner = self.inner.borrow_mut();
253 let ty_vars = inner.type_variables();
254 let getter = move |ty_vid| {
255 let var_origin = ty_vars.var_origin(ty_vid);
256 if let TypeVariableOriginKind::TypeParameterDefinition(name, _) = var_origin.kind {
257 return Some(name.to_string());
261 printer.name_resolver = Some(Box::new(&getter));
262 let _ = if let ty::FnDef(..) = ty.kind {
263 // We don't want the regular output for `fn`s because it includes its path in
264 // invalid pseduo-syntax, we want the `fn`-pointer output instead.
265 ty.fn_sig(self.tcx).print(printer)
272 if let Some(body_id) = body_id {
273 let expr = self.tcx.hir().expect_expr(body_id.hir_id);
274 local_visitor.visit_expr(expr);
276 let err_span = if let Some(pattern) = local_visitor.found_arg_pattern {
278 } else if let Some(span) = name_sp {
279 // `span` here lets us point at `sum` instead of the entire right hand side expr:
280 // error[E0282]: type annotations needed
283 // 3 | let _ = x.sum() as f64;
284 // | ^^^ cannot infer type for `S`
286 } else if let Some(ExprKind::MethodCall(_, call_span, _)) =
287 local_visitor.found_method_call.map(|e| &e.kind)
289 // Point at the call instead of the whole expression:
290 // error[E0284]: type annotations needed
293 // 2 | vec![Ok(2)].into_iter().collect()?;
294 // | ^^^^^^^ cannot infer type
296 // = note: cannot resolve `<_ as std::ops::Try>::Ok == _`
297 if span.contains(*call_span) { *call_span } else { span }
302 let is_named_and_not_impl_trait = |ty: Ty<'_>| {
303 &ty.to_string() != "_" &&
304 // FIXME: Remove this check after `impl_trait_in_bindings` is stabilized. #63527
305 (!ty.is_impl_trait() || self.tcx.features().impl_trait_in_bindings)
308 let ty_msg = match (local_visitor.found_node_ty, local_visitor.found_exact_method_call) {
309 (_, Some(_)) => String::new(),
310 (Some(ty::TyS { kind: ty::Closure(_, substs), .. }), _) => {
311 let fn_sig = substs.as_closure().sig();
312 let args = closure_args(&fn_sig);
313 let ret = fn_sig.output().skip_binder().to_string();
314 format!(" for the closure `fn({}) -> {}`", args, ret)
316 (Some(ty), _) if is_named_and_not_impl_trait(ty) => {
317 let ty = ty_to_string(ty);
318 format!(" for `{}`", ty)
323 // When `name` corresponds to a type argument, show the path of the full type we're
324 // trying to infer. In the following example, `ty_msg` contains
325 // " in `std::result::Result<i32, E>`":
327 // error[E0282]: type annotations needed for `std::result::Result<i32, E>`
330 // L | let b = Ok(4);
331 // | - ^^ cannot infer type for `E` in `std::result::Result<i32, E>`
333 // | consider giving `b` the explicit type `std::result::Result<i32, E>`, where
334 // | the type parameter `E` is specified
336 let error_code = error_code.into();
337 let mut err = self.tcx.sess.struct_span_err_with_code(
339 &format!("type annotations needed{}", ty_msg),
343 let suffix = match local_visitor.found_node_ty {
344 Some(ty::TyS { kind: ty::Closure(_, substs), .. }) => {
345 let fn_sig = substs.as_closure().sig();
346 let ret = fn_sig.output().skip_binder().to_string();
348 let closure_decl_and_body_id =
349 local_visitor.found_closure.and_then(|closure| match &closure.kind {
350 ExprKind::Closure(_, decl, body_id, ..) => Some((decl, *body_id)),
354 if let Some((decl, body_id)) = closure_decl_and_body_id {
355 closure_return_type_suggestion(
359 self.tcx.hir().body(body_id),
366 // We don't want to give the other suggestions when the problem is the
367 // closure return type.
371 // This shouldn't be reachable, but just in case we leave a reasonable fallback.
372 let args = closure_args(&fn_sig);
373 // This suggestion is incomplete, as the user will get further type inference
374 // errors due to the `_` placeholders and the introduction of `Box`, but it does
375 // nudge them in the right direction.
376 format!("a boxed closure type like `Box<dyn Fn({}) -> {}>`", args, ret)
378 Some(ty) if is_named_and_not_impl_trait(ty) && name == "_" => {
379 let ty = ty_to_string(ty);
380 format!("the explicit type `{}`, with the type parameters specified", ty)
382 Some(ty) if is_named_and_not_impl_trait(ty) && ty.to_string() != name => {
383 let ty = ty_to_string(ty);
385 "the explicit type `{}`, where the type parameter `{}` is specified",
389 _ => "a type".to_string(),
392 if let Some(e) = local_visitor.found_exact_method_call {
393 if let ExprKind::MethodCall(segment, ..) = &e.kind {
394 // Suggest specifying type params or point out the return type of the call:
396 // error[E0282]: type annotations needed
397 // --> $DIR/type-annotations-needed-expr.rs:2:39
399 // LL | let _ = x.into_iter().sum() as f64;
402 // | cannot infer type for `S`
403 // | help: consider specifying the type argument in
404 // | the method call: `sum::<S>`
406 // = note: type must be known at this point
410 // error[E0282]: type annotations needed
411 // --> $DIR/issue-65611.rs:59:20
413 // LL | let x = buffer.last().unwrap().0.clone();
416 // | | cannot infer type for `T`
417 // | this method call resolves to `std::option::Option<&T>`
419 // = note: type must be known at this point
420 self.annotate_method_call(segment, e, &mut err);
422 } else if let Some(pattern) = local_visitor.found_arg_pattern {
423 // We don't want to show the default label for closures.
425 // So, before clearing, the output would look something like this:
428 // - ^^^^ cannot infer type for `[_; 0]`
430 // consider giving this closure parameter a type
433 // After clearing, it looks something like this:
436 // ^ consider giving this closure parameter the type `[_; 0]`
437 // with the type parameter `_` specified
441 format!("consider giving this closure parameter {}", suffix),
443 } else if let Some(pattern) = local_visitor.found_local_pattern {
444 let msg = if let Some(simple_ident) = pattern.simple_ident() {
445 match pattern.span.desugaring_kind() {
446 None => format!("consider giving `{}` {}", simple_ident, suffix),
447 Some(DesugaringKind::ForLoop) => {
448 "the element type for this iterator is not specified".to_string()
450 _ => format!("this needs {}", suffix),
453 format!("consider giving this pattern {}", suffix)
455 err.span_label(pattern.span, msg);
456 } else if let Some(e) = local_visitor.found_method_call {
457 if let ExprKind::MethodCall(segment, ..) = &e.kind {
458 // Suggest specifying type params or point out the return type of the call:
460 // error[E0282]: type annotations needed
461 // --> $DIR/type-annotations-needed-expr.rs:2:39
463 // LL | let _ = x.into_iter().sum() as f64;
466 // | cannot infer type for `S`
467 // | help: consider specifying the type argument in
468 // | the method call: `sum::<S>`
470 // = note: type must be known at this point
474 // error[E0282]: type annotations needed
475 // --> $DIR/issue-65611.rs:59:20
477 // LL | let x = buffer.last().unwrap().0.clone();
480 // | | cannot infer type for `T`
481 // | this method call resolves to `std::option::Option<&T>`
483 // = note: type must be known at this point
484 self.annotate_method_call(segment, e, &mut err);
487 // Instead of the following:
488 // error[E0282]: type annotations needed
491 // 3 | let _ = x.sum() as f64;
492 // | --^^^--------- cannot infer type for `S`
494 // = note: type must be known at this point
496 // error[E0282]: type annotations needed
499 // 3 | let _ = x.sum() as f64;
500 // | ^^^ cannot infer type for `S`
502 // = note: type must be known at this point
503 let span = name_sp.unwrap_or(err_span);
508 .any(|span_label| span_label.label.is_some() && span_label.span == span)
509 && local_visitor.found_arg_pattern.is_none()
511 // Avoid multiple labels pointing at `span`.
514 InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr),
521 /// If the `FnSig` for the method call can be found and type arguments are identified as
522 /// needed, suggest annotating the call, otherwise point out the resulting type of the call.
523 fn annotate_method_call(
525 segment: &hir::PathSegment<'_>,
527 err: &mut DiagnosticBuilder<'_>,
529 if let (Some(tables), None) = (self.in_progress_tables, &segment.args) {
530 let borrow = tables.borrow();
531 if let Some((DefKind::AssocFn, did)) = borrow.type_dependent_def(e.hir_id) {
532 let generics = self.tcx.generics_of(did);
533 if !generics.params.is_empty() {
534 err.span_suggestion_verbose(
535 segment.ident.span.shrink_to_hi(),
537 "consider specifying the type argument{} in the method call",
538 pluralize!(generics.params.len()),
545 .map(|p| p.name.to_string())
546 .collect::<Vec<String>>()
549 Applicability::HasPlaceholders,
552 let sig = self.tcx.fn_sig(did);
553 let bound_output = sig.output();
554 let output = bound_output.skip_binder();
555 err.span_label(e.span, &format!("this method call resolves to `{:?}`", output));
556 let kind = &output.kind;
557 if let ty::Projection(proj) = kind {
558 if let Some(span) = self.tcx.hir().span_if_local(proj.item_def_id) {
559 err.span_label(span, &format!("`{:?}` defined here", output));
567 pub fn need_type_info_err_in_generator(
569 kind: hir::GeneratorKind,
572 ) -> DiagnosticBuilder<'tcx> {
573 let ty = self.resolve_vars_if_possible(&ty);
574 let (name, _, descr, parent_name, parent_descr) = self.extract_type_name(&ty, None);
576 let mut err = struct_span_err!(
580 "type inside {} must be known in this context",
583 err.span_label(span, InferCtxt::missing_type_msg(&name, &descr, parent_name, parent_descr));
590 parent_name: Option<String>,
591 parent_descr: Option<&str>,
592 ) -> Cow<'static, str> {
593 if type_name == "_" {
594 "cannot infer type".into()
596 let parent_desc = if let Some(parent_name) = parent_name {
597 let parent_type_descr = if let Some(parent_descr) = parent_descr {
598 format!(" the {}", parent_descr)
603 format!(" declared on{} `{}`", parent_type_descr, parent_name)
608 format!("cannot infer type for {} `{}`{}", descr, type_name, parent_desc).into()