Skip to content

Commit

Permalink
Uninhabited while-let pattern fix
Browse files Browse the repository at this point in the history
  • Loading branch information
canndrew committed Feb 4, 2017
1 parent c781fc4 commit a1f42cd
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 4 deletions.
19 changes: 15 additions & 4 deletions src/librustc_const_eval/check_match.rs
Expand Up @@ -8,6 +8,7 @@
// option. This file may not be copied, modified, or distributed
// except according to those terms.

use std::ptr;
use _match::{MatchCheckCtxt, Matrix, expand_pattern, is_useful};
use _match::Usefulness::*;
use _match::WitnessPreference::*;
Expand Down Expand Up @@ -302,10 +303,20 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
let &(ref first_arm_pats, _) = &arms[0];
let first_pat = &first_arm_pats[0];
let span = first_pat.0.span;
struct_span_err!(cx.tcx.sess, span, E0165,
"irrefutable while-let pattern")
.span_label(span, &format!("irrefutable pattern"))
.emit();

// check which arm we're on.
if ptr::eq(first_arm_pats, pats) {
let mut diagnostic = Diagnostic::new(Level::Warning,
"unreachable pattern");
diagnostic.set_span(pat.span);
cx.tcx.sess.add_lint_diagnostic(lint::builtin::UNREACHABLE_PATTERNS,
hir_pat.id, diagnostic);
} else {
struct_span_err!(cx.tcx.sess, span, E0165,
"irrefutable while-let pattern")
.span_label(span, &format!("irrefutable pattern"))
.emit();
}
},

hir::MatchSource::ForLoopDesugar |
Expand Down
1 change: 1 addition & 0 deletions src/librustc_const_eval/lib.rs
Expand Up @@ -30,6 +30,7 @@
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(i128_type)]
#![feature(ptr_eq)]

extern crate arena;
#[macro_use] extern crate syntax;
Expand Down
8 changes: 8 additions & 0 deletions src/test/compile-fail/uninhabited-patterns.rs
Expand Up @@ -24,6 +24,10 @@ struct NotSoSecretlyEmpty {
_priv: !,
}

fn foo() -> Option<NotSoSecretlyEmpty> {
None
}

fn main() {
let x: &[!] = &[];

Expand All @@ -45,5 +49,9 @@ fn main() {
Err(Err(_y)) => (),
Err(Ok(_y)) => (), //~ ERROR unreachable pattern
}

while let Some(_y) = foo() {
//~^ ERROR unreachable pattern
}
}

0 comments on commit a1f42cd

Please sign in to comment.