Skip to content

Commit

Permalink
Refactor into ban_nonexisting_field method
Browse files Browse the repository at this point in the history
  • Loading branch information
Baranowski committed Sep 28, 2019
1 parent f3744a1 commit 9ad99c3
Showing 1 changed file with 37 additions and 27 deletions.
64 changes: 37 additions & 27 deletions src/librustc_typeck/check/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit 9ad99c3

Please sign in to comment.