Skip to content

Turn constants in modules core::[i/u][8/16/32/64/size] into associated constants #28656

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 3 commits into from
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/liballoc/raw_vec.rs
Original file line number Diff line number Diff line change
@@ -15,7 +15,6 @@ use heap;
use super::oom;
use super::boxed::Box;
use core::ops::Drop;
use core;

/// A low-level utility for more ergonomically allocating, reallocating, and deallocating a
/// 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> {

#[inline]
fn alloc_guard(alloc_size: usize) {
if core::usize::BITS < 64 {
assert!(alloc_size <= ::core::isize::MAX as usize,
"capacity overflow");
if usize::BITS < 64 {
assert!(alloc_size <= isize::MAX as usize, "capacity overflow");
}
}
1 change: 0 additions & 1 deletion src/libcollections/enum_set.rs
Original file line number Diff line number Diff line change
@@ -89,7 +89,6 @@ pub trait CLike {
}

fn bit<E:CLike>(e: &E) -> usize {
use core::usize;
let value = e.to_usize();
assert!(value < usize::BITS,
"EnumSet only supports up to {} variants.", usize::BITS - 1);
1 change: 0 additions & 1 deletion src/libcollections/vec_deque.rs
Original file line number Diff line number Diff line change
@@ -25,7 +25,6 @@ use core::mem;
use core::ops::{Index, IndexMut};
use core::ptr;
use core::slice;
use core::usize;

use core::hash::{Hash, Hasher};
use core::cmp;
2 changes: 1 addition & 1 deletion src/libcore/fmt/mod.rs
Original file line number Diff line number Diff line change
@@ -1387,7 +1387,7 @@ impl<T> Pointer for *const T {
f.flags |= 1 << (FlagV1::SignAwareZeroPad as u32);

if let None = f.width {
f.width = Some((::usize::BITS/4) + 2);
f.width = Some((usize::BITS/4) + 2);
}
}
f.flags |= 1 << (FlagV1::Alternate as u32);
2 changes: 1 addition & 1 deletion src/libcore/hash/mod.rs
Original file line number Diff line number Diff line change
@@ -192,7 +192,7 @@ mod impls {
fn hash_slice<H: Hasher>(data: &[$ty], state: &mut H) {
// FIXME(#23542) Replace with type ascription.
#![allow(trivial_casts)]
let newlen = data.len() * ::$ty::BYTES;
let newlen = data.len() * $ty::BYTES;
let ptr = data.as_ptr() as *const u8;
state.write(unsafe { slice::from_raw_parts(ptr, newlen) })
}
1 change: 1 addition & 0 deletions src/libcore/lib.rs
Original file line number Diff line number Diff line change
@@ -67,6 +67,7 @@
#![deny(missing_docs)]

#![feature(allow_internal_unstable)]
#![feature(associated_consts)]
#![feature(associated_type_defaults)]
#![feature(concat_idents)]
#![feature(const_fn)]
2 changes: 1 addition & 1 deletion src/libcore/num/int_macros.rs
Original file line number Diff line number Diff line change
@@ -31,7 +31,7 @@ pub const BYTES : usize = ($bits / 8);
// calling the `Bounded::min_value` function.
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(missing_docs)]
pub const MIN: $T = (-1 as $T) << (BITS - 1);
pub const MIN: $T = (-1 as $T) << ($bits - 1);
// FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::max_value` function.
75 changes: 61 additions & 14 deletions src/libcore/num/mod.rs
Original file line number Diff line number Diff line change
@@ -115,10 +115,38 @@ unsafe fn bswap8(x: u8) -> u8 { x }

// `Int` + `SignedInt` implemented for signed integers
macro_rules! int_impl {
($ActualT:ty, $UnsignedT:ty, $BITS:expr,
($SelfT:ty, $ActualT:ty, $UnsignedT:ty, $BITS:expr,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {

// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
#[unstable(feature = "num_bits_bytes",
reason = "may want to be an associated function",
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

reason needs update

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why? It still may want to be an associated function, just like std::usize::BYTES.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this reason was written down long before we even had associated constants, but with those I don't think we'd want both a constant and a function, and in today's Rust these are definitely most appropriate as a constant.

issue = "27753")]
#[allow(missing_docs)]
pub const BITS : usize = $BITS;
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `mem::size_of` function.
#[unstable(feature = "num_bits_bytes",
reason = "may want to be an associated function",
issue = "27753")]
#[allow(missing_docs)]
pub const BYTES : usize = ($BITS / 8);

// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::min_value` function.
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(missing_docs)]
pub const MIN: $SelfT = (-1 as $SelfT) << ($BITS - 1);
// FIXME(#9837): Compute MIN like this so the high bits that shouldn't exist are 0.
// FIXME(#11621): Should be deprecated once CTFE is implemented in favour of
// calling the `Bounded::max_value` function.
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(missing_docs)]
pub const MAX: $SelfT = !<$SelfT>::MIN;

/// Returns the smallest value that can be represented by this integer type.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -611,31 +639,31 @@ macro_rules! int_impl {

