Skip to content

Commit 4a1772e

Browse files
committed
Factor the code that generates TyErrs
1 parent e65d49d commit 4a1772e

File tree

1 file changed

+24
-23
lines changed

1 file changed

+24
-23
lines changed

src/librustc_mir_build/hair/pattern/_match.rs

Lines changed: 24 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -877,36 +877,37 @@ impl<'tcx> Constructor<'tcx> {
877877
.fields
878878
.iter()
879879
.map(|field| {
880+
let ty = field.ty(cx.tcx, substs);
880881
let is_visible = adt.is_enum()
881882
|| field.vis.is_accessible_from(cx.module, cx.tcx);
882-
let is_uninhabited = cx.is_uninhabited(field.ty(cx.tcx, substs));
883-
match (is_visible, is_non_exhaustive, is_uninhabited) {
884-
// Treat all uninhabited types in non-exhaustive variants as
885-
// `TyErr`.
886-
(_, true, true) => cx.tcx.types.err,
887-
// Treat all non-visible fields as `TyErr`. They can't appear
888-
// in any other pattern from this match (because they are
889-
// private), so their type does not matter - but we don't want
890-
// to know they are uninhabited.
891-
(false, ..) => cx.tcx.types.err,
892-
(true, ..) => {
893-
let ty = field.ty(cx.tcx, substs);
894-
match ty.kind {
895-
// If the field type returned is an array of an unknown
896-
// size return an TyErr.
897-
ty::Array(_, len)
898-
if len
883+
let is_uninhabited = cx.is_uninhabited(ty);
884+
// Treat all non-visible fields as `TyErr`. They can't appear
885+
// in any other pattern from this match (because they are
886+
// private), so their type does not matter - but we don't want
887+
// to know they are uninhabited.
888+
let allowed_to_inspect = is_visible
889+
&& match (is_non_exhaustive, is_uninhabited) {
890+
// Treat all uninhabited types in non-exhaustive variants as
891+
// `TyErr`.
892+
(true, true) => false,
893+
(_, _) => {
894+
match ty.kind {
895+
// If the field type returned is an array of an unknown
896+
// size return an TyErr.
897+
ty::Array(_, len) => len
899898
.try_eval_usize(cx.tcx, cx.param_env)
900-
.is_none() =>
901-
{
902-
cx.tcx.types.err
899+
.is_some(),
900+
_ => true,
903901
}
904-
_ => ty,
905902
}
906-
}
903+
};
904+
905+
if allowed_to_inspect {
906+
Pat::wildcard_from_ty(ty)
907+
} else {
908+
Pat::wildcard_from_ty(cx.tcx.types.err)
907909
}
908910
})
909-
.map(Pat::wildcard_from_ty)
910911
.collect()
911912
}
912913
}

0 commit comments

Comments
 (0)