diff --git a/src/librustc/middle/check_match.rs b/src/librustc/middle/check_match.rs index 18bf3859f073..ea9d84d0aa33 100644 --- a/src/librustc/middle/check_match.rs +++ b/src/librustc/middle/check_match.rs @@ -104,18 +104,23 @@ pub fn check_arms(cx: &MatchCheckCtxt, arms: &[arm]) { for arm.pats.iter().advance |pat| { // Check that we do not match against a static NaN (#6804) - match cx.tcx.def_map.find(&pat.id) { - Some(&def_static(did, false)) => { - let const_expr = lookup_const_by_id(cx.tcx, did).get(); - match eval_const_expr(cx.tcx, const_expr) { - const_float(f) if f.is_NaN() => { - let msg = "unmatchable NaN in pattern, use is_NaN() in a guard instead"; - cx.tcx.sess.span_warn(pat.span, msg); + let pat_matches_nan: &fn(@pat) -> bool = |p| { + match cx.tcx.def_map.find(&p.id) { + Some(&def_static(did, false)) => { + let const_expr = lookup_const_by_id(cx.tcx, did).get(); + match eval_const_expr(cx.tcx, const_expr) { + const_float(f) if f.is_NaN() => true, + _ => false } - _ => {} } + _ => false + } + }; + for walk_pat(*pat) |p| { + if pat_matches_nan(p) { + cx.tcx.sess.span_warn(p.span, "unmatchable NaN in pattern, \ + use is_NaN() in a guard instead"); } - _ => {} } let v = ~[*pat]; diff --git a/src/test/compile-fail/issue-6804.rs b/src/test/compile-fail/issue-6804.rs index 85e09a777d14..77cabd1db901 100644 --- a/src/test/compile-fail/issue-6804.rs +++ b/src/test/compile-fail/issue-6804.rs @@ -9,6 +9,11 @@ fn main() { _ => {}, }; //~^^^ WARNING unmatchable NaN in pattern, use is_NaN() in a guard instead + match [x, 1.0] { + [NaN, _] => {}, + _ => {}, + }; + //~^^^ WARNING unmatchable NaN in pattern, use is_NaN() in a guard instead } // At least one error is needed so that compilation fails