1
Fork 0

check_match: unify check_irrefutable & check_exhaustive more.

This commit is contained in:
Mazdak Farrokhzad 2019-09-09 16:44:06 +02:00
parent b36a206a30
commit 7d4665b097
19 changed files with 135 additions and 95 deletions

View file

@ -517,9 +517,9 @@ struct PatternContext<'tcx> {
pub struct Witness<'tcx>(Vec<Pattern<'tcx>>);
impl<'tcx> Witness<'tcx> {
pub fn single_pattern(&self) -> &Pattern<'tcx> {
pub fn single_pattern(self) -> Pattern<'tcx> {
assert_eq!(self.0.len(), 1);
&self.0[0]
self.0.into_iter().next().unwrap()
}
fn push_wild_constructor<'a>(

View file

@ -1,4 +1,4 @@
use super::_match::{MatchCheckCtxt, Matrix, Witness, expand_pattern, is_useful};
use super::_match::{MatchCheckCtxt, Matrix, expand_pattern, is_useful};
use super::_match::Usefulness::*;
use super::_match::WitnessPreference::*;
@ -276,26 +276,26 @@ impl<'tcx> MatchVisitor<'_, 'tcx> {
expand_pattern(cx, pattern)
]].into_iter().collect();
let witness = match check_not_useful(cx, pattern_ty, &pats) {
let witnesses = match check_not_useful(cx, pattern_ty, &pats) {
Ok(_) => return,
Err((witness, _)) => witness,
Err(err) => err,
};
let pattern_string = witness[0].single_pattern().to_string();
let joined_patterns = joined_uncovered_patterns(&witnesses);
let mut err = struct_span_err!(
self.tcx.sess, pat.span, E0005,
"refutable pattern in {}: `{}` not covered",
origin, pattern_string
"refutable pattern in {}: {} not covered",
origin, joined_patterns
);
err.span_label(pat.span, match pat.node {
PatKind::Path(hir::QPath::Resolved(None, ref path))
err.span_label(pat.span, match &pat.node {
PatKind::Path(hir::QPath::Resolved(None, path))
if path.segments.len() == 1 && path.segments[0].args.is_none() => {
format!("interpreted as {} {} pattern, not new variable",
path.res.article(), path.res.descr())
}
_ => format!("pattern `{}` not covered", pattern_string),
_ => pattern_not_convered_label(&witnesses, &joined_patterns),
});
adt_defined_here(cx, pattern_ty.peel_refs(), &mut err);
adt_defined_here(cx, &mut err, pattern_ty, &witnesses);
err.emit();
});
}
@ -437,11 +437,15 @@ fn check_not_useful(
cx: &mut MatchCheckCtxt<'_, 'tcx>,
ty: Ty<'tcx>,
matrix: &Matrix<'_, 'tcx>,
) -> Result<(), (Vec<Witness<'tcx>>, Pattern<'tcx>)> {
) -> Result<(), Vec<Pattern<'tcx>>> {
let wild_pattern = Pattern { ty, span: DUMMY_SP, kind: box PatternKind::Wild };
match is_useful(cx, matrix, &[&wild_pattern], ConstructWitness) {
NotUseful => Ok(()), // This is good, wildcard pattern isn't reachable.
UsefulWithWitness(pats) => Err((pats, wild_pattern)),
UsefulWithWitness(pats) => Err(if pats.is_empty() {
vec![wild_pattern]
} else {
pats.into_iter().map(|w| w.single_pattern()).collect()
}),
Useful => bug!(),
}
}
@ -452,42 +456,26 @@ fn check_exhaustive<'tcx>(
sp: Span,
matrix: &Matrix<'_, 'tcx>,
) {
let (pats, wild_pattern) = match check_not_useful(cx, scrut_ty, matrix) {
let witnesses = match check_not_useful(cx, scrut_ty, matrix) {
Ok(_) => return,
Err(err) => err,
};
let witnesses = if pats.is_empty() {
vec![&wild_pattern]
} else {
pats.iter().map(|w| w.single_pattern()).collect()
};
let joined_patterns = joined_uncovered_patterns(&witnesses);
let mut err = create_e0004(cx.tcx.sess, sp, format!(
"non-exhaustive patterns: {} not covered",
joined_patterns,
));
err.span_label(sp, match witnesses.len() {
1 => format!("pattern {} not covered", joined_patterns),
_ => format!("patterns {} not covered", joined_patterns),
});
// point at the definition of non-covered enum variants
let scrut_ty = scrut_ty.peel_refs();
adt_defined_here(cx, scrut_ty, &mut err);
let patterns = witnesses.iter().map(|p| (**p).clone()).collect::<Vec<Pattern<'_>>>();
if patterns.len() < 4 {
for sp in maybe_point_at_variant(scrut_ty, &patterns) {
err.span_label(sp, "not covered");
}
}
err.help("ensure that all possible cases are being handled, \
possibly by adding wildcards or more match arms");
err.emit();
let mut err = create_e0004(
cx.tcx.sess, sp,
format!("non-exhaustive patterns: {} not covered", joined_patterns),
);
err.span_label(sp, pattern_not_convered_label(&witnesses, &joined_patterns));
adt_defined_here(cx, &mut err, scrut_ty, &witnesses);
err.help(
"ensure that all possible cases are being handled, \
possibly by adding wildcards or more match arms"
)
.emit();
}
fn joined_uncovered_patterns(witnesses: &[&Pattern<'_>]) -> String {
fn joined_uncovered_patterns(witnesses: &[Pattern<'_>]) -> String {
const LIMIT: usize = 3;
match witnesses {
[] => bug!(),
@ -504,11 +492,31 @@ fn joined_uncovered_patterns(witnesses: &[&Pattern<'_>]) -> String {
}
}
fn adt_defined_here(cx: &mut MatchCheckCtxt<'_, '_>, ty: Ty<'_>, err: &mut DiagnosticBuilder<'_>) {
fn pattern_not_convered_label(witnesses: &[Pattern<'_>], joined_patterns: &str) -> String {
match witnesses.len() {
1 => format!("pattern {} not covered", joined_patterns),
_ => format!("patterns {} not covered", joined_patterns),
}
}
/// Point at the definition of non-covered `enum` variants.
fn adt_defined_here(
cx: &MatchCheckCtxt<'_, '_>,
err: &mut DiagnosticBuilder<'_>,
ty: Ty<'_>,
witnesses: &[Pattern<'_>],
) {
let ty = ty.peel_refs();
if let ty::Adt(def, _) = ty.sty {
if let Some(sp) = cx.tcx.hir().span_if_local(def.did) {
err.span_label(sp, format!("`{}` defined here", ty));
}
if witnesses.len() < 4 {
for sp in maybe_point_at_variant(ty, &witnesses) {
err.span_label(sp, "not covered");
}
}
}
}

View file

@ -1,8 +1,8 @@
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
--> $DIR/const-match-check.rs:25:15
|
LL | A = { let 0 = 0; 0 },
| ^ pattern `std::i32::MIN..=-1i32` not covered
| ^ patterns `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
error: aborting due to previous error

View file

@ -1,8 +1,8 @@
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
--> $DIR/const-match-check.rs:31:24
|
LL | let x: [i32; { let 0 = 0; 0 }] = [];
| ^ pattern `std::i32::MIN..=-1i32` not covered
| ^ patterns `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
error: aborting due to previous error

View file

@ -1,26 +1,26 @@
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
--> $DIR/const-match-check.rs:4:22
|
LL | const X: i32 = { let 0 = 0; 0 };
| ^ pattern `std::i32::MIN..=-1i32` not covered
| ^ patterns `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
--> $DIR/const-match-check.rs:8:23
|
LL | static Y: i32 = { let 0 = 0; 0 };
| ^ pattern `std::i32::MIN..=-1i32` not covered
| ^ patterns `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
--> $DIR/const-match-check.rs:13:26
|
LL | const X: i32 = { let 0 = 0; 0 };
| ^ pattern `std::i32::MIN..=-1i32` not covered
| ^ patterns `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` not covered
error[E0005]: refutable pattern in local binding: `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
--> $DIR/const-match-check.rs:19:26
|
LL | const X: i32 = { let 0 = 0; 0 };
| ^ pattern `std::i32::MIN..=-1i32` not covered
| ^ patterns `std::i32::MIN..=-1i32` and `1i32..=std::i32::MAX` not covered
error: aborting due to 4 previous errors

View file

@ -9,8 +9,8 @@ use foo::d;
const a: u8 = 2;
fn main() {
let a = 4; //~ ERROR refutable pattern in local binding: `0u8..=1u8` not covered
let c = 4; //~ ERROR refutable pattern in local binding: `0u8..=1u8` not covered
let d = 4; //~ ERROR refutable pattern in local binding: `0u8..=1u8` not covered
let a = 4; //~ ERROR refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX
let c = 4; //~ ERROR refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX
let d = 4; //~ ERROR refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX
fn f() {} // Check that the `NOTE`s still work with an item here (cf. issue #35115).
}

View file

@ -1,16 +1,16 @@
error[E0005]: refutable pattern in local binding: `0u8..=1u8` not covered
error[E0005]: refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX` not covered
--> $DIR/const-pattern-irrefutable.rs:12:9
|
LL | let a = 4;
| ^ interpreted as a constant pattern, not new variable
error[E0005]: refutable pattern in local binding: `0u8..=1u8` not covered
error[E0005]: refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX` not covered
--> $DIR/const-pattern-irrefutable.rs:13:9
|
LL | let c = 4;
| ^ interpreted as a constant pattern, not new variable
error[E0005]: refutable pattern in local binding: `0u8..=1u8` not covered
error[E0005]: refutable pattern in local binding: `0u8..=1u8` and `3u8..=std::u8::MAX` not covered
--> $DIR/const-pattern-irrefutable.rs:14:9
|
LL | let d = 4;

View file

@ -1,8 +1,8 @@
error[E0005]: refutable pattern in function argument: `&[]` not covered
error[E0005]: refutable pattern in function argument: `&[]`, `&[_]` and `&[_, _, _]` not covered
--> $DIR/const_let_refutable.rs:3:16
|
LL | const fn slice([a, b]: &[i32]) -> i32 {
| ^^^^^^ pattern `&[]` not covered
| ^^^^^^ patterns `&[]`, `&[_]` and `&[_, _, _]` not covered
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`
--> $DIR/const_let_refutable.rs:4:5

View file

@ -3,6 +3,7 @@ error[E0005]: refutable pattern in local binding: `T(_, _)` not covered
|
LL | / enum Helper<T, U> {
LL | | T(T, [!; 0]),
| | - not covered
LL | | #[allow(dead_code)]
LL | | U(U),
LL | | }

View file

@ -1,8 +1,8 @@
error[E0005]: refutable pattern in `for` loop binding: `&std::i32::MIN..=0i32` not covered
error[E0005]: refutable pattern in `for` loop binding: `&std::i32::MIN..=0i32` and `&2i32..=std::i32::MAX` not covered
--> $DIR/for-loop-refutable-pattern-error-message.rs:2:9
|
LL | for &1 in [1].iter() {}
| ^^ pattern `&std::i32::MIN..=0i32` not covered
| ^^ patterns `&std::i32::MIN..=0i32` and `&2i32..=std::i32::MAX` not covered
error: aborting due to previous error

View file

@ -2,7 +2,7 @@ fn main() {
let values: Vec<u8> = vec![1,2,3,4,5,6,7,8];
for &[x,y,z] in values.chunks(3).filter(|&xs| xs.len() == 3) {
//~^ ERROR refutable pattern in `for` loop binding: `&[]` not covered
//~^ ERROR refutable pattern in `for` loop binding: `&[]`, `&[_]`, `&[_, _]` and 1 more not
println!("y={}", y);
//~^ ERROR borrow of possibly-uninitialized variable: `y`
}

View file

@ -1,8 +1,8 @@
error[E0005]: refutable pattern in `for` loop binding: `&[]` not covered
error[E0005]: refutable pattern in `for` loop binding: `&[]`, `&[_]`, `&[_, _]` and 1 more not covered
--> $DIR/issue-15381.rs:4:9
|
LL | for &[x,y,z] in values.chunks(3).filter(|&xs| xs.len() == 3) {
| ^^^^^^^^ pattern `&[]` not covered
| ^^^^^^^^ patterns `&[]`, `&[_]`, `&[_, _]` and 1 more not covered
error[E0381]: borrow of possibly-uninitialized variable: `y`
--> $DIR/issue-15381.rs:6:26

View file

@ -6,5 +6,5 @@ enum Thing {
fn main() {
let Thing::Foo(y) = Thing::Foo(1);
//~^ ERROR refutable pattern in local binding: `Bar` not covered
//~^ ERROR refutable pattern in local binding: `Bar` and `Baz` not covered
}

View file

@ -1,15 +1,17 @@
error[E0005]: refutable pattern in local binding: `Bar` not covered
error[E0005]: refutable pattern in local binding: `Bar` and `Baz` not covered
--> $DIR/issue-31561.rs:8:9
|
LL | / enum Thing {
LL | | Foo(u8),
LL | | Bar,
| | --- not covered
LL | | Baz
| | --- not covered
LL | | }
| |_- `Thing` defined here
...
LL | let Thing::Foo(y) = Thing::Foo(1);
| ^^^^^^^^^^^^^ pattern `Bar` not covered
| ^^^^^^^^^^^^^ patterns `Bar` and `Baz` not covered
error: aborting due to previous error

View file

@ -15,10 +15,16 @@ enum E {
//~^ not covered
//~| not covered
//~| not covered
//~| not covered
//~| not covered
//~| not covered
C
//~^ not covered
//~| not covered
//~| not covered
//~| not covered
//~| not covered
//~| not covered
}
fn by_val(e: E) {
@ -27,7 +33,7 @@ fn by_val(e: E) {
E::A => {}
}
let E::A = e; //~ ERROR refutable pattern in local binding: `B` not covered
let E::A = e; //~ ERROR refutable pattern in local binding: `B` and `C` not covered
}
fn by_ref_once(e: &E) {
@ -35,7 +41,7 @@ fn by_ref_once(e: &E) {
E::A => {}
}
let E::A = e; //~ ERROR refutable pattern in local binding: `&B` not covered
let E::A = e; //~ ERROR refutable pattern in local binding: `&B` and `&C` not covered
}
fn by_ref_thrice(e: & &mut &E) {
@ -43,7 +49,8 @@ fn by_ref_thrice(e: & &mut &E) {
E::A => {}
}
let E::A = e; //~ ERROR refutable pattern in local binding: `&&mut &B` not covered
let E::A = e;
//~^ ERROR refutable pattern in local binding: `&&mut &B` and `&&mut &C` not covered
}
enum Opt {

View file

@ -1,5 +1,5 @@
error[E0004]: non-exhaustive patterns: `B` and `C` not covered
--> $DIR/non-exhaustive-defined-here.rs:26:11
--> $DIR/non-exhaustive-defined-here.rs:32:11
|
LL | / enum E {
LL | |
@ -21,23 +21,29 @@ LL | match e1 {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
error[E0005]: refutable pattern in local binding: `B` not covered
--> $DIR/non-exhaustive-defined-here.rs:30:9
error[E0005]: refutable pattern in local binding: `B` and `C` not covered
--> $DIR/non-exhaustive-defined-here.rs:36:9
|
LL | / enum E {
LL | |
LL | |
LL | |
... |
LL | | B,
| | - not covered
... |
LL | | C
| | - not covered
... |
LL | |
LL | | }
| |_- `E` defined here
...
LL | let E::A = e;
| ^^^^ pattern `B` not covered
| ^^^^ patterns `B` and `C` not covered
error[E0004]: non-exhaustive patterns: `&B` and `&C` not covered
--> $DIR/non-exhaustive-defined-here.rs:34:11
--> $DIR/non-exhaustive-defined-here.rs:40:11
|
LL | / enum E {
LL | |
@ -59,23 +65,29 @@ LL | match e {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
error[E0005]: refutable pattern in local binding: `&B` not covered
--> $DIR/non-exhaustive-defined-here.rs:38:9
error[E0005]: refutable pattern in local binding: `&B` and `&C` not covered
--> $DIR/non-exhaustive-defined-here.rs:44:9
|
LL | / enum E {
LL | |
LL | |
LL | |
... |
LL | | B,
| | - not covered
... |
LL | | C
| | - not covered
... |
LL | |
LL | | }
| |_- `E` defined here
...
LL | let E::A = e;
| ^^^^ pattern `&B` not covered
| ^^^^ patterns `&B` and `&C` not covered
error[E0004]: non-exhaustive patterns: `&&mut &B` and `&&mut &C` not covered
--> $DIR/non-exhaustive-defined-here.rs:42:11
--> $DIR/non-exhaustive-defined-here.rs:48:11
|
LL | / enum E {
LL | |
@ -97,23 +109,29 @@ LL | match e {
|
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
error[E0005]: refutable pattern in local binding: `&&mut &B` not covered
--> $DIR/non-exhaustive-defined-here.rs:46:9
error[E0005]: refutable pattern in local binding: `&&mut &B` and `&&mut &C` not covered
--> $DIR/non-exhaustive-defined-here.rs:52:9
|
LL | / enum E {
LL | |
LL | |
LL | |
... |
LL | | B,
| | - not covered
... |
LL | | C
| | - not covered
... |
LL | |
LL | | }
| |_- `E` defined here
...
LL | let E::A = e;
| ^^^^ pattern `&&mut &B` not covered
| ^^^^ patterns `&&mut &B` and `&&mut &C` not covered
error[E0004]: non-exhaustive patterns: `None` not covered
--> $DIR/non-exhaustive-defined-here.rs:58:11
--> $DIR/non-exhaustive-defined-here.rs:65:11
|
LL | / enum Opt {
LL | |
@ -131,13 +149,14 @@ LL | match e {
= help: ensure that all possible cases are being handled, possibly by adding wildcards or more match arms
error[E0005]: refutable pattern in local binding: `None` not covered
--> $DIR/non-exhaustive-defined-here.rs:62:9
--> $DIR/non-exhaustive-defined-here.rs:69:9
|
LL | / enum Opt {
LL | |
LL | |
LL | | Some(u8),
LL | | None,
| | ---- not covered
LL | |
LL | | }
| |_- `Opt` defined here

View file

@ -1,7 +1,9 @@
// ignore-line-length
fn func((1, (Some(1), 2..=3)): (isize, (Option<isize>, isize))) { }
//~^ ERROR refutable pattern in function argument: `(_, _)` not covered
fn main() {
let (1, (Some(1), 2..=3)) = (1, (None, 2));
//~^ ERROR refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` not covered
//~^ ERROR refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` and `(2i32..=std::i32::MAX, _)` not covered
}

View file

@ -1,14 +1,14 @@
error[E0005]: refutable pattern in function argument: `(_, _)` not covered
--> $DIR/refutable-pattern-errors.rs:1:9
--> $DIR/refutable-pattern-errors.rs:3:9
|
LL | fn func((1, (Some(1), 2..=3)): (isize, (Option<isize>, isize))) { }
| ^^^^^^^^^^^^^^^^^^^^^ pattern `(_, _)` not covered
error[E0005]: refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` not covered
--> $DIR/refutable-pattern-errors.rs:5:9
error[E0005]: refutable pattern in local binding: `(std::i32::MIN..=0i32, _)` and `(2i32..=std::i32::MAX, _)` not covered
--> $DIR/refutable-pattern-errors.rs:7:9
|
LL | let (1, (Some(1), 2..=3)) = (1, (None, 2));
| ^^^^^^^^^^^^^^^^^^^^^ pattern `(std::i32::MIN..=0i32, _)` not covered
| ^^^^^^^^^^^^^^^^^^^^^ patterns `(std::i32::MIN..=0i32, _)` and `(2i32..=std::i32::MAX, _)` not covered
error: aborting due to 2 previous errors

View file

@ -3,6 +3,7 @@ error[E0005]: refutable pattern in local binding: `A(_)` not covered
|
LL | / enum Foo {
LL | | A(foo::SecretlyEmpty),
| | - not covered
LL | | B(foo::NotSoSecretlyEmpty),
LL | | C(NotSoSecretlyEmpty),
LL | | D(u32),