From 31a56571091a9bdcc3fc9f4dff9e4fafeddc2ea8 Mon Sep 17 00:00:00 2001 From: Elias Holzmann <9659253+EliasHolzmann@users.noreply.github.com> Date: Sun, 3 Mar 2024 05:17:55 +0100 Subject: [PATCH] Access members of `FormattingOptions` directly instead of via getters/setters --- library/core/src/fmt/float.rs | 6 +- library/core/src/fmt/mod.rs | 74 ++++---- ...to_exponential_common.GVN.panic-abort.diff | 165 +++++++++--------- ...o_exponential_common.GVN.panic-unwind.diff | 165 +++++++++--------- 4 files changed, 205 insertions(+), 205 deletions(-) diff --git a/library/core/src/fmt/float.rs b/library/core/src/fmt/float.rs index ee7a8f08f19..3f10158193d 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; @@ -162,7 +162,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 { @@ -180,7 +180,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 da89acdcd1e..14c70065101 100644 --- a/library/core/src/fmt/mod.rs +++ b/library/core/src/fmt/mod.rs @@ -1466,14 +1466,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 @@ -1613,7 +1613,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 => { @@ -1682,12 +1682,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. @@ -1704,7 +1704,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), @@ -1745,10 +1745,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. @@ -1760,12 +1760,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; @@ -1774,8 +1774,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. @@ -1792,8 +1792,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 @@ -1919,7 +1919,7 @@ impl<'a> Formatter<'a> { or `sign_aware_zero_pad` methods instead" )] pub fn flags(&self) -> u32 { - self.options.get_flags() + self.options.flags } /// Returns the character used as 'fill' whenever there is alignment. @@ -1952,7 +1952,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 } /// Returns a flag indicating what form of alignment was requested. @@ -1987,7 +1987,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 } /// Returns the optionally specified integer width that the output should be. @@ -2017,7 +2017,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 } /// Returns the optionally specified precision for numeric types. @@ -2048,7 +2048,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. @@ -2080,7 +2080,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. @@ -2109,7 +2109,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. @@ -2137,7 +2137,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. @@ -2163,7 +2163,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. @@ -2753,7 +2753,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])) @@ -2777,28 +2777,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 ed04bc7dd56..a1be927e1c0 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,133 +7,134 @@ 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) { - scope 5 (inlined FormattingOptions::get_precision) { - } + debug precision => _8; + let _8: usize; + scope 5 (inlined Formatter::<'_>::precision) { } } } } + scope 4 (inlined Formatter::<'_>::sign_plus) { + let mut _20: u32; + let mut _21: u32; + } bb0: { StorageLive(_4); + StorageLive(_20); + StorageLive(_21); + _21 = copy (((*_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(copy _4) -> [0: bb2, otherwise: bb1]; } bb1: { - StorageDead(_5); - StorageLive(_6); - switchInt(copy _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 = copy (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); + _7 = discriminant(_6); + switchInt(move _7) -> [1: bb4, 0: bb6, otherwise: bb9]; } bb4: { - StorageLive(_7); - _7 = copy (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); - _8 = discriminant(_7); - switchInt(move _8) -> [1: bb5, 0: bb7, otherwise: bb10]; +- StorageLive(_8); ++ nop; + _8 = copy ((_6 as Some).0: usize); + StorageLive(_9); + _9 = copy _1; + StorageLive(_10); + _10 = copy _2; + StorageLive(_11); + _11 = copy _5; + StorageLive(_12); + StorageLive(_13); + StorageLive(_14); + _14 = copy _8; +- _13 = move _14 as u32 (IntToInt); ++ _13 = copy _8 as u32 (IntToInt); + StorageDead(_14); + _12 = Add(move _13, const 1_u32); + StorageDead(_13); + StorageLive(_15); + _15 = copy _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::(copy _1, copy _2, move _11, move _12, copy _3) -> [return: bb5, unwind unreachable]; } bb5: { -- StorageLive(_9); -+ nop; - _9 = copy ((_7 as Some).0: usize); - StorageLive(_10); - _10 = copy _1; - StorageLive(_11); - _11 = copy _2; - StorageLive(_12); - _12 = copy _6; - StorageLive(_13); - StorageLive(_14); - StorageLive(_15); - _15 = copy _9; -- _14 = move _15 as u32 (IntToInt); -+ _14 = copy _9 as u32 (IntToInt); StorageDead(_15); - _13 = Add(move _14, const 1_u32); - StorageDead(_14); - StorageLive(_16); - _16 = copy _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::(copy _1, copy _2, move _12, move _13, copy _3) -> [return: bb6, unwind unreachable]; - } - - bb6: { - StorageDead(_16); - StorageDead(_13); StorageDead(_12); StorageDead(_11); StorageDead(_10); -- StorageDead(_9); + StorageDead(_9); +- StorageDead(_8); + nop; - goto -> bb9; + goto -> bb8; + } + + bb6: { + StorageLive(_16); + _16 = copy _1; + StorageLive(_17); + _17 = copy _2; + StorageLive(_18); + _18 = copy _5; + StorageLive(_19); + _19 = copy _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::(copy _1, copy _2, move _18, copy _3) -> [return: bb7, unwind unreachable]; } bb7: { - StorageLive(_17); - _17 = copy _1; - StorageLive(_18); - _18 = copy _2; - StorageLive(_19); - _19 = copy _6; - StorageLive(_20); - _20 = copy _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::(copy _1, copy _2, move _19, copy _3) -> [return: bb8, unwind unreachable]; - } - - bb8: { - StorageDead(_20); 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; } - bb10: { + bb9: { unreachable; } } 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 33640bb2d8f..87ab71feb2f 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,133 +7,134 @@ 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) { - scope 5 (inlined FormattingOptions::get_precision) { - } + debug precision => _8; + let _8: usize; + scope 5 (inlined Formatter::<'_>::precision) { } } } } + scope 4 (inlined Formatter::<'_>::sign_plus) { + let mut _20: u32; + let mut _21: u32; + } bb0: { StorageLive(_4); + StorageLive(_20); + StorageLive(_21); + _21 = copy (((*_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(copy _4) -> [0: bb2, otherwise: bb1]; } bb1: { - StorageDead(_5); - StorageLive(_6); - switchInt(copy _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 = copy (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); + _7 = discriminant(_6); + switchInt(move _7) -> [1: bb4, 0: bb6, otherwise: bb9]; } bb4: { - StorageLive(_7); - _7 = copy (((*_1).0: std::fmt::FormattingOptions).4: std::option::Option); - _8 = discriminant(_7); - switchInt(move _8) -> [1: bb5, 0: bb7, otherwise: bb10]; +- StorageLive(_8); ++ nop; + _8 = copy ((_6 as Some).0: usize); + StorageLive(_9); + _9 = copy _1; + StorageLive(_10); + _10 = copy _2; + StorageLive(_11); + _11 = copy _5; + StorageLive(_12); + StorageLive(_13); + StorageLive(_14); + _14 = copy _8; +- _13 = move _14 as u32 (IntToInt); ++ _13 = copy _8 as u32 (IntToInt); + StorageDead(_14); + _12 = Add(move _13, const 1_u32); + StorageDead(_13); + StorageLive(_15); + _15 = copy _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::(copy _1, copy _2, move _11, move _12, copy _3) -> [return: bb5, unwind continue]; } bb5: { -- StorageLive(_9); -+ nop; - _9 = copy ((_7 as Some).0: usize); - StorageLive(_10); - _10 = copy _1; - StorageLive(_11); - _11 = copy _2; - StorageLive(_12); - _12 = copy _6; - StorageLive(_13); - StorageLive(_14); - StorageLive(_15); - _15 = copy _9; -- _14 = move _15 as u32 (IntToInt); -+ _14 = copy _9 as u32 (IntToInt); StorageDead(_15); - _13 = Add(move _14, const 1_u32); - StorageDead(_14); - StorageLive(_16); - _16 = copy _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::(copy _1, copy _2, move _12, move _13, copy _3) -> [return: bb6, unwind continue]; - } - - bb6: { - StorageDead(_16); - StorageDead(_13); StorageDead(_12); StorageDead(_11); StorageDead(_10); -- StorageDead(_9); + StorageDead(_9); +- StorageDead(_8); + nop; - goto -> bb9; + goto -> bb8; + } + + bb6: { + StorageLive(_16); + _16 = copy _1; + StorageLive(_17); + _17 = copy _2; + StorageLive(_18); + _18 = copy _5; + StorageLive(_19); + _19 = copy _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::(copy _1, copy _2, move _18, copy _3) -> [return: bb7, unwind continue]; } bb7: { - StorageLive(_17); - _17 = copy _1; - StorageLive(_18); - _18 = copy _2; - StorageLive(_19); - _19 = copy _6; - StorageLive(_20); - _20 = copy _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::(copy _1, copy _2, move _19, copy _3) -> [return: bb8, unwind continue]; - } - - bb8: { - StorageDead(_20); 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; } - bb10: { + bb9: { unreachable; } }