From a1f42cd8930d465bf616d5b2bc5d7b1b945177ab Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Sat, 4 Feb 2017 16:27:45 +0800 Subject: [PATCH 1/3] Uninhabited while-let pattern fix --- src/librustc_const_eval/check_match.rs | 19 +++++++++++++++---- src/librustc_const_eval/lib.rs | 1 + src/test/compile-fail/uninhabited-patterns.rs | 8 ++++++++ 3 files changed, 24 insertions(+), 4 deletions(-) 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 + } } From 0dbb1e4fee5218b338ed1c8307665342c2b99e4f Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Sun, 5 Feb 2017 16:41:32 +0800 Subject: [PATCH 2/3] Remove use of ptr::eq --- src/librustc_const_eval/check_match.rs | 31 +++++++++++++++----------- src/librustc_const_eval/lib.rs | 1 - 2 files changed, 18 insertions(+), 14 deletions(-) diff --git a/src/librustc_const_eval/check_match.rs b/src/librustc_const_eval/check_match.rs index ce478f1a4504e..f53d451152fbc 100644 --- a/src/librustc_const_eval/check_match.rs +++ b/src/librustc_const_eval/check_match.rs @@ -8,7 +8,6 @@ // 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::*; @@ -274,7 +273,7 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>, let mut seen = Matrix::empty(); let mut catchall = None; let mut printed_if_let_err = false; - for &(ref pats, guard) in arms { + for (arm_index, &(ref pats, guard)) in arms.iter().enumerate() { for &(pat, hir_pat) in pats { let v = vec![pat]; @@ -305,17 +304,23 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>, let span = first_pat.0.span; // 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(); + match arm_index { + // The arm with the user-specified pattern. + 0 => { + 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); + }, + // The arm with the wildcard pattern. + 1 => { + struct_span_err!(cx.tcx.sess, span, E0165, + "irrefutable while-let pattern") + .span_label(span, &format!("irrefutable pattern")) + .emit(); + }, + _ => bug!(), } }, diff --git a/src/librustc_const_eval/lib.rs b/src/librustc_const_eval/lib.rs index 7c579f44e79f2..198e49daabc63 100644 --- a/src/librustc_const_eval/lib.rs +++ b/src/librustc_const_eval/lib.rs @@ -30,7 +30,6 @@ #![feature(box_syntax)] #![feature(const_fn)] #![feature(i128_type)] -#![feature(ptr_eq)] extern crate arena; #[macro_use] extern crate syntax; From 7135d0ab944f0bfa5ef4cecfd1dfdd8a79647c53 Mon Sep 17 00:00:00 2001 From: Andrew Cann Date: Sun, 5 Feb 2017 22:29:06 +0800 Subject: [PATCH 3/3] Fix make tidy --- src/librustc_const_eval/check_match.rs | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/librustc_const_eval/check_match.rs b/src/librustc_const_eval/check_match.rs index f53d451152fbc..6f33b4fad769f 100644 --- a/src/librustc_const_eval/check_match.rs +++ b/src/librustc_const_eval/check_match.rs @@ -310,8 +310,9 @@ fn check_arms<'a, 'tcx>(cx: &mut MatchCheckCtxt<'a, 'tcx>, 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); + cx.tcx.sess.add_lint_diagnostic( + lint::builtin::UNREACHABLE_PATTERNS, + hir_pat.id, diagnostic); }, // The arm with the wildcard pattern. 1 => {