Skip to content

Commit 3760d1d

Browse files
committed
Turn constants in modules core::[i/u][8,16,32,64,size] into associated constants
1 parent b94c4f5 commit 3760d1d

File tree

11 files changed

+68
-48
lines changed

11 files changed

+68
-48
lines changed

src/liballoc/raw_vec.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ use heap;
1515
use super::oom;
1616
use super::boxed::Box;
1717
use core::ops::Drop;
18-
use core;
1918

2019
/// A low-level utility for more ergonomically allocating, reallocating, and deallocating a
2120
/// a buffer of memory on the heap without having to worry about all the corner cases
@@ -472,8 +471,7 @@ impl<T> Drop for RawVec<T> {
472471

473472
#[inline]
474473
fn alloc_guard(alloc_size: usize) {
475-
if core::usize::BITS < 64 {
476-
assert!(alloc_size <= ::core::isize::MAX as usize,
477-
"capacity overflow");
474+
if usize::BITS < 64 {
475+
assert!(alloc_size <= isize::MAX as usize, "capacity overflow");
478476
}
479477
}

src/libcore/fmt/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1387,7 +1387,7 @@ impl<T> Pointer for *const T {
13871387
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);
13881388

13891389
if let None = f.width {
1390-
f.width = Some((::usize::BITS/4) + 2);
1390+
f.width = Some((usize::BITS/4) + 2);
13911391
}
13921392
}
13931393
f.flags |= 1 << (FlagV1::Alternate as u32);

src/libcore/hash/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ mod impls {
192192
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
193193
// FIXME(#23542) Replace with type ascription.
194194
#![allow(trivial_casts)]
195-
let newlen = data.len() * ::$ty::BYTES;
195+
let newlen = data.len() * $ty::BYTES;
196196
let ptr = data.as_ptr() as *const u8;
197197
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
198198
}

src/libcore/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#![deny(missing_docs)]
6868

6969
#![feature(allow_internal_unstable)]
70+
#![feature(associated_consts)]
7071
#![feature(associated_type_defaults)]
7172
#![feature(concat_idents)]
7273
#![feature(const_fn)]

src/libcore/num/int_macros.rs

Lines changed: 1 addition & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -12,26 +12,11 @@
1212

1313
macro_rules! int_module { ($T:ty, $bits:expr) => (
1414

15-
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
16-
// calling the `mem::size_of` function.
17-
#[unstable(feature = "num_bits_bytes",
18-
reason = "may want to be an associated function",
19-
issue = "27753")]
20-
#[allow(missing_docs)]
21-
pub const BITS : usize = $bits;
22-
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
23-
// calling the `mem::size_of` function.
24-
#[unstable(feature = "num_bits_bytes",
25-
reason = "may want to be an associated function",
26-
issue = "27753")]
27-
#[allow(missing_docs)]
28-
pub const BYTES : usize = ($bits / 8);
29-
3015
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
3116
// calling the `Bounded::min_value` function.
3217
#[stable(feature = "rust1", since = "1.0.0")]
3318
#[allow(missing_docs)]
34-
pub const MIN: $T = (-1 as $T) << (BITS - 1);
19+
pub const MIN: $T = (-1 as $T) << ($bits - 1);
3520
// FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
3621
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
3722
// calling the `Bounded::max_value` function.

src/libcore/num/mod.rs

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,34 @@ macro_rules! int_impl {
119119
$add_with_overflow:path,
120120
$sub_with_overflow:path,
121121
$mul_with_overflow:path) => {
122+
123+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
124+
// calling the `mem::size_of` function.
125+
#[unstable(feature = "num_bits_bytes",
126+
reason = "may want to be an associated function",
127+
issue = "27753")]
128+
#[allow(missing_docs)]
129+
pub const BITS : usize = $BITS;
130+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
131+
// calling the `mem::size_of` function.
132+
#[unstable(feature = "num_bits_bytes",
133+
reason = "may want to be an associated function",
134+
issue = "27753")]
135+
#[allow(missing_docs)]
136+
pub const BYTES : usize = ($BITS / 8);
137+
138+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
139+
// calling the `Bounded::min_value` function.
140+
#[stable(feature = "rust1", since = "1.0.0")]
141+
#[allow(missing_docs)]
142+
pub const MIN: $ActualT = (-1 as $ActualT) << ($BITS - 1);
143+
// FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
144+
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
145+
// calling the `Bounded::max_value` function.
146+
#[stable(feature = "rust1", since = "1.0.0")]
147+
#[allow(missing_docs)]
148+
pub const MAX: $ActualT = !<$ActualT>::MIN;
149+
122150
/// Returns the smallest value that can be represented by this integer type.
123151
#[stable(feature = "rust1", since = "1.0.0")]
124152
#[inline]
@@ -669,6 +697,25 @@ macro_rules! uint_impl {
669697
$add_with_overflow:path,
670698
$sub_with_overflow:path,
671699
$mul_with_overflow:path) => {
700+
701+
#[unstable(feature = "num_bits_bytes",
702+
reason = "may want to be an associated function",
703+
issue = "27753")]
704+
#[allow(missing_docs)]
705+
pub const BITS : usize = $BITS;
706+
#[unstable(feature = "num_bits_bytes",
707+
reason = "may want to be an associated function",
708+
issue = "27753")]
709+
#[allow(missing_docs)]
710+
pub const BYTES : usize = ($BITS / 8);
711+
712+
#[stable(feature = "rust1", since = "1.0.0")]
713+
#[allow(missing_docs)]
714+
pub const MIN: $ActualT = 0 as $ActualT;
715+
#[stable(feature = "rust1", since = "1.0.0")]
716+
#[allow(missing_docs)]
717+
pub const MAX: $ActualT = !0 as $ActualT;
718+
672719
/// Returns the smallest value that can be represented by this integer type.
673720
#[stable(feature = "rust1", since = "1.0.0")]
674721
#[inline]

