]> git.lizzy.rs Git - rust.git/blob - compiler/rustc_typeck/src/check/callee.rs
Auto merge of #99768 - klensy:bump-deps-07-22, r=Mark-Simulacrum
[rust.git] / compiler / rustc_typeck / src / check / callee.rs
1 use super::method::MethodCallee;
2 use super::{Expectation, FnCtxt, TupleArgumentsFlag};
3 use crate::type_error_struct;
4
5 use rustc_errors::{struct_span_err, Applicability, Diagnostic};
6 use rustc_hir as hir;
7 use rustc_hir::def::{self, Namespace, Res};
8 use rustc_hir::def_id::{DefId, LOCAL_CRATE};
9 use rustc_infer::{
10     infer,
11     traits::{self, Obligation},
12 };
13 use rustc_infer::{
14     infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKind},
15     traits::ObligationCause,
16 };
17 use rustc_middle::ty::adjustment::{
18     Adjust, Adjustment, AllowTwoPhase, AutoBorrow, AutoBorrowMutability,
19 };
20 use rustc_middle::ty::subst::{Subst, SubstsRef};
21 use rustc_middle::ty::{self, Ty, TyCtxt, TypeVisitable};
22 use rustc_span::symbol::{sym, Ident};
23 use rustc_span::Span;
24 use rustc_target::spec::abi;
25 use rustc_trait_selection::autoderef::Autoderef;
26 use rustc_trait_selection::traits::query::evaluate_obligation::InferCtxtExt;
27 use std::iter;
28
29 /// Checks that it is legal to call methods of the trait corresponding
30 /// to `trait_id` (this only cares about the trait, not the specific
31 /// method that is called).
32 pub fn check_legal_trait_for_method_call(
33     tcx: TyCtxt<'_>,
34     span: Span,
35     receiver: Option<Span>,
36     expr_span: Span,
37     trait_id: DefId,
38 ) {
39     if tcx.lang_items().drop_trait() == Some(trait_id) {
40         let mut err = struct_span_err!(tcx.sess, span, E0040, "explicit use of destructor method");
41         err.span_label(span, "explicit destructor calls not allowed");
42
43         let (sp, suggestion) = receiver
44             .and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
45             .filter(|snippet| !snippet.is_empty())
46             .map(|snippet| (expr_span, format!("drop({snippet})")))
47             .unwrap_or_else(|| (span, "drop".to_string()));
48
49         err.span_suggestion(
50             sp,
51             "consider using `drop` function",
52             suggestion,
53             Applicability::MaybeIncorrect,
54         );
55
56         err.emit();
57     }
58 }
59
60 enum CallStep<'tcx> {
61     Builtin(Ty<'tcx>),
62     DeferredClosure(DefId, ty::FnSig<'tcx>),
63     /// E.g., enum variant constructors.
64     Overloaded(MethodCallee<'tcx>),
65 }
66
67 impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
68     pub fn check_call(
69         &self,
70         call_expr: &'tcx hir::Expr<'tcx>,
71         callee_expr: &'tcx hir::Expr<'tcx>,
72         arg_exprs: &'tcx [hir::Expr<'tcx>],
73         expected: Expectation<'tcx>,
74     ) -> Ty<'tcx> {
75         let original_callee_ty = match &callee_expr.kind {
76             hir::ExprKind::Path(hir::QPath::Resolved(..) | hir::QPath::TypeRelative(..)) => self
77                 .check_expr_with_expectation_and_args(
78                     callee_expr,
79                     Expectation::NoExpectation,
80                     arg_exprs,
81                 ),
82             _ => self.check_expr(callee_expr),
83         };
84
85         let expr_ty = self.structurally_resolved_type(call_expr.span, original_callee_ty);
86
87         let mut autoderef = self.autoderef(callee_expr.span, expr_ty);
88         let mut result = None;
89         while result.is_none() && autoderef.next().is_some() {
90             result = self.try_overloaded_call_step(call_expr, callee_expr, arg_exprs, &autoderef);
91         }
92         self.register_predicates(autoderef.into_obligations());
93
94         let output = match result {
95             None => {
96                 // this will report an error since original_callee_ty is not a fn
97                 self.confirm_builtin_call(
98                     call_expr,
99                     callee_expr,
100                     original_callee_ty,
101                     arg_exprs,
102                     expected,
103                 )
104             }
105
106             Some(CallStep::Builtin(callee_ty)) => {
107                 self.confirm_builtin_call(call_expr, callee_expr, callee_ty, arg_exprs, expected)
108             }
109
110             Some(CallStep::DeferredClosure(def_id, fn_sig)) => {
111                 self.confirm_deferred_closure_call(call_expr, arg_exprs, expected, def_id, fn_sig)
112             }
113
114             Some(CallStep::Overloaded(method_callee)) => {
115                 self.confirm_overloaded_call(call_expr, arg_exprs, expected, method_callee)
116             }
117         };
118
119         // we must check that return type of called functions is WF:
120         self.register_wf_obligation(output.into(), call_expr.span, traits::WellFormed(None));
121
122         output
123     }
124
125     fn try_overloaded_call_step(
126         &self,
127         call_expr: &'tcx hir::Expr<'tcx>,
128         callee_expr: &'tcx hir::Expr<'tcx>,
129         arg_exprs: &'tcx [hir::Expr<'tcx>],
130         autoderef: &Autoderef<'a, 'tcx>,
131     ) -> Option<CallStep<'tcx>> {
132         let adjusted_ty =
133             self.structurally_resolved_type(autoderef.span(), autoderef.final_ty(false));
134         debug!(
135             "try_overloaded_call_step(call_expr={:?}, adjusted_ty={:?})",
136             call_expr, adjusted_ty
137         );
138
139         // If the callee is a bare function or a closure, then we're all set.
140         match *adjusted_ty.kind() {
141             ty::FnDef(..) | ty::FnPtr(_) => {
142                 let adjustments = self.adjust_steps(autoderef);
143                 self.apply_adjustments(callee_expr, adjustments);
144                 return Some(CallStep::Builtin(adjusted_ty));
145             }
146
147             ty::Closure(def_id, substs) => {
148                 assert_eq!(def_id.krate, LOCAL_CRATE);
149
150                 // Check whether this is a call to a closure where we
151                 // haven't yet decided on whether the closure is fn vs
152                 // fnmut vs fnonce. If so, we have to defer further processing.
153                 if self.closure_kind(substs).is_none() {
154                     let closure_sig = substs.as_closure().sig();
155                     let closure_sig = self.replace_bound_vars_with_fresh_vars(
156                         call_expr.span,
157                         infer::FnCall,
158                         closure_sig,
159                     );
160                     let adjustments = self.adjust_steps(autoderef);
161                     self.record_deferred_call_resolution(
162                         def_id,
163                         DeferredCallResolution {
164                             call_expr,
165                             callee_expr,
166                             adjusted_ty,
167                             adjustments,
168                             fn_sig: closure_sig,
169                             closure_substs: substs,
170                         },
171                     );
172                     return Some(CallStep::DeferredClosure(def_id, closure_sig));
173                 }
174             }
175
176             // Hack: we know that there are traits implementing Fn for &F
177             // where F:Fn and so forth. In the particular case of types
178             // like `x: &mut FnMut()`, if there is a call `x()`, we would
179             // normally translate to `FnMut::call_mut(&mut x, ())`, but
180             // that winds up requiring `mut x: &mut FnMut()`. A little
181             // over the top. The simplest fix by far is to just ignore
182             // this case and deref again, so we wind up with
183             // `FnMut::call_mut(&mut *x, ())`.
184             ty::Ref(..) if autoderef.step_count() == 0 => {
185                 return None;
186             }
187
188             _ => {}
189         }
190
191         // Now, we look for the implementation of a Fn trait on the object's type.
192         // We first do it with the explicit instruction to look for an impl of
193         // `Fn<Tuple>`, with the tuple `Tuple` having an arity corresponding
194         // to the number of call parameters.
195         // If that fails (or_else branch), we try again without specifying the
196         // shape of the tuple (hence the None). This allows to detect an Fn trait
197         // is implemented, and use this information for diagnostic.
198         self.try_overloaded_call_traits(call_expr, adjusted_ty, Some(arg_exprs))
199             .or_else(|| self.try_overloaded_call_traits(call_expr, adjusted_ty, None))
200             .map(|(autoref, method)| {
201                 let mut adjustments = self.adjust_steps(autoderef);
202                 adjustments.extend(autoref);
203                 self.apply_adjustments(callee_expr, adjustments);
204                 CallStep::Overloaded(method)
205             })
206     }
207
208     fn try_overloaded_call_traits(
209         &self,
210         call_expr: &hir::Expr<'_>,
211         adjusted_ty: Ty<'tcx>,
212         opt_arg_exprs: Option<&'tcx [hir::Expr<'tcx>]>,
213     ) -> Option<(Option<Adjustment<'tcx>>, MethodCallee<'tcx>)> {
214         // Try the options that are least restrictive on the caller first.
215         for (opt_trait_def_id, method_name, borrow) in [
216             (self.tcx.lang_items().fn_trait(), Ident::with_dummy_span(sym::call), true),
217             (self.tcx.lang_items().fn_mut_trait(), Ident::with_dummy_span(sym::call_mut), true),
218             (self.tcx.lang_items().fn_once_trait(), Ident::with_dummy_span(sym::call_once), false),
219         ] {
220             let Some(trait_def_id) = opt_trait_def_id else { continue };
221
222             let opt_input_types = opt_arg_exprs.map(|arg_exprs| {
223                 [self.tcx.mk_tup(arg_exprs.iter().map(|e| {
224                     self.next_ty_var(TypeVariableOrigin {
225                         kind: TypeVariableOriginKind::TypeInference,
226                         span: e.span,
227                     })
228                 }))]
229             });
230             let opt_input_types = opt_input_types.as_ref().map(AsRef::as_ref);
231
232             if let Some(ok) = self.lookup_method_in_trait(
233                 call_expr.span,
234                 method_name,
235                 trait_def_id,
236                 adjusted_ty,
237                 opt_input_types,
238             ) {
239                 let method = self.register_infer_ok_obligations(ok);
240                 let mut autoref = None;
241                 if borrow {
242                     // Check for &self vs &mut self in the method signature. Since this is either
243                     // the Fn or FnMut trait, it should be one of those.
244                     let ty::Ref(region, _, mutbl) = method.sig.inputs()[0].kind() else {
245                         // The `fn`/`fn_mut` lang item is ill-formed, which should have
246                         // caused an error elsewhere.
247                         self.tcx
248                             .sess
249                             .delay_span_bug(call_expr.span, "input to call/call_mut is not a ref?");
250                         return None;
251                     };
252
253                     let mutbl = match mutbl {
254                         hir::Mutability::Not => AutoBorrowMutability::Not,
255                         hir::Mutability::Mut => AutoBorrowMutability::Mut {
256                             // For initial two-phase borrow
257                             // deployment, conservatively omit
258                             // overloaded function call ops.
259                             allow_two_phase_borrow: AllowTwoPhase::No,
260                         },
261                     };
262                     autoref = Some(Adjustment {
263                         kind: Adjust::Borrow(AutoBorrow::Ref(*region, mutbl)),
264                         target: method.sig.inputs()[0],
265                     });
266                 }
267                 return Some((autoref, method));
268             }
269         }
270
271         None
272     }
273
274     /// Give appropriate suggestion when encountering `||{/* not callable */}()`, where the
275     /// likely intention is to call the closure, suggest `(||{})()`. (#55851)
276     fn identify_bad_closure_def_and_call(
277         &self,
278         err: &mut Diagnostic,
279         hir_id: hir::HirId,
280         callee_node: &hir::ExprKind<'_>,
281         callee_span: Span,
282     ) {
283         let hir = self.tcx.hir();
284         let parent_hir_id = hir.get_parent_node(hir_id);
285         let parent_node = hir.get(parent_hir_id);
286         if let (
287             hir::Node::Expr(hir::Expr {
288                 kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, body, .. }),
289                 ..
290             }),
291             hir::ExprKind::Block(..),
292         ) = (parent_node, callee_node)
293         {
294             let fn_decl_span = if hir.body(body).generator_kind
295                 == Some(hir::GeneratorKind::Async(hir::AsyncGeneratorKind::Closure))
296             {
297                 // Actually need to unwrap a few more layers of HIR to get to
298                 // the _real_ closure...
299                 let async_closure = hir.get_parent_node(hir.get_parent_node(parent_hir_id));
300                 if let hir::Node::Expr(hir::Expr {
301                     kind: hir::ExprKind::Closure(&hir::Closure { fn_decl_span, .. }),
302                     ..
303                 }) = hir.get(async_closure)
304                 {
305                     fn_decl_span
306                 } else {
307                     return;
308                 }
309             } else {
310                 fn_decl_span
311             };
312
313             let start = fn_decl_span.shrink_to_lo();
314             let end = callee_span.shrink_to_hi();
315             err.multipart_suggestion(
316                 "if you meant to create this closure and immediately call it, surround the \
317                 closure with parentheses",
318                 vec![(start, "(".to_string()), (end, ")".to_string())],
319                 Applicability::MaybeIncorrect,
320             );
321         }
322     }
323
324     /// Give appropriate suggestion when encountering `[("a", 0) ("b", 1)]`, where the
325     /// likely intention is to create an array containing tuples.
326     fn maybe_suggest_bad_array_definition(
327         &self,
328         err: &mut Diagnostic,
329         call_expr: &'tcx hir::Expr<'tcx>,
330         callee_expr: &'tcx hir::Expr<'tcx>,
331     ) -> bool {
332         let hir_id = self.tcx.hir().get_parent_node(call_expr.hir_id);
333         let parent_node = self.tcx.hir().get(hir_id);
334         if let (
335             hir::Node::Expr(hir::Expr { kind: hir::ExprKind::Array(_), .. }),
336             hir::ExprKind::Tup(exp),
337             hir::ExprKind::Call(_, args),
338         ) = (parent_node, &callee_expr.kind, &call_expr.kind)
339             && args.len() == exp.len()
340         {
341             let start = callee_expr.span.shrink_to_hi();
342             err.span_suggestion(
343                 start,
344                 "consider separating array elements with a comma",
345                 ",",
346                 Applicability::MaybeIncorrect,
347             );
348             return true;
349         }
350         false
351     }
352
353     fn confirm_builtin_call(
354         &self,
355         call_expr: &'tcx hir::Expr<'tcx>,
356         callee_expr: &'tcx hir::Expr<'tcx>,
357         callee_ty: Ty<'tcx>,
358         arg_exprs: &'tcx [hir::Expr<'tcx>],
359         expected: Expectation<'tcx>,
360     ) -> Ty<'tcx> {
361         let (fn_sig, def_id) = match *callee_ty.kind() {
362             ty::FnDef(def_id, subst) => {
363                 let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, subst);
364
365                 // Unit testing: function items annotated with
366                 // `#[rustc_evaluate_where_clauses]` trigger special output
367                 // to let us test the trait evaluation system.
368                 if self.tcx.has_attr(def_id, sym::rustc_evaluate_where_clauses) {
369                     let predicates = self.tcx.predicates_of(def_id);
370                     let predicates = predicates.instantiate(self.tcx, subst);
371                     for (predicate, predicate_span) in
372                         predicates.predicates.iter().zip(&predicates.spans)
373                     {
374                         let obligation = Obligation::new(
375                             ObligationCause::dummy_with_span(callee_expr.span),
376                             self.param_env,
377                             *predicate,
378                         );
379                         let result = self.evaluate_obligation(&obligation);
380                         self.tcx
381                             .sess
382                             .struct_span_err(
383                                 callee_expr.span,
384                                 &format!("evaluate({:?}) = {:?}", predicate, result),
385                             )
386                             .span_label(*predicate_span, "predicate")
387                             .emit();
388                     }
389                 }
390                 (fn_sig, Some(def_id))
391             }
392             ty::FnPtr(sig) => (sig, None),
393             _ => {
394                 let mut unit_variant = None;
395                 if let hir::ExprKind::Path(qpath) = &callee_expr.kind
396                     && let Res::Def(def::DefKind::Ctor(kind, def::CtorKind::Const), _)
397                         = self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
398                     // Only suggest removing parens if there are no arguments
399                     && arg_exprs.is_empty()
400                 {
401                     let descr = match kind {
402                         def::CtorOf::Struct => "struct",
403                         def::CtorOf::Variant => "enum variant",
404                     };
405                     let removal_span =
406                         callee_expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
407                     unit_variant =
408                         Some((removal_span, descr, rustc_hir_pretty::qpath_to_string(qpath)));
409                 }
410
411                 let callee_ty = self.resolve_vars_if_possible(callee_ty);
412                 let mut err = type_error_struct!(
413                     self.tcx.sess,
414                     callee_expr.span,
415                     callee_ty,
416                     E0618,
417                     "expected function, found {}",
418                     match &unit_variant {
419                         Some((_, kind, path)) => format!("{kind} `{path}`"),
420                         None => format!("`{callee_ty}`"),
421                     }
422                 );
423
424                 self.identify_bad_closure_def_and_call(
425                     &mut err,
426                     call_expr.hir_id,
427                     &callee_expr.kind,
428                     callee_expr.span,
429                 );
430
431                 if let Some((removal_span, kind, path)) = &unit_variant {
432                     err.span_suggestion_verbose(
433                         *removal_span,
434                         &format!(
435                             "`{path}` is a unit {kind}, and does not take parentheses to be constructed",
436                         ),
437                         "",
438                         Applicability::MachineApplicable,
439                     );
440                 }
441
442                 let mut inner_callee_path = None;
443                 let def = match callee_expr.kind {
444                     hir::ExprKind::Path(ref qpath) => {
445                         self.typeck_results.borrow().qpath_res(qpath, callee_expr.hir_id)
446                     }
447                     hir::ExprKind::Call(ref inner_callee, _) => {
448                         // If the call spans more than one line and the callee kind is
449                         // itself another `ExprCall`, that's a clue that we might just be
450                         // missing a semicolon (Issue #51055)
451                         let call_is_multiline =
452                             self.tcx.sess.source_map().is_multiline(call_expr.span);
453                         if call_is_multiline {
454                             err.span_suggestion(
455                                 callee_expr.span.shrink_to_hi(),
456                                 "consider using a semicolon here",
457                                 ";",
458                                 Applicability::MaybeIncorrect,
459                             );
460                         }
461                         if let hir::ExprKind::Path(ref inner_qpath) = inner_callee.kind {
462                             inner_callee_path = Some(inner_qpath);
463                             self.typeck_results.borrow().qpath_res(inner_qpath, inner_callee.hir_id)
464                         } else {
465                             Res::Err
466                         }
467                     }
468                     _ => Res::Err,
469                 };
470
471                 if !self.maybe_suggest_bad_array_definition(&mut err, call_expr, callee_expr) {
472                     err.span_label(call_expr.span, "call expression requires function");
473                 }
474
475                 if let Some(span) = self.tcx.hir().res_span(def) {
476                     let callee_ty = callee_ty.to_string();
477                     let label = match (unit_variant, inner_callee_path) {
478                         (Some((_, kind, path)), _) => Some(format!("{kind} `{path}` defined here")),
479                         (_, Some(hir::QPath::Resolved(_, path))) => self
480                             .tcx
481                             .sess
482                             .source_map()
483                             .span_to_snippet(path.span)
484                             .ok()
485                             .map(|p| format!("`{p}` defined here returns `{callee_ty}`")),
486                         _ => {
487                             match def {
488                                 // Emit a different diagnostic for local variables, as they are not
489                                 // type definitions themselves, but rather variables *of* that type.
490                                 Res::Local(hir_id) => Some(format!(
491                                     "`{}` has type `{}`",
492                                     self.tcx.hir().name(hir_id),
493                                     callee_ty
494                                 )),
495                                 Res::Def(kind, def_id) if kind.ns() == Some(Namespace::ValueNS) => {
496                                     Some(format!(
497                                         "`{}` defined here",
498                                         self.tcx.def_path_str(def_id),
499                                     ))
500                                 }
501                                 _ => Some(format!("`{callee_ty}` defined here")),
502                             }
503                         }
504                     };
505                     if let Some(label) = label {
506                         err.span_label(span, label);
507                     }
508                 }
509                 err.emit();
510
511                 // This is the "default" function signature, used in case of error.
512                 // In that case, we check each argument against "error" in order to
513                 // set up all the node type bindings.
514                 (
515                     ty::Binder::dummy(self.tcx.mk_fn_sig(
516                         self.err_args(arg_exprs.len()).into_iter(),
517                         self.tcx.ty_error(),
518                         false,
519                         hir::Unsafety::Normal,
520                         abi::Abi::Rust,
521                     )),
522                     None,
523                 )
524             }
525         };
526
527         // Replace any late-bound regions that appear in the function
528         // signature with region variables. We also have to
529         // renormalize the associated types at this point, since they
530         // previously appeared within a `Binder<>` and hence would not
531         // have been normalized before.
532         let fn_sig = self.replace_bound_vars_with_fresh_vars(call_expr.span, infer::FnCall, fn_sig);
533         let fn_sig = self.normalize_associated_types_in(call_expr.span, fn_sig);
534
535         // Call the generic checker.
536         let expected_arg_tys = self.expected_inputs_for_expected_output(
537             call_expr.span,
538             expected,
539             fn_sig.output(),
540             fn_sig.inputs(),
541         );
542         self.check_argument_types(
543             call_expr.span,
544             call_expr,
545             fn_sig.inputs(),
546             expected_arg_tys,
547             arg_exprs,
548             fn_sig.c_variadic,
549             TupleArgumentsFlag::DontTupleArguments,
550             def_id,
551         );
552
553         fn_sig.output()
554     }
555
556     fn confirm_deferred_closure_call(
557         &self,
558         call_expr: &'tcx hir::Expr<'tcx>,
559         arg_exprs: &'tcx [hir::Expr<'tcx>],
560         expected: Expectation<'tcx>,
561         closure_def_id: DefId,
562         fn_sig: ty::FnSig<'tcx>,
563     ) -> Ty<'tcx> {
564         // `fn_sig` is the *signature* of the closure being called. We
565         // don't know the full details yet (`Fn` vs `FnMut` etc), but we
566         // do know the types expected for each argument and the return
567         // type.
568
569         let expected_arg_tys = self.expected_inputs_for_expected_output(
570             call_expr.span,
571             expected,
572             fn_sig.output(),
573             fn_sig.inputs(),
574         );
575
576         self.check_argument_types(
577             call_expr.span,
578             call_expr,
579             fn_sig.inputs(),
580             expected_arg_tys,
581             arg_exprs,
582             fn_sig.c_variadic,
583             TupleArgumentsFlag::TupleArguments,
584             Some(closure_def_id),
585         );
586
587         fn_sig.output()
588     }
589
590     fn confirm_overloaded_call(
591         &self,
592         call_expr: &'tcx hir::Expr<'tcx>,
593         arg_exprs: &'tcx [hir::Expr<'tcx>],
594         expected: Expectation<'tcx>,
595         method_callee: MethodCallee<'tcx>,
596     ) -> Ty<'tcx> {
597         let output_type = self.check_method_argument_types(
598             call_expr.span,
599             call_expr,
600             Ok(method_callee),
601             arg_exprs,
602             TupleArgumentsFlag::TupleArguments,
603             expected,
604         );
605
606         self.write_method_call(call_expr.hir_id, method_callee);
607         output_type
608     }
609 }
610
611 #[derive(Debug)]
612 pub struct DeferredCallResolution<'tcx> {
613     call_expr: &'tcx hir::Expr<'tcx>,
614     callee_expr: &'tcx hir::Expr<'tcx>,
615     adjusted_ty: Ty<'tcx>,
616     adjustments: Vec<Adjustment<'tcx>>,
617     fn_sig: ty::FnSig<'tcx>,
618     closure_substs: SubstsRef<'tcx>,
619 }
620
621 impl<'a, 'tcx> DeferredCallResolution<'tcx> {
622     pub fn resolve(self, fcx: &FnCtxt<'a, 'tcx>) {
623         debug!("DeferredCallResolution::resolve() {:?}", self);
624
625         // we should not be invoked until the closure kind has been
626         // determined by upvar inference
627         assert!(fcx.closure_kind(self.closure_substs).is_some());
628
629         // We may now know enough to figure out fn vs fnmut etc.
630         match fcx.try_overloaded_call_traits(self.call_expr, self.adjusted_ty, None) {
631             Some((autoref, method_callee)) => {
632                 // One problem is that when we get here, we are going
633                 // to have a newly instantiated function signature
634                 // from the call trait. This has to be reconciled with
635                 // the older function signature we had before. In
636                 // principle we *should* be able to fn_sigs(), but we
637                 // can't because of the annoying need for a TypeTrace.
638                 // (This always bites me, should find a way to
639                 // refactor it.)
640                 let method_sig = method_callee.sig;
641
642                 debug!("attempt_resolution: method_callee={:?}", method_callee);
643
644                 for (method_arg_ty, self_arg_ty) in
645                     iter::zip(method_sig.inputs().iter().skip(1), self.fn_sig.inputs())
646                 {
647                     fcx.demand_eqtype(self.call_expr.span, *self_arg_ty, *method_arg_ty);
648                 }
649
650                 fcx.demand_eqtype(self.call_expr.span, method_sig.output(), self.fn_sig.output());
651
652                 let mut adjustments = self.adjustments;
653                 adjustments.extend(autoref);
654                 fcx.apply_adjustments(self.callee_expr, adjustments);
655
656                 fcx.write_method_call(self.call_expr.hir_id, method_callee);
657             }
658             None => {
659                 // This can happen if `#![no_core]` is used and the `fn/fn_mut/fn_once`
660                 // lang items are not defined (issue #86238).
661                 let mut err = fcx.inh.tcx.sess.struct_span_err(
662                     self.call_expr.span,
663                     "failed to find an overloaded call trait for closure call",
664                 );
665                 err.help(
666                     "make sure the `fn`/`fn_mut`/`fn_once` lang items are defined \
667                      and have associated `call`/`call_mut`/`call_once` functions",
668                 );
669                 err.emit();
670             }
671         }
672     }
673 }