Skip to content

Commit 0375ccd

Browse files
committed
Add refinements to num/mod.rs
1 parent 5cf4d90 commit 0375ccd

File tree

3 files changed

+40
-1
lines changed

3 files changed

+40
-1
lines changed

library/core/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ check-cfg = [
4949
enabled = true
5050
check_overflow = "lazy"
5151
include = [ "src/ascii*.rs",
52+
"src/num/mod.rs",
5253
"src/pat.rs",
5354
"src/bstr/*.rs",
5455
"src/hash/*.rs",

library/core/src/flux_info.rs

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,39 @@
1919
property ShiftLeft[<<](n, k) {
2020
0 < k => n <= [<<](n, k)
2121
}
22+
23+
fn is_ascii_uppercase(n: int) -> bool {
24+
cast('A') <= n && n <= cast('Z')
25+
}
26+
27+
fn is_ascii_lowercase(n: int) -> bool {
28+
cast('a') <= n && n <= cast('z')
29+
}
30+
31+
fn to_ascii_uppercase(n: int) -> int {
32+
n - (cast(is_ascii_lowercase(n)) * 32)
33+
}
34+
35+
fn to_ascii_lowercase(n: int) -> int {
36+
n + (cast(is_ascii_uppercase(n)) * 32)
37+
}
38+
39+
property BitXor0[^](x, y) {
40+
(y == 0) => [^](x, y) == x
41+
}
42+
43+
property BitXor32[^](x, y) {
44+
(is_ascii_lowercase(x) && y == 32) => [^](x, y) == x - 32
45+
}
46+
47+
property BitOr0[|](x, y) {
48+
(y == 0) => [|](x, y) == x
49+
}
50+
51+
property BitOr32[|](x, y) {
52+
(is_ascii_uppercase(x) && y == 32) => [|](x, y) == x + 32
53+
}
54+
2255
}]
2356
#[flux::specs {
2457
mod hash {
@@ -50,7 +83,7 @@
5083
}
5184
5285
impl Debug for time::Duration {
53-
#[trusted(reason="modular arithmetic invariant inside nested fmt_decimal")]
86+
#[trusted(reason="modular arithmetic invariant inside nested fmt_decimal")]
5487
fn fmt(self: &Self, f: &mut fmt::Formatter) -> fmt::Result;
5588
}
5689
}]

library/core/src/num/mod.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -503,6 +503,7 @@ impl u8 {
503503
/// # Safety
504504
///
505505
/// This byte must be valid ASCII, or else this is UB.
506+
#[cfg_attr(flux, flux::spec(fn({&Self[@n] | n <= 127}) -> _))]
506507
#[must_use]
507508
#[unstable(feature = "ascii_char", issue = "110998")]
508509
#[inline]
@@ -533,6 +534,7 @@ impl u8 {
533534
/// ```
534535
///
535536
/// [`make_ascii_uppercase`]: Self::make_ascii_uppercase
537+
#[cfg_attr(flux, flux::spec(fn(&u8[@n]) -> u8[to_ascii_uppercase(n)]))]
536538
#[must_use = "to uppercase the value in-place, use `make_ascii_uppercase()`"]
537539
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
538540
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
@@ -558,6 +560,7 @@ impl u8 {
558560
/// ```
559561
///
560562
/// [`make_ascii_lowercase`]: Self::make_ascii_lowercase
563+
#[cfg_attr(flux, flux::spec(fn(&u8[@n]) -> u8[to_ascii_lowercase(n)]))]
561564
#[must_use = "to lowercase the value in-place, use `make_ascii_lowercase()`"]
562565
#[stable(feature = "ascii_methods_on_intrinsics", since = "1.23.0")]
563566
#[rustc_const_stable(feature = "const_ascii_methods_on_intrinsics", since = "1.52.0")]
@@ -706,6 +709,7 @@ impl u8 {
706709
/// assert!(!lf.is_ascii_uppercase());
707710
/// assert!(!esc.is_ascii_uppercase());
708711
/// ```
712+
#[cfg_attr(flux, flux::spec(fn(&Self[@n]) -> bool[is_ascii_uppercase(n)]))]
709713
#[must_use]
710714
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
711715
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]
@@ -740,6 +744,7 @@ impl u8 {
740744
/// assert!(!lf.is_ascii_lowercase());
741745
/// assert!(!esc.is_ascii_lowercase());
742746
/// ```
747+
#[cfg_attr(flux, flux::spec(fn(&u8[@n]) -> bool[is_ascii_lowercase(n)]))]
743748
#[must_use]
744749
#[stable(feature = "ascii_ctype_on_intrinsics", since = "1.24.0")]
745750
#[rustc_const_stable(feature = "const_ascii_ctype_on_intrinsics", since = "1.47.0")]

0 commit comments

Comments
 (0)