1 use crate::hir::def::Namespace;
2 use crate::hir::{self, Local, Pat, Body, HirId};
3 use crate::hir::intravisit::{self, Visitor, NestedVisitorMap};
4 use crate::infer::InferCtxt;
5 use crate::infer::type_variable::TypeVariableOrigin;
6 use crate::ty::{self, Ty, Infer, TyVar};
7 use crate::ty::print::Print;
8 use syntax::source_map::CompilerDesugaringKind;
10 use errors::DiagnosticBuilder;
12 struct FindLocalByTypeVisitor<'a, 'gcx: 'a + 'tcx, 'tcx: 'a> {
13 infcx: &'a InferCtxt<'a, 'gcx, 'tcx>,
14 target_ty: &'a Ty<'tcx>,
15 hir_map: &'a hir::map::Map<'gcx>,
16 found_local_pattern: Option<&'gcx Pat>,
17 found_arg_pattern: Option<&'gcx Pat>,
20 impl<'a, 'gcx, 'tcx> FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
21 fn node_matches_type(&mut self, hir_id: HirId) -> bool {
22 let ty_opt = self.infcx.in_progress_tables.and_then(|tables| {
23 tables.borrow().node_type_opt(hir_id)
27 let ty = self.infcx.resolve_type_vars_if_possible(&ty);
28 ty.walk().any(|inner_ty| {
29 inner_ty == *self.target_ty || match (&inner_ty.sty, &self.target_ty.sty) {
30 (&Infer(TyVar(a_vid)), &Infer(TyVar(b_vid))) => {
34 .sub_unified(a_vid, b_vid)
45 impl<'a, 'gcx, 'tcx> Visitor<'gcx> for FindLocalByTypeVisitor<'a, 'gcx, 'tcx> {
46 fn nested_visit_map<'this>(&'this mut self) -> NestedVisitorMap<'this, 'gcx> {
47 NestedVisitorMap::OnlyBodies(&self.hir_map)
50 fn visit_local(&mut self, local: &'gcx Local) {
51 if self.found_local_pattern.is_none() && self.node_matches_type(local.hir_id) {
52 self.found_local_pattern = Some(&*local.pat);
54 intravisit::walk_local(self, local);
57 fn visit_body(&mut self, body: &'gcx Body) {
58 for argument in &body.arguments {
59 if self.found_arg_pattern.is_none() && self.node_matches_type(argument.hir_id) {
60 self.found_arg_pattern = Some(&*argument.pat);
63 intravisit::walk_body(self, body);
68 impl<'a, 'gcx, 'tcx> InferCtxt<'a, 'gcx, 'tcx> {
69 pub fn extract_type_name(
72 highlight: Option<ty::print::RegionHighlightMode>,
74 if let ty::Infer(ty::TyVar(ty_vid)) = (*ty).sty {
75 let ty_vars = self.type_variables.borrow();
76 if let TypeVariableOrigin::TypeParameterDefinition(_, name) =
77 *ty_vars.var_origin(ty_vid) {
78 return name.to_string();
82 let mut s = String::new();
83 let mut printer = ty::print::FmtPrinter::new(self.tcx, &mut s, Namespace::TypeNS);
84 if let Some(highlight) = highlight {
85 printer.region_highlight_mode = highlight;
87 let _ = ty.print(printer);
91 pub fn need_type_info_err(&self,
92 body_id: Option<hir::BodyId>,
95 -> DiagnosticBuilder<'gcx> {
96 let ty = self.resolve_type_vars_if_possible(&ty);
97 let name = self.extract_type_name(&ty, None);
99 let mut err_span = span;
100 let mut labels = vec![(
103 "cannot infer type".to_owned()
105 format!("cannot infer type for `{}`", name)
109 let mut local_visitor = FindLocalByTypeVisitor {
112 hir_map: &self.tcx.hir(),
113 found_local_pattern: None,
114 found_arg_pattern: None,
117 if let Some(body_id) = body_id {
118 let expr = self.tcx.hir().expect_expr_by_hir_id(body_id.hir_id);
119 local_visitor.visit_expr(expr);
122 if let Some(pattern) = local_visitor.found_arg_pattern {
123 err_span = pattern.span;
124 // We don't want to show the default label for closures.
126 // So, before clearing, the output would look something like this:
129 // - ^^^^ cannot infer type for `[_; 0]`
131 // consider giving this closure parameter a type
134 // After clearing, it looks something like this:
137 // ^ consider giving this closure parameter a type
141 (pattern.span, "consider giving this closure parameter a type".to_owned()));
142 } else if let Some(pattern) = local_visitor.found_local_pattern {
143 if let Some(simple_ident) = pattern.simple_ident() {
144 match pattern.span.compiler_desugaring_kind() {
145 None => labels.push((pattern.span,
146 format!("consider giving `{}` a type", simple_ident))),
147 Some(CompilerDesugaringKind::ForLoop) => labels.push((
149 "the element type for this iterator is not specified".to_owned(),
154 labels.push((pattern.span, "consider giving the pattern a type".to_owned()));
158 let mut err = struct_span_err!(self.tcx.sess,
161 "type annotations needed");
163 for (target_span, label_message) in labels {
164 err.span_label(target_span, label_message);