Skip to content

Commit de0aa61

Browse files
authored
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
2 parents 4cdd220 + 6728240 commit de0aa61

File tree

1 file changed

+47
-1
lines changed

1 file changed

+47
-1
lines changed

library/core/tests/ops.rs

+47-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo};
1+
use core::ops::{Bound, Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive};
22

33
// Test the Range structs and syntax.
44

@@ -59,6 +59,12 @@ fn test_range_inclusive() {
5959
assert_eq!(r.next(), None);
6060
}
6161

62+
#[test]
63+
fn test_range_to_inclusive() {
64+
// Not much to test.
65+
let _ = RangeToInclusive { end: 42 };
66+
}
67+
6268
#[test]
6369
fn test_range_is_empty() {
6470
assert!(!(0.0..10.0).is_empty());
@@ -151,3 +157,43 @@ fn test_range_syntax_in_return_statement() {
151157
}
152158
// Not much to test.
153159
}
160+
161+
#[test]
162+
fn range_structural_match() {
163+
// test that all range types can be structurally matched upon
164+
165+
const RANGE: Range<usize> = 0..1000;
166+
match RANGE {
167+
RANGE => {}
168+
_ => unreachable!(),
169+
}
170+
171+
const RANGE_FROM: RangeFrom<usize> = 0..;
172+
match RANGE_FROM {
173+
RANGE_FROM => {}
174+
_ => unreachable!(),
175+
}
176+
177+
const RANGE_FULL: RangeFull = ..;
178+
match RANGE_FULL {
179+
RANGE_FULL => {}
180+
}
181+
182+
const RANGE_INCLUSIVE: RangeInclusive<usize> = 0..=999;
183+
match RANGE_INCLUSIVE {
184+
RANGE_INCLUSIVE => {}
185+
_ => unreachable!(),
186+
}
187+
188+
const RANGE_TO: RangeTo<usize> = ..1000;
189+
match RANGE_TO {
190+
RANGE_TO => {}
191+
_ => unreachable!(),
192+
}
193+
194+
const RANGE_TO_INCLUSIVE: RangeToInclusive<usize> = ..=999;
195+
match RANGE_TO_INCLUSIVE {
196+
RANGE_TO_INCLUSIVE => {}
197+
_ => unreachable!(),
198+
}
199+
}

0 commit comments

Comments
 (0)