Skip to content

Commit c42c1e7

Browse files
committed
Auto merge of #24792 - alexcrichton:issue-24748, r=alexcrichton
Ensures that the same error type is propagated throughout. Unnecessary leakage of the internals is prevented through the usage of stability attributes. Closes #24748
2 parents f6574c5 + 1291041 commit c42c1e7

File tree

6 files changed

+22
-47
lines changed

6 files changed

+22
-47
lines changed

src/libcore/num/float_macros.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,8 @@ macro_rules! from_str_radix_float_impl {
3535
}
3636

3737
let (is_positive, src) = match src.slice_shift_char() {
38-
None => return Err(PFE { kind: Empty }),
39-
Some(('-', "")) => return Err(PFE { kind: Empty }),
38+
None => return Err(PFE { __kind: Empty }),
39+
Some(('-', "")) => return Err(PFE { __kind: Empty }),
4040
Some(('-', src)) => (false, src),
4141
Some((_, _)) => (true, src),
4242
};
@@ -88,7 +88,7 @@ macro_rules! from_str_radix_float_impl {
8888
break; // start of fractional part
8989
},
9090
_ => {
91-
return Err(PFE { kind: Invalid });
91+
return Err(PFE { __kind: Invalid });
9292
},
9393
},
9494
}
@@ -122,7 +122,7 @@ macro_rules! from_str_radix_float_impl {
122122
break; // start of exponent
123123
},
124124
_ => {
125-
return Err(PFE { kind: Invalid });
125+
return Err(PFE { __kind: Invalid });
126126
},
127127
},
128128
}
@@ -135,7 +135,7 @@ macro_rules! from_str_radix_float_impl {
135135
let base = match c {
136136
'E' | 'e' if radix == 10 => 10.0,
137137
'P' | 'p' if radix == 16 => 2.0,
138-
_ => return Err(PFE { kind: Invalid }),
138+
_ => return Err(PFE { __kind: Invalid }),
139139
};
140140

141141
// Parse the exponent as decimal integer
@@ -144,13 +144,13 @@ macro_rules! from_str_radix_float_impl {
144144
Some(('-', src)) => (false, src.parse::<usize>()),
145145
Some(('+', src)) => (true, src.parse::<usize>()),
146146
Some((_, _)) => (true, src.parse::<usize>()),
147-
None => return Err(PFE { kind: Invalid }),
147+
None => return Err(PFE { __kind: Invalid }),
148148
};
149149

150150
match (is_positive, exp) {
151151
(true, Ok(exp)) => base.powi(exp as i32),
152152
(false, Ok(exp)) => 1.0 / base.powi(exp as i32),
153-
(_, Err(_)) => return Err(PFE { kind: Invalid }),
153+
(_, Err(_)) => return Err(PFE { __kind: Invalid }),
154154
}
155155
},
156156
None => 1.0, // no exponent

src/libcore/num/mod.rs

