]> git.lizzy.rs Git - rust.git/commitdiff
Refactor into ban_nonexisting_field method
authorWojciech Baranowski <wbaranowski@protonmail.com>
Sat, 28 Sep 2019 12:26:00 +0000 (15:26 +0300)
committerWojciech Baranowski <wbaranowski@protonmail.com>
Sat, 28 Sep 2019 12:49:51 +0000 (15:49 +0300)
src/librustc_typeck/check/expr.rs

index dae4db4003aeb714887648046390cf36f00933e6..f59f908465dd378a90991a6105985ba072b92dcc 100644 (file)
@@ -1390,33 +1390,7 @@ fn check_field(
         } else if self.method_exists(field, expr_t, expr.hir_id, true) {
             self.ban_take_value_of_method(expr, expr_t, field);
         } else if !expr_t.is_primitive_ty() {
-            let mut err = self.no_such_field_err(field.span, field, expr_t);
-
-            match expr_t.peel_refs().kind {
-                ty::Array(_, len) => {
-                    self.maybe_suggest_array_indexing(&mut err, expr, base, field, len);
-                }
-                ty::RawPtr(..) => {
-                    self.suggest_first_deref_field(&mut err, expr, base, field);
-                }
-                ty::Adt(def, _) if !def.is_enum() => {
-                    self.suggest_fields_on_recordish(&mut err, def, field);
-                }
-                ty::Param(param_ty) => {
-                    self.point_at_param_definition(&mut err, param_ty);
-                }
-                _ => {}
-            }
-
-            if field.name == kw::Await {
-                // We know by construction that `<expr>.await` is either on Rust 2015
-                // or results in `ExprKind::Await`. Suggest switching the edition to 2018.
-                err.note("to `.await` a `Future`, switch to Rust 2018");
-                err.help("set `edition = \"2018\"` in `Cargo.toml`");
-                err.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
-            }
-
-            err.emit();
+            self.ban_nonexisting_field(field, base, expr, expr_t);
         } else {
             type_error_struct!(
                 self.tcx().sess,
@@ -1432,6 +1406,42 @@ fn check_field(
         self.tcx().types.err
     }
 
+    fn ban_nonexisting_field(
+        &self,
+        field: ast::Ident,
+        base: &'tcx hir::Expr,
+        expr: &'tcx hir::Expr,
+        expr_t: Ty<'tcx>,
+    ) {
+        let mut err = self.no_such_field_err(field.span, field, expr_t);
+
+        match expr_t.peel_refs().kind {
+            ty::Array(_, len) => {
+                self.maybe_suggest_array_indexing(&mut err, expr, base, field, len);
+            }
+            ty::RawPtr(..) => {
+                self.suggest_first_deref_field(&mut err, expr, base, field);
+            }
+            ty::Adt(def, _) if !def.is_enum() => {
+                self.suggest_fields_on_recordish(&mut err, def, field);
+            }
+            ty::Param(param_ty) => {
+                self.point_at_param_definition(&mut err, param_ty);
+            }
+            _ => {}
+        }
+
+        if field.name == kw::Await {
+            // We know by construction that `<expr>.await` is either on Rust 2015
+            // or results in `ExprKind::Await`. Suggest switching the edition to 2018.
+            err.note("to `.await` a `Future`, switch to Rust 2018");
+            err.help("set `edition = \"2018\"` in `Cargo.toml`");
+            err.note("for more on editions, read https://doc.rust-lang.org/edition-guide");
+        }
+
+        err.emit();
+    }
+
     fn ban_private_field_access(
         &self,
         expr: &hir::Expr,