|
| 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. |
| 4 | +// |
| 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. |
| 10 | + |
| 11 | +//! Give useful errors and suggestions to users when a method can't be |
| 12 | +//! found or is otherwise invalid. |
| 13 | +
|
| 14 | +use astconv::AstConv; |
| 15 | +use check::{self, FnCtxt}; |
| 16 | +use middle::ty::{self, Ty}; |
| 17 | +use util::ppaux::UserString; |
| 18 | + |
| 19 | +use syntax::ast; |
| 20 | +use syntax::codemap::Span; |
| 21 | + |
| 22 | +use super::{MethodError, CandidateSource, impl_method, trait_method}; |
| 23 | + |
| 24 | +pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>, |
| 25 | + span: Span, |
| 26 | + rcvr_ty: Ty<'tcx>, |
| 27 | + method_name: ast::Name, |
| 28 | + error: MethodError) |
| 29 | +{ |
| 30 | + match error { |
| 31 | + MethodError::NoMatch(static_sources) => { |
| 32 | + let cx = fcx.tcx(); |
| 33 | + let method_ustring = method_name.user_string(cx); |
| 34 | + |
| 35 | + // True if the type is a struct and contains a field with |
| 36 | + // the same name as the not-found method |
| 37 | + let is_field = match rcvr_ty.sty { |
| 38 | + ty::ty_struct(did, _) => |
| 39 | + ty::lookup_struct_fields(cx, did) |
| 40 | + .iter() |
| 41 | + .any(|f| f.name.user_string(cx) == method_ustring), |
| 42 | + _ => false |
| 43 | + }; |
| 44 | + |
| 45 | + fcx.type_error_message( |
| 46 | + span, |
| 47 | + |actual| { |
| 48 | + format!("type `{}` does not implement any \ |
| 49 | + method in scope named `{}`", |
| 50 | + actual, |
| 51 | + method_ustring) |
| 52 | + }, |
| 53 | + rcvr_ty, |
| 54 | + None); |
| 55 | + |
| 56 | + // If the method has the name of a field, give a help note |
| 57 | + if is_field { |
| 58 | + cx.sess.span_note(span, |
| 59 | + &format!("use `(s.{0})(...)` if you meant to call the \ |
| 60 | + function stored in the `{0}` field", method_ustring)[]); |
| 61 | + } |
| 62 | + |
| 63 | + if static_sources.len() > 0 { |
| 64 | + fcx.tcx().sess.fileline_note( |
| 65 | + span, |
| 66 | + "found defined static methods, maybe a `self` is missing?"); |
| 67 | + |
| 68 | + report_candidates(fcx, span, method_name, static_sources); |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + MethodError::Ambiguity(sources) => { |
| 73 | + span_err!(fcx.sess(), span, E0034, |
| 74 | + "multiple applicable methods in scope"); |
| 75 | + |
| 76 | + report_candidates(fcx, span, method_name, sources); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + fn report_candidates(fcx: &FnCtxt, |
| 81 | + span: Span, |
| 82 | + method_name: ast::Name, |
| 83 | + mut sources: Vec<CandidateSource>) { |
| 84 | + sources.sort(); |
| 85 | + sources.dedup(); |
| 86 | + |
| 87 | + for (idx, source) in sources.iter().enumerate() { |
| 88 | + match *source { |
| 89 | + CandidateSource::ImplSource(impl_did) => { |
| 90 | + // Provide the best span we can. Use the method, if local to crate, else |
| 91 | + // the impl, if local to crate (method may be defaulted), else the call site. |
| 92 | + let method = impl_method(fcx.tcx(), impl_did, method_name).unwrap(); |
| 93 | + let impl_span = fcx.tcx().map.def_id_span(impl_did, span); |
| 94 | + let method_span = fcx.tcx().map.def_id_span(method.def_id, impl_span); |
| 95 | + |
| 96 | + let impl_ty = check::impl_self_ty(fcx, span, impl_did).ty; |
| 97 | + |
| 98 | + let insertion = match ty::impl_trait_ref(fcx.tcx(), impl_did) { |
| 99 | + None => format!(""), |
| 100 | + Some(trait_ref) => format!(" of the trait `{}`", |
| 101 | + ty::item_path_str(fcx.tcx(), |
| 102 | + trait_ref.def_id)), |
| 103 | + }; |
| 104 | + |
| 105 | + span_note!(fcx.sess(), method_span, |
| 106 | + "candidate #{} is defined in an impl{} for the type `{}`", |
| 107 | + idx + 1u, |
| 108 | + insertion, |
| 109 | + impl_ty.user_string(fcx.tcx())); |
| 110 | + } |
| 111 | + CandidateSource::TraitSource(trait_did) => { |
| 112 | + let (_, method) = trait_method(fcx.tcx(), trait_did, method_name).unwrap(); |
| 113 | + let method_span = fcx.tcx().map.def_id_span(method.def_id, span); |
| 114 | + span_note!(fcx.sess(), method_span, |
| 115 | + "candidate #{} is defined in the trait `{}`", |
| 116 | + idx + 1u, |
| 117 | + ty::item_path_str(fcx.tcx(), trait_did)); |
| 118 | + } |
| 119 | + } |
| 120 | + } |
| 121 | + } |
| 122 | +} |
0 commit comments