Skip to content

Commit e3d9984

Browse files
committed
Auto merge of #66129 - Nadrieril:refactor-slice-pat-usefulness, r=varkor
Refactor slice pattern usefulness checking As a follow up to #65874, this PR changes how variable-length slice patterns are handled in usefulness checking. The objectives are: cleaning up that code to make it easier to understand, and paving the way to handling fixed-length slices more cleverly too, for #53820. Before this, variable-length slice patterns were eagerly expanded into a union of fixed-length slices. Now they have their own special constructor, which allows expanding them a bit more lazily. As a nice side-effect, this improves diagnostics. This PR shows a slight performance improvement, mostly due to 149792b. This will probably have to be reverted in some way when we implement or-patterns.
2 parents e931f00 + fd9921b commit e3d9984

21 files changed

+627
-264
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 344 additions & 249 deletions
Large diffs are not rendered by default.

src/librustc_mir/hair/pattern/check_match.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc::hir::{self, Pat};
1818

1919
use std::slice;
2020

21-
use syntax_pos::{MultiSpan, Span, DUMMY_SP};
21+
use syntax_pos::{MultiSpan, Span};
2222

2323
crate fn check_match(tcx: TyCtxt<'_>, def_id: DefId) {
2424
let body_id = match tcx.hir().as_local_hir_id(def_id) {
@@ -491,7 +491,7 @@ fn check_not_useful(
491491
matrix: &Matrix<'_, 'tcx>,
492492
hir_id: HirId,
493493
) -> Result<(), Vec<super::Pat<'tcx>>> {
494-
let wild_pattern = super::Pat { ty, span: DUMMY_SP, kind: box PatKind::Wild };
494+
let wild_pattern = super::Pat::wildcard_from_ty(ty);
495495
match is_useful(cx, matrix, &PatStack::from_pattern(&wild_pattern), ConstructWitness, hir_id) {
496496
NotUseful => Ok(()), // This is good, wildcard pattern isn't reachable.
497497
UsefulWithWitness(pats) => Err(if pats.is_empty() {

src/librustc_mir/hair/pattern/mod.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ use rustc_index::vec::Idx;
2626
use std::cmp::Ordering;
2727
use std::fmt;
2828
use syntax::ast;
29-
use syntax_pos::Span;
29+
use syntax_pos::{Span, DUMMY_SP};
3030

3131
#[derive(Clone, Debug)]
3232
pub enum PatternError {
@@ -55,6 +55,11 @@ pub struct Pat<'tcx> {
5555
pub kind: Box<PatKind<'tcx>>,
5656
}
5757

58+
impl<'tcx> Pat<'tcx> {
59+
pub(crate) fn wildcard_from_ty(ty: Ty<'tcx>) -> Self {
60+
Pat { ty, span: DUMMY_SP, kind: Box::new(PatKind::Wild) }
61+
}
62+
}
5863

5964
#[derive(Copy, Clone, Debug, PartialEq)]
6065
pub struct PatTyProj<'tcx> {

src/test/ui/consts/const_let_refutable.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0005]: refutable pattern in function argument: `&[]`, `&[_]` and `&[_, _, _]` not covered
1+
error[E0005]: refutable pattern in function argument: `&[]`, `&[_]` and `&[_, _, _, ..]` not covered
22
--> $DIR/const_let_refutable.rs:3:16
33
|
44
LL | const fn slice([a, b]: &[i32]) -> i32 {
5-
| ^^^^^^ patterns `&[]`, `&[_]` and `&[_, _, _]` not covered
5+
| ^^^^^^ patterns `&[]`, `&[_]` and `&[_, _, _, ..]` not covered
66

77
error[E0723]: can only call other `const fn` within a `const fn`, but `const <&i32 as std::ops::Add>::add` is not stable as `const fn`
88
--> $DIR/const_let_refutable.rs:4:5

src/test/ui/pattern/usefulness/match-byte-array-patterns-2.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ LL | match buf {
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

9-
error[E0004]: non-exhaustive patterns: `&[]`, `&[_]`, `&[_, _]` and 3 more not covered
9+
error[E0004]: non-exhaustive patterns: `&[..]` not covered
1010
--> $DIR/match-byte-array-patterns-2.rs:10:11
1111
|
1212
LL | match buf {
13-
| ^^^ patterns `&[]`, `&[_]`, `&[_, _]` and 3 more not covered
13+
| ^^^ pattern `&[..]` not covered
1414
|
1515
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
1616

src/test/ui/pattern/usefulness/match-slice-patterns.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
fn check(list: &[Option<()>]) {
44
match list {
5-
//~^ ERROR `&[_, Some(_), None, _]` not covered
5+
//~^ ERROR `&[_, Some(_), .., None, _]` not covered
66
&[] => {},
77
&[_] => {},
88
&[_, _] => {},

src/test/ui/pattern/usefulness/match-slice-patterns.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
1-
error[E0004]: non-exhaustive patterns: `&[_, Some(_), None, _]` not covered
1+
error[E0004]: non-exhaustive patterns: `&[_, Some(_), .., None, _]` not covered
22
--> $DIR/match-slice-patterns.rs:4:11
33
|
44
LL | match list {
5-
| ^^^^ pattern `&[_, Some(_), None, _]` not covered
5+
| ^^^^ pattern `&[_, Some(_), .., None, _]` not covered
66
|
77
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
88

src/test/ui/pattern/usefulness/non-exhaustive-match.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ fn main() {
4444
}
4545
let vec = vec![0.5f32];
4646
let vec: &[f32] = &vec;
47-
match *vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _]` not covered
47+
match *vec { //~ ERROR non-exhaustive patterns: `[_, _, _, _, ..]` not covered
4848
[0.1, 0.2, 0.3] => (),
4949
[0.1, 0.2] => (),
5050
[0.1] => (),

src/test/ui/pattern/usefulness/non-exhaustive-match.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -66,11 +66,11 @@ LL | match *vec {
6666
|
6767
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
6868

69-
error[E0004]: non-exhaustive patterns: `[_, _, _, _]` not covered
69+
error[E0004]: non-exhaustive patterns: `[_, _, _, _, ..]` not covered
7070
--> $DIR/non-exhaustive-match.rs:47:11
7171
|
7272
LL | match *vec {
73-
| ^^^^ pattern `[_, _, _, _]` not covered
73+
| ^^^^ pattern `[_, _, _, _, ..]` not covered
7474
|
7575
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
7676

0 commit comments

Comments
 (0)