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 /*! Method lookup: the secret sauce of Rust. See `doc.rs`. */
14 use middle::subst::{Subst};
18 use middle::typeck::astconv::AstConv;
19 use middle::typeck::check::{FnCtxt};
20 use middle::typeck::check::{impl_self_ty};
21 use middle::typeck::check::vtable;
22 use middle::typeck::check::vtable::select_new_fcx_obligations;
23 use middle::typeck::infer;
24 use middle::typeck::{MethodCallee};
25 use middle::typeck::{MethodParam, MethodTypeParam};
26 use util::ppaux::{Repr, UserString};
29 use syntax::ast::{DefId};
31 use syntax::codemap::Span;
33 pub use self::MethodError::*;
34 pub use self::CandidateSource::*;
40 pub enum MethodError {
41 // Did not find an applicable method, but we did find various
42 // static methods that may apply.
43 NoMatch(Vec<CandidateSource>),
45 // Multiple methods might apply.
46 Ambiguity(Vec<CandidateSource>),
49 // A pared down enum describing just the places from which a method
50 // candidate can arise. Used for error reporting only.
51 #[deriving(PartialOrd, Ord, PartialEq, Eq)]
52 pub enum CandidateSource {
53 ImplSource(ast::DefId),
54 TraitSource(/* trait id */ ast::DefId),
57 type MethodIndex = uint; // just for doc purposes
59 pub fn exists<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
61 method_name: ast::Name,
63 call_expr_id: ast::NodeId)
67 * Determines whether the type `self_ty` supports a method name `method_name` or not.
70 match probe::probe(fcx, span, method_name, self_ty, call_expr_id) {
72 Err(NoMatch(_)) => false,
73 Err(Ambiguity(_)) => true,
77 pub fn lookup<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
79 method_name: ast::Name,
81 supplied_method_types: Vec<Ty<'tcx>>,
82 call_expr: &ast::Expr,
83 self_expr: &ast::Expr)
84 -> Result<MethodCallee<'tcx>, MethodError>
87 * Performs method lookup. If lookup is successful, it will return the callee
88 * and store an appropriate adjustment for the self-expr. In some cases it may
89 * report an error (e.g., invoking the `drop` method).
93 * Given a method call like `foo.bar::<T1,...Tn>(...)`:
95 * - `fcx`: the surrounding `FnCtxt` (!)
96 * - `span`: the span for the method call
97 * - `method_name`: the name of the method being called (`bar`)
98 * - `self_ty`: the (unadjusted) type of the self expression (`foo`)
99 * - `supplied_method_types`: the explicit method type parameters, if any (`T1..Tn`)
100 * - `self_expr`: the self expression (`foo`)
103 debug!("lookup(method_name={}, self_ty={}, call_expr={}, self_expr={})",
104 method_name.repr(fcx.tcx()),
105 self_ty.repr(fcx.tcx()),
106 call_expr.repr(fcx.tcx()),
107 self_expr.repr(fcx.tcx()));
109 let pick = try!(probe::probe(fcx, span, method_name, self_ty, call_expr.id));
110 Ok(confirm::confirm(fcx, span, self_expr, call_expr, self_ty, pick, supplied_method_types))
113 pub fn lookup_in_trait<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
115 self_expr: Option<&'a ast::Expr>,
119 opt_input_types: Option<Vec<Ty<'tcx>>>)
120 -> Option<MethodCallee<'tcx>>
122 lookup_in_trait_adjusted(fcx, span, self_expr, m_name, trait_def_id,
123 ty::AutoDerefRef { autoderefs: 0, autoref: None },
124 self_ty, opt_input_types)
127 pub fn lookup_in_trait_adjusted<'a, 'tcx>(fcx: &'a FnCtxt<'a, 'tcx>,
129 self_expr: Option<&'a ast::Expr>,
132 autoderefref: ty::AutoDerefRef<'tcx>,
134 opt_input_types: Option<Vec<Ty<'tcx>>>)
135 -> Option<MethodCallee<'tcx>>
138 * `lookup_in_trait_adjusted` is used for overloaded operators. It
139 * does a very narrow slice of what the normal probe/confirm path
140 * does. In particular, it doesn't really do any probing: it
141 * simply constructs an obligation for a particular trait with the
142 * given self-type and checks whether that trait is implemented.
144 * FIXME(#18741) -- It seems likely that we can consolidate some of this
145 * code with the other method-lookup code. In particular,
146 * autoderef on index is basically identical to autoderef with
147 * normal probes, except that the test also looks for built-in
148 * indexing. Also, the second half of this method is basically
149 * the same as confirmation.
152 debug!("lookup_in_trait_adjusted(self_ty={}, self_expr={}, m_name={}, trait_def_id={})",
153 self_ty.repr(fcx.tcx()),
154 self_expr.repr(fcx.tcx()),
155 m_name.repr(fcx.tcx()),
156 trait_def_id.repr(fcx.tcx()));
158 let trait_def = ty::lookup_trait_def(fcx.tcx(), trait_def_id);
160 let expected_number_of_input_types = trait_def.generics.types.len(subst::TypeSpace);
161 let input_types = match opt_input_types {
162 Some(input_types) => {
163 assert_eq!(expected_number_of_input_types, input_types.len());
168 fcx.inh.infcx.next_ty_vars(expected_number_of_input_types)
172 let number_assoc_types = trait_def.generics.types.len(subst::AssocSpace);
173 let assoc_types = fcx.inh.infcx.next_ty_vars(number_assoc_types);
175 assert_eq!(trait_def.generics.types.len(subst::FnSpace), 0);
176 assert!(trait_def.generics.regions.is_empty());
178 // Construct a trait-reference `self_ty : Trait<input_tys>`
179 let substs = subst::Substs::new_trait(input_types, Vec::new(), assoc_types, self_ty);
180 let trait_ref = Rc::new(ty::TraitRef::new(trait_def_id, substs));
182 // Construct an obligation
183 let obligation = traits::Obligation::misc(span, trait_ref.clone());
185 // Now we want to know if this can be matched
186 let mut selcx = traits::SelectionContext::new(fcx.infcx(),
189 if !selcx.evaluate_obligation(&obligation) {
190 debug!("--> Cannot match obligation");
191 return None; // Cannot be matched, no such method resolution is possible.
194 // Trait must have a method named `m_name` and it should not have
195 // type parameters or early-bound regions.
197 let (method_num, method_ty) = trait_method(tcx, trait_def_id, m_name).unwrap();
198 assert_eq!(method_ty.generics.types.len(subst::FnSpace), 0);
199 assert_eq!(method_ty.generics.regions.len(subst::FnSpace), 0);
201 // Substitute the trait parameters into the method type and
202 // instantiate late-bound regions to get the actual method type.
204 // Note that as the method comes from a trait, it can only have
205 // late-bound regions from the fn itself, not the impl.
206 let ref bare_fn_ty = method_ty.fty;
207 let fn_sig = bare_fn_ty.sig.subst(tcx, &trait_ref.substs);
208 let fn_sig = fcx.infcx().replace_late_bound_regions_with_fresh_var(span,
211 let transformed_self_ty = fn_sig.inputs[0];
212 let fty = ty::mk_bare_fn(tcx, ty::BareFnTy {
214 fn_style: bare_fn_ty.fn_style,
215 abi: bare_fn_ty.abi.clone(),
218 debug!("matched method fty={} obligation={}",
220 obligation.repr(fcx.tcx()));
222 // Register obligations for the parameters. This will include the
223 // `Self` parameter, which in turn has a bound of the main trait,
224 // so this also effectively registers `obligation` as well. (We
225 // used to register `obligation` explicitly, but that resulted in
226 // double error messages being reported.)
228 // Note that as the method comes from a trait, it should not have
229 // any late-bound regions appearing in its bounds.
230 let method_bounds = method_ty.generics.to_bounds(fcx.tcx(), &trait_ref.substs);
231 assert!(!method_bounds.has_escaping_regions());
232 fcx.add_obligations_for_parameters(
233 traits::ObligationCause::misc(span),
237 // FIXME(#18653) -- Try to resolve obligations, giving us more
238 // typing information, which can sometimes be needed to avoid
239 // pathological region inference failures.
240 vtable::select_new_fcx_obligations(fcx);
242 // Insert any adjustments needed (always an autoref of some mutability).
247 debug!("inserting adjustment if needed (self-id = {}, \
248 base adjustment = {}, explicit self = {})",
249 self_expr.id, autoderefref, method_ty.explicit_self);
251 match method_ty.explicit_self {
252 ty::ByValueExplicitSelfCategory => {
253 // Trait method is fn(self), no transformation needed.
254 if !autoderefref.is_identity() {
255 fcx.write_adjustment(
258 ty::AdjustDerefRef(autoderefref));
262 ty::ByReferenceExplicitSelfCategory(..) => {
263 // Trait method is fn(&self) or fn(&mut self), need an
264 // autoref. Pull the region etc out of the type of first argument.
265 match transformed_self_ty.sty {
266 ty::ty_rptr(region, ty::mt { mutbl, ty: _ }) => {
267 let ty::AutoDerefRef { autoderefs, autoref } = autoderefref;
268 let autoref = autoref.map(|r| box r);
269 fcx.write_adjustment(
272 ty::AdjustDerefRef(ty::AutoDerefRef {
273 autoderefs: autoderefs,
274 autoref: Some(ty::AutoPtr(region, mutbl, autoref))
279 fcx.tcx().sess.span_bug(
282 "trait method is &self but first arg is: {}",
283 transformed_self_ty.repr(fcx.tcx())).as_slice());
289 fcx.tcx().sess.span_bug(
292 "unexpected explicit self type in operator method: {}",
293 method_ty.explicit_self).as_slice());
299 let callee = MethodCallee {
300 origin: MethodTypeParam(MethodParam{trait_ref: trait_ref.clone(),
301 method_num: method_num}),
303 substs: trait_ref.substs.clone()
306 debug!("callee = {}", callee.repr(fcx.tcx()));
311 pub fn report_error<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
314 method_name: ast::Name,
318 NoMatch(static_sources) => {
320 let method_ustring = method_name.user_string(cx);
322 // True if the type is a struct and contains a field with
323 // the same name as the not-found method
324 let is_field = match rcvr_ty.sty {
326 ty::lookup_struct_fields(cx, did)
328 .any(|f| f.name.user_string(cx) == method_ustring),
332 fcx.type_error_message(
335 format!("type `{}` does not implement any \
336 method in scope named `{}`",
343 // If the method has the name of a field, give a help note
345 cx.sess.span_note(span,
346 format!("use `(s.{0})(...)` if you meant to call the \
347 function stored in the `{0}` field", method_ustring).as_slice());
350 if static_sources.len() > 0 {
351 fcx.tcx().sess.fileline_note(
353 "found defined static methods, maybe a `self` is missing?");
355 report_candidates(fcx, span, method_name, static_sources);
359 Ambiguity(sources) => {
360 span_err!(fcx.sess(), span, E0034,
361 "multiple applicable methods in scope");
363 report_candidates(fcx, span, method_name, sources);
367 fn report_candidates(fcx: &FnCtxt,
369 method_name: ast::Name,
370 mut sources: Vec<CandidateSource>) {
374 for (idx, source) in sources.iter().enumerate() {
376 ImplSource(impl_did) => {
377 // Provide the best span we can. Use the method, if local to crate, else
378 // the impl, if local to crate (method may be defaulted), else the call site.
379 let method = impl_method(fcx.tcx(), impl_did, method_name).unwrap();
380 let impl_span = fcx.tcx().map.def_id_span(impl_did, span);
381 let method_span = fcx.tcx().map.def_id_span(method.def_id, impl_span);
383 let impl_ty = impl_self_ty(fcx, span, impl_did).ty;
385 let insertion = match impl_trait_ref(fcx.tcx(), impl_did) {
387 Some(trait_ref) => format!(" of the trait `{}`",
388 ty::item_path_str(fcx.tcx(),
392 span_note!(fcx.sess(), method_span,
393 "candidate #{} is defined in an impl{} for the type `{}`",
396 impl_ty.user_string(fcx.tcx()));
398 TraitSource(trait_did) => {
399 let (_, method) = trait_method(fcx.tcx(), trait_did, method_name).unwrap();
400 let method_span = fcx.tcx().map.def_id_span(method.def_id, span);
401 span_note!(fcx.sess(), method_span,
402 "candidate #{} is defined in the trait `{}`",
404 ty::item_path_str(fcx.tcx(), trait_did));
411 fn trait_method<'tcx>(tcx: &ty::ctxt<'tcx>,
412 trait_def_id: ast::DefId,
413 method_name: ast::Name)
414 -> Option<(uint, Rc<ty::Method<'tcx>>)>
417 * Find method with name `method_name` defined in `trait_def_id` and return it,
418 * along with its index (or `None`, if no such method).
421 let trait_items = ty::trait_items(tcx, trait_def_id);
425 .find(|&(_, ref item)| item.name() == method_name)
426 .and_then(|(idx, item)| item.as_opt_method().map(|m| (idx, m)))
429 fn impl_method<'tcx>(tcx: &ty::ctxt<'tcx>,
430 impl_def_id: ast::DefId,
431 method_name: ast::Name)
432 -> Option<Rc<ty::Method<'tcx>>>
434 let impl_items = tcx.impl_items.borrow();
435 let impl_items = impl_items.get(&impl_def_id).unwrap();
438 .map(|&did| ty::impl_or_trait_item(tcx, did.def_id()))
439 .find(|m| m.name() == method_name)
440 .and_then(|item| item.as_opt_method())