From 4a1772ea92ce7949beade6c9d2f110e64de30aa0 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Mon, 13 Apr 2020 15:16:43 +0100 Subject: [PATCH] Factor the code that generates TyErrs --- src/librustc_mir_build/hair/pattern/_match.rs | 47 ++++++++++--------- 1 file 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 01e3bacda7531..4bc6166041e83 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() } }