2017-05-16 23:54:04 +02:00
|
|
|
use super::autoderef::Autoderef;
|
2017-05-20 19:03:04 +02:00
|
|
|
use super::method::MethodCallee;
|
2018-11-04 16:14:44 +01:00
|
|
|
use super::{Expectation, FnCtxt, Needs, TupleArgumentsFlag};
|
2019-12-31 21:25:16 +01:00
|
|
|
use crate::type_error_struct;
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2019-12-31 21:25:16 +01:00
|
|
|
use errors::{struct_span_err, Applicability, DiagnosticBuilder};
|
2019-04-20 18:36:05 +02:00
|
|
|
use hir::def::Res;
|
2016-08-31 13:00:29 +02:00
|
|
|
use hir::def_id::{DefId, LOCAL_CRATE};
|
2019-12-22 23:42:04 +01:00
|
|
|
use rustc::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind};
|
2018-11-04 16:14:44 +01:00
|
|
|
use rustc::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability};
|
2019-09-26 19:30:44 +02:00
|
|
|
use rustc::ty::subst::SubstsRef;
|
2019-12-22 23:42:04 +01:00
|
|
|
use rustc::ty::{self, Ty, TyCtxt, TypeFoldable};
|
2016-08-18 17:52:05 +02:00
|
|
|
use rustc::{infer, traits};
|
2019-12-31 18:15:40 +01:00
|
|
|
use rustc_span::Span;
|
2018-04-25 18:30:39 +02:00
|
|
|
use rustc_target::spec::abi;
|
2018-06-10 21:24:24 +02:00
|
|
|
use syntax::ast::Ident;
|
2014-12-06 00:53:30 +01:00
|
|
|
|
2020-01-05 02:37:57 +01:00
|
|
|
use rustc_hir as hir;
|
2015-07-31 09:04:06 +02:00
|
|
|
|
2019-11-11 22:46:56 +01:00
|
|
|
use rustc_error_codes::*;
|
|
|
|
|
2019-02-08 14:53:55 +01:00
|
|
|
/// Checks that it is legal to call methods of the trait corresponding
|
2014-12-06 00:53:30 +01:00
|
|
|
/// to `trait_id` (this only cares about the trait, not the specific
|
2019-02-08 14:53:55 +01:00
|
|
|
/// method that is called).
|
2019-06-13 23:48:52 +02:00
|
|
|
pub fn check_legal_trait_for_method_call(tcx: TyCtxt<'_>, span: Span, trait_id: DefId) {
|
2017-08-31 17:57:41 +02:00
|
|
|
if tcx.lang_items().drop_trait() == Some(trait_id) {
|
2016-10-04 01:19:40 +02:00
|
|
|
struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method")
|
2017-05-04 14:17:23 +02:00
|
|
|
.span_label(span, "explicit destructor calls not allowed")
|
2016-08-05 05:46:59 +02:00
|
|
|
.emit();
|
2014-12-06 00:53:30 +01:00
|
|
|
}
|
|
|
|
}
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2016-03-12 02:06:24 +01:00
|
|
|
enum CallStep<'tcx> {
|
2017-05-16 23:54:04 +02:00
|
|
|
Builtin(Ty<'tcx>),
|
2016-03-12 02:06:24 +01:00
|
|
|
DeferredClosure(ty::FnSig<'tcx>),
|
2019-02-08 14:53:55 +01:00
|
|
|
/// E.g., enum variant constructors.
|
2017-05-20 19:03:04 +02:00
|
|
|
Overloaded(MethodCallee<'tcx>),
|
2016-03-12 02:06:24 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:48:52 +02:00
|
|
|
impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
|
2018-11-04 16:14:44 +01:00
|
|
|
pub fn check_call(
|
|
|
|
&self,
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
callee_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
arg_exprs: &'tcx [hir::Expr<'tcx>],
|
2018-11-04 16:14:44 +01:00
|
|
|
expected: Expectation<'tcx>,
|
|
|
|
) -> Ty<'tcx> {
|
2016-08-09 18:13:20 +02:00
|
|
|
let original_callee_ty = self.check_expr(callee_expr);
|
2016-09-29 06:07:03 +02:00
|
|
|
let expr_ty = self.structurally_resolved_type(call_expr.span, original_callee_ty);
|
2016-05-24 13:37:11 +02:00
|
|
|
|
2016-09-29 06:07:03 +02:00
|
|
|
let mut autoderef = self.autoderef(callee_expr.span, expr_ty);
|
2017-05-16 23:54:04 +02:00
|
|
|
let mut result = None;
|
|
|
|
while result.is_none() && autoderef.next().is_some() {
|
2018-11-04 11:23:45 +01:00
|
|
|
result = self.try_overloaded_call_step(call_expr, callee_expr, arg_exprs, &autoderef);
|
2017-05-16 23:54:04 +02:00
|
|
|
}
|
2018-09-15 17:10:10 +02:00
|
|
|
autoderef.finalize(self);
|
2016-05-11 07:48:12 +02:00
|
|
|
|
2016-08-18 17:52:05 +02:00
|
|
|
let output = match result {
|
2016-05-11 07:48:12 +02:00
|
|
|
None => {
|
|
|
|
// this will report an error since original_callee_ty is not a fn
|
2016-08-09 14:57:41 +02:00
|
|
|
self.confirm_builtin_call(call_expr, original_callee_ty, arg_exprs, expected)
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2017-05-16 23:54:04 +02:00
|
|
|
Some(CallStep::Builtin(callee_ty)) => {
|
2016-08-09 14:57:41 +02:00
|
|
|
self.confirm_builtin_call(call_expr, callee_ty, arg_exprs, expected)
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2016-05-11 07:48:12 +02:00
|
|
|
Some(CallStep::DeferredClosure(fn_sig)) => {
|
2016-08-09 14:57:41 +02:00
|
|
|
self.confirm_deferred_closure_call(call_expr, arg_exprs, expected, fn_sig)
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2016-05-11 07:48:12 +02:00
|
|
|
Some(CallStep::Overloaded(method_callee)) => {
|
2017-05-17 21:51:18 +02:00
|
|
|
self.confirm_overloaded_call(call_expr, arg_exprs, expected, method_callee)
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2016-08-18 17:52:05 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
// we must check that return type of called functions is WF:
|
|
|
|
self.register_wf_obligation(output, call_expr.span, traits::MiscObligation);
|
|
|
|
|
|
|
|
output
|
2015-01-02 21:30:26 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
fn try_overloaded_call_step(
|
|
|
|
&self,
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
callee_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
arg_exprs: &'tcx [hir::Expr<'tcx>],
|
2019-06-13 23:48:52 +02:00
|
|
|
autoderef: &Autoderef<'a, 'tcx>,
|
2018-11-04 16:14:44 +01:00
|
|
|
) -> Option<CallStep<'tcx>> {
|
2018-09-15 17:10:10 +02:00
|
|
|
let adjusted_ty = autoderef.unambiguous_final_ty(self);
|
2018-11-04 16:14:44 +01:00
|
|
|
debug!(
|
|
|
|
"try_overloaded_call_step(call_expr={:?}, adjusted_ty={:?})",
|
|
|
|
call_expr, adjusted_ty
|
|
|
|
);
|
2016-05-11 07:48:12 +02:00
|
|
|
|
|
|
|
// If the callee is a bare function or a closure, then we're all set.
|
2019-09-16 20:08:35 +02:00
|
|
|
match adjusted_ty.kind {
|
2018-08-22 02:35:02 +02:00
|
|
|
ty::FnDef(..) | ty::FnPtr(_) => {
|
2018-09-15 17:10:10 +02:00
|
|
|
let adjustments = autoderef.adjust_steps(self, Needs::None);
|
2017-05-27 09:29:24 +02:00
|
|
|
self.apply_adjustments(callee_expr, adjustments);
|
2017-05-16 23:54:04 +02:00
|
|
|
return Some(CallStep::Builtin(adjusted_ty));
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2018-08-22 02:35:02 +02:00
|
|
|
ty::Closure(def_id, substs) => {
|
2016-05-11 07:48:12 +02:00
|
|
|
assert_eq!(def_id.krate, LOCAL_CRATE);
|
|
|
|
|
|
|
|
// Check whether this is a call to a closure where we
|
|
|
|
// haven't yet decided on whether the closure is fn vs
|
|
|
|
// fnmut vs fnonce. If so, we have to defer further processing.
|
2017-11-08 15:45:48 +01:00
|
|
|
if self.closure_kind(def_id, substs).is_none() {
|
2017-12-01 14:51:01 +01:00
|
|
|
let closure_ty = self.closure_sig(def_id, substs);
|
2018-11-04 16:14:44 +01:00
|
|
|
let fn_sig = self
|
|
|
|
.replace_bound_vars_with_fresh_vars(
|
|
|
|
call_expr.span,
|
|
|
|
infer::FnCall,
|
|
|
|
&closure_ty,
|
|
|
|
)
|
|
|
|
.0;
|
2018-09-15 17:10:10 +02:00
|
|
|
let adjustments = autoderef.adjust_steps(self, Needs::None);
|
2018-11-04 16:14:44 +01:00
|
|
|
self.record_deferred_call_resolution(
|
|
|
|
def_id,
|
|
|
|
DeferredCallResolution {
|
|
|
|
call_expr,
|
|
|
|
callee_expr,
|
|
|
|
adjusted_ty,
|
|
|
|
adjustments,
|
|
|
|
fn_sig,
|
|
|
|
closure_def_id: def_id,
|
|
|
|
closure_substs: substs,
|
|
|
|
},
|
|
|
|
);
|
2016-05-11 07:48:12 +02:00
|
|
|
return Some(CallStep::DeferredClosure(fn_sig));
|
|
|
|
}
|
2015-01-31 12:29:04 +01:00
|
|
|
}
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2016-05-11 07:48:12 +02:00
|
|
|
// Hack: we know that there are traits implementing Fn for &F
|
|
|
|
// where F:Fn and so forth. In the particular case of types
|
|
|
|
// like `x: &mut FnMut()`, if there is a call `x()`, we would
|
|
|
|
// normally translate to `FnMut::call_mut(&mut x, ())`, but
|
|
|
|
// that winds up requiring `mut x: &mut FnMut()`. A little
|
|
|
|
// over the top. The simplest fix by far is to just ignore
|
|
|
|
// this case and deref again, so we wind up with
|
|
|
|
// `FnMut::call_mut(&mut *x, ())`.
|
2018-08-22 02:35:02 +02:00
|
|
|
ty::Ref(..) if autoderef.step_count() == 0 => {
|
2016-05-11 07:48:12 +02:00
|
|
|
return None;
|
|
|
|
}
|
|
|
|
|
|
|
|
_ => {}
|
2015-03-31 10:38:43 +02:00
|
|
|
}
|
|
|
|
|
2018-11-21 10:10:35 +01:00
|
|
|
// Now, we look for the implementation of a Fn trait on the object's type.
|
|
|
|
// We first do it with the explicit instruction to look for an impl of
|
|
|
|
// `Fn<Tuple>`, with the tuple `Tuple` having an arity corresponding
|
|
|
|
// to the number of call parameters.
|
|
|
|
// If that fails (or_else branch), we try again without specifying the
|
|
|
|
// shape of the tuple (hence the None). This allows to detect an Fn trait
|
|
|
|
// is implemented, and use this information for diagnostic.
|
2018-11-04 11:23:45 +01:00
|
|
|
self.try_overloaded_call_traits(call_expr, adjusted_ty, Some(arg_exprs))
|
2018-11-21 10:10:16 +01:00
|
|
|
.or_else(|| self.try_overloaded_call_traits(call_expr, adjusted_ty, None))
|
2018-11-04 16:14:44 +01:00
|
|
|
.map(|(autoref, method)| {
|
|
|
|
let mut adjustments = autoderef.adjust_steps(self, Needs::None);
|
|
|
|
adjustments.extend(autoref);
|
|
|
|
self.apply_adjustments(callee_expr, adjustments);
|
|
|
|
CallStep::Overloaded(method)
|
|
|
|
})
|
2015-01-02 21:30:26 +01:00
|
|
|
}
|
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
fn try_overloaded_call_traits(
|
|
|
|
&self,
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &hir::Expr<'_>,
|
2018-11-04 16:14:44 +01:00
|
|
|
adjusted_ty: Ty<'tcx>,
|
2019-11-30 15:08:22 +01:00
|
|
|
opt_arg_exprs: Option<&'tcx [hir::Expr<'tcx>]>,
|
2018-11-04 16:14:44 +01:00
|
|
|
) -> Option<(Option<Adjustment<'tcx>>, MethodCallee<'tcx>)> {
|
2016-05-11 07:48:12 +02:00
|
|
|
// Try the options that are least restrictive on the caller first.
|
2018-11-04 16:14:44 +01:00
|
|
|
for &(opt_trait_def_id, method_name, borrow) in &[
|
2019-12-22 23:42:04 +01:00
|
|
|
(self.tcx.lang_items().fn_trait(), Ident::from_str("call"), true),
|
|
|
|
(self.tcx.lang_items().fn_mut_trait(), Ident::from_str("call_mut"), true),
|
|
|
|
(self.tcx.lang_items().fn_once_trait(), Ident::from_str("call_once"), false),
|
2018-11-04 16:14:44 +01:00
|
|
|
] {
|
2016-05-11 07:48:12 +02:00
|
|
|
let trait_def_id = match opt_trait_def_id {
|
|
|
|
Some(def_id) => def_id,
|
|
|
|
None => continue,
|
|
|
|
};
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
let opt_input_types = opt_arg_exprs.map(|arg_exprs| {
|
|
|
|
[self.tcx.mk_tup(arg_exprs.iter().map(|e| {
|
2019-03-21 01:00:24 +01:00
|
|
|
self.next_ty_var(TypeVariableOrigin {
|
|
|
|
kind: TypeVariableOriginKind::TypeInference,
|
|
|
|
span: e.span,
|
|
|
|
})
|
2019-12-22 23:42:04 +01:00
|
|
|
}))]
|
|
|
|
});
|
2018-11-04 11:23:45 +01:00
|
|
|
let opt_input_types = opt_input_types.as_ref().map(AsRef::as_ref);
|
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
if let Some(ok) = self.lookup_method_in_trait(
|
|
|
|
call_expr.span,
|
|
|
|
method_name,
|
|
|
|
trait_def_id,
|
|
|
|
adjusted_ty,
|
2018-11-04 11:23:45 +01:00
|
|
|
opt_input_types,
|
2018-11-04 16:14:44 +01:00
|
|
|
) {
|
2018-09-19 13:51:51 +02:00
|
|
|
let method = self.register_infer_ok_obligations(ok);
|
|
|
|
let mut autoref = None;
|
|
|
|
if borrow {
|
2019-09-16 20:08:35 +02:00
|
|
|
if let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind {
|
2018-09-19 13:51:51 +02:00
|
|
|
let mutbl = match mutbl {
|
2019-12-16 17:28:40 +01:00
|
|
|
hir::Mutability::Not => AutoBorrowMutability::Not,
|
|
|
|
hir::Mutability::Mut => AutoBorrowMutability::Mut {
|
2018-09-19 13:51:51 +02:00
|
|
|
// For initial two-phase borrow
|
|
|
|
// deployment, conservatively omit
|
|
|
|
// overloaded function call ops.
|
|
|
|
allow_two_phase_borrow: AllowTwoPhase::No,
|
2018-11-04 16:14:44 +01:00
|
|
|
},
|
2018-09-19 13:51:51 +02:00
|
|
|
};
|
|
|
|
autoref = Some(Adjustment {
|
|
|
|
kind: Adjust::Borrow(AutoBorrow::Ref(region, mutbl)),
|
2018-11-04 16:14:44 +01:00
|
|
|
target: method.sig.inputs()[0],
|
2018-09-19 13:51:51 +02:00
|
|
|
});
|
2017-05-27 09:29:24 +02:00
|
|
|
}
|
|
|
|
}
|
2018-09-19 13:51:51 +02:00
|
|
|
return Some((autoref, method));
|
2015-01-02 21:30:26 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-05-11 07:48:12 +02:00
|
|
|
None
|
|
|
|
}
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2019-03-09 04:08:28 +01:00
|
|
|
/// Give appropriate suggestion when encountering `||{/* not callable */}()`, where the
|
|
|
|
/// likely intention is to call the closure, suggest `(||{})()`. (#55851)
|
|
|
|
fn identify_bad_closure_def_and_call(
|
|
|
|
&self,
|
|
|
|
err: &mut DiagnosticBuilder<'a>,
|
|
|
|
hir_id: hir::HirId,
|
2019-11-30 15:08:22 +01:00
|
|
|
callee_node: &hir::ExprKind<'_>,
|
2019-03-09 04:08:28 +01:00
|
|
|
callee_span: Span,
|
|
|
|
) {
|
2019-06-24 09:46:38 +02:00
|
|
|
let hir_id = self.tcx.hir().get_parent_node(hir_id);
|
2019-06-20 10:39:19 +02:00
|
|
|
let parent_node = self.tcx.hir().get(hir_id);
|
2019-03-09 04:08:28 +01:00
|
|
|
if let (
|
2019-09-26 15:39:48 +02:00
|
|
|
hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Closure(_, _, _, sp, ..), .. }),
|
2019-03-09 04:08:28 +01:00
|
|
|
hir::ExprKind::Block(..),
|
2019-12-22 23:42:04 +01:00
|
|
|
) = (parent_node, callee_node)
|
|
|
|
{
|
2019-03-09 04:08:28 +01:00
|
|
|
let start = sp.shrink_to_lo();
|
|
|
|
let end = self.tcx.sess.source_map().next_point(callee_span);
|
|
|
|
err.multipart_suggestion(
|
|
|
|
"if you meant to create this closure and immediately call it, surround the \
|
|
|
|
closure with parenthesis",
|
|
|
|
vec![(start, "(".to_string()), (end, ")".to_string())],
|
|
|
|
Applicability::MaybeIncorrect,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
fn confirm_builtin_call(
|
|
|
|
&self,
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &'tcx hir::Expr<'tcx>,
|
2018-11-04 16:14:44 +01:00
|
|
|
callee_ty: Ty<'tcx>,
|
2019-11-30 15:08:22 +01:00
|
|
|
arg_exprs: &'tcx [hir::Expr<'tcx>],
|
2018-11-04 16:14:44 +01:00
|
|
|
expected: Expectation<'tcx>,
|
|
|
|
) -> Ty<'tcx> {
|
2019-09-16 20:08:35 +02:00
|
|
|
let (fn_sig, def_span) = match callee_ty.kind {
|
2019-12-22 23:42:04 +01:00
|
|
|
ty::FnDef(def_id, _) => {
|
|
|
|
(callee_ty.fn_sig(self.tcx), self.tcx.hir().span_if_local(def_id))
|
|
|
|
}
|
2018-08-22 02:35:02 +02:00
|
|
|
ty::FnPtr(sig) => (sig, None),
|
2016-09-13 07:11:05 +02:00
|
|
|
ref t => {
|
|
|
|
let mut unit_variant = None;
|
2018-08-22 02:35:02 +02:00
|
|
|
if let &ty::Adt(adt_def, ..) = t {
|
2016-09-13 07:11:05 +02:00
|
|
|
if adt_def.is_enum() {
|
2019-09-26 15:39:48 +02:00
|
|
|
if let hir::ExprKind::Call(ref expr, _) = call_expr.kind {
|
2019-02-24 09:33:17 +01:00
|
|
|
unit_variant = Some(self.tcx.hir().hir_to_pretty_string(expr.hir_id))
|
2016-09-13 07:11:05 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-01-15 14:18:06 +01:00
|
|
|
|
2019-09-26 15:39:48 +02:00
|
|
|
if let hir::ExprKind::Call(ref callee, _) = call_expr.kind {
|
2018-11-11 05:46:05 +01:00
|
|
|
let mut err = type_error_struct!(
|
|
|
|
self.tcx.sess,
|
|
|
|
callee.span,
|
|
|
|
callee_ty,
|
|
|
|
E0618,
|
|
|
|
"expected function, found {}",
|
|
|
|
match unit_variant {
|
|
|
|
Some(ref path) => format!("enum variant `{}`", path),
|
|
|
|
None => format!("`{}`", callee_ty),
|
2018-11-04 16:14:44 +01:00
|
|
|
}
|
|
|
|
);
|
2018-01-15 14:18:06 +01:00
|
|
|
|
2019-03-09 04:08:28 +01:00
|
|
|
self.identify_bad_closure_def_and_call(
|
|
|
|
&mut err,
|
|
|
|
call_expr.hir_id,
|
2019-09-26 15:39:48 +02:00
|
|
|
&callee.kind,
|
2019-03-09 04:08:28 +01:00
|
|
|
callee.span,
|
|
|
|
);
|
|
|
|
|
2018-11-11 05:46:05 +01:00
|
|
|
if let Some(ref path) = unit_variant {
|
2019-01-25 22:03:27 +01:00
|
|
|
err.span_suggestion(
|
2018-11-11 05:46:05 +01:00
|
|
|
call_expr.span,
|
2018-11-04 16:14:44 +01:00
|
|
|
&format!(
|
|
|
|
"`{}` is a unit variant, you need to write it \
|
|
|
|
without the parenthesis",
|
|
|
|
path
|
|
|
|
),
|
2018-11-11 05:46:05 +01:00
|
|
|
path.to_string(),
|
2018-11-04 16:14:44 +01:00
|
|
|
Applicability::MachineApplicable,
|
2018-11-11 05:46:05 +01:00
|
|
|
);
|
|
|
|
}
|
2016-05-11 07:48:12 +02:00
|
|
|
|
2018-11-11 05:46:05 +01:00
|
|
|
let mut inner_callee_path = None;
|
2019-09-26 15:39:48 +02:00
|
|
|
let def = match callee.kind {
|
2018-11-11 05:46:05 +01:00
|
|
|
hir::ExprKind::Path(ref qpath) => {
|
2019-04-20 18:36:05 +02:00
|
|
|
self.tables.borrow().qpath_res(qpath, callee.hir_id)
|
2018-11-04 16:14:44 +01:00
|
|
|
}
|
2018-11-11 05:46:05 +01:00
|
|
|
hir::ExprKind::Call(ref inner_callee, _) => {
|
|
|
|
// If the call spans more than one line and the callee kind is
|
|
|
|
// itself another `ExprCall`, that's a clue that we might just be
|
|
|
|
// missing a semicolon (Issue #51055)
|
2018-11-04 16:14:44 +01:00
|
|
|
let call_is_multiline =
|
|
|
|
self.tcx.sess.source_map().is_multiline(call_expr.span);
|
2018-11-11 05:46:05 +01:00
|
|
|
if call_is_multiline {
|
|
|
|
let span = self.tcx.sess.source_map().next_point(callee.span);
|
2019-01-25 22:03:27 +01:00
|
|
|
err.span_suggestion(
|
2018-11-11 05:46:05 +01:00
|
|
|
span,
|
|
|
|
"try adding a semicolon",
|
|
|
|
";".to_owned(),
|
2018-11-04 16:14:44 +01:00
|
|
|
Applicability::MaybeIncorrect,
|
2018-11-11 05:46:05 +01:00
|
|
|
);
|
|
|
|
}
|
2019-09-26 15:39:48 +02:00
|
|
|
if let hir::ExprKind::Path(ref inner_qpath) = inner_callee.kind {
|
2018-11-11 05:46:05 +01:00
|
|
|
inner_callee_path = Some(inner_qpath);
|
2019-12-22 23:42:04 +01:00
|
|
|
self.tables.borrow().qpath_res(inner_qpath, inner_callee.hir_id)
|
2018-11-11 05:46:05 +01:00
|
|
|
} else {
|
2019-04-20 18:36:05 +02:00
|
|
|
Res::Err
|
2018-11-11 05:46:05 +01:00
|
|
|
}
|
|
|
|
}
|
2019-04-20 18:36:05 +02:00
|
|
|
_ => Res::Err,
|
2016-11-25 12:21:19 +01:00
|
|
|
};
|
2018-11-11 05:46:05 +01:00
|
|
|
|
|
|
|
err.span_label(call_expr.span, "call expression requires function");
|
|
|
|
|
2019-09-28 02:30:48 +02:00
|
|
|
if let Some(span) = self.tcx.hir().res_span(def) {
|
2018-11-11 05:46:05 +01:00
|
|
|
let label = match (unit_variant, inner_callee_path) {
|
|
|
|
(Some(path), _) => format!("`{}` defined here", path),
|
|
|
|
(_, Some(hir::QPath::Resolved(_, path))) => format!(
|
2018-11-04 16:14:44 +01:00
|
|
|
"`{}` defined here returns `{}`",
|
|
|
|
path,
|
|
|
|
callee_ty.to_string()
|
2018-11-11 05:46:05 +01:00
|
|
|
),
|
|
|
|
_ => format!("`{}` defined here", callee_ty.to_string()),
|
2018-01-15 14:18:06 +01:00
|
|
|
};
|
2018-11-11 05:46:05 +01:00
|
|
|
err.span_label(span, label);
|
2015-11-23 15:48:55 +01:00
|
|
|
}
|
2018-11-11 05:46:05 +01:00
|
|
|
err.emit();
|
|
|
|
} else {
|
2019-12-22 23:42:04 +01:00
|
|
|
bug!("call_expr.kind should be an ExprKind::Call, got {:?}", call_expr.kind);
|
2015-11-23 15:48:55 +01:00
|
|
|
}
|
|
|
|
|
2016-05-11 07:48:12 +02:00
|
|
|
// This is the "default" function signature, used in case of error.
|
|
|
|
// In that case, we check each argument against "error" in order to
|
|
|
|
// set up all the node type bindings.
|
2018-11-04 16:14:44 +01:00
|
|
|
(
|
|
|
|
ty::Binder::bind(self.tcx.mk_fn_sig(
|
|
|
|
self.err_args(arg_exprs.len()).into_iter(),
|
|
|
|
self.tcx.types.err,
|
|
|
|
false,
|
|
|
|
hir::Unsafety::Normal,
|
|
|
|
abi::Abi::Rust,
|
|
|
|
)),
|
|
|
|
None,
|
|
|
|
)
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
|
|
|
};
|
2015-01-02 21:30:26 +01:00
|
|
|
|
2016-05-11 07:48:12 +02:00
|
|
|
// Replace any late-bound regions that appear in the function
|
|
|
|
// signature with region variables. We also have to
|
|
|
|
// renormalize the associated types at this point, since they
|
|
|
|
// previously appeared within a `Binder<>` and hence would not
|
|
|
|
// have been normalized before.
|
2019-12-22 23:42:04 +01:00
|
|
|
let fn_sig =
|
|
|
|
self.replace_bound_vars_with_fresh_vars(call_expr.span, infer::FnCall, &fn_sig).0;
|
2016-09-26 05:41:27 +02:00
|
|
|
let fn_sig = self.normalize_associated_types_in(call_expr.span, &fn_sig);
|
2016-05-11 07:48:12 +02:00
|
|
|
|
|
|
|
// Call the generic checker.
|
2018-11-04 16:14:44 +01:00
|
|
|
let expected_arg_tys = self.expected_inputs_for_expected_output(
|
|
|
|
call_expr.span,
|
|
|
|
expected,
|
|
|
|
fn_sig.output(),
|
2019-08-10 13:38:17 +02:00
|
|
|
fn_sig.inputs(),
|
2018-11-04 16:14:44 +01:00
|
|
|
);
|
|
|
|
self.check_argument_types(
|
|
|
|
call_expr.span,
|
2019-09-20 20:58:20 +02:00
|
|
|
call_expr,
|
2019-08-10 13:38:17 +02:00
|
|
|
fn_sig.inputs(),
|
2018-11-04 16:14:44 +01:00
|
|
|
&expected_arg_tys[..],
|
|
|
|
arg_exprs,
|
2019-02-08 18:30:42 +01:00
|
|
|
fn_sig.c_variadic,
|
2018-11-04 16:14:44 +01:00
|
|
|
TupleArgumentsFlag::DontTupleArguments,
|
|
|
|
def_span,
|
|
|
|
);
|
2016-05-11 07:48:12 +02:00
|
|
|
|
2016-11-29 03:35:38 +01:00
|
|
|
fn_sig.output()
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
fn confirm_deferred_closure_call(
|
|
|
|
&self,
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
arg_exprs: &'tcx [hir::Expr<'tcx>],
|
2018-11-04 16:14:44 +01:00
|
|
|
expected: Expectation<'tcx>,
|
|
|
|
fn_sig: ty::FnSig<'tcx>,
|
|
|
|
) -> Ty<'tcx> {
|
2016-05-11 07:48:12 +02:00
|
|
|
// `fn_sig` is the *signature* of the cosure being called. We
|
|
|
|
// don't know the full details yet (`Fn` vs `FnMut` etc), but we
|
|
|
|
// do know the types expected for each argument and the return
|
|
|
|
// type.
|
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
let expected_arg_tys = self.expected_inputs_for_expected_output(
|
|
|
|
call_expr.span,
|
|
|
|
expected,
|
|
|
|
fn_sig.output().clone(),
|
|
|
|
fn_sig.inputs(),
|
|
|
|
);
|
|
|
|
|
|
|
|
self.check_argument_types(
|
|
|
|
call_expr.span,
|
2019-09-20 20:58:20 +02:00
|
|
|
call_expr,
|
2018-11-04 16:14:44 +01:00
|
|
|
fn_sig.inputs(),
|
|
|
|
&expected_arg_tys,
|
|
|
|
arg_exprs,
|
2019-02-08 18:30:42 +01:00
|
|
|
fn_sig.c_variadic,
|
2018-11-04 16:14:44 +01:00
|
|
|
TupleArgumentsFlag::TupleArguments,
|
|
|
|
None,
|
|
|
|
);
|
2016-05-11 07:48:12 +02:00
|
|
|
|
2016-11-29 03:35:38 +01:00
|
|
|
fn_sig.output()
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
fn confirm_overloaded_call(
|
|
|
|
&self,
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
arg_exprs: &'tcx [hir::Expr<'tcx>],
|
2018-11-04 16:14:44 +01:00
|
|
|
expected: Expectation<'tcx>,
|
|
|
|
method_callee: MethodCallee<'tcx>,
|
|
|
|
) -> Ty<'tcx> {
|
|
|
|
let output_type = self.check_method_argument_types(
|
|
|
|
call_expr.span,
|
2019-09-20 20:58:20 +02:00
|
|
|
call_expr,
|
2018-11-04 16:14:44 +01:00
|
|
|
Ok(method_callee),
|
|
|
|
arg_exprs,
|
|
|
|
TupleArgumentsFlag::TupleArguments,
|
|
|
|
expected,
|
|
|
|
);
|
2016-05-11 07:48:12 +02:00
|
|
|
|
2017-08-07 14:43:43 +02:00
|
|
|
self.write_method_call(call_expr.hir_id, method_callee);
|
2016-08-18 17:52:05 +02:00
|
|
|
output_type
|
2016-05-11 07:48:12 +02:00
|
|
|
}
|
2015-01-02 21:30:26 +01:00
|
|
|
}
|
|
|
|
|
2015-06-18 07:51:23 +02:00
|
|
|
#[derive(Debug)]
|
2019-06-13 23:48:52 +02:00
|
|
|
pub struct DeferredCallResolution<'tcx> {
|
2019-11-30 15:08:22 +01:00
|
|
|
call_expr: &'tcx hir::Expr<'tcx>,
|
|
|
|
callee_expr: &'tcx hir::Expr<'tcx>,
|
2015-01-24 23:32:10 +01:00
|
|
|
adjusted_ty: Ty<'tcx>,
|
2017-05-27 09:29:24 +02:00
|
|
|
adjustments: Vec<Adjustment<'tcx>>,
|
2015-01-24 23:32:10 +01:00
|
|
|
fn_sig: ty::FnSig<'tcx>,
|
2015-08-16 12:32:28 +02:00
|
|
|
closure_def_id: DefId,
|
2019-09-26 19:30:44 +02:00
|
|
|
closure_substs: SubstsRef<'tcx>,
|
2015-01-24 23:32:10 +01:00
|
|
|
}
|
|
|
|
|
2019-06-13 23:48:52 +02:00
|
|
|
impl<'a, 'tcx> DeferredCallResolution<'tcx> {
|
|
|
|
pub fn resolve(self, fcx: &FnCtxt<'a, 'tcx>) {
|
2016-09-26 05:41:27 +02:00
|
|
|
debug!("DeferredCallResolution::resolve() {:?}", self);
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2015-01-31 12:29:04 +01:00
|
|
|
// we should not be invoked until the closure kind has been
|
|
|
|
// determined by upvar inference
|
2019-12-22 23:42:04 +01:00
|
|
|
assert!(fcx.closure_kind(self.closure_def_id, self.closure_substs).is_some());
|
2015-01-31 02:23:17 +01:00
|
|
|
|
2015-01-24 23:32:10 +01:00
|
|
|
// We may now know enough to figure out fn vs fnmut etc.
|
2018-11-04 11:23:45 +01:00
|
|
|
match fcx.try_overloaded_call_traits(self.call_expr, self.adjusted_ty, None) {
|
2017-05-16 23:54:04 +02:00
|
|
|
Some((autoref, method_callee)) => {
|
2015-01-24 23:32:10 +01:00
|
|
|
// One problem is that when we get here, we are going
|
|
|
|
// to have a newly instantiated function signature
|
|
|
|
// from the call trait. This has to be reconciled with
|
|
|
|
// the older function signature we had before. In
|
|
|
|
// principle we *should* be able to fn_sigs(), but we
|
|
|
|
// can't because of the annoying need for a TypeTrace.
|
|
|
|
// (This always bites me, should find a way to
|
|
|
|
// refactor it.)
|
2017-05-17 21:51:18 +02:00
|
|
|
let method_sig = method_callee.sig;
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2016-09-26 05:41:27 +02:00
|
|
|
debug!("attempt_resolution: method_callee={:?}", method_callee);
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2016-11-29 03:35:38 +01:00
|
|
|
for (method_arg_ty, self_arg_ty) in
|
2018-11-04 16:14:44 +01:00
|
|
|
method_sig.inputs().iter().skip(1).zip(self.fn_sig.inputs())
|
|
|
|
{
|
2016-11-29 03:35:38 +01:00
|
|
|
fcx.demand_eqtype(self.call_expr.span, &self_arg_ty, &method_arg_ty);
|
2015-01-24 23:32:10 +01:00
|
|
|
}
|
|
|
|
|
2019-12-22 23:42:04 +01:00
|
|
|
fcx.demand_eqtype(self.call_expr.span, method_sig.output(), self.fn_sig.output());
|
2015-01-24 23:32:10 +01:00
|
|
|
|
2017-05-27 09:29:24 +02:00
|
|
|
let mut adjustments = self.adjustments;
|
|
|
|
adjustments.extend(autoref);
|
|
|
|
fcx.apply_adjustments(self.callee_expr, adjustments);
|
2017-05-16 23:54:04 +02:00
|
|
|
|
2018-11-04 16:14:44 +01:00
|
|
|
fcx.write_method_call(self.call_expr.hir_id, method_callee);
|
2015-01-31 12:29:04 +01:00
|
|
|
}
|
|
|
|
None => {
|
2018-11-04 16:14:44 +01:00
|
|
|
span_bug!(
|
|
|
|
self.call_expr.span,
|
|
|
|
"failed to find an overloaded call trait for closure call"
|
|
|
|
);
|
2015-01-24 23:32:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|