about summary refs log tree commit diff
diff options
context:
space:
mode:
authorvarkor <github@varkor.com>2018-05-21 20:40:38 +0100
committervarkor <github@varkor.com>2018-08-16 20:09:04 +0100
commita20cb1084a5a90cf76ca3207d42b16b9c3ab1c76 (patch)
treeb9d46f3f6487155ff4fb837a27f9afeeed4fe4a3
parent9778a81e9287571710ebbd83aba1a6b816297c3b (diff)
downloadrust-a20cb1084a5a90cf76ca3207d42b16b9c3ab1c76.tar.gz
rust-a20cb1084a5a90cf76ca3207d42b16b9c3ab1c76.zip
Require just the Unicode Scalar Values to be matched for a char
-rw-r--r--src/librustc_mir/hair/pattern/_match.rs12
-rw-r--r--src/test/ui/exhaustive_integer_patterns.rs13
2 files changed, 19 insertions, 6 deletions
diff --git a/src/librustc_mir/hair/pattern/_match.rs b/src/librustc_mir/hair/pattern/_match.rs
index c7deb30dd3f..4a94030e876 100644
--- a/src/librustc_mir/hair/pattern/_match.rs
+++ b/src/librustc_mir/hair/pattern/_match.rs
@@ -460,11 +460,15 @@ fn all_constructors<'a, 'tcx: 'a>(cx: &mut MatchCheckCtxt<'a, 'tcx>,
                 .collect()
         }
         ty::TyChar if exhaustive_integer_patterns => {
-            let (min, max) = (0u128, char::MAX as u128);
+            let endpoint = |c: char| {
+                ty::Const::from_bits(cx.tcx, c as u128, cx.tcx.types.char)
+            };
             value_constructors = true;
-            vec![ConstantRange(ty::Const::from_bits(cx.tcx, min, cx.tcx.types.char),
-                               ty::Const::from_bits(cx.tcx, max, cx.tcx.types.char),
-                               RangeEnd::Included)]
+            vec![
+                // The valid Unicode Scalar Value ranges.
+                ConstantRange(endpoint('\u{0000}'), endpoint('\u{D7FF}'), RangeEnd::Included),
+                ConstantRange(endpoint('\u{E000}'), endpoint('\u{10FFFF}'), RangeEnd::Included),
+            ]
         }
         ty::TyInt(int_ty) if exhaustive_integer_patterns => {
             use syntax::ast::IntTy::*;
diff --git a/src/test/ui/exhaustive_integer_patterns.rs b/src/test/ui/exhaustive_integer_patterns.rs
index 39bac8919ff..80764415045 100644
--- a/src/test/ui/exhaustive_integer_patterns.rs
+++ b/src/test/ui/exhaustive_integer_patterns.rs
@@ -55,10 +55,19 @@ fn main() {
     }
 
     // Let's test other types too!
-    match '\u{0}' {
+    let c: char = '\u{0}';
+    match c {
         '\u{0}' ..= char::MAX => {} // ok
     }
 
+    // We can actually get away with just covering the
+    // following two ranges, which correspond to all
+    // valid Unicode Scalar Values.
+    match c {
+        '\u{0000}' ..= '\u{D7FF}' => {}
+        '\u{E000}' ..= '\u{10_FFFF}' => {}
+    }
+
     match 0usize {
         0 ..= usize::MAX => {} // ok
     }
@@ -84,7 +93,7 @@ fn main() {
     }
 
     match 0i8 {
-        -128..=127 => {} // ok
+        -128 ..= 127 => {} // ok
     }
 
     match 0i16 {