1 // Copyright 2014 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
11 //! Give useful errors and suggestions to users when a method can't be
12 //! found or is otherwise invalid.
17 use check::{self, FnCtxt};
18 use middle::ty::{self, Ty};
20 use metadata::{csearch, cstore, decoder};
21 use util::ppaux::UserString;
23 use syntax::{ast, ast_util};
24 use syntax::codemap::Span;
27 use std::cmp::Ordering;
29 use super::{MethodError, CandidateSource, impl_method, trait_method};
31 pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
34 method_name: ast::Name,
38 MethodError::NoMatch(static_sources, out_of_scope_traits) => {
40 let method_ustring = method_name.user_string(cx);
42 // True if the type is a struct and contains a field with
43 // the same name as the not-found method
44 let is_field = match rcvr_ty.sty {
45 ty::ty_struct(did, _) =>
46 ty::lookup_struct_fields(cx, did)
48 .any(|f| f.name.user_string(cx) == method_ustring),
52 fcx.type_error_message(
55 format!("type `{}` does not implement any \
56 method in scope named `{}`",
63 // If the method has the name of a field, give a help note
65 cx.sess.span_note(span,
66 &format!("use `(s.{0})(...)` if you meant to call the \
67 function stored in the `{0}` field", method_ustring)[]);
70 if static_sources.len() > 0 {
71 fcx.tcx().sess.fileline_note(
73 "found defined static methods, maybe a `self` is missing?");
75 report_candidates(fcx, span, method_name, static_sources);
78 suggest_traits_to_import(fcx, span, rcvr_ty, method_name, out_of_scope_traits)
81 MethodError::Ambiguity(sources) => {
82 span_err!(fcx.sess(), span, E0034,
83 "multiple applicable methods in scope");
85 report_candidates(fcx, span, method_name, sources);
89 fn report_candidates(fcx: &FnCtxt,
91 method_name: ast::Name,
92 mut sources: Vec<CandidateSource>) {
96 for (idx, source) in sources.iter().enumerate() {
98 CandidateSource::ImplSource(impl_did) => {
99 // Provide the best span we can. Use the method, if local to crate, else
100 // the impl, if local to crate (method may be defaulted), else the call site.
101 let method = impl_method(fcx.tcx(), impl_did, method_name).unwrap();
102 let impl_span = fcx.tcx().map.def_id_span(impl_did, span);
103 let method_span = fcx.tcx().map.def_id_span(method.def_id, impl_span);
105 let impl_ty = check::impl_self_ty(fcx, span, impl_did).ty;
107 let insertion = match ty::impl_trait_ref(fcx.tcx(), impl_did) {
109 Some(trait_ref) => format!(" of the trait `{}`",
110 ty::item_path_str(fcx.tcx(),
114 span_note!(fcx.sess(), method_span,
115 "candidate #{} is defined in an impl{} for the type `{}`",
118 impl_ty.user_string(fcx.tcx()));
120 CandidateSource::TraitSource(trait_did) => {
121 let (_, method) = trait_method(fcx.tcx(), trait_did, method_name).unwrap();
122 let method_span = fcx.tcx().map.def_id_span(method.def_id, span);
123 span_note!(fcx.sess(), method_span,
124 "candidate #{} is defined in the trait `{}`",
126 ty::item_path_str(fcx.tcx(), trait_did));
134 pub type AllTraitsVec = Vec<TraitInfo>;
136 fn suggest_traits_to_import<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
139 method_name: ast::Name,
140 valid_out_of_scope_traits: Vec<ast::DefId>)
143 let method_ustring = method_name.user_string(tcx);
145 if !valid_out_of_scope_traits.is_empty() {
146 let mut candidates = valid_out_of_scope_traits;
149 "methods from traits can only be called if the trait is in scope; \
150 the following {traits_are} implemented but not in scope, \
151 perhaps add a `use` for {one_of_them}:",
152 traits_are = if candidates.len() == 1 {"trait is"} else {"traits are"},
153 one_of_them = if candidates.len() == 1 {"it"} else {"one of them"});
155 fcx.sess().fileline_help(span, &msg[]);
157 for (i, trait_did) in candidates.iter().enumerate() {
158 fcx.sess().fileline_help(span,
159 &*format!("candidate #{}: use `{}`",
161 ty::item_path_str(fcx.tcx(), *trait_did)))
167 // there's no implemented traits, so lets suggest some traits to implement
168 let mut candidates = all_traits(fcx.ccx)
169 .filter(|info| trait_method(tcx, info.def_id, method_name).is_some())
170 .collect::<Vec<_>>();
172 if candidates.len() > 0 {
173 // sort from most relevant to least relevant
174 candidates.sort_by(|a, b| a.cmp(b).reverse());
177 "methods from traits can only be called if the trait is implemented and in scope; \
178 the following {traits_define} a method `{name}`, \
179 perhaps you need to implement {one_of_them}:",
180 traits_define = if candidates.len() == 1 {"trait defines"} else {"traits define"},
181 one_of_them = if candidates.len() == 1 {"it"} else {"one of them"},
182 name = method_ustring);
184 fcx.sess().fileline_help(span, &msg[]);
186 for (i, trait_info) in candidates.iter().enumerate() {
187 fcx.sess().fileline_help(span,
188 &*format!("candidate #{}: `{}`",
190 ty::item_path_str(fcx.tcx(), trait_info.def_id)))
196 pub struct TraitInfo {
197 pub def_id: ast::DefId,
201 fn new(def_id: ast::DefId) -> TraitInfo {
207 impl PartialEq for TraitInfo {
208 fn eq(&self, other: &TraitInfo) -> bool {
209 self.cmp(other) == Ordering::Equal
212 impl Eq for TraitInfo {}
213 impl PartialOrd for TraitInfo {
214 fn partial_cmp(&self, other: &TraitInfo) -> Option<Ordering> { Some(self.cmp(other)) }
216 impl Ord for TraitInfo {
217 fn cmp(&self, other: &TraitInfo) -> Ordering {
218 // accessible traits are more important/relevant than
219 // inaccessible ones, local crates are more important than
220 // remote ones (local: cnum == 0), and NodeIds just for
223 let lhs = (other.def_id.krate, other.def_id.node);
224 let rhs = (self.def_id.krate, self.def_id.node);
229 /// Retrieve all traits in this crate and any dependent crates.
230 pub fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> {
231 if ccx.all_traits.borrow().is_none() {
234 let mut traits = vec![];
239 struct Visitor<'a, 'b: 'a, 'tcx: 'a + 'b> {
240 traits: &'a mut AllTraitsVec,
242 impl<'v,'a, 'b, 'tcx> visit::Visitor<'v> for Visitor<'a, 'b, 'tcx> {
243 fn visit_item(&mut self, i: &'v ast::Item) {
245 ast::ItemTrait(..) => {
246 self.traits.push(TraitInfo::new(ast_util::local_def(i.id)));
250 visit::walk_item(self, i)
253 visit::walk_crate(&mut Visitor {
255 }, ccx.tcx.map.krate());
258 fn handle_external_def(traits: &mut AllTraitsVec,
260 cstore: &cstore::CStore,
261 dl: decoder::DefLike) {
263 decoder::DlDef(def::DefTrait(did)) => {
264 traits.push(TraitInfo::new(did));
266 decoder::DlDef(def::DefMod(did)) => {
267 csearch::each_child_of_item(cstore, did, |dl, _, _| {
268 handle_external_def(traits, ccx, cstore, dl)
274 let cstore = &ccx.tcx.sess.cstore;
275 cstore.iter_crate_data(|&mut: cnum, _| {
276 csearch::each_top_level_item_of_crate(cstore, cnum, |dl, _, _| {
277 handle_external_def(&mut traits, ccx, cstore, dl)
281 *ccx.all_traits.borrow_mut() = Some(traits);
284 let borrow = ccx.all_traits.borrow();
285 assert!(borrow.is_some());
292 pub struct AllTraits<'a> {
293 borrow: cell::Ref<'a Option<AllTraitsVec>>,
297 impl<'a> Iterator for AllTraits<'a> {
298 type Item = TraitInfo;
300 fn next(&mut self) -> Option<TraitInfo> {
301 let AllTraits { ref borrow, ref mut idx } = *self;
303 borrow.as_ref().unwrap().get(*idx).map(|info| {