Skip to content

Commit a20cb10

Browse files
committed
Require just the Unicode Scalar Values to be matched for a char
1 parent 9778a81 commit a20cb10

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/librustc_mir/hair/pattern/_match.rs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -460,11 +460,15 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
460460
.collect()
461461
}
462462
ty::TyChar if exhaustive_integer_patterns => {
463-
let (min, max) = (0u128, char::MAX as u128);
463+
let endpoint = |c: char| {
464+
ty::Const::from_bits(cx.tcx, c as u128, cx.tcx.types.char)
465+
};
464466
value_constructors = true;
465-
vec![ConstantRange(ty::Const::from_bits(cx.tcx, min, cx.tcx.types.char),
466-
ty::Const::from_bits(cx.tcx, max, cx.tcx.types.char),
467-
RangeEnd::Included)]
467+
vec![
468+
// The valid Unicode Scalar Value ranges.
469+
ConstantRange(endpoint('\u{0000}'), endpoint('\u{D7FF}'), RangeEnd::Included),
470+
ConstantRange(endpoint('\u{E000}'), endpoint('\u{10FFFF}'), RangeEnd::Included),
471+
]
468472
}
469473
ty::TyInt(int_ty) if exhaustive_integer_patterns => {
470474
use syntax::ast::IntTy::*;

src/test/ui/exhaustive_integer_patterns.rs

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,10 +55,19 @@ fn main() {
5555
}
5656

5757
// Let's test other types too!
58-
match '\u{0}' {
58+
let c: char = '\u{0}';
59+
match c {
5960
'\u{0}' ..= char::MAX => {} // ok
6061
}
6162

63+
// We can actually get away with just covering the
64+
// following two ranges, which correspond to all
65+
// valid Unicode Scalar Values.
66+
match c {
67+
'\u{0000}' ..= '\u{D7FF}' => {}
68+
'\u{E000}' ..= '\u{10_FFFF}' => {}
69+
}
70+
6271
match 0usize {
6372
0 ..= usize::MAX => {} // ok
6473
}
@@ -84,7 +93,7 @@ fn main() {
8493
}
8594

8695
match 0i8 {
87-
-128..=127 => {} // ok
96+
-128 ..= 127 => {} // ok
8897
}
8998

9099
match 0i16 {

0 commit comments

Comments
 (0)