]> git.proxmox.com Git - rustc.git/blobdiff - compiler/rustc_typeck/src/check/callee.rs
New upstream version 1.62.1+dfsg1
[rustc.git] / compiler / rustc_typeck / src / check / callee.rs
index 2a5cf03e9d0baaf4f042e096250a5a3d02095409..0a84d41b4f31caa0cf99b50ecd5fb750761dd27a 100644 (file)
@@ -43,7 +43,7 @@ pub fn check_legal_trait_for_method_call(
         let (sp, suggestion) = receiver
             .and_then(|s| tcx.sess.source_map().span_to_snippet(s).ok())
             .filter(|snippet| !snippet.is_empty())
-            .map(|snippet| (expr_span, format!("drop({})", snippet)))
+            .map(|snippet| (expr_span, format!("drop({snippet})")))
             .unwrap_or_else(|| (span, "drop".to_string()));
 
         err.span_suggestion(
@@ -59,7 +59,7 @@ pub fn check_legal_trait_for_method_call(
 
 enum CallStep<'tcx> {
     Builtin(Ty<'tcx>),
-    DeferredClosure(ty::FnSig<'tcx>),
+    DeferredClosure(DefId, ty::FnSig<'tcx>),
     /// E.g., enum variant constructors.
     Overloaded(MethodCallee<'tcx>),
 }
@@ -107,8 +107,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 self.confirm_builtin_call(call_expr, callee_expr, callee_ty, arg_exprs, expected)
             }
 
-            Some(CallStep::DeferredClosure(fn_sig)) => {
-                self.confirm_deferred_closure_call(call_expr, arg_exprs, expected, fn_sig)
+            Some(CallStep::DeferredClosure(def_id, fn_sig)) => {
+                self.confirm_deferred_closure_call(call_expr, arg_exprs, expected, def_id, fn_sig)
             }
 
             Some(CallStep::Overloaded(method_callee)) => {
@@ -171,7 +171,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                             closure_substs: substs,
                         },
                     );
-                    return Some(CallStep::DeferredClosure(closure_sig));
+                    return Some(CallStep::DeferredClosure(def_id, closure_sig));
                 }
             }
 
@@ -315,17 +315,16 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             hir::ExprKind::Tup(exp),
             hir::ExprKind::Call(_, args),
         ) = (parent_node, &callee_expr.kind, &call_expr.kind)
+            && args.len() == exp.len()
         {
-            if args.len() == exp.len() {
-                let start = callee_expr.span.shrink_to_hi();
-                err.span_suggestion(
-                    start,
-                    "consider separating array elements with a comma",
-                    ",".to_string(),
-                    Applicability::MaybeIncorrect,
-                );
-                return true;
-            }
+            let start = callee_expr.span.shrink_to_hi();
+            err.span_suggestion(
+                start,
+                "consider separating array elements with a comma",
+                ",".to_string(),
+                Applicability::MaybeIncorrect,
+            );
+            return true;
         }
         false
     }
@@ -340,7 +339,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
     ) -> Ty<'tcx> {
         let (fn_sig, def_id) = match *callee_ty.kind() {
             ty::FnDef(def_id, subst) => {
-                let fn_sig = self.tcx.fn_sig(def_id).subst(self.tcx, subst);
+                let fn_sig = self.tcx.bound_fn_sig(def_id).subst(self.tcx, subst);
 
                 // Unit testing: function items annotated with
                 // `#[rustc_evaluate_where_clauses]` trigger special output
@@ -373,15 +372,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             ref t => {
                 let mut unit_variant = None;
                 let mut removal_span = call_expr.span;
-                if let ty::Adt(adt_def, ..) = t {
-                    if adt_def.is_enum() {
-                        if let hir::ExprKind::Call(expr, _) = call_expr.kind {
-                            removal_span =
-                                expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
-                            unit_variant =
-                                self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
-                        }
-                    }
+                if let ty::Adt(adt_def, ..) = t
+                    && adt_def.is_enum()
+                    && let hir::ExprKind::Call(expr, _) = call_expr.kind
+                {
+                    removal_span =
+                        expr.span.shrink_to_hi().to(call_expr.span.shrink_to_hi());
+                    unit_variant =
+                        self.tcx.sess.source_map().span_to_snippet(expr.span).ok();
                 }
 
                 let callee_ty = self.resolve_vars_if_possible(callee_ty);
@@ -392,8 +390,8 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     E0618,
                     "expected function, found {}",
                     match unit_variant {
-                        Some(ref path) => format!("enum variant `{}`", path),
-                        None => format!("`{}`", callee_ty),
+                        Some(ref path) => format!("enum variant `{path}`"),
+                        None => format!("`{callee_ty}`"),
                     }
                 );
 
@@ -408,8 +406,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                     err.span_suggestion_verbose(
                         removal_span,
                         &format!(
-                            "`{}` is a unit variant, you need to write it without the parentheses",
-                            path
+                            "`{path}` is a unit variant, you need to write it without the parentheses",
                         ),
                         String::new(),
                         Applicability::MachineApplicable,
@@ -452,14 +449,14 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                 if let Some(span) = self.tcx.hir().res_span(def) {
                     let callee_ty = callee_ty.to_string();
                     let label = match (unit_variant, inner_callee_path) {
-                        (Some(path), _) => Some(format!("`{}` defined here", path)),
+                        (Some(path), _) => Some(format!("`{path}` defined here")),
                         (_, Some(hir::QPath::Resolved(_, path))) => self
                             .tcx
                             .sess
                             .source_map()
                             .span_to_snippet(path.span)
                             .ok()
-                            .map(|p| format!("`{}` defined here returns `{}`", p, callee_ty)),
+                            .map(|p| format!("`{p}` defined here returns `{callee_ty}`")),
                         _ => {
                             match def {
                                 // Emit a different diagnostic for local variables, as they are not
@@ -475,7 +472,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
                                         self.tcx.def_path_str(def_id),
                                     ))
                                 }
-                                _ => Some(format!("`{}` defined here", callee_ty)),
+                                _ => Some(format!("`{callee_ty}` defined here")),
                             }
                         }
                     };
@@ -536,6 +533,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
         call_expr: &'tcx hir::Expr<'tcx>,
         arg_exprs: &'tcx [hir::Expr<'tcx>],
         expected: Expectation<'tcx>,
+        closure_def_id: DefId,
         fn_sig: ty::FnSig<'tcx>,
     ) -> Ty<'tcx> {
         // `fn_sig` is the *signature* of the closure being called. We
@@ -558,7 +556,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
             arg_exprs,
             fn_sig.c_variadic,
             TupleArgumentsFlag::TupleArguments,
-            None,
+            Some(closure_def_id),
         );
 
         fn_sig.output()