Skip to content

Commit 028569a

Browse files
committed
Auto merge of #43266 - feadoor:issue-43253-exclusive-range-warning, r=nikomatsakis
Fix `range_covered_by_constructor` for exclusive ranges. This resolves #43253
2 parents 5803f99 + 1b3c339 commit 028569a

File tree

3 files changed

+78
-7
lines changed

3 files changed

+78
-7
lines changed

src/librustc_const_eval/_match.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -835,7 +835,7 @@ fn slice_pat_covered_by_constructor(_tcx: TyCtxt, _span: Span,
835835
Ok(true)
836836
}
837837

838-
fn range_covered_by_constructor(tcx: TyCtxt, span: Span,
838+
fn constructor_covered_by_range(tcx: TyCtxt, span: Span,
839839
ctor: &Constructor,
840840
from: &ConstVal, to: &ConstVal,
841841
end: RangeEnd)
@@ -845,14 +845,14 @@ fn range_covered_by_constructor(tcx: TyCtxt, span: Span,
845845
match *ctor {
846846
ConstantValue(ref value) => {
847847
let to = cmp_to(value)?;
848-
let end = (to != Ordering::Greater) ||
849-
(end == RangeEnd::Excluded && to == Ordering::Equal);
848+
let end = (to == Ordering::Less) ||
849+
(end == RangeEnd::Included && to == Ordering::Equal);
850850
Ok(cmp_from(value)? && end)
851851
},
852852
ConstantRange(ref from, ref to, RangeEnd::Included) => {
853853
let to = cmp_to(to)?;
854-
let end = (to != Ordering::Greater) ||
855-
(end == RangeEnd::Excluded && to == Ordering::Equal);
854+
let end = (to == Ordering::Less) ||
855+
(end == RangeEnd::Included && to == Ordering::Equal);
856856
Ok(cmp_from(from)? && end)
857857
},
858858
ConstantRange(ref from, ref to, RangeEnd::Excluded) => {
@@ -933,7 +933,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>(
933933
"unexpected const-val {:?} with ctor {:?}", value, constructor)
934934
},
935935
_ => {
936-
match range_covered_by_constructor(
936+
match constructor_covered_by_range(
937937
cx.tcx, pat.span, constructor, value, value, RangeEnd::Included
938938
) {
939939
Ok(true) => Some(vec![]),
@@ -945,7 +945,7 @@ fn specialize<'p, 'a: 'p, 'tcx: 'a>(
945945
}
946946

947947
PatternKind::Range { ref lo, ref hi, ref end } => {
948-
match range_covered_by_constructor(
948+
match constructor_covered_by_range(
949949
cx.tcx, pat.span, constructor, lo, hi, end.clone()
950950
) {
951951
Ok(true) => Some(vec![]),
+51
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Copyright 2017 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
#![feature(exclusive_range_pattern)]
12+
13+
fn main() {
14+
// These cases should generate no warning.
15+
match 10 {
16+
1..10 => {},
17+
10 => {},
18+
_ => {},
19+
}
20+
21+
match 10 {
22+
1..10 => {},
23+
9...10 => {},
24+
_ => {},
25+
}
26+
27+
match 10 {
28+
1..10 => {},
29+
10...10 => {},
30+
_ => {},
31+
}
32+
33+
// These cases should generate an "unreachable pattern" warning.
34+
match 10 {
35+
1..10 => {},
36+
9 => {},
37+
_ => {},
38+
}
39+
40+
match 10 {
41+
1..10 => {},
42+
8...9 => {},
43+
_ => {},
44+
}
45+
46+
match 10 {
47+
1..10 => {},
48+
9...9 => {},
49+
_ => {},
50+
}
51+
}
+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
warning: unreachable pattern
2+
--> $DIR/issue-43253.rs:36:9
3+
|
4+
36 | 9 => {},
5+
| ^
6+
|
7+
= note: #[warn(unreachable_patterns)] on by default
8+
9+
warning: unreachable pattern
10+
--> $DIR/issue-43253.rs:42:9
11+
|
12+
42 | 8...9 => {},
13+
| ^^^^^
14+
15+
warning: unreachable pattern
16+
--> $DIR/issue-43253.rs:48:9
17+
|
18+
48 | 9...9 => {},
19+
| ^^^^^
20+

0 commit comments

Comments
 (0)