Skip to content

Commit 8ee6855

Browse files
canndrewalexcrichton
authored andcommitted
Uninhabited while-let pattern fix
1 parent c354ba1 commit 8ee6855

File tree

3 files changed

+24
-4
lines changed

3 files changed

+24
-4
lines changed

src/librustc_const_eval/check_match.rs

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
// option. This file may not be copied, modified, or distributed
99
// except according to those terms.
1010

11+
use std::ptr;
1112
use _match::{MatchCheckCtxt, Matrix, expand_pattern, is_useful};
1213
use _match::Usefulness::*;
1314
use _match::WitnessPreference::*;
@@ -302,10 +303,20 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
302303
let &(ref first_arm_pats, _) = &arms[0];
303304
let first_pat = &first_arm_pats[0];
304305
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();
306+
307+
// check which arm we're on.
308+
if ptr::eq(first_arm_pats, pats) {
309+
let mut diagnostic = Diagnostic::new(Level::Warning,
310+
"unreachable pattern");
311+
diagnostic.set_span(pat.span);
312+
cx.tcx.sess.add_lint_diagnostic(lint::builtin::UNREACHABLE_PATTERNS,
313+
hir_pat.id, diagnostic);
314+
} else {
315+
struct_span_err!(cx.tcx.sess, span, E0165,
316+
"irrefutable while-let pattern")
317+
.span_label(span, &format!("irrefutable pattern"))
318+
.emit();
319+
}
309320
},
310321

311322
hir::MatchSource::ForLoopDesugar |

src/librustc_const_eval/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(box_patterns)]
3030
#![feature(box_syntax)]
3131
#![feature(const_fn)]
32+
#![feature(ptr_eq)]
3233

3334
extern crate arena;
3435
#[macro_use] extern crate syntax;

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)