diff options
| author | Wojciech Baranowski <wbaranowski@protonmail.com> | 2019-09-28 15:26:00 +0300 |
|---|---|---|
| committer | Wojciech Baranowski <wbaranowski@protonmail.com> | 2019-09-28 15:49:51 +0300 |
| commit | 9ad99c30cbf344b763602698b67c04ec3ce3de56 (patch) | |
| tree | 254e7ede4f335fd0b44036356548ae27fb5fade7 | |
| parent | f3744a1b3dd1c3d6a27a6061fc1866194a453ee2 (diff) | |
| download | rust-9ad99c30cbf344b763602698b67c04ec3ce3de56.tar.gz rust-9ad99c30cbf344b763602698b67c04ec3ce3de56.zip | |
Refactor into ban_nonexisting_field method
| -rw-r--r-- | src/librustc_typeck/check/expr.rs | 64 |
1 files changed, 37 insertions, 27 deletions
diff --git a/src/librustc_typeck/check/expr.rs b/src/librustc_typeck/check/expr.rs index dae4db4003a..f59f908465d 100644 --- a/src/librustc_typeck/check/expr.rs +++ b/src/librustc_typeck/check/expr.rs @@ -1390,33 +1390,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { } 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 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> { 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, |
