Skip to content

Commit f95cbac

Browse files
Nadrierilmark-i-m
authored andcommitted
Inline cx methods used only once
1 parent af175e0 commit f95cbac

File tree

1 file changed

+12
-51
lines changed

1 file changed

+12
-51
lines changed

src/librustc_mir_build/hair/pattern/_match.rs

Lines changed: 12 additions & 51 deletions
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ use rustc_hir::{HirId, RangeEnd};
242242
use rustc_middle::mir::interpret::{truncate, AllocId, ConstValue, Pointer, Scalar};
243243
use rustc_middle::mir::Field;
244244
use rustc_middle::ty::layout::IntegerExt;
245-
use rustc_middle::ty::{self, Const, FieldDef, Ty, TyCtxt, VariantDef};
245+
use rustc_middle::ty::{self, Const, Ty, TyCtxt};
246246
use rustc_session::lint;
247247
use rustc_span::{Span, DUMMY_SP};
248248
use rustc_target::abi::{Integer, Size, VariantIdx};
@@ -614,36 +614,6 @@ impl<'a, 'tcx> MatchCheckCtxt<'a, 'tcx> {
614614
_ => false,
615615
}
616616
}
617-
618-
/// Returns whether the given variant is from another crate and has its fields declared
619-
/// `#[non_exhaustive]`.
620-
fn is_foreign_non_exhaustive_variant(&self, ty: Ty<'tcx>, variant: &VariantDef) -> bool {
621-
match ty.kind {
622-
ty::Adt(def, ..) => variant.is_field_list_non_exhaustive() && !def.did.is_local(),
623-
_ => false,
624-
}
625-
}
626-
627-
/// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
628-
/// uninhabited fields in order not to reveal the uninhabitedness of the whole variant.
629-
fn hide_uninhabited_field(
630-
&self,
631-
adt_ty: Ty<'tcx>,
632-
variant: &VariantDef,
633-
field: &FieldDef,
634-
) -> bool {
635-
match adt_ty.kind {
636-
ty::Adt(adt, substs) => {
637-
let is_non_exhaustive = self.is_foreign_non_exhaustive_variant(adt_ty, variant);
638-
let field_ty = field.ty(self.tcx, substs);
639-
let is_visible =
640-
adt.is_enum() || field.vis.is_accessible_from(self.module, self.tcx);
641-
let is_uninhabited = self.is_uninhabited(field_ty);
642-
is_uninhabited && (!is_visible || is_non_exhaustive)
643-
}
644-
_ => false,
645-
}
646-
}
647617
}
648618

649619
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
@@ -789,21 +759,6 @@ impl<'tcx> Constructor<'tcx> {
789759
}
790760
}
791761

792-
/// Returns whether the fields of this constructor should be treated as `non_exhaustive`.
793-
fn is_field_list_non_exhaustive<'a>(
794-
&self,
795-
cx: &MatchCheckCtxt<'a, 'tcx>,
796-
ty: Ty<'tcx>,
797-
) -> bool {
798-
match ty.kind {
799-
ty::Adt(adt, _) => {
800-
let variant = &adt.variants[self.variant_index_for_adt(cx, adt)];
801-
cx.is_foreign_non_exhaustive_variant(ty, variant)
802-
}
803-
_ => false,
804-
}
805-
}
806-
807762
fn variant_index_for_adt<'a>(
808763
&self,
809764
cx: &MatchCheckCtxt<'a, 'tcx>,
@@ -1000,7 +955,6 @@ impl<'tcx> Constructor<'tcx> {
1000955
#[derive(Debug, Clone)]
1001956
struct StructFields<'p, 'tcx> {
1002957
fields: SmallVec<[StructField<'p, 'tcx>; 2]>,
1003-
is_non_exhaustive: bool,
1004958
}
1005959

1006960
#[derive(Debug, Copy, Clone)]
@@ -1051,13 +1005,21 @@ impl<'p, 'tcx> StructFields<'p, 'tcx> {
10511005
smallvec![StructField::wildcard_from_ty(cx, substs.type_at(0))]
10521006
} else {
10531007
let variant = &adt.variants[constructor.variant_index_for_adt(cx, adt)];
1008+
// Whether we must not match the fields of this variant exhaustively.
1009+
let is_non_exhaustive =
1010+
variant.is_field_list_non_exhaustive() && !adt.did.is_local();
10541011
variant
10551012
.fields
10561013
.iter()
10571014
.map(|field| {
10581015
let field_ty = field.ty(cx.tcx, substs);
1059-
// Filter out hidden fields so we don't know they are uninhabited.
1060-
if cx.hide_uninhabited_field(ty, variant, field) {
1016+
let is_visible =
1017+
adt.is_enum() || field.vis.is_accessible_from(cx.module, cx.tcx);
1018+
let is_uninhabited = cx.is_uninhabited(field_ty);
1019+
1020+
// In the cases of either a `#[non_exhaustive]` field list or a non-public field, we hide
1021+
// uninhabited fields in order not to reveal the uninhabitedness of the whole variant.
1022+
if is_uninhabited && (!is_visible || is_non_exhaustive) {
10611023
StructField::Hidden(field_ty)
10621024
} else {
10631025
StructField::wildcard_from_ty(cx, field_ty)
@@ -1068,8 +1030,7 @@ impl<'p, 'tcx> StructFields<'p, 'tcx> {
10681030
}
10691031
_ => smallvec![],
10701032
};
1071-
let is_non_exhaustive = constructor.is_field_list_non_exhaustive(cx, ty);
1072-
StructFields { fields, is_non_exhaustive }
1033+
StructFields { fields }
10731034
}
10741035

10751036
/// Number of (filtered) patterns contained.

0 commit comments

Comments
 (0)