]> git.lizzy.rs Git - rust.git/blob - src/librustc_typeck/check/callee.rs
Auto merge of #28585 - ranma42:simpler-panic, r=alexcrichton
[rust.git] / src / librustc_typeck / check / callee.rs
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 use super::autoderef;
12 use super::check_argument_types;
13 use super::check_expr;
14 use super::check_method_argument_types;
15 use super::demand;
16 use super::DeferredCallResolution;
17 use super::err_args;
18 use super::Expectation;
19 use super::expected_types_for_fn_args;
20 use super::FnCtxt;
21 use super::method;
22 use super::structurally_resolved_type;
23 use super::TupleArgumentsFlag;
24 use super::UnresolvedTypeAction;
25 use super::write_call;
26
27 use CrateCtxt;
28 use middle::def_id::{DefId, LOCAL_CRATE};
29 use middle::infer;
30 use middle::ty::{self, LvaluePreference, Ty};
31 use syntax::codemap::Span;
32 use syntax::parse::token;
33 use syntax::ptr::P;
34
35 use rustc_front::hir;
36
37 /// Check that it is legal to call methods of the trait corresponding
38 /// to `trait_id` (this only cares about the trait, not the specific
39 /// method that is called)
40 pub fn check_legal_trait_for_method_call(ccx: &CrateCtxt, span: Span, trait_id: DefId) {
41     let tcx = ccx.tcx;
42     let did = Some(trait_id);
43     let li = &tcx.lang_items;
44
45     if did == li.drop_trait() {
46         span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
47     } else if !tcx.sess.features.borrow().unboxed_closures {
48         // the #[feature(unboxed_closures)] feature isn't
49         // activated so we need to enforce the closure
50         // restrictions.
51
52         let method = if did == li.fn_trait() {
53             "call"
54         } else if did == li.fn_mut_trait() {
55             "call_mut"
56         } else if did == li.fn_once_trait() {
57             "call_once"
58         } else {
59             return // not a closure method, everything is OK.
60         };
61
62         span_err!(tcx.sess, span, E0174,
63                   "explicit use of unboxed closure method `{}` is experimental",
64                   method);
65         fileline_help!(tcx.sess, span,
66                    "add `#![feature(unboxed_closures)]` to the crate attributes to enable");
67     }
68 }
69
70 pub fn check_call<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
71                             call_expr: &'tcx hir::Expr,
72                             callee_expr: &'tcx hir::Expr,
73                             arg_exprs: &'tcx [P<hir::Expr>],
74                             expected: Expectation<'tcx>)
75 {
76     check_expr(fcx, callee_expr);
77     let original_callee_ty = fcx.expr_ty(callee_expr);
78     let (callee_ty, _, result) =
79         autoderef(fcx,
80                   callee_expr.span,
81                   original_callee_ty,
82                   Some(callee_expr),
83                   UnresolvedTypeAction::Error,
84                   LvaluePreference::NoPreference,
85                   |adj_ty, idx| {
86                       try_overloaded_call_step(fcx, call_expr, callee_expr, adj_ty, idx)
87                   });
88
89     match result {
90         None => {
91             // this will report an error since original_callee_ty is not a fn
92             confirm_builtin_call(fcx, call_expr, original_callee_ty, arg_exprs, expected);
93         }
94
95         Some(CallStep::Builtin) => {
96             confirm_builtin_call(fcx, call_expr, callee_ty, arg_exprs, expected);
97         }
98
99         Some(CallStep::DeferredClosure(fn_sig)) => {
100             confirm_deferred_closure_call(fcx, call_expr, arg_exprs, expected, fn_sig);
101         }
102
103         Some(CallStep::Overloaded(method_callee)) => {
104             confirm_overloaded_call(fcx, call_expr, callee_expr,
105                                     arg_exprs, expected, method_callee);
106         }
107     }
108 }
109
110 enum CallStep<'tcx> {
111     Builtin,
112     DeferredClosure(ty::FnSig<'tcx>),
113     Overloaded(ty::MethodCallee<'tcx>)
114 }
115
116 fn try_overloaded_call_step<'a, 'tcx>(fcx: &FnCtxt<'a, 'tcx>,
117                                       call_expr: &'tcx hir::Expr,
118                                       callee_expr: &'tcx hir::Expr,
119                                       adjusted_ty: Ty<'tcx>,
120                                       autoderefs: usize)
121                                       -> Option<CallStep<'tcx>>
122 {
123     debug!("try_overloaded_call_step(call_expr={:?}, adjusted_ty={:?}, autoderefs={})",
124            call_expr,
125            adjusted_ty,
126            autoderefs);
127
128     // If the callee is a bare function or a closure, then we're all set.
129     match structurally_resolved_type(fcx, callee_expr.span, adjusted_ty).sty {
130         ty::TyBareFn(..) => {
131             fcx.write_autoderef_adjustment(callee_expr.id, autoderefs);
132             return Some(CallStep::Builtin);
133         }
134
135         ty::TyClosure(def_id, ref substs) => {
136             assert_eq!(def_id.krate, LOCAL_CRATE);
137
138             // Check whether this is a call to a closure where we
139             // haven't yet decided on whether the closure is fn vs
140             // fnmut vs fnonce. If so, we have to defer further processing.
141             if fcx.infcx().closure_kind(def_id).is_none() {
142                 let closure_ty =
143                     fcx.infcx().closure_type(def_id, substs);
144                 let fn_sig =
145                     fcx.infcx().replace_late_bound_regions_with_fresh_var(call_expr.span,
146                                                                           infer::FnCall,
147                                                                           &closure_ty.sig).0;
148                 fcx.record_deferred_call_resolution(def_id, Box::new(CallResolution {
149                     call_expr: call_expr,
150                     callee_expr: callee_expr,
151                     adjusted_ty: adjusted_ty,
152                     autoderefs: autoderefs,
153                     fn_sig: fn_sig.clone(),
154                     closure_def_id: def_id
155                 }));
156                 return Some(CallStep::DeferredClosure(fn_sig));
157             }
158         }
159
160         // Hack: we know that there are traits implementing Fn for &F
161         // where F:Fn and so forth. In the particular case of types
162         // like `x: &mut FnMut()`, if there is a call `x()`, we would
163         // normally translate to `FnMut::call_mut(&mut x, ())`, but
164         // that winds up requiring `mut x: &mut FnMut()`. A little
165         // over the top. The simplest fix by far is to just ignore
166         // this case and deref again, so we wind up with
167         // `FnMut::call_mut(&mut *x, ())`.
168         ty::TyRef(..) if autoderefs == 0 => {
169             return None;
170         }
171
172         _ => {}
173     }
174
175     try_overloaded_call_traits(fcx, call_expr, callee_expr, adjusted_ty, autoderefs)
176         .map(|method_callee| CallStep::Overloaded(method_callee))
177 }
178
179 fn try_overloaded_call_traits<'a,'tcx>(fcx: &FnCtxt<'a, 'tcx>,
180                                        call_expr: &hir::Expr,
181                                        callee_expr: &hir::Expr,
182                                        adjusted_ty: Ty<'tcx>,
183                                        autoderefs: usize)
184                                        -> Option<ty::MethodCallee<'tcx>>
185 {
186     // Try the options that are least restrictive on the caller first.
187     for &(opt_trait_def_id, method_name) in &[
188         (fcx.tcx().lang_items.fn_trait(), token::intern("call")),
189         (fcx.tcx().lang_items.fn_mut_trait(), token::intern("call_mut")),
190         (fcx.tcx().lang_items.fn_once_trait(), token::intern("call_once")),
191     ] {
192         let trait_def_id = match opt_trait_def_id {
193             Some(def_id) => def_id,
194             None => continue,
195         };
196
197         match method::lookup_in_trait_adjusted(fcx,
198                                                call_expr.span,
199                                                Some(&*callee_expr),
200                                                method_name,
201                                                trait_def_id,
202                                                autoderefs,
203                                                false,
204                                                adjusted_ty,
205                                                None) {
206             None => continue,
207             Some(method_callee) => {
208                 return Some(method_callee);
209             }
210         }
211     }
212
213     None
214 }
215
216 fn confirm_builtin_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
217                                  call_expr: &hir::Expr,
218                                  callee_ty: Ty<'tcx>,
219                                  arg_exprs: &'tcx [P<hir::Expr>],
220                                  expected: Expectation<'tcx>)
221 {
222     let error_fn_sig;
223
224     let fn_sig = match callee_ty.sty {
225         ty::TyBareFn(_, &ty::BareFnTy {ref sig, ..}) => {
226             sig
227         }
228         _ => {
229             fcx.type_error_message(call_expr.span, |actual| {
230                 format!("expected function, found `{}`", actual)
231             }, callee_ty, None);
232
233             // This is the "default" function signature, used in case of error.
234             // In that case, we check each argument against "error" in order to
235             // set up all the node type bindings.
236             error_fn_sig = ty::Binder(ty::FnSig {
237                 inputs: err_args(fcx.tcx(), arg_exprs.len()),
238                 output: ty::FnConverging(fcx.tcx().types.err),
239                 variadic: false
240             });
241
242             &error_fn_sig
243         }
244     };
245
246     // Replace any late-bound regions that appear in the function
247     // signature with region variables. We also have to
248     // renormalize the associated types at this point, since they
249     // previously appeared within a `Binder<>` and hence would not
250     // have been normalized before.
251     let fn_sig =
252         fcx.infcx().replace_late_bound_regions_with_fresh_var(call_expr.span,
253                                                               infer::FnCall,
254                                                               fn_sig).0;
255     let fn_sig =
256         fcx.normalize_associated_types_in(call_expr.span, &fn_sig);
257
258     // Call the generic checker.
259     let expected_arg_tys = expected_types_for_fn_args(fcx,
260                                                       call_expr.span,
261                                                       expected,
262                                                       fn_sig.output,
263                                                       &fn_sig.inputs);
264     check_argument_types(fcx,
265                          call_expr.span,
266                          &fn_sig.inputs,
267                          &expected_arg_tys[..],
268                          arg_exprs,
269                          fn_sig.variadic,
270                          TupleArgumentsFlag::DontTupleArguments);
271
272     write_call(fcx, call_expr, fn_sig.output);
273 }
274
275 fn confirm_deferred_closure_call<'a,'tcx>(fcx: &FnCtxt<'a,'tcx>,
276                                           call_expr: &hir::Expr,
277                                           arg_exprs: &'tcx [P<hir::Expr>],
278                                           expected: Expectation<'tcx>,
279                                           fn_sig: ty::FnSig<'tcx>)
280 {
281     // `fn_sig` is the *signature* of the cosure being called. We
282     // don't know the full details yet (`Fn` vs `FnMut` etc), but we
283     // do know the types expected for each argument and the return
284     // type.
285
286     let expected_arg_tys =
287         expected_types_for_fn_args(fcx,
288                                    call_expr.span,
289                                    expected,
290                                    fn_sig.output.clone(),
291                                    &*fn_sig.inputs);
292
293     check_argument_types(fcx,
294                          call_expr.span,
295                          &*fn_sig.inputs,
296                          &*expected_arg_tys,
297                          arg_exprs,
298                          fn_sig.variadic,
299                          TupleArgumentsFlag::TupleArguments);
300
301     write_call(fcx, call_expr, fn_sig.output);
302 }
303
304 fn confirm_overloaded_call<'a,'tcx>(fcx: &FnCtxt<'a, 'tcx>,
305                                     call_expr: &hir::Expr,
306                                     callee_expr: &'tcx hir::Expr,
307                                     arg_exprs: &'tcx [P<hir::Expr>],
308                                     expected: Expectation<'tcx>,
309                                     method_callee: ty::MethodCallee<'tcx>)
310 {
311     let output_type =
312         check_method_argument_types(fcx,
313                                     call_expr.span,
314                                     method_callee.ty,
315                                     callee_expr,
316                                     arg_exprs,
317                                     TupleArgumentsFlag::TupleArguments,
318                                     expected);
319     write_call(fcx, call_expr, output_type);
320
321     write_overloaded_call_method_map(fcx, call_expr, method_callee);
322 }
323
324 fn write_overloaded_call_method_map<'a,'tcx>(fcx: &FnCtxt<'a, 'tcx>,
325                                              call_expr: &hir::Expr,
326                                              method_callee: ty::MethodCallee<'tcx>) {
327     let method_call = ty::MethodCall::expr(call_expr.id);
328     fcx.inh.tables.borrow_mut().method_map.insert(method_call, method_callee);
329 }
330
331 #[derive(Debug)]
332 struct CallResolution<'tcx> {
333     call_expr: &'tcx hir::Expr,
334     callee_expr: &'tcx hir::Expr,
335     adjusted_ty: Ty<'tcx>,
336     autoderefs: usize,
337     fn_sig: ty::FnSig<'tcx>,
338     closure_def_id: DefId,
339 }
340
341 impl<'tcx> DeferredCallResolution<'tcx> for CallResolution<'tcx> {
342     fn resolve<'a>(&mut self, fcx: &FnCtxt<'a,'tcx>) {
343         debug!("DeferredCallResolution::resolve() {:?}",
344                self);
345
346         // we should not be invoked until the closure kind has been
347         // determined by upvar inference
348         assert!(fcx.infcx().closure_kind(self.closure_def_id).is_some());
349
350         // We may now know enough to figure out fn vs fnmut etc.
351         match try_overloaded_call_traits(fcx, self.call_expr, self.callee_expr,
352                                          self.adjusted_ty, self.autoderefs) {
353             Some(method_callee) => {
354                 // One problem is that when we get here, we are going
355                 // to have a newly instantiated function signature
356                 // from the call trait. This has to be reconciled with
357                 // the older function signature we had before. In
358                 // principle we *should* be able to fn_sigs(), but we
359                 // can't because of the annoying need for a TypeTrace.
360                 // (This always bites me, should find a way to
361                 // refactor it.)
362                 let method_sig = fcx.tcx().no_late_bound_regions(method_callee.ty.fn_sig())
363                                           .unwrap();
364
365                 debug!("attempt_resolution: method_callee={:?}",
366                        method_callee);
367
368                 for (&method_arg_ty, &self_arg_ty) in
369                     method_sig.inputs[1..].iter().zip(&self.fn_sig.inputs)
370                 {
371                     demand::eqtype(fcx, self.call_expr.span, self_arg_ty, method_arg_ty);
372                 }
373
374                 let nilty = fcx.tcx().mk_nil();
375                 demand::eqtype(fcx,
376                                self.call_expr.span,
377                                method_sig.output.unwrap_or(nilty),
378                                self.fn_sig.output.unwrap_or(nilty));
379
380                 write_overloaded_call_method_map(fcx, self.call_expr, method_callee);
381             }
382             None => {
383                 fcx.tcx().sess.span_bug(
384                     self.call_expr.span,
385                     "failed to find an overloaded call trait for closure call");
386             }
387         }
388     }
389 }