diff --git a/src/librustc_const_eval/check_match.rs b/src/librustc_const_eval/check_match.rs index 0b1f2465a4d59..ce478f1a4504e 100644 --- a/src/librustc_const_eval/check_match.rs +++ b/src/librustc_const_eval/check_match.rs @@ -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::*; @@ -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 | diff --git a/src/librustc_const_eval/lib.rs b/src/librustc_const_eval/lib.rs index 198e49daabc63..7c579f44e79f2 100644 --- a/src/librustc_const_eval/lib.rs +++ b/src/librustc_const_eval/lib.rs @@ -30,6 +30,7 @@ #![feature(box_syntax)] #![feature(const_fn)] #![feature(i128_type)] +#![feature(ptr_eq)] extern crate arena; #[macro_use] extern crate syntax; diff --git a/src/test/compile-fail/uninhabited-patterns.rs b/src/test/compile-fail/uninhabited-patterns.rs index 0de29f3a8d737..4c894b0bdd3dd 100644 --- a/src/test/compile-fail/uninhabited-patterns.rs +++ b/src/test/compile-fail/uninhabited-patterns.rs @@ -24,6 +24,10 @@ struct NotSoSecretlyEmpty { _priv: !, } +fn foo() -> Option { + None +} + fn main() { let x: &[!] = &[]; @@ -45,5 +49,9 @@ fn main() { Err(Err(_y)) => (), Err(Ok(_y)) => (), //~ ERROR unreachable pattern } + + while let Some(_y) = foo() { + //~^ ERROR unreachable pattern + } }