1
Fork 0

Reduce indentation by replacing match arm w/ early return.

This commit is contained in:
jumbatm 2020-08-16 10:50:20 +10:00
parent 80c2c80d52
commit bca48ad7ac

View file

@ -2198,45 +2198,46 @@ impl ClashingExternDeclarations {
debug!("structurally_same_type_impl(cx, a = {:?}, b = {:?})", a, b); debug!("structurally_same_type_impl(cx, a = {:?}, b = {:?})", a, b);
match seen_types.get(a, b) { match seen_types.get(a, b) {
// If we've already computed the result, just return the memoized result. // If we've already computed the result, just return the memoized result.
SeenSetResult::Computed(result) => result, SeenSetResult::Computed(result) => return result,
// We are already in the process of computing structural sameness for this type, // We are already in the process of computing structural sameness for this type,
// meaning we've found a cycle. The types are structurally same, then. // meaning we've found a cycle. The types are structurally same, then.
SeenSetResult::Computing => true, SeenSetResult::Computing => return true,
// We haven't seen this combination of types at all -- compute their sameness. // We haven't seen this combination of types at all -- continue on to computing
SeenSetResult::Unseen => { // their sameness.
seen_types.mark_computing(a, b); SeenSetResult::Unseen => (),
let tcx = cx.tcx; }
let result = if a == b || rustc_middle::ty::TyS::same_type(a, b) { seen_types.mark_computing(a, b);
// All nominally-same types are structurally same, too. let tcx = cx.tcx;
true let result = if a == b || rustc_middle::ty::TyS::same_type(a, b) {
} else { // All nominally-same types are structurally same, too.
// Do a full, depth-first comparison between the two. true
use rustc_middle::ty::TyKind::*; } else {
let a_kind = &a.kind; // Do a full, depth-first comparison between the two.
let b_kind = &b.kind; use rustc_middle::ty::TyKind::*;
let a_kind = &a.kind;
let b_kind = &b.kind;
let compare_layouts = |a, b| -> bool { let compare_layouts = |a, b| -> bool {
let a_layout = &cx.layout_of(a).unwrap().layout.abi; let a_layout = &cx.layout_of(a).unwrap().layout.abi;
let b_layout = &cx.layout_of(b).unwrap().layout.abi; let b_layout = &cx.layout_of(b).unwrap().layout.abi;
debug!("{:?} == {:?} = {}", a_layout, b_layout, a_layout == b_layout); debug!("{:?} == {:?} = {}", a_layout, b_layout, a_layout == b_layout);
a_layout == b_layout a_layout == b_layout
}; };
#[allow(rustc::usage_of_ty_tykind)] #[allow(rustc::usage_of_ty_tykind)]
let is_primitive_or_pointer = |kind: &ty::TyKind<'_>| { let is_primitive_or_pointer =
kind.is_primitive() || matches!(kind, RawPtr(..)) |kind: &ty::TyKind<'_>| kind.is_primitive() || matches!(kind, RawPtr(..));
};
match (a_kind, b_kind) { match (a_kind, b_kind) {
(Adt(a_def, a_substs), Adt(b_def, b_substs)) => { (Adt(a_def, a_substs), Adt(b_def, b_substs)) => {
let a = a.subst(cx.tcx, a_substs); let a = a.subst(cx.tcx, a_substs);
let b = b.subst(cx.tcx, b_substs); let b = b.subst(cx.tcx, b_substs);
debug!("Comparing {:?} and {:?}", a, b); debug!("Comparing {:?} and {:?}", a, b);
// Grab a flattened representation of all fields. // Grab a flattened representation of all fields.
let a_fields = a_def.variants.iter().flat_map(|v| v.fields.iter()); let a_fields = a_def.variants.iter().flat_map(|v| v.fields.iter());
let b_fields = b_def.variants.iter().flat_map(|v| v.fields.iter()); let b_fields = b_def.variants.iter().flat_map(|v| v.fields.iter());
compare_layouts(a, b) compare_layouts(a, b)
&& a_fields.eq_by( && a_fields.eq_by(
b_fields, b_fields,
|&ty::FieldDef { did: a_did, .. }, |&ty::FieldDef { did: a_did, .. },
@ -2250,99 +2251,91 @@ impl ClashingExternDeclarations {
) )
}, },
) )
} }
(Array(a_ty, a_const), Array(b_ty, b_const)) => { (Array(a_ty, a_const), Array(b_ty, b_const)) => {
// For arrays, we also check the constness of the type. // For arrays, we also check the constness of the type.
a_const.val == b_const.val a_const.val == b_const.val
&& structurally_same_type_impl( && structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
seen_types, cx, a_ty, b_ty, ckind, }
) (Slice(a_ty), Slice(b_ty)) => {
} structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
(Slice(a_ty), Slice(b_ty)) => { }
structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind) (RawPtr(a_tymut), RawPtr(b_tymut)) => {
} a_tymut.mutbl == b_tymut.mutbl
(RawPtr(a_tymut), RawPtr(b_tymut)) => { && structurally_same_type_impl(
a_tymut.mutbl == b_tymut.mutbl seen_types,
&& structurally_same_type_impl( cx,
seen_types, &a_tymut.ty,
cx, &b_tymut.ty,
&a_tymut.ty, ckind,
&b_tymut.ty, )
ckind, }
) (Ref(_a_region, a_ty, a_mut), Ref(_b_region, b_ty, b_mut)) => {
} // For structural sameness, we don't need the region to be same.
(Ref(_a_region, a_ty, a_mut), Ref(_b_region, b_ty, b_mut)) => { a_mut == b_mut
// For structural sameness, we don't need the region to be same. && structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
a_mut == b_mut }
&& structurally_same_type_impl( (FnDef(..), FnDef(..)) => {
seen_types, cx, a_ty, b_ty, ckind, let a_poly_sig = a.fn_sig(tcx);
) let b_poly_sig = b.fn_sig(tcx);
}
(FnDef(..), FnDef(..)) => {
let a_poly_sig = a.fn_sig(tcx);
let b_poly_sig = b.fn_sig(tcx);
// As we don't compare regions, skip_binder is fine. // As we don't compare regions, skip_binder is fine.
let a_sig = a_poly_sig.skip_binder(); let a_sig = a_poly_sig.skip_binder();
let b_sig = b_poly_sig.skip_binder(); let b_sig = b_poly_sig.skip_binder();
(a_sig.abi, a_sig.unsafety, a_sig.c_variadic) (a_sig.abi, a_sig.unsafety, a_sig.c_variadic)
== (b_sig.abi, b_sig.unsafety, b_sig.c_variadic) == (b_sig.abi, b_sig.unsafety, b_sig.c_variadic)
&& a_sig.inputs().iter().eq_by(b_sig.inputs().iter(), |a, b| { && a_sig.inputs().iter().eq_by(b_sig.inputs().iter(), |a, b| {
structurally_same_type_impl(seen_types, cx, a, b, ckind) structurally_same_type_impl(seen_types, cx, a, b, ckind)
}) })
&& structurally_same_type_impl( && structurally_same_type_impl(
seen_types, seen_types,
cx, cx,
a_sig.output(), a_sig.output(),
b_sig.output(), b_sig.output(),
ckind, ckind,
) )
} }
(Tuple(a_substs), Tuple(b_substs)) => { (Tuple(a_substs), Tuple(b_substs)) => {
a_substs.types().eq_by(b_substs.types(), |a_ty, b_ty| { a_substs.types().eq_by(b_substs.types(), |a_ty, b_ty| {
structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind) structurally_same_type_impl(seen_types, cx, a_ty, b_ty, ckind)
}) })
} }
// For these, it's not quite as easy to define structural-sameness quite so easily. // For these, it's not quite as easy to define structural-sameness quite so easily.
// For the purposes of this lint, take the conservative approach and mark them as // For the purposes of this lint, take the conservative approach and mark them as
// not structurally same. // not structurally same.
(Dynamic(..), Dynamic(..)) (Dynamic(..), Dynamic(..))
| (Error(..), Error(..)) | (Error(..), Error(..))
| (Closure(..), Closure(..)) | (Closure(..), Closure(..))
| (Generator(..), Generator(..)) | (Generator(..), Generator(..))
| (GeneratorWitness(..), GeneratorWitness(..)) | (GeneratorWitness(..), GeneratorWitness(..))
| (Projection(..), Projection(..)) | (Projection(..), Projection(..))
| (Opaque(..), Opaque(..)) => false, | (Opaque(..), Opaque(..)) => false,
// These definitely should have been caught above. // These definitely should have been caught above.
(Bool, Bool) | (Char, Char) | (Never, Never) | (Str, Str) => { (Bool, Bool) | (Char, Char) | (Never, Never) | (Str, Str) => unreachable!(),
unreachable!()
}
// An Adt and a primitive type. This can be FFI-safe is the ADT is an enum with a // An Adt and a primitive type. This can be FFI-safe is the ADT is an enum with a
// non-null field. // non-null field.
(Adt(..), other_kind) | (other_kind, Adt(..)) (Adt(..), other_kind) | (other_kind, Adt(..))
if is_primitive_or_pointer(other_kind) => if is_primitive_or_pointer(other_kind) =>
{ {
let (primitive, adt) = let (primitive, adt) =
if is_primitive_or_pointer(&a.kind) { (a, b) } else { (b, a) }; if is_primitive_or_pointer(&a.kind) { (a, b) } else { (b, a) };
if let Some(ty) = crate::types::repr_nullable_ptr(cx, adt, ckind) { if let Some(ty) = crate::types::repr_nullable_ptr(cx, adt, ckind) {
ty == primitive ty == primitive
} else { } else {
compare_layouts(a, b) compare_layouts(a, b)
}
}
// Otherwise, just compare the layouts. This may fail to lint for some
// incompatible types, but at the very least, will stop reads into
// uninitialised memory.
_ => compare_layouts(a, b),
} }
}; }
seen_types.mark_computed(a, b, result); // Otherwise, just compare the layouts. This may fail to lint for some
result // incompatible types, but at the very least, will stop reads into
// uninitialised memory.
_ => compare_layouts(a, b),
} }
} };
seen_types.mark_computed(a, b, result);
result
} }
let mut seen_types = SeenSet::new(); let mut seen_types = SeenSet::new();
structurally_same_type_impl(&mut seen_types, cx, a, b, ckind) structurally_same_type_impl(&mut seen_types, cx, a, b, ckind)