Skip to content

Commit ec7292a

Browse files
committed
core: add unstable no_fp_fmt_parse to disable float fmt/parse code
In some projects (e.g. kernel), floating point is forbidden. They can disable hardware floating point support and use `+soft-float` to avoid fp instructions from being generated, but as libcore contains the formatting code for `f32` and `f64`, some fp intrinsics are depended. One could define stubs for these intrinsics that just panic [1], but it means that if any formatting functions are accidentally used, mistake can only be caught during the runtime rather than during compile-time or link-time, and they consume a lot of space without LTO. This patch provides an unstable cfg `no_fp_fmt_parse` to disable these. A panicking stub is still provided for the `Debug` implementation (unfortunately) because there are some SIMD types that use `#[derive(Debug)]`. [1]: https://lkml.org/lkml/2021/4/14/1028
1 parent 37647d1 commit ec7292a

File tree

4 files changed

+27
-0
lines changed

4 files changed

+27
-0
lines changed

library/core/src/fmt/mod.rs

+3
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,10 @@ use crate::result;
1313
use crate::str;
1414

1515
mod builders;
16+
#[cfg(not(no_fp_fmt_parse))]
1617
mod float;
18+
#[cfg(no_fp_fmt_parse)]
19+
mod nofloat;
1720
mod num;
1821

1922
#[stable(feature = "fmt_flags_align", since = "1.28.0")]

library/core/src/fmt/nofloat.rs

+15
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
use crate::fmt::{Debug, Formatter, Result};
2+
3+
macro_rules! floating {
4+
($ty:ident) => {
5+
#[stable(feature = "rust1", since = "1.0.0")]
6+
impl Debug for $ty {
7+
fn fmt(&self, _fmt: &mut Formatter<'_>) -> Result {
8+
panic!("floating point support is turned off");
9+
}
10+
}
11+
};
12+
}
13+
14+
floating! { f32 }
15+
floating! { f64 }

library/core/src/num/mod.rs

+5
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,13 @@ macro_rules! unlikely {
2525
}
2626

2727
// All these modules are technically private and only exposed for coretests:
28+
#[cfg(not(no_fp_fmt_parse))]
2829
pub mod bignum;
30+
#[cfg(not(no_fp_fmt_parse))]
2931
pub mod dec2flt;
32+
#[cfg(not(no_fp_fmt_parse))]
3033
pub mod diy_float;
34+
#[cfg(not(no_fp_fmt_parse))]
3135
pub mod flt2dec;
3236
pub mod fmt;
3337

@@ -44,6 +48,7 @@ mod wrapping;
4448
pub use wrapping::Wrapping;
4549

4650
#[stable(feature = "rust1", since = "1.0.0")]
51+
#[cfg(not(no_fp_fmt_parse))]
4752
pub use dec2flt::ParseFloatError;
4853

4954
#[stable(feature = "rust1", since = "1.0.0")]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
-include ../tools.mk
2+
3+
all:
4+
$(RUSTC) --edition=2018 --crate-type=rlib ../../../../library/core/src/lib.rs --cfg no_fp_fmt_parse

0 commit comments

Comments
 (0)