about summary refs log tree commit diff
diff options
context:
space:
mode:
authorMara Bos <m-ou.se@m-ou.se>2020-11-16 17:26:13 +0100
committerGitHub <noreply@github.com>2020-11-16 17:26:13 +0100
commitde0aa6169f0c7abbc8aa4d7e735f5da57a1e65ab (patch)
treef6bef1fbd59655adb14a0e5986d1906c38569afc
parent4cdd22062580b46cbb6cf97ca56d59ebeb1e91b7 (diff)
parent6728240f36c7dd1ecb16d396c9c61933d2f01da9 (diff)
downloadrust-de0aa6169f0c7abbc8aa4d7e735f5da57a1e65ab.tar.gz
rust-de0aa6169f0c7abbc8aa4d7e735f5da57a1e65ab.zip
Rollup merge of #76339 - CDirkx:structural-match-range, r=Mark-Simulacrum
Test structural matching for all range types

As of #70166 all range types (`core::ops::Range` etc.) can be structurally matched upon, and by extension used in const generics. In reference to the fact that this is a publicly observable property of these types, and thus falls under the Rust stability guarantees of the standard library, a regression test was added in #70283.

This regression test was implemented by me by testing for the ability to use the range types within const generics, but that is not the actual property the std guarantees now (const generics is still unstable). This PR addresses that situation by adding extra tests for the range types that directly test whether they can be structurally matched upon.

Note: also adds the otherwise unrelated test `test_range_to_inclusive` for completeness with the other range unit tests
-rw-r--r--library/core/tests/ops.rs48
1 files changed, 47 insertions, 1 deletions
diff --git a/library/core/tests/ops.rs b/library/core/tests/ops.rs
index 8f0cd3be406..e9d595e65e2 100644
--- a/library/core/tests/ops.rs
+++ b/library/core/tests/ops.rs
@@ -1,4 +1,4 @@
-use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo};
+use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
 
 // Test the Range structs and syntax.
 
@@ -60,6 +60,12 @@ fn test_range_inclusive() {
 }
 
 #[test]
+fn test_range_to_inclusive() {
+    // Not much to test.
+    let _ = RangeToInclusive { end: 42 };
+}
+
+#[test]
 fn test_range_is_empty() {
     assert!(!(0.0..10.0).is_empty());
     assert!((-0.0..0.0).is_empty());
@@ -151,3 +157,43 @@ fn test_range_syntax_in_return_statement() {
     }
     // Not much to test.
 }
+
+#[test]
+fn range_structural_match() {
+    // test that all range types can be structurally matched upon
+
+    const RANGE: Range<usize> = 0..1000;
+    match RANGE {
+        RANGE => {}
+        _ => unreachable!(),
+    }
+
+    const RANGE_FROM: RangeFrom<usize> = 0..;
+    match RANGE_FROM {
+        RANGE_FROM => {}
+        _ => unreachable!(),
+    }
+
+    const RANGE_FULL: RangeFull = ..;
+    match RANGE_FULL {
+        RANGE_FULL => {}
+    }
+
+    const RANGE_INCLUSIVE: RangeInclusive<usize> = 0..=999;
+    match RANGE_INCLUSIVE {
+        RANGE_INCLUSIVE => {}
+        _ => unreachable!(),
+    }
+
+    const RANGE_TO: RangeTo<usize> = ..1000;
+    match RANGE_TO {
+        RANGE_TO => {}
+        _ => unreachable!(),
+    }
+
+    const RANGE_TO_INCLUSIVE: RangeToInclusive<usize> = ..=999;
+    match RANGE_TO_INCLUSIVE {
+        RANGE_TO_INCLUSIVE => {}
+        _ => unreachable!(),
+    }
+}