Skip to content

Fully destructure slice and array constants into patterns #77390

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

Closed
wants to merge 7 commits into from
Closed
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
7 changes: 3 additions & 4 deletions compiler/rustc_mir_build/src/thir/pattern/_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1485,9 +1485,7 @@ fn all_constructors<'a, 'tcx>(
)
};
match *pcx.ty.kind() {
ty::Bool => {
[true, false].iter().map(|&b| ConstantValue(ty::Const::from_bool(cx.tcx, b))).collect()
}
ty::Bool => vec![make_range(0, 1)],
ty::Array(ref sub_ty, len) if len.try_eval_usize(cx.tcx, cx.param_env).is_some() => {
Copy link
Member

@Nadrieril Nadrieril Oct 12, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't this affect diagnostics? I'd expect this would display "pattern 0 not covered" instead of "pattern false not covered", but that would clearly be caught by the tests. I don't understand what piece of code makes it so it doesn't.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I had also wondered this, but assumed this would be caught by tests, so it must have been okay. But you're right that it would be better to understand this (and add a comment).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because while decisions are made on Constructors, they do not directly flow into diagnostics. Instead diagnostics are reported on Pats in https://github.com/rust-lang/rust/blob/6553d38bcb0c9193808ae93db8bd95bcf4c4824a/compiler/rustc_mir_build/src/thir/pattern/check_match.rs

let len = len.eval_usize(cx.tcx, cx.param_env);
if len != 0 && cx.is_uninhabited(sub_ty) {
Expand Down Expand Up @@ -1600,7 +1598,7 @@ impl<'tcx> IntRange<'tcx> {
#[inline]
fn is_integral(ty: Ty<'_>) -> bool {
match ty.kind() {
ty::Char | ty::Int(_) | ty::Uint(_) => true,
ty::Char | ty::Int(_) | ty::Uint(_) | ty::Bool => true,
_ => false,
}
}
Expand All @@ -1622,6 +1620,7 @@ impl<'tcx> IntRange<'tcx> {
#[inline]
fn integral_size_and_signed_bias(tcx: TyCtxt<'tcx>, ty: Ty<'_>) -> Option<(Size, u128)> {
match *ty.kind() {
ty::Bool => Some((Size::from_bytes(1), 0)),
ty::Char => Some((Size::from_bytes(4), 0)),
ty::Int(ity) => {
let size = Integer::from_attr(&tcx, SignedInt(ity)).size();
Expand Down