Skip to content

Commit

Permalink
Factor the code that generates TyErrs
Browse files Browse the repository at this point in the history
  • Loading branch information
Nadrieril committed May 17, 2020
1 parent e65d49d commit 4a1772e
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions src/librustc_mir_build/hair/pattern/_match.rs
Expand Up @@ -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()
}
}
Expand Down

0 comments on commit 4a1772e

Please sign in to comment.