#[lang = "i8"]
impl i8 {
int_impl! { i8, u8, 8,
int_impl! { i8, i8, u8, 8,
intrinsics::i8_add_with_overflow,
intrinsics::i8_sub_with_overflow,
intrinsics::i8_mul_with_overflow }
}

#[lang = "i16"]
impl i16 {
int_impl! { i16, u16, 16,
int_impl! { i16, i16, u16, 16,
intrinsics::i16_add_with_overflow,
intrinsics::i16_sub_with_overflow,
intrinsics::i16_mul_with_overflow }
}

#[lang = "i32"]
impl i32 {
int_impl! { i32, u32, 32,
int_impl! { i32, i32, u32, 32,
intrinsics::i32_add_with_overflow,
intrinsics::i32_sub_with_overflow,
intrinsics::i32_mul_with_overflow }
}

#[lang = "i64"]
impl i64 {
int_impl! { i64, u64, 64,
int_impl! { i64, i64, u64, 64,
intrinsics::i64_add_with_overflow,
intrinsics::i64_sub_with_overflow,
intrinsics::i64_mul_with_overflow }
@@ -644,7 +672,7 @@ impl i64 {
#[cfg(target_pointer_width = "32")]
#[lang = "isize"]
impl isize {
int_impl! { i32, u32, 32,
int_impl! { isize, i32, u32, 32,
intrinsics::i32_add_with_overflow,
intrinsics::i32_sub_with_overflow,
intrinsics::i32_mul_with_overflow }
@@ -653,22 +681,41 @@ impl isize {
#[cfg(target_pointer_width = "64")]
#[lang = "isize"]
impl isize {
int_impl! { i64, u64, 64,
int_impl! { isize, i64, u64, 64,
intrinsics::i64_add_with_overflow,
intrinsics::i64_sub_with_overflow,
intrinsics::i64_mul_with_overflow }
}

// `Int` + `UnsignedInt` implemented for signed integers
macro_rules! uint_impl {
($ActualT:ty, $BITS:expr,
($SelfT:ty, $ActualT:ty, $BITS:expr,
$ctpop:path,
$ctlz:path,
$cttz:path,
$bswap:path,
$add_with_overflow:path,
$sub_with_overflow:path,
$mul_with_overflow:path) => {

#[unstable(feature = "num_bits_bytes",
reason = "may want to be an associated function",
issue = "27753")]
#[allow(missing_docs)]
pub const BITS : usize = $BITS;
#[unstable(feature = "num_bits_bytes",
reason = "may want to be an associated function",
issue = "27753")]
#[allow(missing_docs)]
pub const BYTES : usize = ($BITS / 8);

#[stable(feature = "rust1", since = "1.0.0")]
#[allow(missing_docs)]
pub const MIN: $SelfT = 0 as $SelfT;
#[stable(feature = "rust1", since = "1.0.0")]
#[allow(missing_docs)]
pub const MAX: $SelfT = !0 as $SelfT;

/// Returns the smallest value that can be represented by this integer type.
#[stable(feature = "rust1", since = "1.0.0")]
#[inline]
@@ -1163,7 +1210,7 @@ macro_rules! uint_impl {

#[lang = "u8"]
impl u8 {
uint_impl! { u8, 8,
uint_impl! { u8, u8, 8,
intrinsics::ctpop8,
intrinsics::ctlz8,
intrinsics::cttz8,
@@ -1175,7 +1222,7 @@ impl u8 {

#[lang = "u16"]
impl u16 {
uint_impl! { u16, 16,
uint_impl! { u16, u16, 16,
intrinsics::ctpop16,
intrinsics::ctlz16,
intrinsics::cttz16,
@@ -1187,7 +1234,7 @@ impl u16 {

#[lang = "u32"]
impl u32 {
uint_impl! { u32, 32,
uint_impl! { u32, u32, 32,
intrinsics::ctpop32,
intrinsics::ctlz32,
intrinsics::cttz32,
@@ -1200,7 +1247,7 @@ impl u32 {

#[lang = "u64"]
impl u64 {
uint_impl! { u64, 64,
uint_impl! { u64, u64, 64,
intrinsics::ctpop64,
intrinsics::ctlz64,
intrinsics::cttz64,
@@ -1213,7 +1260,7 @@ impl u64 {
#[cfg(target_pointer_width = "32")]
#[lang = "usize"]
impl usize {
uint_impl! { u32, 32,
uint_impl! { usize, u32, 32,
intrinsics::ctpop32,
intrinsics::ctlz32,
intrinsics::cttz32,
@@ -1226,7 +1273,7 @@ impl usize {
#[cfg(target_pointer_width = "64")]
#[lang = "usize"]
impl usize {
uint_impl! { u64, 64,
uint_impl! { usize, u64, 64,
intrinsics::ctpop64,
intrinsics::ctlz64,
intrinsics::cttz64,
2 changes: 1 addition & 1 deletion src/libcore/num/usize.rs
Original file line number Diff line number Diff line change
@@ -14,4 +14,4 @@

#![stable(feature = "rust1", since = "1.0.0")]

uint_module! { usize, isize, ::isize::BITS }
uint_module! { usize, isize, isize::BITS }
12 changes: 6 additions & 6 deletions src/libcoretest/num/int_macros.rs
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ mod tests {

#[test]
fn test_overflows() {
assert!(MAX > 0);
assert!(MIN <= 0);
assert!(MIN + MAX + 1 == 0);
assert!($T::MAX > 0);
assert!($T::MIN <= 0);
assert!($T::MIN + $T::MAX + 1 == 0);
}

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

#[test]
fn test_count_zeros() {
assert!(A.count_zeros() == BITS as u32 - 3);
assert!(B.count_zeros() == BITS as u32 - 2);
assert!(C.count_zeros() == BITS as u32 - 5);
assert!(A.count_zeros() == $T::BITS as u32 - 3);
assert!(B.count_zeros() == $T::BITS as u32 - 2);
assert!(C.count_zeros() == $T::BITS as u32 - 5);
}

#[test]
12 changes: 6 additions & 6 deletions src/libcoretest/num/uint_macros.rs
Original file line number Diff line number Diff line change
@@ -18,9 +18,9 @@ mod tests {

#[test]
fn test_overflows() {
assert!(MAX > 0);
assert!(MIN <= 0);
assert!((MIN + MAX).wrapping_add(1) == 0);
assert!($T::MAX > 0);
assert!($T::MIN <= 0);
assert!(($T::MIN + $T::MAX).wrapping_add(1) == 0);
}

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

#[test]
fn test_count_zeros() {
assert!(A.count_zeros() == BITS as u32 - 3);
assert!(B.count_zeros() == BITS as u32 - 2);
assert!(C.count_zeros() == BITS as u32 - 5);
assert!(A.count_zeros() == $T::BITS as u32 - 3);
assert!(B.count_zeros() == $T::BITS as u32 - 2);
assert!(C.count_zeros() == $T::BITS as u32 - 5);
}

#[test]
1 change: 0 additions & 1 deletion src/librustc/middle/dataflow.rs
Original file line number Diff line number Diff line change
@@ -20,7 +20,6 @@ use middle::cfg;
use middle::cfg::CFGIndex;
use middle::ty;
use std::io;
use std::usize;
use syntax::ast;
use syntax::ast_util::IdRange;
use syntax::print::pp;
2 changes: 1 addition & 1 deletion src/librustc_lint/types.rs
Original file line number Diff line number Diff line change
@@ -19,7 +19,7 @@ use lint::{LateContext, LintContext, LintArray};
use lint::{LintPass, LateLintPass};

use std::cmp;
use std::{i8, i16, i32, i64, u8, u16, u32, u64, f32, f64};
use std::{f32, f64};

use syntax::{abi, ast};
use syntax::attr::{self, AttrMetaMethods};
11 changes: 6 additions & 5 deletions src/librustdoc/html/render.rs
Original file line number Diff line number Diff line change
@@ -533,12 +533,13 @@ fn build_index(krate: &clean::Crate, cache: &mut Cache) -> io::Result<String> {
try!(write!(&mut w, r#"],"paths":["#));

for (i, &did) in pathid_to_nodeid.iter().enumerate() {
let &(ref fqp, short) = cache.paths.get(&did).unwrap();
if i > 0 {
try!(write!(&mut w, ","));
if let Some(&(ref fqp, short)) = cache.paths.get(&did) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How come this was necessary? It seems like this may indicate a bug in rustdoc rather than something that should be ignored?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, it's certainly a rustdoc bug, I spent may be a couple of hours trying to fix it, but haven't found out what's wrong.

if i > 0 {
try!(write!(&mut w, ","));
}
try!(write!(&mut w, r#"[{},"{}"]"#,
short as usize, *fqp.last().unwrap()));
}
try!(write!(&mut w, r#"[{},"{}"]"#,
short as usize, *fqp.last().unwrap()));
}

try!(write!(&mut w, "]}};"));
1 change: 0 additions & 1 deletion src/libserialize/collection_impls.rs
Original file line number Diff line number Diff line change
@@ -10,7 +10,6 @@

//! Implementations of serialization for structures found in libcollections

use std::usize;
use std::default::Default;
use std::hash::Hash;
use std::collections::hash_state::HashState;
4 changes: 2 additions & 2 deletions src/test/compile-fail/lint-exceeding-bitshifts.rs
Original file line number Diff line number Diff line change
@@ -57,8 +57,8 @@ fn main() {
let n = 1u8 << (4+3);
let n = 1u8 << (4+4); //~ ERROR: bitshift exceeds the type's number of bits

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


let n = 1i8<<(1isize+-1);
2 changes: 1 addition & 1 deletion src/test/run-pass/vector-sort-panic-safe.rs
Original file line number Diff line number Diff line change
@@ -46,7 +46,7 @@ impl Drop for DropCounter {
}

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