diff --git a/src/libcore/lib.rs b/src/libcore/lib.rs index f77b4d7461e74..ce7ddffd82584 100644 --- a/src/libcore/lib.rs +++ b/src/libcore/lib.rs @@ -134,6 +134,7 @@ #![feature(const_type_id)] #![feature(const_caller_location)] #![cfg_attr(bootstrap, feature(slice_patterns))] +#![feature(assoc_int_consts)] #[prelude_import] #[allow(unused)] diff --git a/src/libcore/num/dec2flt/rawfp.rs b/src/libcore/num/dec2flt/rawfp.rs index a127c6c3fa7ce..0ab15b23e5337 100644 --- a/src/libcore/num/dec2flt/rawfp.rs +++ b/src/libcore/num/dec2flt/rawfp.rs @@ -129,15 +129,15 @@ macro_rules! other_constants { ($type: ident) => { const EXPLICIT_SIG_BITS: u8 = Self::SIG_BITS - 1; const MAX_EXP: i16 = (1 << (Self::EXP_BITS - 1)) - 1; - const MIN_EXP: i16 = -Self::MAX_EXP + 1; - const MAX_EXP_INT: i16 = Self::MAX_EXP - (Self::SIG_BITS as i16 - 1); + const MIN_EXP: i16 = -::MAX_EXP + 1; + const MAX_EXP_INT: i16 = ::MAX_EXP - (Self::SIG_BITS as i16 - 1); const MAX_ENCODED_EXP: i16 = (1 << Self::EXP_BITS) - 1; - const MIN_EXP_INT: i16 = Self::MIN_EXP - (Self::SIG_BITS as i16 - 1); + const MIN_EXP_INT: i16 = ::MIN_EXP - (Self::SIG_BITS as i16 - 1); const MAX_SIG: u64 = (1 << Self::SIG_BITS) - 1; const MIN_SIG: u64 = 1 << (Self::SIG_BITS - 1); - const INFINITY: Self = $crate::$type::INFINITY; - const NAN: Self = $crate::$type::NAN; + const INFINITY: Self = $type::INFINITY; + const NAN: Self = $type::NAN; const ZERO: Self = 0.0; }; } diff --git a/src/libcore/num/f32.rs b/src/libcore/num/f32.rs index 505484c2a49dc..da8dd9acd9f48 100644 --- a/src/libcore/num/f32.rs +++ b/src/libcore/num/f32.rs @@ -15,14 +15,14 @@ use crate::num::FpCategory; /// The radix or base of the internal representation of `f32`. #[stable(feature = "rust1", since = "1.0.0")] -pub const RADIX: u32 = 2; +pub const RADIX: u32 = f32::RADIX; /// Number of significant digits in base 2. #[stable(feature = "rust1", since = "1.0.0")] -pub const MANTISSA_DIGITS: u32 = 24; +pub const MANTISSA_DIGITS: u32 = f32::MANTISSA_DIGITS; /// Approximate number of significant digits in base 10. #[stable(feature = "rust1", since = "1.0.0")] -pub const DIGITS: u32 = 6; +pub const DIGITS: u32 = f32::DIGITS; /// [Machine epsilon] value for `f32`. /// @@ -30,41 +30,41 @@ pub const DIGITS: u32 = 6; /// /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon #[stable(feature = "rust1", since = "1.0.0")] -pub const EPSILON: f32 = 1.1920929e-7_f32; +pub const EPSILON: f32 = f32::EPSILON; /// Smallest finite `f32` value. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN: f32 = -3.40282347e+38_f32; +pub const MIN: f32 = f32::MIN; /// Smallest positive normal `f32` value. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; +pub const MIN_POSITIVE: f32 = f32::MIN_POSITIVE; /// Largest finite `f32` value. #[stable(feature = "rust1", since = "1.0.0")] -pub const MAX: f32 = 3.40282347e+38_f32; +pub const MAX: f32 = f32::MAX; /// One greater than the minimum possible normal power of 2 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN_EXP: i32 = -125; +pub const MIN_EXP: i32 = f32::MIN_EXP; /// Maximum possible power of 2 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MAX_EXP: i32 = 128; +pub const MAX_EXP: i32 = f32::MAX_EXP; /// Minimum possible normal power of 10 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN_10_EXP: i32 = -37; +pub const MIN_10_EXP: i32 = f32::MIN_10_EXP; /// Maximum possible power of 10 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MAX_10_EXP: i32 = 38; +pub const MAX_10_EXP: i32 = f32::MAX_10_EXP; /// Not a Number (NaN). #[stable(feature = "rust1", since = "1.0.0")] -pub const NAN: f32 = 0.0_f32 / 0.0_f32; +pub const NAN: f32 = f32::NAN; /// Infinity (∞). #[stable(feature = "rust1", since = "1.0.0")] -pub const INFINITY: f32 = 1.0_f32 / 0.0_f32; +pub const INFINITY: f32 = f32::INFINITY; /// Negative infinity (−∞). #[stable(feature = "rust1", since = "1.0.0")] -pub const NEG_INFINITY: f32 = -1.0_f32 / 0.0_f32; +pub const NEG_INFINITY: f32 = f32::NEG_INFINITY; /// Basic mathematical constants. #[stable(feature = "rust1", since = "1.0.0")] @@ -153,6 +153,60 @@ pub mod consts { #[lang = "f32"] #[cfg(not(test))] impl f32 { + /// The radix or base of the internal representation of `f32`. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const RADIX: u32 = 2; + + /// Number of significant digits in base 2. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MANTISSA_DIGITS: u32 = 24; + + /// Approximate number of significant digits in base 10. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const DIGITS: u32 = 6; + + /// [Machine epsilon] value for `f32`. + /// + /// This is the difference between `1.0` and the next larger representable number. + /// + /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const EPSILON: f32 = 1.19209290e-07_f32; + + /// Smallest finite `f32` value. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN: f32 = -3.40282347e+38_f32; + /// Smallest positive normal `f32` value. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN_POSITIVE: f32 = 1.17549435e-38_f32; + /// Largest finite `f32` value. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX: f32 = 3.40282347e+38_f32; + + /// One greater than the minimum possible normal power of 2 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN_EXP: i32 = -125; + /// Maximum possible power of 2 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX_EXP: i32 = 128; + + /// Minimum possible normal power of 10 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN_10_EXP: i32 = -37; + /// Maximum possible power of 10 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX_10_EXP: i32 = 38; + + /// Not a Number (NaN). + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const NAN: f32 = 0.0_f32 / 0.0_f32; + /// Infinity (∞). + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const INFINITY: f32 = 1.0_f32 / 0.0_f32; + /// Negative infinity (-∞). + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const NEG_INFINITY: f32 = -1.0_f32 / 0.0_f32; + /// Returns `true` if this value is `NaN`. /// /// ``` diff --git a/src/libcore/num/f64.rs b/src/libcore/num/f64.rs index 8f3af42d25d80..a6081f184ab22 100644 --- a/src/libcore/num/f64.rs +++ b/src/libcore/num/f64.rs @@ -15,14 +15,14 @@ use crate::num::FpCategory; /// The radix or base of the internal representation of `f64`. #[stable(feature = "rust1", since = "1.0.0")] -pub const RADIX: u32 = 2; +pub const RADIX: u32 = f64::RADIX; /// Number of significant digits in base 2. #[stable(feature = "rust1", since = "1.0.0")] -pub const MANTISSA_DIGITS: u32 = 53; +pub const MANTISSA_DIGITS: u32 = f64::MANTISSA_DIGITS; /// Approximate number of significant digits in base 10. #[stable(feature = "rust1", since = "1.0.0")] -pub const DIGITS: u32 = 15; +pub const DIGITS: u32 = f64::DIGITS; /// [Machine epsilon] value for `f64`. /// @@ -30,41 +30,41 @@ pub const DIGITS: u32 = 15; /// /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon #[stable(feature = "rust1", since = "1.0.0")] -pub const EPSILON: f64 = 2.2204460492503131e-16_f64; +pub const EPSILON: f64 = f64::EPSILON; /// Smallest finite `f64` value. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN: f64 = -1.7976931348623157e+308_f64; +pub const MIN: f64 = f64::MIN; /// Smallest positive normal `f64` value. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64; +pub const MIN_POSITIVE: f64 = f64::MIN_POSITIVE; /// Largest finite `f64` value. #[stable(feature = "rust1", since = "1.0.0")] -pub const MAX: f64 = 1.7976931348623157e+308_f64; +pub const MAX: f64 = f64::MAX; /// One greater than the minimum possible normal power of 2 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN_EXP: i32 = -1021; +pub const MIN_EXP: i32 = f64::MIN_EXP; /// Maximum possible power of 2 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MAX_EXP: i32 = 1024; +pub const MAX_EXP: i32 = f64::MAX_EXP; /// Minimum possible normal power of 10 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MIN_10_EXP: i32 = -307; +pub const MIN_10_EXP: i32 = f64::MIN_10_EXP; /// Maximum possible power of 10 exponent. #[stable(feature = "rust1", since = "1.0.0")] -pub const MAX_10_EXP: i32 = 308; +pub const MAX_10_EXP: i32 = f64::MAX_10_EXP; /// Not a Number (NaN). #[stable(feature = "rust1", since = "1.0.0")] -pub const NAN: f64 = 0.0_f64 / 0.0_f64; +pub const NAN: f64 = f64::NAN; /// Infinity (∞). #[stable(feature = "rust1", since = "1.0.0")] -pub const INFINITY: f64 = 1.0_f64 / 0.0_f64; +pub const INFINITY: f64 = f64::INFINITY; /// Negative infinity (−∞). #[stable(feature = "rust1", since = "1.0.0")] -pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64; +pub const NEG_INFINITY: f64 = f64::NEG_INFINITY; /// Basic mathematical constants. #[stable(feature = "rust1", since = "1.0.0")] @@ -153,6 +153,59 @@ pub mod consts { #[lang = "f64"] #[cfg(not(test))] impl f64 { + /// The radix or base of the internal representation of `f64`. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const RADIX: u32 = 2; + + /// Number of significant digits in base 2. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MANTISSA_DIGITS: u32 = 53; + /// Approximate number of significant digits in base 10. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const DIGITS: u32 = 15; + + /// [Machine epsilon] value for `f64`. + /// + /// This is the difference between `1.0` and the next larger representable number. + /// + /// [Machine epsilon]: https://en.wikipedia.org/wiki/Machine_epsilon + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const EPSILON: f64 = 2.2204460492503131e-16_f64; + + /// Smallest finite `f64` value. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN: f64 = -1.7976931348623157e+308_f64; + /// Smallest positive normal `f64` value. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN_POSITIVE: f64 = 2.2250738585072014e-308_f64; + /// Largest finite `f64` value. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX: f64 = 1.7976931348623157e+308_f64; + + /// One greater than the minimum possible normal power of 2 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN_EXP: i32 = -1021; + /// Maximum possible power of 2 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX_EXP: i32 = 1024; + + /// Minimum possible normal power of 10 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN_10_EXP: i32 = -307; + /// Maximum possible power of 10 exponent. + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX_10_EXP: i32 = 308; + + /// Not a Number (NaN). + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const NAN: f64 = 0.0_f64 / 0.0_f64; + /// Infinity (∞). + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const INFINITY: f64 = 1.0_f64 / 0.0_f64; + /// Negative infinity (-∞). + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const NEG_INFINITY: f64 = -1.0_f64 / 0.0_f64; + /// Returns `true` if this value is `NaN`. /// /// ``` diff --git a/src/libcore/num/mod.rs b/src/libcore/num/mod.rs index 5d2c1d0de2c9b..39c7d6d24ed04 100644 --- a/src/libcore/num/mod.rs +++ b/src/libcore/num/mod.rs @@ -249,42 +249,56 @@ macro_rules! int_impl { $reversed:expr, $le_bytes:expr, $be_bytes:expr, $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => { doc_comment! { - concat!("Returns the smallest value that can be represented by this integer type. + concat!("The smallest value that can be represented by this integer type. # Examples Basic usage: ``` -", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), ", stringify!($Min), ");", +#![feature(assoc_int_consts)] +", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, ", stringify!($Min), ");", $EndFeature, " ```"), - #[stable(feature = "rust1", since = "1.0.0")] - #[inline(always)] - #[rustc_promotable] - #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")] - pub const fn min_value() -> Self { - !0 ^ ((!0 as $UnsignedT) >> 1) as Self - } + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN: Self = !0 ^ ((!0 as $UnsignedT) >> 1) as Self; } doc_comment! { - concat!("Returns the largest value that can be represented by this integer type. + concat!("The largest value that can be represented by this integer type. # Examples Basic usage: ``` -", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value(), ", stringify!($Max), ");", +#![feature(assoc_int_consts)] +", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($Max), ");", $EndFeature, " ```"), + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX: Self = !Self::MIN; + } + + doc_comment! { + "Returns the smallest value that can be represented by this integer type.", + #[stable(feature = "rust1", since = "1.0.0")] + #[inline(always)] + #[rustc_promotable] + #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")] + pub const fn min_value() -> Self { + Self::MIN + } + } + + doc_comment! { + "Returns the largest value that can be represented by this integer type.", #[stable(feature = "rust1", since = "1.0.0")] #[inline(always)] #[rustc_promotable] #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")] pub const fn max_value() -> Self { - !Self::min_value() + Self::MAX } } @@ -2388,38 +2402,52 @@ macro_rules! uint_impl { $reversed:expr, $le_bytes:expr, $be_bytes:expr, $to_xe_bytes_doc:expr, $from_xe_bytes_doc:expr) => { doc_comment! { - concat!("Returns the smallest value that can be represented by this integer type. + concat!("The smallest value that can be represented by this integer type. # Examples Basic usage: ``` -", $Feature, "assert_eq!(", stringify!($SelfT), "::min_value(), 0);", $EndFeature, " +#![feature(assoc_int_consts)] +", $Feature, "assert_eq!(", stringify!($SelfT), "::MIN, 0);", $EndFeature, " ```"), - #[stable(feature = "rust1", since = "1.0.0")] - #[rustc_promotable] - #[inline(always)] - #[rustc_const_stable(feature = "const_min_value", since = "1.32.0")] - pub const fn min_value() -> Self { 0 } + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MIN: Self = 0; } doc_comment! { - concat!("Returns the largest value that can be represented by this integer type. + concat!("The largest value that can be represented by this integer type. # Examples Basic usage: ``` -", $Feature, "assert_eq!(", stringify!($SelfT), "::max_value(), ", -stringify!($MaxV), ");", $EndFeature, " +#![feature(assoc_int_consts)] +", $Feature, "assert_eq!(", stringify!($SelfT), "::MAX, ", stringify!($MaxV), ");", +$EndFeature, " ```"), + #[unstable(feature = "assoc_int_consts", reason = "recently added", issue = "68490")] + pub const MAX: Self = !0; + } + + doc_comment! { + "Returns the smallest value that can be represented by this integer type.", + #[stable(feature = "rust1", since = "1.0.0")] + #[rustc_promotable] + #[inline(always)] + #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")] + pub const fn min_value() -> Self { Self::MIN } + } + + doc_comment! { + "Returns the largest value that can be represented by this integer type.", #[stable(feature = "rust1", since = "1.0.0")] #[rustc_promotable] #[inline(always)] #[rustc_const_stable(feature = "const_max_value", since = "1.32.0")] - pub const fn max_value() -> Self { !0 } + pub const fn max_value() -> Self { Self::MAX } } doc_comment! { diff --git a/src/libstd/lib.rs b/src/libstd/lib.rs index dc93ac90482fb..bc07c6b487b17 100644 --- a/src/libstd/lib.rs +++ b/src/libstd/lib.rs @@ -237,6 +237,7 @@ #![feature(arbitrary_self_types)] #![feature(array_error_internals)] #![feature(asm)] +#![feature(assoc_int_consts)] #![feature(associated_type_bounds)] #![feature(box_syntax)] #![feature(c_variadic)] diff --git a/src/test/rustdoc/show-const-contents.rs b/src/test/rustdoc/show-const-contents.rs index e84f6e52c75aa..064c026e6a078 100644 --- a/src/test/rustdoc/show-const-contents.rs +++ b/src/test/rustdoc/show-const-contents.rs @@ -47,9 +47,9 @@ pub struct MyTypeWithStr(&'static str); // @!has show_const_contents/constant.MY_TYPE_WITH_STR.html '; //' pub const MY_TYPE_WITH_STR: MyTypeWithStr = MyTypeWithStr("show this"); -// @has show_const_contents/constant.EPSILON.html '1.1920929e-7f32;' -// @!has show_const_contents/constant.EPSILON.html '; //' -pub use std::f32::EPSILON; +// @has show_const_contents/constant.PI.html '= 3.14159265358979323846264338327950288f32;' +// @has show_const_contents/constant.PI.html '; // 3.14159274f32' +pub use std::f32::consts::PI; // @has show_const_contents/constant.MAX.html '= i32::max_value(); // 2_147_483_647i32' pub use std::i32::MAX; diff --git a/src/test/ui/issues/issue-22933-3.rs b/src/test/ui/issues/issue-22933-3.rs deleted file mode 100644 index fbcce4b834467..0000000000000 --- a/src/test/ui/issues/issue-22933-3.rs +++ /dev/null @@ -1,4 +0,0 @@ -const FOO: [u32; u8::MIN as usize] = []; -//~^ ERROR no associated item named `MIN` found - -fn main() {} diff --git a/src/test/ui/issues/issue-22933-3.stderr b/src/test/ui/issues/issue-22933-3.stderr deleted file mode 100644 index 72bca3b040839..0000000000000 --- a/src/test/ui/issues/issue-22933-3.stderr +++ /dev/null @@ -1,14 +0,0 @@ -error[E0599]: no associated item named `MIN` found for type `u8` in the current scope - --> $DIR/issue-22933-3.rs:1:22 - | -LL | const FOO: [u32; u8::MIN as usize] = []; - | ^^^ associated item not found in `u8` - | -help: you are looking for the module in `std`, not the primitive type - | -LL | const FOO: [u32; std::u8::MIN as usize] = []; - | ^^^^^^^^^^^^ - -error: aborting due to previous error - -For more information about this error, try `rustc --explain E0599`. diff --git a/src/test/ui/use-module-level-int-consts.rs b/src/test/ui/use-module-level-int-consts.rs new file mode 100644 index 0000000000000..758fb414eadf2 --- /dev/null +++ b/src/test/ui/use-module-level-int-consts.rs @@ -0,0 +1,11 @@ +// run-pass + +// Make sure the module level constants are still there and accessible even after +// the corresponding associated constants have been added, and later stabilized. +use std::{u16, f32}; + +fn main() { + let _ = u16::MAX; + let _ = f32::EPSILON; + let _ = std::f64::MANTISSA_DIGITS; +}