Skip to content

Commit 4e67bf9

Browse files
authored
Rollup merge of #39526 - canndrew:uninhabited-while-let-fix, r=arielb1
Uninhabited while-let pattern fix This fix makes it so while-let with an unsatisfiable pattern raises a correct warning rather than an incorrect error.
2 parents 13b8e4b + 7135d0a commit 4e67bf9

File tree

2 files changed

+30
-5
lines changed

2 files changed

+30
-5
lines changed

src/librustc_const_eval/check_match.rs

Lines changed: 22 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -273,7 +273,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
273273
let mut seen = Matrix::empty();
274274
let mut catchall = None;
275275
let mut printed_if_let_err = false;
276-
for &(ref pats, guard) in arms {
276+
for (arm_index, &(ref pats, guard)) in arms.iter().enumerate() {
277277
for &(pat, hir_pat) in pats {
278278
let v = vec![pat];
279279

@@ -302,10 +302,27 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
302302
let &(ref first_arm_pats, _) = &arms[0];
303303
let first_pat = &first_arm_pats[0];
304304
let span = first_pat.0.span;
305-
struct_span_err!(cx.tcx.sess, span, E0165,
306-
"irrefutable while-let pattern")
307-
.span_label(span, &format!("irrefutable pattern"))
308-
.emit();
305+
306+
// check which arm we're on.
307+
match arm_index {
308+
// The arm with the user-specified pattern.
309+
0 => {
310+
let mut diagnostic = Diagnostic::new(Level::Warning,
311+
"unreachable pattern");
312+
diagnostic.set_span(pat.span);
313+
cx.tcx.sess.add_lint_diagnostic(
314+
lint::builtin::UNREACHABLE_PATTERNS,
315+
hir_pat.id, diagnostic);
316+
},
317+
// The arm with the wildcard pattern.
318+
1 => {
319+
struct_span_err!(cx.tcx.sess, span, E0165,
320+
"irrefutable while-let pattern")
321+
.span_label(span, &format!("irrefutable pattern"))
322+
.emit();
323+
},
324+
_ => bug!(),
325+
}
309326
},
310327

311328
hir::MatchSource::ForLoopDesugar |

src/test/compile-fail/uninhabited-patterns.rs

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,10 @@ struct NotSoSecretlyEmpty {
2424
_priv: !,
2525
}
2626

27+
fn foo() -> Option<NotSoSecretlyEmpty> {
28+
None
29+
}
30+
2731
fn main() {
2832
let x: &[!] = &[];
2933

@@ -45,5 +49,9 @@ fn main() {
4549
Err(Err(_y)) => (),
4650
Err(Ok(_y)) => (), //~ ERROR unreachable pattern
4751
}
52+
53+
while let Some(_y) = foo() {
54+
//~^ ERROR unreachable pattern
55+
}
4856
}
4957

0 commit comments

Comments
 (0)