Factor the code that generates TyErrs

This commit is contained in:
Nadrieril 2020-04-13 15:16:43 +01:00
parent e65d49d338
commit 4a1772ea92

View File

@ -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()
}
}