Skip to content

Commit fdd71be

Browse files
committed
Generalize NaN pattern detection using walk_pat
1 parent 5c729c0 commit fdd71be

File tree

2 files changed

+19
-9
lines changed

2 files changed

+19
-9
lines changed

src/librustc/middle/check_match.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -104,18 +104,23 @@ pub fn check_arms(cx: &MatchCheckCtxt, arms: &[arm]) {
104104
for arm.pats.iter().advance |pat| {
105105

106106
// Check that we do not match against a static NaN (#6804)
107-
match cx.tcx.def_map.find(&pat.id) {
108-
Some(&def_static(did, false)) => {
109-
let const_expr = lookup_const_by_id(cx.tcx, did).get();
110-
match eval_const_expr(cx.tcx, const_expr) {
111-
const_float(f) if f.is_NaN() => {
112-
let msg = "unmatchable NaN in pattern, use is_NaN() in a guard instead";
113-
cx.tcx.sess.span_warn(pat.span, msg);
107+
let pat_matches_nan: &fn(@pat) -> bool = |p| {
108+
match cx.tcx.def_map.find(&p.id) {
109+
Some(&def_static(did, false)) => {
110+
let const_expr = lookup_const_by_id(cx.tcx, did).get();
111+
match eval_const_expr(cx.tcx, const_expr) {
112+
const_float(f) if f.is_NaN() => true,
113+
_ => false
114114
}
115-
_ => {}
116115
}
116+
_ => false
117+
}
118+
};
119+
for walk_pat(*pat) |p| {
120+
if pat_matches_nan(p) {
121+
cx.tcx.sess.span_warn(p.span, "unmatchable NaN in pattern, \
122+
use is_NaN() in a guard instead");
117123
}
118-
_ => {}
119124
}
120125

121126
let v = ~[*pat];

src/test/compile-fail/issue-6804.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,11 @@ fn main() {
99
_ => {},
1010
};
1111
//~^^^ WARNING unmatchable NaN in pattern, use is_NaN() in a guard instead
12+
match [x, 1.0] {
13+
[NaN, _] => {},
14+
_ => {},
15+
};
16+
//~^^^ WARNING unmatchable NaN in pattern, use is_NaN() in a guard instead
1217
}
1318

1419
// At least one error is needed so that compilation fails

0 commit comments

Comments
 (0)