Skip to content

Commit 72e9aaa

Browse files
committed
Use associated constants in std::num::{Zero,One}
This commit causes an ICE!
1 parent cf11c26 commit 72e9aaa

File tree

9 files changed

+43
-48
lines changed

9 files changed

+43
-48
lines changed

src/libcore/fmt/num.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ trait GenericRadix {
5252
fn fmt_int<T: Int>(&self, mut x: T, f: &mut fmt::Formatter) -> fmt::Result {
5353
// The radix can be as low as 2, so we need a buffer of at least 64
5454
// characters for a base 2 number.
55-
let zero = T::zero();
55+
let zero = T::ZERO;
5656
let is_positive = x >= zero;
5757
let mut buf = [0; 64];
5858
let mut curr = buf.len();

src/libcore/iter.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -1065,7 +1065,7 @@ pub trait Iterator {
10651065
S: Add<Self::Item, Output=S> + Zero,
10661066
Self: Sized,
10671067
{
1068-
self.fold(Zero::zero(), |s, e| s + e)
1068+
self.fold(Zero::ZERO, |s, e| s + e)
10691069
}
10701070

10711071
/// Iterates over the entire iterator, multiplying all the elements
@@ -1087,7 +1087,7 @@ pub trait Iterator {
10871087
P: Mul<Self::Item, Output=P> + One,
10881088
Self: Sized,
10891089
{
1090-
self.fold(One::one(), |p, e| p * e)
1090+
self.fold(One::ONE, |p, e| p * e)
10911091
}
10921092
}
10931093

@@ -2830,7 +2830,7 @@ impl<A> DoubleEndedIterator for RangeInclusive<A> where
28302830
fn next_back(&mut self) -> Option<A> {
28312831
if self.range.end > self.range.start {
28322832
let result = self.range.end.clone();
2833-
self.range.end = &self.range.end - &A::one();
2833+
self.range.end = &self.range.end - &A::ONE;
28342834
Some(result)
28352835
} else if !self.done && self.range.start == self.range.end {
28362836
self.done = true;
@@ -2848,7 +2848,7 @@ impl<A: Step + Zero + Clone> Iterator for StepBy<A, ops::Range<A>> {
28482848

28492849
#[inline]
28502850
fn next(&mut self) -> Option<A> {
2851-
let rev = self.step_by < A::zero();
2851+
let rev = self.step_by < A::ZERO;
28522852
if (rev && self.range.start > self.range.end) ||
28532853
(!rev && self.range.start < self.range.end)
28542854
{
@@ -2896,7 +2896,7 @@ impl<A: Step + One> Iterator for ops::Range<A> where
28962896
#[inline]
28972897
fn next(&mut self) -> Option<A> {
28982898
if self.start < self.end {
2899-
let mut n = &self.start + &A::one();
2899+
let mut n = &self.start + &A::ONE;
29002900
mem::swap(&mut n, &mut self.start);
29012901
Some(n)
29022902
} else {
@@ -2906,7 +2906,7 @@ impl<A: Step + One> Iterator for ops::Range<A> where
29062906

29072907
#[inline]
29082908
fn size_hint(&self) -> (usize, Option<usize>) {
2909-
match Step::steps_between(&self.start, &self.end, &A::one()) {
2909+
match Step::steps_between(&self.start, &self.end, &A::ONE) {
29102910
Some(hint) => (hint, Some(hint)),
29112911
None => (0, None)
29122912
}
@@ -2926,7 +2926,7 @@ impl<A: Step + One + Clone> DoubleEndedIterator for ops::Range<A> where
29262926
#[inline]
29272927
fn next_back(&mut self) -> Option<A> {
29282928
if self.start < self.end {
2929-
self.end = &self.end - &A::one();
2929+
self.end = &self.end - &A::ONE;
29302930
Some(self.end.clone())
29312931
} else {
29322932
None
@@ -2943,7 +2943,7 @@ impl<A: Step + One> Iterator for ops::RangeFrom<A> where
29432943

29442944
#[inline]
29452945
fn next(&mut self) -> Option<A> {
2946-
let mut n = &self.start + &A::one();
2946+
let mut n = &self.start + &A::ONE;
29472947
mem::swap(&mut n, &mut self.start);
29482948
Some(n)
29492949
}

src/libcore/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,7 @@
6767
#![feature(on_unimplemented)]
6868
#![feature(simd)]
6969
#![feature(staged_api)]
70+
#![feature(associated_consts)]
7071
#![feature(unboxed_closures)]
7172
#![feature(rustc_attrs)]
7273
#![feature(optin_builtin_traits)]

src/libcore/num/mod.rs

+19-25
Original file line numberDiff line numberDiff line change
@@ -50,34 +50,30 @@ pub mod flt2dec;
5050
/// Types that have a "zero" value.
5151
///
5252
/// This trait is intended for use in conjunction with `Add`, as an identity:
53-
/// `x + T::zero() == x`.
54-
#[unstable(feature = "zero_one",
55-
reason = "unsure of placement, wants to use associated constants")]
53+
/// `x + T::ZERO == x`.
54+
#[unstable(feature = "zero_one", reason = "unsure of placement")]
5655
pub trait Zero {
5756
/// The "zero" (usually, additive identity) for this type.
58-
fn zero() -> Self;
57+
const ZERO: Self;
5958
}
6059

6160
/// Types that have a "one" value.
6261
///
6362
/// This trait is intended for use in conjunction with `Mul`, as an identity:
64-
/// `x * T::one() == x`.
65-
#[unstable(feature = "zero_one",
66-
reason = "unsure of placement, wants to use associated constants")]
63+
/// `x * T::ONE == x`.
64+
#[unstable(feature = "zero_one", reason = "unsure of placement")]
6765
pub trait One {
6866
/// The "one" (usually, multiplicative identity) for this type.
69-
fn one() -> Self;
67+
const ONE: Self;
7068
}
7169

7270
macro_rules! zero_one_impl {
7371
($($t:ty)*) => ($(
7472
impl Zero for $t {
75-
#[inline]
76-
fn zero() -> Self { 0 }
73+
const ZERO: $t = 0;
7774
}
7875
impl One for $t {
79-
#[inline]
80-
fn one() -> Self { 1 }
76+
const ONE: $t = 1;
8177
}
8278
)*)
8379
}
@@ -86,12 +82,10 @@ zero_one_impl! { u8 u16 u32 u64 usize i8 i16 i32 i64 isize }
8682
macro_rules! zero_one_impl_float {
8783
($($t:ty)*) => ($(
8884
impl Zero for $t {
89-
#[inline]
90-
fn zero() -> Self { 0.0 }
85+
const ZERO: $t = 0.0;
9186
}
9287
impl One for $t {
93-
#[inline]
94-
fn one() -> Self { 1.0 }
88+
const ONE: $t = 1.0;
9589
}
9690
)*)
9791
}
@@ -415,7 +409,7 @@ macro_rules! int_impl {
415409
pub fn saturating_add(self, other: Self) -> Self {
416410
match self.checked_add(other) {
417411
Some(x) => x,
418-
None if other >= Self::zero() => Self::max_value(),
412+
None if other >= Self::ZERO => Self::max_value(),
419413
None => Self::min_value(),
420414
}
421415
}
@@ -427,7 +421,7 @@ macro_rules! int_impl {
427421
pub fn saturating_sub(self, other: Self) -> Self {
428422
match self.checked_sub(other) {
429423
Some(x) => x,
430-
None if other >= Self::zero() => Self::min_value(),
424+
None if other >= Self::ZERO => Self::min_value(),
431425
None => Self::max_value(),
432426
}
433427
}
@@ -535,7 +529,7 @@ macro_rules! int_impl {
535529
#[inline]
536530
pub fn pow(self, mut exp: u32) -> Self {
537531
let mut base = self;
538-
let mut acc = Self::one();
532+
let mut acc = Self::ONE;
539533

540534
let mut prev_base = self;
541535
let mut base_oflo = false;
@@ -985,7 +979,7 @@ macro_rules! uint_impl {
985979
pub fn saturating_add(self, other: Self) -> Self {
986980
match self.checked_add(other) {
987981
Some(x) => x,
988-
None if other >= Self::zero() => Self::max_value(),
982+
None if other >= Self::ZERO => Self::max_value(),
989983
None => Self::min_value(),
990984
}
991985
}
@@ -997,7 +991,7 @@ macro_rules! uint_impl {
997991
pub fn saturating_sub(self, other: Self) -> Self {
998992
match self.checked_sub(other) {
999993
Some(x) => x,
1000-
None if other >= Self::zero() => Self::min_value(),
994+
None if other >= Self::ZERO => Self::min_value(),
1001995
None => Self::max_value(),
1002996
}
1003997
}
@@ -1103,7 +1097,7 @@ macro_rules! uint_impl {
11031097
#[inline]
11041098
pub fn pow(self, mut exp: u32) -> Self {
11051099
let mut base = self;
1106-
let mut acc = Self::one();
1100+
let mut acc = Self::ONE;
11071101

11081102
let mut prev_base = self;
11091103
let mut base_oflo = false;
@@ -1131,8 +1125,8 @@ macro_rules! uint_impl {
11311125
#[stable(feature = "rust1", since = "1.0.0")]
11321126
#[inline]
11331127
pub fn is_power_of_two(self) -> bool {
1134-
(self.wrapping_sub(Self::one())) & self == Self::zero() &&
1135-
!(self == Self::zero())
1128+
(self.wrapping_sub(Self::ONE)) & self == Self::ZERO &&
1129+
!(self == Self::ZERO)
11361130
}
11371131

11381132
/// Returns the smallest power of two greater than or equal to `self`.
@@ -1141,7 +1135,7 @@ macro_rules! uint_impl {
11411135
#[inline]
11421136
pub fn next_power_of_two(self) -> Self {
11431137
let bits = size_of::<Self>() * 8;
1144-
let one: Self = Self::one();
1138+
let one = Self::ONE;
11451139
one << ((bits - self.wrapping_sub(one).leading_zeros() as usize) % bits)
11461140
}
11471141

src/libstd/num/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -202,7 +202,7 @@ mod tests {
202202
#[test]
203203
fn test_pow() {
204204
fn naive_pow<T: Mul<Output=T> + One + Copy>(base: T, exp: usize) -> T {
205-
let one: T = T::one();
205+
let one: T = T::ONE;
206206
(0..exp).fold(one, |acc, _| acc * base)
207207
}
208208
macro_rules! assert_pow {

src/libstd/sys/unix/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -75,7 +75,7 @@ pub fn decode_error_kind(errno: i32) -> ErrorKind {
7575
}
7676

7777
pub fn cvt<T: One + PartialEq + Neg<Output=T>>(t: T) -> io::Result<T> {
78-
let one: T = T::one();
78+
let one: T = T::ONE;
7979
if t == -one {
8080
Err(io::Error::last_os_error())
8181
} else {

src/libstd/sys/windows/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ pub fn truncate_utf16_at_nul<'a>(v: &'a [u16]) -> &'a [u16] {
145145
}
146146

147147
fn cvt<I: PartialEq + Zero>(i: I) -> io::Result<I> {
148-
if i == I::zero() {
148+
if i == I::ZERO {
149149
Err(io::Error::last_os_error())
150150
} else {
151151
Ok(i)

src/libstd/sys/windows/net.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,7 @@ fn last_error() -> io::Error {
5050
/// and if so, returns the last error from the Windows socket interface. . This
5151
/// function must be called before another call to the socket API is made.
5252
pub fn cvt<T: One + Neg<Output=T> + PartialEq>(t: T) -> io::Result<T> {
53-
let one: T = T::one();
53+
let one: T = T::ONE;
5454
if t == -one {
5555
Err(last_error())
5656
} else {

src/test/run-pass/issue-8460.rs

+10-10
Original file line numberDiff line numberDiff line change
@@ -19,19 +19,19 @@ fn main() {
1919
assert!(thread::spawn(move|| { i16::min_value() / -1; }).join().is_err());
2020
assert!(thread::spawn(move|| { i32::min_value() / -1; }).join().is_err());
2121
assert!(thread::spawn(move|| { i64::min_value() / -1; }).join().is_err());
22-
assert!(thread::spawn(move|| { 1isize / isize::zero(); }).join().is_err());
23-
assert!(thread::spawn(move|| { 1i8 / i8::zero(); }).join().is_err());
24-
assert!(thread::spawn(move|| { 1i16 / i16::zero(); }).join().is_err());
25-
assert!(thread::spawn(move|| { 1i32 / i32::zero(); }).join().is_err());
26-
assert!(thread::spawn(move|| { 1i64 / i64::zero(); }).join().is_err());
22+
assert!(thread::spawn(move|| { 1isize / isize::ZERO; }).join().is_err());
23+
assert!(thread::spawn(move|| { 1i8 / i8::ZERO; }).join().is_err());
24+
assert!(thread::spawn(move|| { 1i16 / i16::ZERO; }).join().is_err());
25+
assert!(thread::spawn(move|| { 1i32 / i32::ZERO; }).join().is_err());
26+
assert!(thread::spawn(move|| { 1i64 / i64::ZERO; }).join().is_err());
2727
assert!(thread::spawn(move|| { isize::min_value() % -1; }).join().is_err());
2828
assert!(thread::spawn(move|| { i8::min_value() % -1; }).join().is_err());
2929
assert!(thread::spawn(move|| { i16::min_value() % -1; }).join().is_err());
3030
assert!(thread::spawn(move|| { i32::min_value() % -1; }).join().is_err());
3131
assert!(thread::spawn(move|| { i64::min_value() % -1; }).join().is_err());
32-
assert!(thread::spawn(move|| { 1isize % isize::zero(); }).join().is_err());
33-
assert!(thread::spawn(move|| { 1i8 % i8::zero(); }).join().is_err());
34-
assert!(thread::spawn(move|| { 1i16 % i16::zero(); }).join().is_err());
35-
assert!(thread::spawn(move|| { 1i32 % i32::zero(); }).join().is_err());
36-
assert!(thread::spawn(move|| { 1i64 % i64::zero(); }).join().is_err());
32+
assert!(thread::spawn(move|| { 1isize % isize::ZERO; }).join().is_err());
33+
assert!(thread::spawn(move|| { 1i8 % i8::ZERO; }).join().is_err());
34+
assert!(thread::spawn(move|| { 1i16 % i16::ZERO; }).join().is_err());
35+
assert!(thread::spawn(move|| { 1i32 % i32::ZERO; }).join().is_err());
36+
assert!(thread::spawn(move|| { 1i64 % i64::ZERO; }).join().is_err());
3737
}

0 commit comments

Comments
 (0)