diff --git a/library/core/src/fmt/float.rs b/library/core/src/fmt/float.rs index 6915edc813045..2a9afc66c1955 100644 --- a/library/core/src/fmt/float.rs +++ b/library/core/src/fmt/float.rs @@ -86,7 +86,7 @@ where true => flt2dec::Sign::MinusPlus, }; - if let Some(precision) = fmt.precision() { + if let Some(precision) = fmt.options.precision { float_to_decimal_common_exact(fmt, num, sign, precision) } else { let min_precision = 0; @@ -161,7 +161,7 @@ where true => flt2dec::Sign::MinusPlus, }; - if let Some(precision) = fmt.precision() { + if let Some(precision) = fmt.options.precision { // 1 integral digit + `precision` fractional digits = `precision + 1` total digits float_to_exponential_common_exact(fmt, num, sign, precision + 1, upper) } else { @@ -179,7 +179,7 @@ where true => flt2dec::Sign::MinusPlus, }; - if let Some(precision) = fmt.precision() { + if let Some(precision) = fmt.options.precision { // this behavior of {:.PREC?} predates exponential formatting for {:?} float_to_decimal_common_exact(fmt, num, sign, precision) } else { diff --git a/library/core/src/fmt/mod.rs b/library/core/src/fmt/mod.rs index 32ec48067a1ea..d580071019482 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1400,14 +1400,14 @@ pub fn write(output: &mut dyn Write, args: Arguments<'_>) -> Result { } unsafe fn run(fmt: &mut Formatter<'_>, arg: &rt::Placeholder, args: &[rt::Argument<'_>]) -> Result { - fmt.options.fill(arg.fill); - fmt.options.align(arg.align.into()); - fmt.options.flags(arg.flags); + fmt.options.fill = arg.fill; + fmt.options.align = arg.align.into(); + fmt.options.flags = arg.flags; // SAFETY: arg and args come from the same Arguments, // which guarantees the indexes are always within bounds. unsafe { - fmt.options.width(getcount(args, &arg.width)); - fmt.options.precision(getcount(args, &arg.precision)); + fmt.options.width = getcount(args, &arg.width); + fmt.options.precision = getcount(args, &arg.precision); } // Extract the correct argument @@ -1546,7 +1546,7 @@ impl<'a> Formatter<'a> { } // The `width` field is more of a `min-width` parameter at this point. - match self.width() { + match self.options.width { // If there's no minimum length requirements then we can just // write the bytes. None => { @@ -1614,12 +1614,12 @@ impl<'a> Formatter<'a> { #[stable(feature = "rust1", since = "1.0.0")] pub fn pad(&mut self, s: &str) -> Result { // Make sure there's a fast path up front - if self.width().is_none() && self.precision().is_none() { + if self.options.width.is_none() && self.options.precision.is_none() { return self.buf.write_str(s); } // The `precision` field can be interpreted as a `max-width` for the // string being formatted. - let s = if let Some(max) = self.precision() { + let s = if let Some(max) = self.options.precision { // If our string is longer that the precision, then we must have // truncation. However other flags like `fill`, `width` and `align` // must act as always. @@ -1636,7 +1636,7 @@ impl<'a> Formatter<'a> { &s }; // The `width` field is more of a `min-width` parameter at this point. - match self.width() { + match self.options.width { // If we're under the maximum length, and there's no minimum length // requirements, then we can just emit the string None => self.buf.write_str(s), @@ -1676,10 +1676,10 @@ impl<'a> Formatter<'a> { }; for _ in 0..pre_pad { - self.buf.write_char(self.fill())?; + self.buf.write_char(self.options.fill)?; } - Ok(PostPadding::new(self.fill(), post_pad)) + Ok(PostPadding::new(self.options.fill, post_pad)) } /// Takes the formatted parts and applies the padding. @@ -1690,12 +1690,12 @@ impl<'a> Formatter<'a> { /// /// Any `numfmt::Part::Copy` parts in `formatted` must contain valid UTF-8. unsafe fn pad_formatted_parts(&mut self, formatted: &numfmt::Formatted<'_>) -> Result { - if let Some(mut width) = self.width() { + if let Some(mut width) = self.options.width { // for the sign-aware zero padding, we render the sign first and // behave as if we had no sign from the beginning. let mut formatted = formatted.clone(); - let old_fill = self.fill(); - let old_align = self.align(); + let old_fill = self.options.fill; + let old_align = self.options.align; if self.sign_aware_zero_pad() { // a sign always goes first let sign = formatted.sign; @@ -1704,8 +1704,8 @@ impl<'a> Formatter<'a> { // remove the sign from the formatted parts formatted.sign = ""; width = width.saturating_sub(sign.len()); - self.options.fill('0'); - self.options.align(Some(Alignment::Right)); + self.options.fill = '0'; + self.options.align = Some(Alignment::Right); } // remaining parts go through the ordinary padding process. @@ -1722,8 +1722,8 @@ impl<'a> Formatter<'a> { } post_padding.write(self) }; - self.options.fill(old_fill); - self.options.align(old_align); + self.options.fill = old_fill; + self.options.align = old_align; ret } else { // this is the common case and we take a shortcut @@ -1839,7 +1839,7 @@ impl<'a> Formatter<'a> { or `sign_aware_zero_pad` methods instead" )] pub fn flags(&self) -> u32 { - self.options.get_flags() + self.options.flags } /// Character used as 'fill' whenever there is alignment. @@ -1872,7 +1872,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn fill(&self) -> char { - self.options.get_fill() + self.options.fill } /// Flag indicating what form of alignment was requested. @@ -1907,7 +1907,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags_align", since = "1.28.0")] pub fn align(&self) -> Option { - self.options.get_align() + self.options.align } /// Optionally specified integer width that the output should be. @@ -1937,7 +1937,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn width(&self) -> Option { - self.options.get_width() + self.options.width } /// Optionally specified precision for numeric types. Alternatively, the @@ -1968,7 +1968,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn precision(&self) -> Option { - self.options.get_precision() + self.options.precision } /// Determines if the `+` flag was specified. @@ -2000,7 +2000,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_plus(&self) -> bool { - self.options.get_sign() == Some(Sign::Plus) + self.options.flags & (1 << rt::Flag::SignPlus as u32) != 0 } /// Determines if the `-` flag was specified. @@ -2029,7 +2029,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_minus(&self) -> bool { - self.options.get_sign() == Some(Sign::Minus) + self.options.flags & (1 << rt::Flag::SignMinus as u32) != 0 } /// Determines if the `#` flag was specified. @@ -2057,7 +2057,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn alternate(&self) -> bool { - self.options.get_alternate() + self.options.flags & (1 << rt::Flag::Alternate as u32) != 0 } /// Determines if the `0` flag was specified. @@ -2083,7 +2083,7 @@ impl<'a> Formatter<'a> { #[must_use] #[stable(feature = "fmt_flags", since = "1.5.0")] pub fn sign_aware_zero_pad(&self) -> bool { - self.options.get_sign_aware_zero_pad() + self.options.flags & (1 << rt::Flag::SignAwareZeroPad as u32) != 0 } // FIXME: Decide what public API we want for these two flags. @@ -2635,7 +2635,7 @@ impl Debug for char { #[stable(feature = "rust1", since = "1.0.0")] impl Display for char { fn fmt(&self, f: &mut Formatter<'_>) -> Result { - if f.width().is_none() && f.precision().is_none() { + if f.options.width.is_none() && f.options.precision.is_none() { f.write_char(*self) } else { f.pad(self.encode_utf8(&mut [0; 4])) @@ -2659,28 +2659,26 @@ impl Pointer for *const T { /// /// [problematic]: https://github.com/rust-lang/rust/issues/95489 pub(crate) fn pointer_fmt_inner(ptr_addr: usize, f: &mut Formatter<'_>) -> Result { - let old_width = f.width(); - let old_alternate = f.alternate(); - let old_zero_pad = f.sign_aware_zero_pad(); + let old_width = f.options.width; + let old_flags = f.options.flags; // The alternate flag is already treated by LowerHex as being special- // it denotes whether to prefix with 0x. We use it to work out whether // or not to zero extend, and then unconditionally set it to get the // prefix. if f.alternate() { - f.options.sign_aware_zero_pad(true); + f.options.flags |= 1 << (rt::Flag::SignAwareZeroPad as u32); - if f.width().is_none() { - f.options.width(Some((usize::BITS / 4) as usize + 2)); + if f.options.width.is_none() { + f.options.width = Some((usize::BITS / 4) as usize + 2); } } - f.options.alternate(true); + f.options.flags |= 1 << (rt::Flag::Alternate as u32); let ret = LowerHex::fmt(&ptr_addr, f); - f.options.width(old_width); - f.options.alternate(old_alternate); - f.options.sign_aware_zero_pad(old_zero_pad); + f.options.width = old_width; + f.options.flags = old_flags; ret } diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-abort.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-abort.diff index b190868912e22..d774ee4316207 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-abort.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-abort.diff @@ -7,135 +7,132 @@ debug upper => _3; let mut _0: std::result::Result<(), std::fmt::Error>; let _4: bool; - let mut _5: &std::fmt::Formatter<'_>; - let mut _7: std::option::Option; - let mut _8: isize; - let mut _10: &mut std::fmt::Formatter<'_>; - let mut _11: &T; - let mut _12: core::num::flt2dec::Sign; + let mut _6: std::option::Option; + let mut _7: isize; + let mut _9: &mut std::fmt::Formatter<'_>; + let mut _10: &T; + let mut _11: core::num::flt2dec::Sign; + let mut _12: u32; let mut _13: u32; - let mut _14: u32; - let mut _15: usize; - let mut _16: bool; - let mut _17: &mut std::fmt::Formatter<'_>; - let mut _18: &T; - let mut _19: core::num::flt2dec::Sign; - let mut _20: bool; + let mut _14: usize; + let mut _15: bool; + let mut _16: &mut std::fmt::Formatter<'_>; + let mut _17: &T; + let mut _18: core::num::flt2dec::Sign; + let mut _19: bool; scope 1 { debug force_sign => _4; - let _6: core::num::flt2dec::Sign; + let _5: core::num::flt2dec::Sign; scope 2 { - debug sign => _6; + debug sign => _5; scope 3 { - debug precision => _9; - let _9: usize; - scope 4 (inlined Formatter::<'_>::precision) { + debug precision => _8; + let _8: usize; + scope 5 (inlined Formatter::<'_>::precision) { debug self => _1; - let mut _21: &std::fmt::FormattingOptions; - scope 5 (inlined FormattingOptions::get_precision) { - debug self => _21; - } } } } } + scope 4 (inlined Formatter::<'_>::sign_plus) { + debug self => _1; + let mut _20: u32; + let mut _21: u32; + } bb0: { StorageLive(_4); + StorageLive(_20); + StorageLive(_21); + _21 = (((*_1).0: std::fmt::FormattingOptions).0: u32); + _20 = BitAnd(move _21, const 1_u32); + StorageDead(_21); + _4 = Ne(move _20, const 0_u32); + StorageDead(_20); StorageLive(_5); - _5 = &(*_1); - _4 = Formatter::<'_>::sign_plus(move _5) -> [return: bb1, unwind unreachable]; + switchInt(_4) -> [0: bb2, otherwise: bb1]; } bb1: { - StorageDead(_5); - StorageLive(_6); - switchInt(_4) -> [0: bb3, otherwise: bb2]; +- _5 = MinusPlus; ++ _5 = const MinusPlus; + goto -> bb3; } bb2: { -- _6 = MinusPlus; -+ _6 = const MinusPlus; - goto -> bb4; +- _5 = core::num::flt2dec::Sign::Minus; ++ _5 = const core::num::flt2dec::Sign::Minus; + goto -> bb3; } bb3: { -- _6 = core::num::flt2dec::Sign::Minus; -+ _6 = const core::num::flt2dec::Sign::Minus; - goto -> bb4; + StorageLive(_6); + _6 = (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); + _7 = discriminant(_6); + switchInt(move _7) -> [1: bb4, otherwise: bb6]; } bb4: { - StorageLive(_7); - StorageLive(_21); - _21 = &((*_1).0: std::fmt::FormattingOptions); - _7 = (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); - StorageDead(_21); - _8 = discriminant(_7); - switchInt(move _8) -> [1: bb5, otherwise: bb7]; - } - - bb5: { -- StorageLive(_9); +- StorageLive(_8); + nop; - _9 = ((_7 as Some).0: usize); + _8 = ((_6 as Some).0: usize); + StorageLive(_9); + _9 = _1; StorageLive(_10); - _10 = _1; + _10 = _2; StorageLive(_11); - _11 = _2; + _11 = _5; StorageLive(_12); - _12 = _6; StorageLive(_13); StorageLive(_14); - StorageLive(_15); - _15 = _9; -- _14 = move _15 as u32 (IntToInt); -+ _14 = _9 as u32 (IntToInt); - StorageDead(_15); - _13 = Add(move _14, const 1_u32); + _14 = _8; +- _13 = move _14 as u32 (IntToInt); ++ _13 = _8 as u32 (IntToInt); StorageDead(_14); - StorageLive(_16); - _16 = _3; -- _0 = float_to_exponential_common_exact::(move _10, move _11, move _12, move _13, move _16) -> [return: bb6, unwind unreachable]; -+ _0 = float_to_exponential_common_exact::(_1, _2, move _12, move _13, _3) -> [return: bb6, unwind unreachable]; + _12 = Add(move _13, const 1_u32); + StorageDead(_13); + StorageLive(_15); + _15 = _3; +- _0 = float_to_exponential_common_exact::(move _9, move _10, move _11, move _12, move _15) -> [return: bb5, unwind unreachable]; ++ _0 = float_to_exponential_common_exact::(_1, _2, move _11, move _12, _3) -> [return: bb5, unwind unreachable]; } - bb6: { - StorageDead(_16); - StorageDead(_13); + bb5: { + StorageDead(_15); StorageDead(_12); StorageDead(_11); StorageDead(_10); -- StorageDead(_9); + StorageDead(_9); +- StorageDead(_8); + nop; - goto -> bb9; + goto -> bb8; } - bb7: { + bb6: { + StorageLive(_16); + _16 = _1; StorageLive(_17); - _17 = _1; + _17 = _2; StorageLive(_18); - _18 = _2; + _18 = _5; StorageLive(_19); - _19 = _6; - StorageLive(_20); - _20 = _3; -- _0 = float_to_exponential_common_shortest::(move _17, move _18, move _19, move _20) -> [return: bb8, unwind unreachable]; -+ _0 = float_to_exponential_common_shortest::(_1, _2, move _19, _3) -> [return: bb8, unwind unreachable]; + _19 = _3; +- _0 = float_to_exponential_common_shortest::(move _16, move _17, move _18, move _19) -> [return: bb7, unwind unreachable]; ++ _0 = float_to_exponential_common_shortest::(_1, _2, move _18, _3) -> [return: bb7, unwind unreachable]; } - bb8: { - StorageDead(_20); + bb7: { StorageDead(_19); StorageDead(_18); StorageDead(_17); - goto -> bb9; + StorageDead(_16); + goto -> bb8; } - bb9: { - StorageDead(_6); + bb8: { + StorageDead(_5); StorageDead(_4); - StorageDead(_7); + StorageDead(_6); return; } } diff --git a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-unwind.diff b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-unwind.diff index 665930c5f70a2..7ec84843d27d9 100644 --- a/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-unwind.diff +++ b/tests/mir-opt/funky_arms.float_to_exponential_common.GVN.panic-unwind.diff @@ -7,135 +7,132 @@ debug upper => _3; let mut _0: std::result::Result<(), std::fmt::Error>; let _4: bool; - let mut _5: &std::fmt::Formatter<'_>; - let mut _7: std::option::Option; - let mut _8: isize; - let mut _10: &mut std::fmt::Formatter<'_>; - let mut _11: &T; - let mut _12: core::num::flt2dec::Sign; + let mut _6: std::option::Option; + let mut _7: isize; + let mut _9: &mut std::fmt::Formatter<'_>; + let mut _10: &T; + let mut _11: core::num::flt2dec::Sign; + let mut _12: u32; let mut _13: u32; - let mut _14: u32; - let mut _15: usize; - let mut _16: bool; - let mut _17: &mut std::fmt::Formatter<'_>; - let mut _18: &T; - let mut _19: core::num::flt2dec::Sign; - let mut _20: bool; + let mut _14: usize; + let mut _15: bool; + let mut _16: &mut std::fmt::Formatter<'_>; + let mut _17: &T; + let mut _18: core::num::flt2dec::Sign; + let mut _19: bool; scope 1 { debug force_sign => _4; - let _6: core::num::flt2dec::Sign; + let _5: core::num::flt2dec::Sign; scope 2 { - debug sign => _6; + debug sign => _5; scope 3 { - debug precision => _9; - let _9: usize; - scope 4 (inlined Formatter::<'_>::precision) { + debug precision => _8; + let _8: usize; + scope 5 (inlined Formatter::<'_>::precision) { debug self => _1; - let mut _21: &std::fmt::FormattingOptions; - scope 5 (inlined FormattingOptions::get_precision) { - debug self => _21; - } } } } } + scope 4 (inlined Formatter::<'_>::sign_plus) { + debug self => _1; + let mut _20: u32; + let mut _21: u32; + } bb0: { StorageLive(_4); + StorageLive(_20); + StorageLive(_21); + _21 = (((*_1).0: std::fmt::FormattingOptions).0: u32); + _20 = BitAnd(move _21, const 1_u32); + StorageDead(_21); + _4 = Ne(move _20, const 0_u32); + StorageDead(_20); StorageLive(_5); - _5 = &(*_1); - _4 = Formatter::<'_>::sign_plus(move _5) -> [return: bb1, unwind continue]; + switchInt(_4) -> [0: bb2, otherwise: bb1]; } bb1: { - StorageDead(_5); - StorageLive(_6); - switchInt(_4) -> [0: bb3, otherwise: bb2]; +- _5 = MinusPlus; ++ _5 = const MinusPlus; + goto -> bb3; } bb2: { -- _6 = MinusPlus; -+ _6 = const MinusPlus; - goto -> bb4; +- _5 = core::num::flt2dec::Sign::Minus; ++ _5 = const core::num::flt2dec::Sign::Minus; + goto -> bb3; } bb3: { -- _6 = core::num::flt2dec::Sign::Minus; -+ _6 = const core::num::flt2dec::Sign::Minus; - goto -> bb4; + StorageLive(_6); + _6 = (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); + _7 = discriminant(_6); + switchInt(move _7) -> [1: bb4, otherwise: bb6]; } bb4: { - StorageLive(_7); - StorageLive(_21); - _21 = &((*_1).0: std::fmt::FormattingOptions); - _7 = (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); - StorageDead(_21); - _8 = discriminant(_7); - switchInt(move _8) -> [1: bb5, otherwise: bb7]; - } - - bb5: { -- StorageLive(_9); +- StorageLive(_8); + nop; - _9 = ((_7 as Some).0: usize); + _8 = ((_6 as Some).0: usize); + StorageLive(_9); + _9 = _1; StorageLive(_10); - _10 = _1; + _10 = _2; StorageLive(_11); - _11 = _2; + _11 = _5; StorageLive(_12); - _12 = _6; StorageLive(_13); StorageLive(_14); - StorageLive(_15); - _15 = _9; -- _14 = move _15 as u32 (IntToInt); -+ _14 = _9 as u32 (IntToInt); - StorageDead(_15); - _13 = Add(move _14, const 1_u32); + _14 = _8; +- _13 = move _14 as u32 (IntToInt); ++ _13 = _8 as u32 (IntToInt); StorageDead(_14); - StorageLive(_16); - _16 = _3; -- _0 = float_to_exponential_common_exact::(move _10, move _11, move _12, move _13, move _16) -> [return: bb6, unwind continue]; -+ _0 = float_to_exponential_common_exact::(_1, _2, move _12, move _13, _3) -> [return: bb6, unwind continue]; + _12 = Add(move _13, const 1_u32); + StorageDead(_13); + StorageLive(_15); + _15 = _3; +- _0 = float_to_exponential_common_exact::(move _9, move _10, move _11, move _12, move _15) -> [return: bb5, unwind continue]; ++ _0 = float_to_exponential_common_exact::(_1, _2, move _11, move _12, _3) -> [return: bb5, unwind continue]; } - bb6: { - StorageDead(_16); - StorageDead(_13); + bb5: { + StorageDead(_15); StorageDead(_12); StorageDead(_11); StorageDead(_10); -- StorageDead(_9); + StorageDead(_9); +- StorageDead(_8); + nop; - goto -> bb9; + goto -> bb8; } - bb7: { + bb6: { + StorageLive(_16); + _16 = _1; StorageLive(_17); - _17 = _1; + _17 = _2; StorageLive(_18); - _18 = _2; + _18 = _5; StorageLive(_19); - _19 = _6; - StorageLive(_20); - _20 = _3; -- _0 = float_to_exponential_common_shortest::(move _17, move _18, move _19, move _20) -> [return: bb8, unwind continue]; -+ _0 = float_to_exponential_common_shortest::(_1, _2, move _19, _3) -> [return: bb8, unwind continue]; + _19 = _3; +- _0 = float_to_exponential_common_shortest::(move _16, move _17, move _18, move _19) -> [return: bb7, unwind continue]; ++ _0 = float_to_exponential_common_shortest::(_1, _2, move _18, _3) -> [return: bb7, unwind continue]; } - bb8: { - StorageDead(_20); + bb7: { StorageDead(_19); StorageDead(_18); StorageDead(_17); - goto -> bb9; + StorageDead(_16); + goto -> bb8; } - bb9: { - StorageDead(_6); + bb8: { + StorageDead(_5); StorageDead(_4); - StorageDead(_7); + StorageDead(_6); return; } }