Skip to content

De-abuse TyKind::Error in exhaustiveness checking #71930

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
May 21, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
Factor the code that generates TyErrs
  • Loading branch information
Nadrieril committed May 17, 2020
commit 4a1772ea92ce7949beade6c9d2f110e64de30aa0
47 changes: 24 additions & 23 deletions src/librustc_mir_build/hair/pattern/_match.rs
Original file line number Diff line number Diff line change
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