src/libcore/num/uint_macros.rs

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -12,17 +12,6 @@
1212

1313
macro_rules! uint_module { ($T:ty, $T_SIGNED:ty, $bits:expr) => (
1414

15-
#[unstable(feature = "num_bits_bytes",
16-
reason = "may want to be an associated function",
17-
issue = "27753")]
18-
#[allow(missing_docs)]
19-
pub const BITS : usize = $bits;
20-
#[unstable(feature = "num_bits_bytes",
21-
reason = "may want to be an associated function",
22-
issue = "27753")]
23-
#[allow(missing_docs)]
24-
pub const BYTES : usize = ($bits / 8);
25-
2615
#[stable(feature = "rust1", since = "1.0.0")]
2716
#[allow(missing_docs)]
2817
pub const MIN: $T = 0 as $T;

src/libcoretest/num/int_macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ mod tests {
1818

1919
#[test]
2020
fn test_overflows() {
21-
assert!(MAX > 0);
22-
assert!(MIN <= 0);
23-
assert!(MIN + MAX + 1 == 0);
21+
assert!($T::MAX > 0);
22+
assert!($T::MIN <= 0);
23+
assert!($T::MIN + $T::MAX + 1 == 0);
2424
}
2525

2626
#[test]
@@ -85,9 +85,9 @@ mod tests {
8585

8686
#[test]
8787
fn test_count_zeros() {
88-
assert!(A.count_zeros() == BITS as u32 - 3);
89-
assert!(B.count_zeros() == BITS as u32 - 2);
90-
assert!(C.count_zeros() == BITS as u32 - 5);
88+
assert!(A.count_zeros() == $T::BITS as u32 - 3);
89+
assert!(B.count_zeros() == $T::BITS as u32 - 2);
90+
assert!(C.count_zeros() == $T::BITS as u32 - 5);
9191
}
9292

9393
#[test]

src/libcoretest/num/uint_macros.rs

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,9 @@ mod tests {
1818

1919
#[test]
2020
fn test_overflows() {
21-
assert!(MAX > 0);
22-
assert!(MIN <= 0);
23-
assert!((MIN + MAX).wrapping_add(1) == 0);
21+
assert!($T::MAX > 0);
22+
assert!($T::MIN <= 0);
23+
assert!(($T::MIN + $T::MAX).wrapping_add(1) == 0);
2424
}
2525

2626
#[test]
@@ -54,9 +54,9 @@ mod tests {
5454

5555
#[test]
5656
fn test_count_zeros() {
57-
assert!(A.count_zeros() == BITS as u32 - 3);
58-
assert!(B.count_zeros() == BITS as u32 - 2);
59-
assert!(C.count_zeros() == BITS as u32 - 5);
57+
assert!(A.count_zeros() == $T::BITS as u32 - 3);
58+
assert!(B.count_zeros() == $T::BITS as u32 - 2);
59+
assert!(C.count_zeros() == $T::BITS as u32 - 5);
6060
}
6161

6262
#[test]

src/test/compile-fail/lint-exceeding-bitshifts.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -57,8 +57,8 @@ fn main() {
5757
let n = 1u8 << (4+3);
5858
let n = 1u8 << (4+4); //~ ERROR: bitshift exceeds the type's number of bits
5959

60-
let n = 1_isize << std::isize::BITS; //~ ERROR: bitshift exceeds the type's number of bits
61-
let n = 1_usize << std::usize::BITS; //~ ERROR: bitshift exceeds the type's number of bits
60+
let n = 1_isize << isize::BITS; //~ ERROR: bitshift exceeds the type's number of bits
61+
let n = 1_usize << usize::BITS; //~ ERROR: bitshift exceeds the type's number of bits
6262

6363

6464
let n = 1i8<<(1isize+-1);

src/test/run-pass/vector-sort-panic-safe.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ impl Drop for DropCounter {
4646
}
4747

4848
pub fn main() {
49-
assert!(MAX_LEN <= std::usize::BITS);
49+
assert!(MAX_LEN <= usize::BITS);
5050
// len can't go above 64.
5151
for len in 2..MAX_LEN {
5252
for _ in 0..REPEATS {

0 commit comments

Comments
 (0)