Use if-let instead of match with guard

This commit is contained in:
Michael Goulet 2022-06-11 09:57:33 -07:00
parent 6c00e54667
commit d9ddaf0d6f

View File

@ -41,8 +41,7 @@ use rustc_infer::infer::type_variable::{TypeVariableOrigin, TypeVariableOriginKi
use rustc_infer::infer::InferOk; use rustc_infer::infer::InferOk;
use rustc_middle::middle::stability; use rustc_middle::middle::stability;
use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase}; use rustc_middle::ty::adjustment::{Adjust, Adjustment, AllowTwoPhase};
use rustc_middle::ty::error::ExpectedFound; use rustc_middle::ty::error::TypeError::FieldMisMatch;
use rustc_middle::ty::error::TypeError::{FieldMisMatch, Sorts};
use rustc_middle::ty::subst::SubstsRef; use rustc_middle::ty::subst::SubstsRef;
use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TypeFoldable}; use rustc_middle::ty::{self, AdtKind, DefIdTree, Ty, TypeFoldable};
use rustc_session::parse::feature_err; use rustc_session::parse::feature_err;
@ -65,7 +64,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
expr: &'tcx hir::Expr<'tcx>, expr: &'tcx hir::Expr<'tcx>,
expected: Ty<'tcx>, expected: Ty<'tcx>,
extend_err: impl Fn(&mut Diagnostic), extend_err: impl FnMut(&mut Diagnostic),
) -> Ty<'tcx> { ) -> Ty<'tcx> {
self.check_expr_meets_expectation_or_error(expr, ExpectHasType(expected), extend_err) self.check_expr_meets_expectation_or_error(expr, ExpectHasType(expected), extend_err)
} }
@ -74,7 +73,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
&self, &self,
expr: &'tcx hir::Expr<'tcx>, expr: &'tcx hir::Expr<'tcx>,
expected: Expectation<'tcx>, expected: Expectation<'tcx>,
extend_err: impl Fn(&mut Diagnostic), mut extend_err: impl FnMut(&mut Diagnostic),
) -> Ty<'tcx> { ) -> Ty<'tcx> {
let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool); let expected_ty = expected.to_option(&self).unwrap_or(self.tcx.types.bool);
let mut ty = self.check_expr_with_expectation(expr, expected); let mut ty = self.check_expr_with_expectation(expr, expected);
@ -1557,8 +1556,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// FIXME: We are currently creating two branches here in order to maintain // FIXME: We are currently creating two branches here in order to maintain
// consistency. But they should be merged as much as possible. // consistency. But they should be merged as much as possible.
let fru_tys = if self.tcx.features().type_changing_struct_update { let fru_tys = if self.tcx.features().type_changing_struct_update {
match adt_ty.kind() { if let ty::Adt(adt, substs) = adt_ty.kind() && adt.is_struct() {
ty::Adt(adt, substs) if adt.is_struct() => {
// Make an ADT with fresh inference substitutions. This // Make an ADT with fresh inference substitutions. This
// will allow us to guide inference along so that, e.g. // will allow us to guide inference along so that, e.g.
// ``` // ```
@ -1572,21 +1570,23 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
// will allow us to then do a subtyping relation on all // will allow us to then do a subtyping relation on all
// of the `remaining_fields` below, per the RFC. // of the `remaining_fields` below, per the RFC.
let fresh_substs = self.fresh_substs_for_item(base_expr.span, adt.did()); let fresh_substs = self.fresh_substs_for_item(base_expr.span, adt.did());
let fresh_base_ty = self.tcx.mk_adt(*adt, fresh_substs);
let base_ty = self.check_expr_has_type_or_error( let base_ty = self.check_expr_has_type_or_error(
base_expr, base_expr,
self.tcx.mk_adt(*adt, fresh_substs), fresh_base_ty,
|_| {}, |_| {
error_happened = true;
},
); );
let base_ty = self.shallow_resolve(base_ty); let base_ty = self.shallow_resolve(base_ty);
match base_ty.kind() { if let ty::Adt(base_adt, base_substs) = base_ty.kind() && adt == base_adt {
ty::Adt(base_adt, base_subs) if adt == base_adt => {
variant variant
.fields .fields
.iter() .iter()
.map(|f| { .map(|f| {
let fru_ty = self.normalize_associated_types_in( let fru_ty = self.normalize_associated_types_in(
expr_span, expr_span,
self.field_ty(base_expr.span, f, base_subs), self.field_ty(base_expr.span, f, base_substs),
); );
let ident = self let ident = self
.tcx .tcx
@ -1603,8 +1603,7 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.register_predicates(obligations) self.register_predicates(obligations)
} }
// FIXME: Need better diagnostics for `FieldMisMatch` error // FIXME: Need better diagnostics for `FieldMisMatch` error
Err(type_error) => { Err(_) => {
debug!("check_expr_struct_fields: {fru_ty} sub {target_ty} failed: {type_error:?}");
self.report_mismatched_types( self.report_mismatched_types(
&cause, &cause,
target_ty, target_ty,
@ -1618,20 +1617,13 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
self.resolve_vars_if_possible(fru_ty) self.resolve_vars_if_possible(fru_ty)
}) })
.collect() .collect()
} else {
if !error_happened && !base_ty.references_error() {
span_bug!(base_expr.span, "expected an error to have been reported in `check_expr_has_type_or_error`");
} }
_ => {
self.report_mismatched_types(
&self.misc(base_expr.span),
adt_ty,
base_ty,
Sorts(ExpectedFound::new(true, adt_ty, base_ty)),
)
.emit();
return; return;
} }
} } else {
}
_ => {
// Check the base_expr, regardless of a bad expected adt_ty, so we can get // Check the base_expr, regardless of a bad expected adt_ty, so we can get
// type errors on that expression, too. // type errors on that expression, too.
self.check_expr(base_expr); self.check_expr(base_expr);
@ -1640,7 +1632,6 @@ impl<'a, 'tcx> FnCtxt<'a, 'tcx> {
.emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span }); .emit_err(FunctionalRecordUpdateOnNonStruct { span: base_expr.span });
return; return;
} }
}
} else { } else {
self.check_expr_has_type_or_error(base_expr, adt_ty, |_| { self.check_expr_has_type_or_error(base_expr, adt_ty, |_| {
let base_ty = self.typeck_results.borrow().expr_ty(*base_expr); let base_ty = self.typeck_results.borrow().expr_ty(*base_expr);