From 10c4a5d633ebb5ad65e1da63bd42f0556592d61e Mon Sep 17 00:00:00 2001 From: Kim Hermansson Date: Sat, 23 Nov 2019 09:58:32 +0100 Subject: [PATCH] Added the upper and lower bounds to all integer types. --- src/libcore/internal_macros.rs | 8 ++++++++ src/libcore/num/i128.rs | 7 ++++++- src/libcore/num/i16.rs | 2 +- src/libcore/num/i32.rs | 2 +- src/libcore/num/i64.rs | 2 +- src/libcore/num/i8.rs | 2 +- src/libcore/num/int_macros.rs | 36 ++++++++++++++++++++++++++-------- src/libcore/num/isize.rs | 9 ++++++++- src/libcore/num/u128.rs | 6 +++++- src/libcore/num/u16.rs | 2 +- src/libcore/num/u32.rs | 2 +- src/libcore/num/u64.rs | 2 +- src/libcore/num/u8.rs | 2 +- src/libcore/num/uint_macros.rs | 30 ++++++++++++++++++++-------- src/libcore/num/usize.rs | 10 +++++++++- 15 files changed, 94 insertions(+), 28 deletions(-) diff --git a/src/libcore/internal_macros.rs b/src/libcore/internal_macros.rs index 3acf2ec837d88..c65e1ca23f97f 100644 --- a/src/libcore/internal_macros.rs +++ b/src/libcore/internal_macros.rs @@ -117,3 +117,11 @@ macro_rules! impl_fn_for_zst { )+ } } + +/// Create a dynamic doc comment. +macro_rules! doc_comment { + ($x:expr, $($tt:tt)*) => { + #[doc = $x] + $($tt)* + }; +} diff --git a/src/libcore/num/i128.rs b/src/libcore/num/i128.rs index 564ed598a882c..1c3046d574daf 100644 --- a/src/libcore/num/i128.rs +++ b/src/libcore/num/i128.rs @@ -4,4 +4,9 @@ #![stable(feature = "i128", since = "1.26.0")] -int_module! { i128, #[stable(feature = "i128", since="1.26.0")] } +int_module! { + i128, + -170141183460469231731687303715884105728, + 170141183460469231731687303715884105727, + #[stable(feature = "i128", since="1.26.0")] +} diff --git a/src/libcore/num/i16.rs b/src/libcore/num/i16.rs index 44d6aaef25ba0..d0a06e5094736 100644 --- a/src/libcore/num/i16.rs +++ b/src/libcore/num/i16.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -int_module! { i16 } +int_module! { i16, -32768, 32767 } diff --git a/src/libcore/num/i32.rs b/src/libcore/num/i32.rs index 90a5f89195e31..f7c5a15b7ad19 100644 --- a/src/libcore/num/i32.rs +++ b/src/libcore/num/i32.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -int_module! { i32 } +int_module! { i32, -2147483648, 2147483647 } diff --git a/src/libcore/num/i64.rs b/src/libcore/num/i64.rs index 04a8a9d757915..ed21ed3737e44 100644 --- a/src/libcore/num/i64.rs +++ b/src/libcore/num/i64.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -int_module! { i64 } +int_module! { i64, -9223372036854775808, 9223372036854775807 } diff --git a/src/libcore/num/i8.rs b/src/libcore/num/i8.rs index 5a52a967cf96d..81e13a93c43a7 100644 --- a/src/libcore/num/i8.rs +++ b/src/libcore/num/i8.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -int_module! { i8 } +int_module! { i8, -128, 127 } diff --git a/src/libcore/num/int_macros.rs b/src/libcore/num/int_macros.rs index 5c59fe25f6483..2acd91a3666b6 100644 --- a/src/libcore/num/int_macros.rs +++ b/src/libcore/num/int_macros.rs @@ -1,13 +1,33 @@ #![doc(hidden)] macro_rules! int_module { - ($T:ident) => (int_module!($T, #[stable(feature = "rust1", since = "1.0.0")]);); - ($T:ident, #[$attr:meta]) => ( - /// The smallest value that can be represented by this integer type. - #[$attr] - pub const MIN: $T = $T::min_value(); - /// The largest value that can be represented by this integer type. - #[$attr] - pub const MAX: $T = $T::max_value(); + ($T:ident, $Min:expr, $Max:expr) => ( + int_module!( + $T, + $Min, + $Max, + #[stable(feature = "rust1", since = "1.0.0")] + ); + ); + ($T:ident, $Min:expr, $Max:expr, #[$attr:meta]) => ( + doc_comment! { + concat!( + "The smallest value that can be represented by this integer type, which is ", + stringify!($Min), + "." + ), + #[$attr] + pub const MIN: $T = $T::min_value(); + } + + doc_comment! { + concat!( + "The largest value that can be represented by this integer type, which is ", + stringify!($Max), + "." + ), + #[$attr] + pub const MAX: $T = $T::max_value(); + } ) } diff --git a/src/libcore/num/isize.rs b/src/libcore/num/isize.rs index 143f8b3b272d6..a6295f301b094 100644 --- a/src/libcore/num/isize.rs +++ b/src/libcore/num/isize.rs @@ -4,4 +4,11 @@ #![stable(feature = "rust1", since = "1.0.0")] -int_module! { isize } +#[cfg(target_pointer_width = "16")] +int_module! { isize, -32768, 32767 } + +#[cfg(target_pointer_width = "32")] +int_module! { isize, -2147483648, 2147483647 } + +#[cfg(target_pointer_width = "64")] +int_module! { isize, -9223372036854775808, 9223372036854775807 } diff --git a/src/libcore/num/u128.rs b/src/libcore/num/u128.rs index 7d1aa664de30c..3fca9cce74a51 100644 --- a/src/libcore/num/u128.rs +++ b/src/libcore/num/u128.rs @@ -3,4 +3,8 @@ //! *[See also the `u128` primitive type](../../std/primitive.u128.html).* #![stable(feature = "i128", since = "1.26.0")] -uint_module! { u128, #[stable(feature = "i128", since="1.26.0")] } +uint_module! { + u128, + 340282366920938463463374607431768211455, + #[stable(feature = "i128", since="1.26.0")] +} diff --git a/src/libcore/num/u16.rs b/src/libcore/num/u16.rs index 34f80abaecc05..81f007c8473be 100644 --- a/src/libcore/num/u16.rs +++ b/src/libcore/num/u16.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -uint_module! { u16 } +uint_module! { u16, 65535 } diff --git a/src/libcore/num/u32.rs b/src/libcore/num/u32.rs index 5fd486f546608..82a1a9664c86c 100644 --- a/src/libcore/num/u32.rs +++ b/src/libcore/num/u32.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -uint_module! { u32 } +uint_module! { u32, 4294967295 } diff --git a/src/libcore/num/u64.rs b/src/libcore/num/u64.rs index 044d238aea9be..4a32a083f106b 100644 --- a/src/libcore/num/u64.rs +++ b/src/libcore/num/u64.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -uint_module! { u64 } +uint_module! { u64, 18446744073709551615 } diff --git a/src/libcore/num/u8.rs b/src/libcore/num/u8.rs index 6747e6a0f6c24..f9cd0e1dc2110 100644 --- a/src/libcore/num/u8.rs +++ b/src/libcore/num/u8.rs @@ -4,4 +4,4 @@ #![stable(feature = "rust1", since = "1.0.0")] -uint_module! { u8 } +uint_module! { u8, 255 } diff --git a/src/libcore/num/uint_macros.rs b/src/libcore/num/uint_macros.rs index a94b541ddb907..21c27eb7056b6 100644 --- a/src/libcore/num/uint_macros.rs +++ b/src/libcore/num/uint_macros.rs @@ -1,13 +1,27 @@ #![doc(hidden)] macro_rules! uint_module { - ($T:ident) => (uint_module!($T, #[stable(feature = "rust1", since = "1.0.0")]);); - ($T:ident, #[$attr:meta]) => ( - /// The smallest value that can be represented by this integer type. - #[$attr] - pub const MIN: $T = $T::min_value(); - /// The largest value that can be represented by this integer type. - #[$attr] - pub const MAX: $T = $T::max_value(); + ($T:ident, $Max:expr) => ( + uint_module!( + $T, + $Max, + #[stable(feature = "rust1", since = "1.0.0")] + ); + ); + ($T:ident, $Max:expr, #[$attr:meta]) => ( + doc_comment! { + "The smallest value that can be represented by this integer type, which is 0.", + #[$attr] + pub const MIN: $T = $T::min_value(); + } + doc_comment! { + concat!( + "The largest value that can be represented by this integer type, which is ", + stringify!($Max), + "." + ), + #[$attr] + pub const MAX: $T = $T::max_value(); + } ) } diff --git a/src/libcore/num/usize.rs b/src/libcore/num/usize.rs index e3a5239d908dc..532efc8a6952e 100644 --- a/src/libcore/num/usize.rs +++ b/src/libcore/num/usize.rs @@ -4,4 +4,12 @@ #![stable(feature = "rust1", since = "1.0.0")] -uint_module! { usize } + +#[cfg(target_pointer_width = "16")] +uint_module! { usize, 65535 } + +#[cfg(target_pointer_width = "32")] +uint_module! { usize, 4294967295 } + +#[cfg(target_pointer_width = "64")] +uint_module! { usize, 18446744073709551615 }