]> git.lizzy.rs Git - rust.git/commitdiff
typeck: extract ban_take_value_of_method
authorMazdak Farrokhzad <twingoow@gmail.com>
Tue, 13 Aug 2019 21:59:22 +0000 (23:59 +0200)
committerMazdak Farrokhzad <twingoow@gmail.com>
Tue, 13 Aug 2019 21:59:22 +0000 (23:59 +0200)
src/librustc_typeck/check/expr.rs

index b4154c15d67394a51b1c9e899fd87c382472fae2..c25a60d065d6b2773b7ff5382b30c02728554cff 100644 (file)
@@ -1342,23 +1342,7 @@ fn check_field(
         } else if field.name == kw::Invalid {
             self.tcx().types.err
         } else if self.method_exists(field, expr_t, expr.hir_id, true) {
-            let mut err = type_error_struct!(self.tcx().sess, field.span, expr_t, E0615,
-                               "attempted to take value of method `{}` on type `{}`",
-                               field, expr_t);
-
-            if !self.expr_in_place(expr.hir_id) {
-                self.suggest_method_call(
-                    &mut err,
-                    "use parentheses to call the method",
-                    field,
-                    expr_t,
-                    expr.hir_id
-                );
-            } else {
-                err.help("methods are immutable and cannot be assigned to");
-            }
-
-            err.emit();
+            self.ban_take_value_of_method(expr, expr_t, field);
             self.tcx().types.err
         } else {
             if !expr_t.is_primitive_ty() {
@@ -1436,9 +1420,9 @@ fn ban_private_field_access(
         expr: &'tcx hir::Expr,
         expr_t: Ty<'tcx>,
         field: ast::Ident,
-        def_id: DefId,
+        base_did: DefId,
     ) {
-        let struct_path = self.tcx().def_path_str(def_id);
+        let struct_path = self.tcx().def_path_str(base_did);
         let mut err = struct_span_err!(
             self.tcx().sess,
             expr.span,
@@ -1462,6 +1446,32 @@ fn ban_private_field_access(
         err.emit();
     }
 
+    fn ban_take_value_of_method(&self, expr: &'tcx hir::Expr, expr_t: Ty<'tcx>, field: ast::Ident) {
+        let mut err = type_error_struct!(
+            self.tcx().sess,
+            field.span,
+            expr_t,
+            E0615,
+            "attempted to take value of method `{}` on type `{}`",
+            field,
+            expr_t
+        );
+
+        if !self.expr_in_place(expr.hir_id) {
+            self.suggest_method_call(
+                &mut err,
+                "use parentheses to call the method",
+                field,
+                expr_t,
+                expr.hir_id
+            );
+        } else {
+            err.help("methods are immutable and cannot be assigned to");
+        }
+
+        err.emit();
+    }
+
     fn no_such_field_err<T: Display>(&self, span: Span, field: T, expr_t: &ty::TyS<'_>)
         -> DiagnosticBuilder<'_> {
         type_error_struct!(self.tcx().sess, span, expr_t, E0609,