]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/method/suggest.rs
remove unnecessary hir::map imports
[rust.git] / src / librustc_typeck / check / method / suggest.rs
1 //! Give useful errors and suggestions to users when an item can't be
2 //! found or is otherwise invalid.
3
4 use crate::check::FnCtxt;
5 use crate::middle::lang_items::FnOnceTraitLangItem;
6 use rustc::hir::map as hir_map;
7 use rustc::ty::print::with_crate_prefix;
8 use rustc::ty::{self, ToPolyTraitRef, ToPredicate, Ty, TyCtxt, TypeFoldable, WithConstness};
9 use rustc_ast::ast;
10 use rustc_ast::util::lev_distance;
11 use rustc_data_structures::fx::{FxHashMap, FxHashSet};
12 use rustc_errors::{pluralize, struct_span_err, Applicability, DiagnosticBuilder};
13 use rustc_hir as hir;
14 use rustc_hir::def::{DefKind, Namespace, Res};
15 use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX, LOCAL_CRATE};
16 use rustc_hir::intravisit;
17 use rustc_hir::{ExprKind, Node, QPath};
18 use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
19 use rustc_span::symbol::kw;
20 use rustc_span::{source_map, FileName, Span};
21 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
22 use rustc_trait_selection::traits::Obligation;
23
24 use std::cmp::Ordering;
25
26 use super::probe::Mode;
27 use super::{CandidateSource, MethodError, NoMatchData};
28
29 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
30     fn is_fn_ty(&self, ty: Ty<'tcx>, span: Span) -> bool {
31         let tcx = self.tcx;
32         match ty.kind {
33             // Not all of these (e.g., unsafe fns) implement `FnOnce`,
34             // so we look for these beforehand.
35             ty::Closure(..) | ty::FnDef(..) | ty::FnPtr(_) => true,
36             // If it's not a simple function, look for things which implement `FnOnce`.
37             _ => {
38                 let fn_once = match tcx.lang_items().require(FnOnceTraitLangItem) {
39                     Ok(fn_once) => fn_once,
40                     Err(..) => return false,
41                 };
42
43                 self.autoderef(span, ty).any(|(ty, _)| {
44                     self.probe(|_| {
45                         let fn_once_substs = tcx.mk_substs_trait(
46                             ty,
47                             &[self
48                                 .next_ty_var(TypeVariableOrigin {
49                                     kind: TypeVariableOriginKind::MiscVariable,
50                                     span,
51                                 })
52                                 .into()],
53                         );
54                         let trait_ref = ty::TraitRef::new(fn_once, fn_once_substs);
55                         let poly_trait_ref = trait_ref.to_poly_trait_ref();
56                         let obligation = Obligation::misc(
57                             span,
58                             self.body_id,
59                             self.param_env,
60                             poly_trait_ref.without_const().to_predicate(),
61                         );
62                         self.predicate_may_hold(&obligation)
63                     })
64                 })
65             }
66         }
67     }
68
69     pub fn report_method_error<'b>(
70         &self,
71         span: Span,
72         rcvr_ty: Ty<'tcx>,
73         item_name: ast::Ident,
74         source: SelfSource<'b>,
75         error: MethodError<'tcx>,
76         args: Option<&'tcx [hir::Expr<'tcx>]>,
77     ) -> Option<DiagnosticBuilder<'_>> {
78         let orig_span = span;
79         let mut span = span;
80         // Avoid suggestions when we don't know what's going on.
81         if rcvr_ty.references_error() {
82             return None;
83         }
84
85         let report_candidates = |span: Span,
86                                  err: &mut DiagnosticBuilder<'_>,
87                                  mut sources: Vec<CandidateSource>,
88                                  sugg_span: Span| {
89             sources.sort();
90             sources.dedup();
91             // Dynamic limit to avoid hiding just one candidate, which is silly.
92             let limit = if sources.len() == 5 { 5 } else { 4 };
93
94             for (idx, source) in sources.iter().take(limit).enumerate() {
95                 match *source {
96                     CandidateSource::ImplSource(impl_did) => {
97                         // Provide the best span we can. Use the item, if local to crate, else
98                         // the impl, if local to crate (item may be defaulted), else nothing.
99                         let item = match self
100                             .associated_item(impl_did, item_name, Namespace::ValueNS)
101                             .or_else(|| {
102                                 let impl_trait_ref = self.tcx.impl_trait_ref(impl_did)?;
103                                 self.associated_item(
104                                     impl_trait_ref.def_id,
105                                     item_name,
106                                     Namespace::ValueNS,
107                                 )
108                             }) {
109                             Some(item) => item,
110                             None => continue,
111                         };
112                         let note_span = self
113                             .tcx
114                             .hir()
115                             .span_if_local(item.def_id)
116                             .or_else(|| self.tcx.hir().span_if_local(impl_did));
117
118                         let impl_ty = self.impl_self_ty(span, impl_did).ty;
119
120                         let insertion = match self.tcx.impl_trait_ref(impl_did) {
121                             None => String::new(),
122                             Some(trait_ref) => format!(
123                                 " of the trait `{}`",
124                                 self.tcx.def_path_str(trait_ref.def_id)
125                             ),
126                         };
127
128                         let (note_str, idx) = if sources.len() > 1 {
129                             (
130                                 format!(
131                                     "candidate #{} is defined in an impl{} for the type `{}`",
132                                     idx + 1,
133                                     insertion,
134                                     impl_ty,
135                                 ),
136                                 Some(idx + 1),
137                             )
138                         } else {
139                             (
140                                 format!(
141                                     "the candidate is defined in an impl{} for the type `{}`",
142                                     insertion, impl_ty,
143                                 ),
144                                 None,
145                             )
146                         };
147                         if let Some(note_span) = note_span {
148                             // We have a span pointing to the method. Show note with snippet.
149                             err.span_note(
150                                 self.tcx.sess.source_map().def_span(note_span),
151                                 &note_str,
152                             );
153                         } else {
154                             err.note(&note_str);
155                         }
156                         if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_did) {
157                             let path = self.tcx.def_path_str(trait_ref.def_id);
158
159                             let ty = match item.kind {
160                                 ty::AssocKind::Const
161                                 | ty::AssocKind::Type
162                                 | ty::AssocKind::OpaqueTy => rcvr_ty,
163                                 ty::AssocKind::Method => self
164                                     .tcx
165                                     .fn_sig(item.def_id)
166                                     .inputs()
167                                     .skip_binder()
168                                     .get(0)
169                                     .filter(|ty| ty.is_region_ptr() && !rcvr_ty.is_region_ptr())
170                                     .copied()
171                                     .unwrap_or(rcvr_ty),
172                             };
173                             print_disambiguation_help(
174                                 item_name,
175                                 args,
176                                 err,
177                                 path,
178                                 ty,
179                                 item.kind,
180                                 sugg_span,
181                                 idx,
182                                 self.tcx.sess.source_map(),
183                             );
184                         }
185                     }
186                     CandidateSource::TraitSource(trait_did) => {
187                         let item =
188                             match self.associated_item(trait_did, item_name, Namespace::ValueNS) {
189                                 Some(item) => item,
190                                 None => continue,
191                             };
192                         let item_span =
193                             self.tcx.sess.source_map().def_span(self.tcx.def_span(item.def_id));
194                         let idx = if sources.len() > 1 {
195                             let msg = &format!(
196                                 "candidate #{} is defined in the trait `{}`",
197                                 idx + 1,
198                                 self.tcx.def_path_str(trait_did)
199                             );
200                             err.span_note(item_span, msg);
201                             Some(idx + 1)
202                         } else {
203                             let msg = &format!(
204                                 "the candidate is defined in the trait `{}`",
205                                 self.tcx.def_path_str(trait_did)
206                             );
207                             err.span_note(item_span, msg);
208                             None
209                         };
210                         let path = self.tcx.def_path_str(trait_did);
211                         print_disambiguation_help(
212                             item_name,
213                             args,
214                             err,
215                             path,
216                             rcvr_ty,
217                             item.kind,
218                             sugg_span,
219                             idx,
220                             self.tcx.sess.source_map(),
221                         );
222                     }
223                 }
224             }
225             if sources.len() > limit {
226                 err.note(&format!("and {} others", sources.len() - limit));
227             }
228         };
229
230         let sugg_span = if let SelfSource::MethodCall(expr) = source {
231             // Given `foo.bar(baz)`, `expr` is `bar`, but we want to point to the whole thing.
232             self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id)).span
233         } else {
234             span
235         };
236
237         match error {
238             MethodError::NoMatch(NoMatchData {
239                 static_candidates: static_sources,
240                 unsatisfied_predicates,
241                 out_of_scope_traits,
242                 lev_candidate,
243                 mode,
244             }) => {
245                 let tcx = self.tcx;
246
247                 let actual = self.resolve_vars_if_possible(&rcvr_ty);
248                 let ty_str = self.ty_to_string(actual);
249                 let is_method = mode == Mode::MethodCall;
250                 let item_kind = if is_method {
251                     "method"
252                 } else if actual.is_enum() {
253                     "variant or associated item"
254                 } else {
255                     match (item_name.as_str().chars().next(), actual.is_fresh_ty()) {
256                         (Some(name), false) if name.is_lowercase() => "function or associated item",
257                         (Some(_), false) => "associated item",
258                         (Some(_), true) | (None, false) => "variant or associated item",
259                         (None, true) => "variant",
260                     }
261                 };
262                 let mut err = if !actual.references_error() {
263                     // Suggest clamping down the type if the method that is being attempted to
264                     // be used exists at all, and the type is an ambiguous numeric type
265                     // ({integer}/{float}).
266                     let mut candidates = all_traits(self.tcx).into_iter().filter_map(|info| {
267                         self.associated_item(info.def_id, item_name, Namespace::ValueNS)
268                     });
269                     if let (true, false, SelfSource::MethodCall(expr), Some(_)) = (
270                         actual.is_numeric(),
271                         actual.has_concrete_skeleton(),
272                         source,
273                         candidates.next(),
274                     ) {
275                         let mut err = struct_span_err!(
276                             tcx.sess,
277                             span,
278                             E0689,
279                             "can't call {} `{}` on ambiguous numeric type `{}`",
280                             item_kind,
281                             item_name,
282                             ty_str
283                         );
284                         let concrete_type = if actual.is_integral() { "i32" } else { "f32" };
285                         match expr.kind {
286                             ExprKind::Lit(ref lit) => {
287                                 // numeric literal
288                                 let snippet = tcx
289                                     .sess
290                                     .source_map()
291                                     .span_to_snippet(lit.span)
292                                     .unwrap_or_else(|_| "<numeric literal>".to_owned());
293
294                                 err.span_suggestion(
295                                     lit.span,
296                                     &format!(
297                                         "you must specify a concrete type for \
298                                               this numeric value, like `{}`",
299                                         concrete_type
300                                     ),
301                                     format!("{}_{}", snippet, concrete_type),
302                                     Applicability::MaybeIncorrect,
303                                 );
304                             }
305                             ExprKind::Path(ref qpath) => {
306                                 // local binding
307                                 if let &QPath::Resolved(_, ref path) = &qpath {
308                                     if let hir::def::Res::Local(hir_id) = path.res {
309                                         let span = tcx.hir().span(hir_id);
310                                         let snippet = tcx.sess.source_map().span_to_snippet(span);
311                                         let filename = tcx.sess.source_map().span_to_filename(span);
312
313                                         let parent_node = self
314                                             .tcx
315                                             .hir()
316                                             .get(self.tcx.hir().get_parent_node(hir_id));
317                                         let msg = format!(
318                                             "you must specify a type for this binding, like `{}`",
319                                             concrete_type,
320                                         );
321
322                                         match (filename, parent_node, snippet) {
323                                             (
324                                                 FileName::Real(_),
325                                                 Node::Local(hir::Local {
326                                                     source: hir::LocalSource::Normal,
327                                                     ty,
328                                                     ..
329                                                 }),
330                                                 Ok(ref snippet),
331                                             ) => {
332                                                 err.span_suggestion(
333                                                     // account for `let x: _ = 42;`
334                                                     //                  ^^^^
335                                                     span.to(ty
336                                                         .as_ref()
337                                                         .map(|ty| ty.span)
338                                                         .unwrap_or(span)),
339                                                     &msg,
340                                                     format!("{}: {}", snippet, concrete_type),
341                                                     Applicability::MaybeIncorrect,
342                                                 );
343                                             }
344                                             _ => {
345                                                 err.span_label(span, msg);
346                                             }
347                                         }
348                                     }
349                                 }
350                             }
351                             _ => {}
352                         }
353                         err.emit();
354                         return None;
355                     } else {
356                         span = item_name.span;
357                         let mut err = struct_span_err!(
358                             tcx.sess,
359                             span,
360                             E0599,
361                             "no {} named `{}` found for {} `{}` in the current scope",
362                             item_kind,
363                             item_name,
364                             actual.prefix_string(),
365                             ty_str,
366                         );
367                         if let Some(span) =
368                             tcx.sess.confused_type_with_std_module.borrow().get(&span)
369                         {
370                             if let Ok(snippet) = tcx.sess.source_map().span_to_snippet(*span) {
371                                 err.span_suggestion(
372                                     *span,
373                                     "you are looking for the module in `std`, \
374                                      not the primitive type",
375                                     format!("std::{}", snippet),
376                                     Applicability::MachineApplicable,
377                                 );
378                             }
379                         }
380                         if let ty::RawPtr(_) = &actual.kind {
381                             err.note(
382                                 "try using `<*const T>::as_ref()` to get a reference to the \
383                                       type behind the pointer: https://doc.rust-lang.org/std/\
384                                       primitive.pointer.html#method.as_ref",
385                             );
386                             err.note(
387                                 "using `<*const T>::as_ref()` on a pointer \
388                                       which is unaligned or points to invalid \
389                                       or uninitialized memory is undefined behavior",
390                             );
391                         }
392                         err
393                     }
394                 } else {
395                     tcx.sess.diagnostic().struct_dummy()
396                 };
397
398                 if let Some(def) = actual.ty_adt_def() {
399                     if let Some(full_sp) = tcx.hir().span_if_local(def.did) {
400                         let def_sp = tcx.sess.source_map().def_span(full_sp);
401                         err.span_label(
402                             def_sp,
403                             format!(
404                                 "{} `{}` not found {}",
405                                 item_kind,
406                                 item_name,
407                                 if def.is_enum() && !is_method { "here" } else { "for this" }
408                             ),
409                         );
410                     }
411                 }
412
413                 // If the method name is the name of a field with a function or closure type,
414                 // give a helping note that it has to be called as `(x.f)(...)`.
415                 if let SelfSource::MethodCall(expr) = source {
416                     let field_receiver =
417                         self.autoderef(span, rcvr_ty).find_map(|(ty, _)| match ty.kind {
418                             ty::Adt(def, substs) if !def.is_enum() => {
419                                 let variant = &def.non_enum_variant();
420                                 self.tcx.find_field_index(item_name, variant).map(|index| {
421                                     let field = &variant.fields[index];
422                                     let field_ty = field.ty(tcx, substs);
423                                     (field, field_ty)
424                                 })
425                             }
426                             _ => None,
427                         });
428
429                     if let Some((field, field_ty)) = field_receiver {
430                         let scope = self.tcx.parent_module(self.body_id);
431                         let is_accessible = field.vis.is_accessible_from(scope, self.tcx);
432
433                         if is_accessible {
434                             if self.is_fn_ty(&field_ty, span) {
435                                 let expr_span = expr.span.to(item_name.span);
436                                 err.multipart_suggestion(
437                                     &format!(
438                                         "to call the function stored in `{}`, \
439                                          surround the field access with parentheses",
440                                         item_name,
441                                     ),
442                                     vec![
443                                         (expr_span.shrink_to_lo(), '('.to_string()),
444                                         (expr_span.shrink_to_hi(), ')'.to_string()),
445                                     ],
446                                     Applicability::MachineApplicable,
447                                 );
448                             } else {
449                                 let call_expr = self
450                                     .tcx
451                                     .hir()
452                                     .expect_expr(self.tcx.hir().get_parent_node(expr.hir_id));
453
454                                 if let Some(span) = call_expr.span.trim_start(item_name.span) {
455                                     err.span_suggestion(
456                                         span,
457                                         "remove the arguments",
458                                         String::new(),
459                                         Applicability::MaybeIncorrect,
460                                     );
461                                 }
462                             }
463                         }
464
465                         let field_kind = if is_accessible { "field" } else { "private field" };
466                         err.span_label(item_name.span, format!("{}, not a method", field_kind));
467                     } else if lev_candidate.is_none() && static_sources.is_empty() {
468                         err.span_label(span, format!("{} not found in `{}`", item_kind, ty_str));
469                         self.tcx.sess.trait_methods_not_found.borrow_mut().insert(orig_span);
470                     }
471                 } else {
472                     err.span_label(span, format!("{} not found in `{}`", item_kind, ty_str));
473                     self.tcx.sess.trait_methods_not_found.borrow_mut().insert(orig_span);
474                 }
475
476                 if self.is_fn_ty(&rcvr_ty, span) {
477                     macro_rules! report_function {
478                         ($span:expr, $name:expr) => {
479                             err.note(&format!(
480                                 "`{}` is a function, perhaps you wish to call it",
481                                 $name
482                             ));
483                         };
484                     }
485
486                     if let SelfSource::MethodCall(expr) = source {
487                         if let Ok(expr_string) = tcx.sess.source_map().span_to_snippet(expr.span) {
488                             report_function!(expr.span, expr_string);
489                         } else if let ExprKind::Path(QPath::Resolved(_, ref path)) = expr.kind {
490                             if let Some(segment) = path.segments.last() {
491                                 report_function!(expr.span, segment.ident);
492                             }
493                         }
494                     }
495                 }
496
497                 if !static_sources.is_empty() {
498                     err.note(
499                         "found the following associated functions; to be used as methods, \
500                          functions must have a `self` parameter",
501                     );
502                     err.span_label(span, "this is an associated function, not a method");
503                 }
504                 if static_sources.len() == 1 {
505                     let ty_str = if let Some(CandidateSource::ImplSource(impl_did)) =
506                         static_sources.get(0)
507                     {
508                         // When the "method" is resolved through dereferencing, we really want the
509                         // original type that has the associated function for accurate suggestions.
510                         // (#61411)
511                         let ty = self.impl_self_ty(span, *impl_did).ty;
512                         match (&ty.peel_refs().kind, &actual.peel_refs().kind) {
513                             (ty::Adt(def, _), ty::Adt(def_actual, _)) if def == def_actual => {
514                                 // Use `actual` as it will have more `substs` filled in.
515                                 self.ty_to_value_string(actual.peel_refs())
516                             }
517                             _ => self.ty_to_value_string(ty.peel_refs()),
518                         }
519                     } else {
520                         self.ty_to_value_string(actual.peel_refs())
521                     };
522                     if let SelfSource::MethodCall(expr) = source {
523                         err.span_suggestion(
524                             expr.span.to(span),
525                             "use associated function syntax instead",
526                             format!("{}::{}", ty_str, item_name),
527                             Applicability::MachineApplicable,
528                         );
529                     } else {
530                         err.help(&format!("try with `{}::{}`", ty_str, item_name,));
531                     }
532
533                     report_candidates(span, &mut err, static_sources, sugg_span);
534                 } else if static_sources.len() > 1 {
535                     report_candidates(span, &mut err, static_sources, sugg_span);
536                 }
537
538                 let mut restrict_type_params = false;
539                 if !unsatisfied_predicates.is_empty() {
540                     let def_span =
541                         |def_id| self.tcx.sess.source_map().def_span(self.tcx.def_span(def_id));
542                     let mut type_params = FxHashMap::default();
543                     let mut bound_spans = vec![];
544                     let mut collect_type_param_suggestions =
545                         |self_ty: Ty<'_>, parent_pred: &ty::Predicate<'_>, obligation: &str| {
546                             if let (ty::Param(_), ty::Predicate::Trait(p, _)) =
547                                 (&self_ty.kind, parent_pred)
548                             {
549                                 if let ty::Adt(def, _) = p.skip_binder().trait_ref.self_ty().kind {
550                                     let node = self
551                                         .tcx
552                                         .hir()
553                                         .as_local_hir_id(def.did)
554                                         .map(|id| self.tcx.hir().get(id));
555                                     match node {
556                                         Some(hir::Node::Item(hir::Item { kind, .. })) => {
557                                             if let Some(g) = kind.generics() {
558                                                 let key = match &g.where_clause.predicates[..] {
559                                                     [.., pred] => {
560                                                         (pred.span().shrink_to_hi(), false)
561                                                     }
562                                                     [] => (
563                                                         g.where_clause
564                                                             .span_for_predicates_or_empty_place(),
565                                                         true,
566                                                     ),
567                                                 };
568                                                 type_params
569                                                     .entry(key)
570                                                     .or_insert_with(FxHashSet::default)
571                                                     .insert(obligation.to_owned());
572                                             }
573                                         }
574                                         _ => {}
575                                     }
576                                 }
577                             }
578                         };
579                     let mut bound_span_label = |self_ty: Ty<'_>, obligation: &str, quiet: &str| {
580                         let msg = format!(
581                             "doesn't satisfy `{}`",
582                             if obligation.len() > 50 { quiet } else { obligation }
583                         );
584                         match &self_ty.kind {
585                             // Point at the type that couldn't satisfy the bound.
586                             ty::Adt(def, _) => bound_spans.push((def_span(def.did), msg)),
587                             // Point at the trait object that couldn't satisfy the bound.
588                             ty::Dynamic(preds, _) => {
589                                 for pred in *preds.skip_binder() {
590                                     match pred {
591                                         ty::ExistentialPredicate::Trait(tr) => {
592                                             bound_spans.push((def_span(tr.def_id), msg.clone()))
593                                         }
594                                         ty::ExistentialPredicate::Projection(_)
595                                         | ty::ExistentialPredicate::AutoTrait(_) => {}
596                                     }
597                                 }
598                             }
599                             // Point at the closure that couldn't satisfy the bound.
600                             ty::Closure(def_id, _) => bound_spans
601                                 .push((def_span(*def_id), format!("doesn't satisfy `{}`", quiet))),
602                             _ => {}
603                         }
604                     };
605                     let mut format_pred = |pred| {
606                         match pred {
607                             ty::Predicate::Projection(pred) => {
608                                 // `<Foo as Iterator>::Item = String`.
609                                 let trait_ref =
610                                     pred.skip_binder().projection_ty.trait_ref(self.tcx);
611                                 let assoc = self
612                                     .tcx
613                                     .associated_item(pred.skip_binder().projection_ty.item_def_id);
614                                 let ty = pred.skip_binder().ty;
615                                 let obligation = format!("{}::{} = {}", trait_ref, assoc.ident, ty);
616                                 let quiet = format!(
617                                     "<_ as {}>::{} = {}",
618                                     trait_ref.print_only_trait_path(),
619                                     assoc.ident,
620                                     ty
621                                 );
622                                 bound_span_label(trait_ref.self_ty(), &obligation, &quiet);
623                                 Some((obligation, trait_ref.self_ty()))
624                             }
625                             ty::Predicate::Trait(poly_trait_ref, _) => {
626                                 let p = poly_trait_ref.skip_binder().trait_ref;
627                                 let self_ty = p.self_ty();
628                                 let path = p.print_only_trait_path();
629                                 let obligation = format!("{}: {}", self_ty, path);
630                                 let quiet = format!("_: {}", path);
631                                 bound_span_label(self_ty, &obligation, &quiet);
632                                 Some((obligation, self_ty))
633                             }
634                             _ => None,
635                         }
636                     };
637                     let mut bound_list = unsatisfied_predicates
638                         .iter()
639                         .filter_map(|(pred, parent_pred)| {
640                             format_pred(*pred).map(|(p, self_ty)| match parent_pred {
641                                 None => format!("`{}`", p),
642                                 Some(parent_pred) => match format_pred(*parent_pred) {
643                                     None => format!("`{}`", p),
644                                     Some((parent_p, _)) => {
645                                         collect_type_param_suggestions(self_ty, parent_pred, &p);
646                                         format!("`{}`\nwhich is required by `{}`", p, parent_p)
647                                     }
648                                 },
649                             })
650                         })
651                         .enumerate()
652                         .collect::<Vec<(usize, String)>>();
653                     for ((span, empty_where), obligations) in type_params.into_iter() {
654                         restrict_type_params = true;
655                         err.span_suggestion_verbose(
656                             span,
657                             &format!(
658                                 "consider restricting the type parameter{s} to satisfy the \
659                                  trait bound{s}",
660                                 s = pluralize!(obligations.len())
661                             ),
662                             format!(
663                                 "{} {}",
664                                 if empty_where { " where" } else { "," },
665                                 obligations.into_iter().collect::<Vec<_>>().join(", ")
666                             ),
667                             Applicability::MaybeIncorrect,
668                         );
669                     }
670
671                     bound_list.sort_by(|(_, a), (_, b)| a.cmp(&b)); // Sort alphabetically.
672                     bound_list.dedup_by(|(_, a), (_, b)| a == b); // #35677
673                     bound_list.sort_by_key(|(pos, _)| *pos); // Keep the original predicate order.
674                     bound_spans.sort();
675                     bound_spans.dedup();
676                     for (span, msg) in bound_spans.into_iter() {
677                         err.span_label(span, &msg);
678                     }
679                     if !bound_list.is_empty() {
680                         let bound_list = bound_list
681                             .into_iter()
682                             .map(|(_, path)| path)
683                             .collect::<Vec<_>>()
684                             .join("\n");
685                         err.note(&format!(
686                             "the method `{}` exists but the following trait bounds were not \
687                              satisfied:\n{}",
688                             item_name, bound_list
689                         ));
690                     }
691                 }
692
693                 if actual.is_numeric() && actual.is_fresh() || restrict_type_params {
694                 } else {
695                     self.suggest_traits_to_import(
696                         &mut err,
697                         span,
698                         rcvr_ty,
699                         item_name,
700                         source,
701                         out_of_scope_traits,
702                         &unsatisfied_predicates,
703                     );
704                 }
705
706                 if actual.is_enum() {
707                     let adt_def = actual.ty_adt_def().expect("enum is not an ADT");
708                     if let Some(suggestion) = lev_distance::find_best_match_for_name(
709                         adt_def.variants.iter().map(|s| &s.ident.name),
710                         &item_name.as_str(),
711                         None,
712                     ) {
713                         err.span_suggestion(
714                             span,
715                             "there is a variant with a similar name",
716                             suggestion.to_string(),
717                             Applicability::MaybeIncorrect,
718                         );
719                     }
720                 }
721
722                 let mut fallback_span = true;
723                 let msg = "remove this method call";
724                 if item_name.as_str() == "as_str" && actual.peel_refs().is_str() {
725                     if let SelfSource::MethodCall(expr) = source {
726                         let call_expr =
727                             self.tcx.hir().expect_expr(self.tcx.hir().get_parent_node(expr.hir_id));
728                         if let Some(span) = call_expr.span.trim_start(expr.span) {
729                             err.span_suggestion(
730                                 span,
731                                 msg,
732                                 String::new(),
733                                 Applicability::MachineApplicable,
734                             );
735                             fallback_span = false;
736                         }
737                     }
738                     if fallback_span {
739                         err.span_label(span, msg);
740                     }
741                 } else if let Some(lev_candidate) = lev_candidate {
742                     let def_kind = lev_candidate.def_kind();
743                     err.span_suggestion(
744                         span,
745                         &format!(
746                             "there is {} {} with a similar name",
747                             def_kind.article(),
748                             def_kind.descr(lev_candidate.def_id),
749                         ),
750                         lev_candidate.ident.to_string(),
751                         Applicability::MaybeIncorrect,
752                     );
753                 }
754
755                 return Some(err);
756             }
757
758             MethodError::Ambiguity(sources) => {
759                 let mut err = struct_span_err!(
760                     self.sess(),
761                     span,
762                     E0034,
763                     "multiple applicable items in scope"
764                 );
765                 err.span_label(span, format!("multiple `{}` found", item_name));
766
767                 report_candidates(span, &mut err, sources, sugg_span);
768                 err.emit();
769             }
770
771             MethodError::PrivateMatch(kind, def_id, out_of_scope_traits) => {
772                 let mut err = struct_span_err!(
773                     self.tcx.sess,
774                     span,
775                     E0624,
776                     "{} `{}` is private",
777                     kind.descr(def_id),
778                     item_name
779                 );
780                 self.suggest_valid_traits(&mut err, out_of_scope_traits);
781                 err.emit();
782             }
783
784             MethodError::IllegalSizedBound(candidates, needs_mut, bound_span) => {
785                 let msg = format!("the `{}` method cannot be invoked on a trait object", item_name);
786                 let mut err = self.sess().struct_span_err(span, &msg);
787                 err.span_label(bound_span, "this has a `Sized` requirement");
788                 if !candidates.is_empty() {
789                     let help = format!(
790                         "{an}other candidate{s} {were} found in the following trait{s}, perhaps \
791                          add a `use` for {one_of_them}:",
792                         an = if candidates.len() == 1 { "an" } else { "" },
793                         s = pluralize!(candidates.len()),
794                         were = if candidates.len() == 1 { "was" } else { "were" },
795                         one_of_them = if candidates.len() == 1 { "it" } else { "one_of_them" },
796                     );
797                     self.suggest_use_candidates(&mut err, help, candidates);
798                 }
799                 if let ty::Ref(region, t_type, mutability) = rcvr_ty.kind {
800                     if needs_mut {
801                         let trait_type = self.tcx.mk_ref(
802                             region,
803                             ty::TypeAndMut { ty: t_type, mutbl: mutability.invert() },
804                         );
805                         err.note(&format!("you need `{}` instead of `{}`", trait_type, rcvr_ty));
806                     }
807                 }
808                 err.emit();
809             }
810
811             MethodError::BadReturnType => bug!("no return type expectations but got BadReturnType"),
812         }
813         None
814     }
815
816     /// Print out the type for use in value namespace.
817     fn ty_to_value_string(&self, ty: Ty<'tcx>) -> String {
818         match ty.kind {
819             ty::Adt(def, substs) => format!("{}", ty::Instance::new(def.did, substs)),
820             _ => self.ty_to_string(ty),
821         }
822     }
823
824     fn suggest_use_candidates(
825         &self,
826         err: &mut DiagnosticBuilder<'_>,
827         mut msg: String,
828         candidates: Vec<DefId>,
829     ) {
830         let module_did = self.tcx.parent_module(self.body_id);
831         let module_id = self.tcx.hir().as_local_hir_id(module_did).unwrap();
832         let krate = self.tcx.hir().krate();
833         let (span, found_use) = UsePlacementFinder::check(self.tcx, krate, module_id);
834         if let Some(span) = span {
835             let path_strings = candidates.iter().map(|did| {
836                 // Produce an additional newline to separate the new use statement
837                 // from the directly following item.
838                 let additional_newline = if found_use { "" } else { "\n" };
839                 format!(
840                     "use {};\n{}",
841                     with_crate_prefix(|| self.tcx.def_path_str(*did)),
842                     additional_newline
843                 )
844             });
845
846             err.span_suggestions(span, &msg, path_strings, Applicability::MaybeIncorrect);
847         } else {
848             let limit = if candidates.len() == 5 { 5 } else { 4 };
849             for (i, trait_did) in candidates.iter().take(limit).enumerate() {
850                 if candidates.len() > 1 {
851                     msg.push_str(&format!(
852                         "\ncandidate #{}: `use {};`",
853                         i + 1,
854                         with_crate_prefix(|| self.tcx.def_path_str(*trait_did))
855                     ));
856                 } else {
857                     msg.push_str(&format!(
858                         "\n`use {};`",
859                         with_crate_prefix(|| self.tcx.def_path_str(*trait_did))
860                     ));
861                 }
862             }
863             if candidates.len() > limit {
864                 msg.push_str(&format!("\nand {} others", candidates.len() - limit));
865             }
866             err.note(&msg[..]);
867         }
868     }
869
870     fn suggest_valid_traits(
871         &self,
872         err: &mut DiagnosticBuilder<'_>,
873         valid_out_of_scope_traits: Vec<DefId>,
874     ) -> bool {
875         if !valid_out_of_scope_traits.is_empty() {
876             let mut candidates = valid_out_of_scope_traits;
877             candidates.sort();
878             candidates.dedup();
879             err.help("items from traits can only be used if the trait is in scope");
880             let msg = format!(
881                 "the following {traits_are} implemented but not in scope; \
882                  perhaps add a `use` for {one_of_them}:",
883                 traits_are = if candidates.len() == 1 { "trait is" } else { "traits are" },
884                 one_of_them = if candidates.len() == 1 { "it" } else { "one of them" },
885             );
886
887             self.suggest_use_candidates(err, msg, candidates);
888             true
889         } else {
890             false
891         }
892     }
893
894     fn suggest_traits_to_import<'b>(
895         &self,
896         err: &mut DiagnosticBuilder<'_>,
897         span: Span,
898         rcvr_ty: Ty<'tcx>,
899         item_name: ast::Ident,
900         source: SelfSource<'b>,
901         valid_out_of_scope_traits: Vec<DefId>,
902         unsatisfied_predicates: &[(ty::Predicate<'tcx>, Option<ty::Predicate<'tcx>>)],
903     ) {
904         if self.suggest_valid_traits(err, valid_out_of_scope_traits) {
905             return;
906         }
907
908         let type_is_local = self.type_derefs_to_local(span, rcvr_ty, source);
909
910         let mut arbitrary_rcvr = vec![];
911         // There are no traits implemented, so lets suggest some traits to
912         // implement, by finding ones that have the item name, and are
913         // legal to implement.
914         let mut candidates = all_traits(self.tcx)
915             .into_iter()
916             .filter(|info| {
917                 // We approximate the coherence rules to only suggest
918                 // traits that are legal to implement by requiring that
919                 // either the type or trait is local. Multi-dispatch means
920                 // this isn't perfect (that is, there are cases when
921                 // implementing a trait would be legal but is rejected
922                 // here).
923                 !unsatisfied_predicates.iter().any(|(p, _)| match p {
924                     // Hide traits if they are present in predicates as they can be fixed without
925                     // having to implement them.
926                     ty::Predicate::Trait(t, _) => t.def_id() != info.def_id,
927                     ty::Predicate::Projection(p) => p.item_def_id() != info.def_id,
928                     _ => true,
929                 }) && (type_is_local || info.def_id.is_local())
930                     && self
931                         .associated_item(info.def_id, item_name, Namespace::ValueNS)
932                         .filter(|item| {
933                             if let ty::AssocKind::Method = item.kind {
934                                 let id = self.tcx.hir().as_local_hir_id(item.def_id);
935                                 if let Some(hir::Node::TraitItem(hir::TraitItem {
936                                     kind: hir::TraitItemKind::Fn(fn_sig, method),
937                                     ..
938                                 })) = id.map(|id| self.tcx.hir().get(id))
939                                 {
940                                     let self_first_arg = match method {
941                                         hir::TraitMethod::Required([ident, ..]) => {
942                                             ident.name == kw::SelfLower
943                                         }
944                                         hir::TraitMethod::Provided(body_id) => {
945                                             match &self.tcx.hir().body(*body_id).params[..] {
946                                                 [hir::Param {
947                                                     pat:
948                                                         hir::Pat {
949                                                             kind:
950                                                                 hir::PatKind::Binding(
951                                                                     _,
952                                                                     _,
953                                                                     ident,
954                                                                     ..,
955                                                                 ),
956                                                             ..
957                                                         },
958                                                     ..
959                                                 }, ..] => ident.name == kw::SelfLower,
960                                                 _ => false,
961                                             }
962                                         }
963                                         _ => false,
964                                     };
965
966                                     if !fn_sig.decl.implicit_self.has_implicit_self()
967                                         && self_first_arg
968                                     {
969                                         if let Some(ty) = fn_sig.decl.inputs.get(0) {
970                                             arbitrary_rcvr.push(ty.span);
971                                         }
972                                         return false;
973                                     }
974                                 }
975                             }
976                             // We only want to suggest public or local traits (#45781).
977                             item.vis == ty::Visibility::Public || info.def_id.is_local()
978                         })
979                         .is_some()
980             })
981             .collect::<Vec<_>>();
982         for span in &arbitrary_rcvr {
983             err.span_label(
984                 *span,
985                 "the method might not be found because of this arbitrary self type",
986             );
987         }
988
989         if !candidates.is_empty() {
990             // Sort from most relevant to least relevant.
991             candidates.sort_by(|a, b| a.cmp(b).reverse());
992             candidates.dedup();
993
994             let param_type = match rcvr_ty.kind {
995                 ty::Param(param) => Some(param),
996                 ty::Ref(_, ty, _) => match ty.kind {
997                     ty::Param(param) => Some(param),
998                     _ => None,
999                 },
1000                 _ => None,
1001             };
1002             err.help(if param_type.is_some() {
1003                 "items from traits can only be used if the type parameter is bounded by the trait"
1004             } else {
1005                 "items from traits can only be used if the trait is implemented and in scope"
1006             });
1007             let message = |action| {
1008                 format!(
1009                     "the following {traits_define} an item `{name}`, perhaps you need to {action} \
1010                      {one_of_them}:",
1011                     traits_define =
1012                         if candidates.len() == 1 { "trait defines" } else { "traits define" },
1013                     action = action,
1014                     one_of_them = if candidates.len() == 1 { "it" } else { "one of them" },
1015                     name = item_name,
1016                 )
1017             };
1018             // Obtain the span for `param` and use it for a structured suggestion.
1019             let mut suggested = false;
1020             if let (Some(ref param), Some(ref table)) = (param_type, self.in_progress_tables) {
1021                 let table = table.borrow();
1022                 if let Some(did) = table.local_id_root {
1023                     let generics = self.tcx.generics_of(did);
1024                     let type_param = generics.type_param(param, self.tcx);
1025                     let hir = &self.tcx.hir();
1026                     if let Some(id) = hir.as_local_hir_id(type_param.def_id) {
1027                         // Get the `hir::Param` to verify whether it already has any bounds.
1028                         // We do this to avoid suggesting code that ends up as `T: FooBar`,
1029                         // instead we suggest `T: Foo + Bar` in that case.
1030                         match hir.get(id) {
1031                             Node::GenericParam(ref param) => {
1032                                 let mut impl_trait = false;
1033                                 let has_bounds = if let hir::GenericParamKind::Type {
1034                                     synthetic: Some(_),
1035                                     ..
1036                                 } = &param.kind
1037                                 {
1038                                     // We've found `fn foo(x: impl Trait)` instead of
1039                                     // `fn foo<T>(x: T)`. We want to suggest the correct
1040                                     // `fn foo(x: impl Trait + TraitBound)` instead of
1041                                     // `fn foo<T: TraitBound>(x: T)`. (#63706)
1042                                     impl_trait = true;
1043                                     param.bounds.get(1)
1044                                 } else {
1045                                     param.bounds.get(0)
1046                                 };
1047                                 let sp = hir.span(id);
1048                                 let sp = if let Some(first_bound) = has_bounds {
1049                                     // `sp` only covers `T`, change it so that it covers
1050                                     // `T:` when appropriate
1051                                     sp.until(first_bound.span())
1052                                 } else {
1053                                     sp
1054                                 };
1055                                 let trait_def_ids: FxHashSet<DefId> = param
1056                                     .bounds
1057                                     .iter()
1058                                     .filter_map(|bound| bound.trait_def_id())
1059                                     .collect();
1060                                 if !candidates.iter().any(|t| trait_def_ids.contains(&t.def_id)) {
1061                                     err.span_suggestions(
1062                                         sp,
1063                                         &message(format!(
1064                                             "restrict type parameter `{}` with",
1065                                             param.name.ident(),
1066                                         )),
1067                                         candidates.iter().map(|t| {
1068                                             format!(
1069                                                 "{}{} {}{}",
1070                                                 param.name.ident(),
1071                                                 if impl_trait { " +" } else { ":" },
1072                                                 self.tcx.def_path_str(t.def_id),
1073                                                 if has_bounds.is_some() { " + " } else { "" },
1074                                             )
1075                                         }),
1076                                         Applicability::MaybeIncorrect,
1077                                     );
1078                                 }
1079                                 suggested = true;
1080                             }
1081                             Node::Item(hir::Item {
1082                                 kind: hir::ItemKind::Trait(.., bounds, _),
1083                                 ident,
1084                                 ..
1085                             }) => {
1086                                 let (sp, sep, article) = if bounds.is_empty() {
1087                                     (ident.span.shrink_to_hi(), ":", "a")
1088                                 } else {
1089                                     (bounds.last().unwrap().span().shrink_to_hi(), " +", "another")
1090                                 };
1091                                 err.span_suggestions(
1092                                     sp,
1093                                     &message(format!("add {} supertrait for", article)),
1094                                     candidates.iter().map(|t| {
1095                                         format!("{} {}", sep, self.tcx.def_path_str(t.def_id),)
1096                                     }),
1097                                     Applicability::MaybeIncorrect,
1098                                 );
1099                                 suggested = true;
1100                             }
1101                             _ => {}
1102                         }
1103                     }
1104                 };
1105             }
1106
1107             if !suggested {
1108                 let action = if let Some(param) = param_type {
1109                     format!("restrict type parameter `{}` with", param)
1110                 } else {
1111                     // FIXME: it might only need to be imported into scope, not implemented.
1112                     "implement".to_string()
1113                 };
1114                 let mut use_note = true;
1115                 if let [trait_info] = &candidates[..] {
1116                     if let Some(span) = self.tcx.hir().span_if_local(trait_info.def_id) {
1117                         err.span_note(
1118                             self.tcx.sess.source_map().def_span(span),
1119                             &format!(
1120                                 "`{}` defines an item `{}`, perhaps you need to {} it",
1121                                 self.tcx.def_path_str(trait_info.def_id),
1122                                 item_name,
1123                                 action
1124                             ),
1125                         );
1126                         use_note = false
1127                     }
1128                 }
1129                 if use_note {
1130                     let mut msg = message(action);
1131                     for (i, trait_info) in candidates.iter().enumerate() {
1132                         msg.push_str(&format!(
1133                             "\ncandidate #{}: `{}`",
1134                             i + 1,
1135                             self.tcx.def_path_str(trait_info.def_id),
1136                         ));
1137                     }
1138                     err.note(&msg[..]);
1139                 }
1140             }
1141         }
1142     }
1143
1144     /// Checks whether there is a local type somewhere in the chain of
1145     /// autoderefs of `rcvr_ty`.
1146     fn type_derefs_to_local(&self, span: Span, rcvr_ty: Ty<'tcx>, source: SelfSource<'_>) -> bool {
1147         fn is_local(ty: Ty<'_>) -> bool {
1148             match ty.kind {
1149                 ty::Adt(def, _) => def.did.is_local(),
1150                 ty::Foreign(did) => did.is_local(),
1151
1152                 ty::Dynamic(ref tr, ..) => {
1153                     tr.principal().map(|d| d.def_id().is_local()).unwrap_or(false)
1154                 }
1155
1156                 ty::Param(_) => true,
1157
1158                 // Everything else (primitive types, etc.) is effectively
1159                 // non-local (there are "edge" cases, e.g., `(LocalType,)`, but
1160                 // the noise from these sort of types is usually just really
1161                 // annoying, rather than any sort of help).
1162                 _ => false,
1163             }
1164         }
1165
1166         // This occurs for UFCS desugaring of `T::method`, where there is no
1167         // receiver expression for the method call, and thus no autoderef.
1168         if let SelfSource::QPath(_) = source {
1169             return is_local(self.resolve_vars_with_obligations(rcvr_ty));
1170         }
1171
1172         self.autoderef(span, rcvr_ty).any(|(ty, _)| is_local(ty))
1173     }
1174 }
1175
1176 #[derive(Copy, Clone)]
1177 pub enum SelfSource<'a> {
1178     QPath(&'a hir::Ty<'a>),
1179     MethodCall(&'a hir::Expr<'a> /* rcvr */),
1180 }
1181
1182 #[derive(Copy, Clone)]
1183 pub struct TraitInfo {
1184     pub def_id: DefId,
1185 }
1186
1187 impl PartialEq for TraitInfo {
1188     fn eq(&self, other: &TraitInfo) -> bool {
1189         self.cmp(other) == Ordering::Equal
1190     }
1191 }
1192 impl Eq for TraitInfo {}
1193 impl PartialOrd for TraitInfo {
1194     fn partial_cmp(&self, other: &TraitInfo) -> Option<Ordering> {
1195         Some(self.cmp(other))
1196     }
1197 }
1198 impl Ord for TraitInfo {
1199     fn cmp(&self, other: &TraitInfo) -> Ordering {
1200         // Local crates are more important than remote ones (local:
1201         // `cnum == 0`), and otherwise we throw in the defid for totality.
1202
1203         let lhs = (other.def_id.krate, other.def_id);
1204         let rhs = (self.def_id.krate, self.def_id);
1205         lhs.cmp(&rhs)
1206     }
1207 }
1208
1209 /// Retrieves all traits in this crate and any dependent crates.
1210 pub fn all_traits(tcx: TyCtxt<'_>) -> Vec<TraitInfo> {
1211     tcx.all_traits(LOCAL_CRATE).iter().map(|&def_id| TraitInfo { def_id }).collect()
1212 }
1213
1214 /// Computes all traits in this crate and any dependent crates.
1215 fn compute_all_traits(tcx: TyCtxt<'_>) -> Vec<DefId> {
1216     use hir::itemlikevisit;
1217
1218     let mut traits = vec![];
1219
1220     // Crate-local:
1221
1222     struct Visitor<'a, 'tcx> {
1223         map: &'a hir_map::Map<'tcx>,
1224         traits: &'a mut Vec<DefId>,
1225     }
1226
1227     impl<'v, 'a, 'tcx> itemlikevisit::ItemLikeVisitor<'v> for Visitor<'a, 'tcx> {
1228         fn visit_item(&mut self, i: &'v hir::Item<'v>) {
1229             match i.kind {
1230                 hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => {
1231                     let def_id = self.map.local_def_id(i.hir_id);
1232                     self.traits.push(def_id);
1233                 }
1234                 _ => (),
1235             }
1236         }
1237
1238         fn visit_trait_item(&mut self, _trait_item: &hir::TraitItem<'_>) {}
1239
1240         fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
1241     }
1242
1243     tcx.hir().krate().visit_all_item_likes(&mut Visitor { map: &tcx.hir(), traits: &mut traits });
1244
1245     // Cross-crate:
1246
1247     let mut external_mods = FxHashSet::default();
1248     fn handle_external_res(
1249         tcx: TyCtxt<'_>,
1250         traits: &mut Vec<DefId>,
1251         external_mods: &mut FxHashSet<DefId>,
1252         res: Res,
1253     ) {
1254         match res {
1255             Res::Def(DefKind::Trait, def_id) | Res::Def(DefKind::TraitAlias, def_id) => {
1256                 traits.push(def_id);
1257             }
1258             Res::Def(DefKind::Mod, def_id) => {
1259                 if !external_mods.insert(def_id) {
1260                     return;
1261                 }
1262                 for child in tcx.item_children(def_id).iter() {
1263                     handle_external_res(tcx, traits, external_mods, child.res)
1264                 }
1265             }
1266             _ => {}
1267         }
1268     }
1269     for &cnum in tcx.crates().iter() {
1270         let def_id = DefId { krate: cnum, index: CRATE_DEF_INDEX };
1271         handle_external_res(tcx, &mut traits, &mut external_mods, Res::Def(DefKind::Mod, def_id));
1272     }
1273
1274     traits
1275 }
1276
1277 pub fn provide(providers: &mut ty::query::Providers<'_>) {
1278     providers.all_traits = |tcx, cnum| {
1279         assert_eq!(cnum, LOCAL_CRATE);
1280         &tcx.arena.alloc(compute_all_traits(tcx))[..]
1281     }
1282 }
1283
1284 struct UsePlacementFinder<'tcx> {
1285     target_module: hir::HirId,
1286     span: Option<Span>,
1287     found_use: bool,
1288     tcx: TyCtxt<'tcx>,
1289 }
1290
1291 impl UsePlacementFinder<'tcx> {
1292     fn check(
1293         tcx: TyCtxt<'tcx>,
1294         krate: &'tcx hir::Crate<'tcx>,
1295         target_module: hir::HirId,
1296     ) -> (Option<Span>, bool) {
1297         let mut finder = UsePlacementFinder { target_module, span: None, found_use: false, tcx };
1298         intravisit::walk_crate(&mut finder, krate);
1299         (finder.span, finder.found_use)
1300     }
1301 }
1302
1303 impl intravisit::Visitor<'tcx> for UsePlacementFinder<'tcx> {
1304     fn visit_mod(&mut self, module: &'tcx hir::Mod<'tcx>, _: Span, hir_id: hir::HirId) {
1305         if self.span.is_some() {
1306             return;
1307         }
1308         if hir_id != self.target_module {
1309             intravisit::walk_mod(self, module, hir_id);
1310             return;
1311         }
1312         // Find a `use` statement.
1313         for item_id in module.item_ids {
1314             let item = self.tcx.hir().expect_item(item_id.id);
1315             match item.kind {
1316                 hir::ItemKind::Use(..) => {
1317                     // Don't suggest placing a `use` before the prelude
1318                     // import or other generated ones.
1319                     if !item.span.from_expansion() {
1320                         self.span = Some(item.span.shrink_to_lo());
1321                         self.found_use = true;
1322                         return;
1323                     }
1324                 }
1325                 // Don't place `use` before `extern crate`...
1326                 hir::ItemKind::ExternCrate(_) => {}
1327                 // ...but do place them before the first other item.
1328                 _ => {
1329                     if self.span.map_or(true, |span| item.span < span) {
1330                         if !item.span.from_expansion() {
1331                             // Don't insert between attributes and an item.
1332                             if item.attrs.is_empty() {
1333                                 self.span = Some(item.span.shrink_to_lo());
1334                             } else {
1335                                 // Find the first attribute on the item.
1336                                 for attr in item.attrs {
1337                                     if self.span.map_or(true, |span| attr.span < span) {
1338                                         self.span = Some(attr.span.shrink_to_lo());
1339                                     }
1340                                 }
1341                             }
1342                         }
1343                     }
1344                 }
1345             }
1346         }
1347     }
1348
1349     type Map = intravisit::ErasedMap<'tcx>;
1350
1351     fn nested_visit_map(&mut self) -> intravisit::NestedVisitorMap<Self::Map> {
1352         intravisit::NestedVisitorMap::None
1353     }
1354 }
1355
1356 fn print_disambiguation_help(
1357     item_name: ast::Ident,
1358     args: Option<&'tcx [hir::Expr<'tcx>]>,
1359     err: &mut DiagnosticBuilder<'_>,
1360     trait_name: String,
1361     rcvr_ty: Ty<'_>,
1362     kind: ty::AssocKind,
1363     span: Span,
1364     candidate: Option<usize>,
1365     source_map: &source_map::SourceMap,
1366 ) {
1367     let mut applicability = Applicability::MachineApplicable;
1368     let sugg_args = if let (ty::AssocKind::Method, Some(args)) = (kind, args) {
1369         format!(
1370             "({}{})",
1371             if rcvr_ty.is_region_ptr() {
1372                 if rcvr_ty.is_mutable_ptr() { "&mut " } else { "&" }
1373             } else {
1374                 ""
1375             },
1376             args.iter()
1377                 .map(|arg| source_map.span_to_snippet(arg.span).unwrap_or_else(|_| {
1378                     applicability = Applicability::HasPlaceholders;
1379                     "_".to_owned()
1380                 }))
1381                 .collect::<Vec<_>>()
1382                 .join(", "),
1383         )
1384     } else {
1385         String::new()
1386     };
1387     let sugg = format!("{}::{}{}", trait_name, item_name, sugg_args);
1388     err.span_suggestion(
1389         span,
1390         &format!(
1391             "disambiguate the {} for {}",
1392             kind.suggestion_descr(),
1393             if let Some(candidate) = candidate {
1394                 format!("candidate #{}", candidate)
1395             } else {
1396                 "the candidate".to_string()
1397             },
1398         ),
1399         sugg,
1400         applicability,
1401     );
1402 }