|
11 | 11 | //! Give useful errors and suggestions to users when a method can't be
|
12 | 12 | //! found or is otherwise invalid.
|
13 | 13 |
|
| 14 | +use CrateCtxt; |
| 15 | + |
14 | 16 | use astconv::AstConv;
|
15 | 17 | use check::{self, FnCtxt};
|
16 | 18 | use middle::ty::{self, Ty};
|
| 19 | +use middle::def; |
| 20 | +use metadata::{csearch, cstore, decoder}; |
17 | 21 | use util::ppaux::UserString;
|
18 | 22 |
|
19 |
| -use syntax::ast; |
| 23 | +use syntax::{ast, ast_util}; |
20 | 24 | use syntax::codemap::Span;
|
21 | 25 |
|
| 26 | +use std::cell; |
| 27 | +use std::cmp::Ordering; |
| 28 | + |
22 | 29 | use super::{MethodError, CandidateSource, impl_method, trait_method};
|
23 | 30 |
|
24 | 31 | pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
@@ -67,6 +74,8 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
67 | 74 |
|
68 | 75 | report_candidates(fcx, span, method_name, static_sources);
|
69 | 76 | }
|
| 77 | + |
| 78 | + suggest_traits_to_import(fcx, span, rcvr_ty, method_name) |
70 | 79 | }
|
71 | 80 |
|
72 | 81 | MethodError::Ambiguity(sources) => {
|
@@ -120,3 +129,159 @@ pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
|
120 | 129 | }
|
121 | 130 | }
|
122 | 131 | }
|
| 132 | + |
| 133 | + |
| 134 | +pub type AllTraitsVec = Vec<TraitInfo>; |
| 135 | + |
| 136 | +fn suggest_traits_to_import<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, |
| 137 | + span: Span, |
| 138 | + _rcvr_ty: Ty<'tcx>, |
| 139 | + method_name: ast::Name) |
| 140 | +{ |
| 141 | + let tcx = fcx.tcx(); |
| 142 | + |
| 143 | + let mut candidates = all_traits(fcx.ccx) |
| 144 | + .filter(|info| trait_method(tcx, info.def_id, method_name).is_some()) |
| 145 | + .collect::<Vec<_>>(); |
| 146 | + |
| 147 | + if candidates.len() > 0 { |
| 148 | + // sort from most relevant to least relevant |
| 149 | + candidates.sort_by(|a, b| a.cmp(b).reverse()); |
| 150 | + |
| 151 | + let method_ustring = method_name.user_string(tcx); |
| 152 | + |
| 153 | + span_help!(fcx.sess(), span, |
| 154 | + "methods from traits can only be called if the trait is implemented \ |
| 155 | + and in scope; the following trait{s} define{inv_s} a method `{name}`:", |
| 156 | + s = if candidates.len() == 1 {""} else {"s"}, |
| 157 | + inv_s = if candidates.len() == 1 {"s"} else {""}, |
| 158 | + name = method_ustring); |
| 159 | + |
| 160 | + for (i, trait_info) in candidates.iter().enumerate() { |
| 161 | + // provide a good-as-possible span; the span of |
| 162 | + // the trait if it is local, or the span of the |
| 163 | + // method call itself if not |
| 164 | + let trait_span = fcx.tcx().map.def_id_span(trait_info.def_id, span); |
| 165 | + |
| 166 | + fcx.sess().fileline_help(trait_span, |
| 167 | + &*format!("candidate #{}: `{}`", |
| 168 | + i + 1, |
| 169 | + ty::item_path_str(fcx.tcx(), trait_info.def_id))) |
| 170 | + } |
| 171 | + } |
| 172 | +} |
| 173 | + |
| 174 | +#[derive(Copy)] |
| 175 | +pub struct TraitInfo { |
| 176 | + def_id: ast::DefId, |
| 177 | +} |
| 178 | + |
| 179 | +impl TraitInfo { |
| 180 | + fn new(def_id: ast::DefId) -> TraitInfo { |
| 181 | + TraitInfo { |
| 182 | + def_id: def_id, |
| 183 | + } |
| 184 | + } |
| 185 | +} |
| 186 | +impl PartialEq for TraitInfo { |
| 187 | + fn eq(&self, other: &TraitInfo) -> bool { |
| 188 | + self.cmp(other) == Ordering::Equal |
| 189 | + } |
| 190 | +} |
| 191 | +impl Eq for TraitInfo {} |
| 192 | +impl PartialOrd for TraitInfo { |
| 193 | + fn partial_cmp(&self, other: &TraitInfo) -> Option<Ordering> { Some(self.cmp(other)) } |
| 194 | +} |
| 195 | +impl Ord for TraitInfo { |
| 196 | + fn cmp(&self, other: &TraitInfo) -> Ordering { |
| 197 | + // accessible traits are more important/relevant than |
| 198 | + // inaccessible ones, local crates are more important than |
| 199 | + // remote ones (local: cnum == 0), and NodeIds just for |
| 200 | + // totality. |
| 201 | + |
| 202 | + let lhs = (other.def_id.krate, other.def_id.node); |
| 203 | + let rhs = (self.def_id.krate, self.def_id.node); |
| 204 | + lhs.cmp(&rhs) |
| 205 | + } |
| 206 | +} |
| 207 | + |
| 208 | +/// Retrieve all traits in this crate and any dependent crates. |
| 209 | +fn all_traits<'a>(ccx: &'a CrateCtxt) -> AllTraits<'a> { |
| 210 | + if ccx.all_traits.borrow().is_none() { |
| 211 | + use syntax::visit; |
| 212 | + |
| 213 | + let mut traits = vec![]; |
| 214 | + |
| 215 | + // Crate-local: |
| 216 | + // |
| 217 | + // meh. |
| 218 | + struct Visitor<'a, 'b: 'a, 'tcx: 'a + 'b> { |
| 219 | + traits: &'a mut AllTraitsVec, |
| 220 | + } |
| 221 | + impl<'v,'a, 'b, 'tcx> visit::Visitor<'v> for Visitor<'a, 'b, 'tcx> { |
| 222 | + fn visit_item(&mut self, i: &'v ast::Item) { |
| 223 | + match i.node { |
| 224 | + ast::ItemTrait(..) => { |
| 225 | + self.traits.push(TraitInfo::new(ast_util::local_def(i.id))); |
| 226 | + } |
| 227 | + _ => {} |
| 228 | + } |
| 229 | + visit::walk_item(self, i) |
| 230 | + } |
| 231 | + } |
| 232 | + visit::walk_crate(&mut Visitor { |
| 233 | + traits: &mut traits |
| 234 | + }, ccx.tcx.map.krate()); |
| 235 | + |
| 236 | + // Cross-crate: |
| 237 | + fn handle_external_def(traits: &mut AllTraitsVec, |
| 238 | + ccx: &CrateCtxt, |
| 239 | + cstore: &cstore::CStore, |
| 240 | + dl: decoder::DefLike) { |
| 241 | + match dl { |
| 242 | + decoder::DlDef(def::DefTrait(did)) => { |
| 243 | + traits.push(TraitInfo::new(did)); |
| 244 | + } |
| 245 | + decoder::DlDef(def::DefMod(did)) => { |
| 246 | + csearch::each_child_of_item(cstore, did, |dl, _, _| { |
| 247 | + handle_external_def(traits, ccx, cstore, dl) |
| 248 | + }) |
| 249 | + } |
| 250 | + _ => {} |
| 251 | + } |
| 252 | + } |
| 253 | + let cstore = &ccx.tcx.sess.cstore; |
| 254 | + cstore.iter_crate_data(|&mut: cnum, _| { |
| 255 | + csearch::each_top_level_item_of_crate(cstore, cnum, |dl, _, _| { |
| 256 | + handle_external_def(&mut traits, ccx, cstore, dl) |
| 257 | + }) |
| 258 | + }); |
| 259 | + |
| 260 | + *ccx.all_traits.borrow_mut() = Some(traits); |
| 261 | + } |
| 262 | + |
| 263 | + let borrow = ccx.all_traits.borrow(); |
| 264 | + assert!(borrow.is_some()); |
| 265 | + AllTraits { |
| 266 | + borrow: borrow, |
| 267 | + idx: 0 |
| 268 | + } |
| 269 | +} |
| 270 | + |
| 271 | +struct AllTraits<'a> { |
| 272 | + borrow: cell::Ref<'a Option<AllTraitsVec>>, |
| 273 | + idx: usize |
| 274 | +} |
| 275 | + |
| 276 | +impl<'a> Iterator for AllTraits<'a> { |
| 277 | + type Item = TraitInfo; |
| 278 | + |
| 279 | + fn next(&mut self) -> Option<TraitInfo> { |
| 280 | + let AllTraits { ref borrow, ref mut idx } = *self; |
| 281 | + // ugh. |
| 282 | + borrow.as_ref().unwrap().get(*idx).map(|info| { |
| 283 | + *idx += 1; |
| 284 | + *info |
| 285 | + }) |
| 286 | + } |
| 287 | +} |
0 commit comments