about summary refs log tree commit diff
diff options
context:
space:
mode:
authorNadrieril <nadrieril+git@gmail.com>2020-04-13 15:16:43 +0100
committerNadrieril <nadrieril+git@gmail.com>2020-05-17 17:38:19 +0100
commit4a1772ea92ce7949beade6c9d2f110e64de30aa0 (patch)
tree6500b577c1cfb15594bb6ffc71bd61a96915f6b1
parente65d49d33860befebb3c5b2a13907983d57929a3 (diff)
downloadrust-4a1772ea92ce7949beade6c9d2f110e64de30aa0.tar.gz
rust-4a1772ea92ce7949beade6c9d2f110e64de30aa0.zip
Factor the code that generates TyErrs
-rw-r--r--src/librustc_mir_build/hair/pattern/_match.rs47
1 files changed, 24 insertions, 23 deletions
diff --git a/src/librustc_mir_build/hair/pattern/_match.rs b/src/librustc_mir_build/hair/pattern/_match.rs
index 01e3bacda75..4bc6166041e 100644
--- a/src/librustc_mir_build/hair/pattern/_match.rs
+++ b/src/librustc_mir_build/hair/pattern/_match.rs
@@ -877,36 +877,37 @@ impl<'tcx> Constructor<'tcx> {
                             .fields
                             .iter()
                             .map(|field| {
+                                let ty = field.ty(cx.tcx, substs);
                                 let is_visible = adt.is_enum()
                                     || field.vis.is_accessible_from(cx.module, cx.tcx);
-                                let is_uninhabited = cx.is_uninhabited(field.ty(cx.tcx, substs));
-                                match (is_visible, is_non_exhaustive, is_uninhabited) {
-                                    // Treat all uninhabited types in non-exhaustive variants as
-                                    // `TyErr`.
-                                    (_, true, true) => cx.tcx.types.err,
-                                    // Treat all non-visible fields as `TyErr`. They can't appear
-                                    // in any other pattern from this match (because they are
-                                    // private), so their type does not matter - but we don't want
-                                    // to know they are uninhabited.
-                                    (false, ..) => cx.tcx.types.err,
-                                    (true, ..) => {
-                                        let ty = field.ty(cx.tcx, substs);
-                                        match ty.kind {
-                                            // If the field type returned is an array of an unknown
-                                            // size return an TyErr.
-                                            ty::Array(_, len)
-                                                if len
+                                let is_uninhabited = cx.is_uninhabited(ty);
+                                // Treat all non-visible fields as `TyErr`. They can't appear
+                                // in any other pattern from this match (because they are
+                                // private), so their type does not matter - but we don't want
+                                // to know they are uninhabited.
+                                let allowed_to_inspect = is_visible
+                                    && match (is_non_exhaustive, is_uninhabited) {
+                                        // Treat all uninhabited types in non-exhaustive variants as
+                                        // `TyErr`.
+                                        (true, true) => false,
+                                        (_, _) => {
+                                            match ty.kind {
+                                                // If the field type returned is an array of an unknown
+                                                // size return an TyErr.
+                                                ty::Array(_, len) => len
                                                     .try_eval_usize(cx.tcx, cx.param_env)
-                                                    .is_none() =>
-                                            {
-                                                cx.tcx.types.err
+                                                    .is_some(),
+                                                _ => true,
                                             }
-                                            _ => ty,
                                         }
-                                    }
+                                    };
+
+                                if allowed_to_inspect {
+                                    Pat::wildcard_from_ty(ty)
+                                } else {
+                                    Pat::wildcard_from_ty(cx.tcx.types.err)
                                 }
                             })
-                            .map(Pat::wildcard_from_ty)
                             .collect()
                     }
                 }