+9-5
Original file line numberDiff line numberDiff line change
@@ -1524,7 +1524,11 @@ impl fmt::Display for ParseIntError {
15241524

15251525
/// An error which can be returned when parsing a float.
15261526
#[derive(Debug, Clone, PartialEq)]
1527-
pub struct ParseFloatError { pub kind: FloatErrorKind }
1527+
#[stable(feature = "rust1", since = "1.0.0")]
1528+
pub struct ParseFloatError {
1529+
#[doc(hidden)]
1530+
pub __kind: FloatErrorKind
1531+
}
15281532

15291533
#[derive(Debug, Clone, PartialEq)]
15301534
pub enum FloatErrorKind {
@@ -1533,9 +1537,9 @@ pub enum FloatErrorKind {
15331537
}
15341538

15351539
impl ParseFloatError {
1536-
#[unstable(feature = "core", reason = "available through Error trait")]
1537-
pub fn description(&self) -> &str {
1538-
match self.kind {
1540+
#[doc(hidden)]
1541+
pub fn __description(&self) -> &str {
1542+
match self.__kind {
15391543
FloatErrorKind::Empty => "cannot parse float from empty string",
15401544
FloatErrorKind::Invalid => "invalid float literal",
15411545
}
@@ -1545,6 +1549,6 @@ impl ParseFloatError {
15451549
#[stable(feature = "rust1", since = "1.0.0")]
15461550
impl fmt::Display for ParseFloatError {
15471551
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
1548-
self.description().fmt(f)
1552+
self.__description().fmt(f)
15491553
}
15501554
}

src/libstd/error.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -147,7 +147,7 @@ impl Error for num::ParseIntError {
147147
#[stable(feature = "rust1", since = "1.0.0")]
148148
impl Error for num::ParseFloatError {
149149
fn description(&self) -> &str {
150-
self.description()
150+
self.__description()
151151
}
152152
}
153153

src/libstd/num/f32.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ use core::num;
2121
use intrinsics;
2222
use libc::c_int;
2323
use num::{FpCategory, ParseFloatError};
24-
use sys_common::FromInner;
2524

2625
pub use core::f32::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
2726
pub use core::f32::{MIN_EXP, MAX_EXP, MIN_10_EXP};
@@ -77,7 +76,7 @@ impl f32 {
7776
/// Parses a float as with a given radix
7877
#[unstable(feature = "float_from_str_radix", reason = "recently moved API")]
7978
pub fn from_str_radix(s: &str, radix: u32) -> Result<f32, ParseFloatError> {
80-
num::Float::from_str_radix(s, radix).map_err(FromInner::from_inner)
79+
num::Float::from_str_radix(s, radix)
8180
}
8281

8382
/// Returns `true` if this value is `NaN` and false otherwise.

src/libstd/num/f64.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,6 @@ use core::num;
2020
use intrinsics;
2121
use libc::c_int;
2222
use num::{FpCategory, ParseFloatError};
23-
use sys_common::FromInner;
2423

2524
pub use core::f64::{RADIX, MANTISSA_DIGITS, DIGITS, EPSILON};
2625
pub use core::f64::{MIN_EXP, MAX_EXP, MIN_10_EXP};
@@ -85,7 +84,7 @@ impl f64 {
8584
/// Parses a float as with a given radix
8685
#[unstable(feature = "float_from_str_radix", reason = "recently moved API")]
8786
pub fn from_str_radix(s: &str, radix: u32) -> Result<f64, ParseFloatError> {
88-
num::Float::from_str_radix(s, radix).map_err(FromInner::from_inner)
87+
num::Float::from_str_radix(s, radix)
8988
}
9089

9190
/// Returns `true` if this value is `NaN` and false otherwise.

src/libstd/num/mod.rs

+3-30
Original file line numberDiff line numberDiff line change
@@ -16,16 +16,14 @@
1616
#![stable(feature = "rust1", since = "1.0.0")]
1717
#![allow(missing_docs)]
1818

19-
use fmt;
20-
use core::num;
21-
2219
pub use core::num::{Zero, One};
23-
pub use core::num::{FpCategory, ParseIntError};
20+
pub use core::num::{FpCategory, ParseIntError, ParseFloatError};
2421
pub use core::num::{wrapping, Wrapping};
2522

26-
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
2723
#[cfg(test)] use cmp::PartialEq;
24+
#[cfg(test)] use fmt;
2825
#[cfg(test)] use marker::Copy;
26+
#[cfg(test)] use ops::{Add, Sub, Mul, Div, Rem};
2927

3028
/// Helper function for testing numeric operations
3129
#[cfg(test)]
@@ -43,31 +41,6 @@ pub fn test_num<T>(ten: T, two: T) where
4341
assert_eq!(ten.rem(two), ten % two);
4442
}
4543

46-
/// An error which can be returned when parsing a float.
47-
#[derive(Debug, Clone, PartialEq)]
48-
#[stable(feature = "rust1", since = "1.0.0")]
49-
pub struct ParseFloatError { inner: num::ParseFloatError }
50-
51-
impl ::sys_common::FromInner<num::ParseFloatError> for ParseFloatError {
52-
fn from_inner(inner: num::ParseFloatError) -> ParseFloatError {
53-
ParseFloatError { inner: inner }
54-
}
55-
}
56-
57-
impl ParseFloatError {
58-
#[unstable(feature = "core", reason = "available through Error trait")]
59-
pub fn description(&self) -> &str {
60-
self.inner.description()
61-
}
62-
}
63-
64-
#[stable(feature = "rust1", since = "1.0.0")]
65-
impl fmt::Display for ParseFloatError {
66-
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
67-
self.description().fmt(f)
68-
}
69-
}
70-
7144
#[cfg(test)]
7245
mod tests {
7346
use core::prelude::*;

0 commit comments

Comments
 (0)