diff --git a/src/config/config_type.rs b/src/config/config_type.rs index 69749feb0f3..152332d3c7d 100644 --- a/src/config/config_type.rs +++ b/src/config/config_type.rs @@ -435,6 +435,44 @@ macro_rules! create_config { )+ } + /// Reduce `max_width` by `delta` saturating on underflow. See `adjust_max_width`. + #[allow(unreachable_pub)] + pub fn reduce_max_width(&mut self, delta: usize) { + self.adjust_max_width(-(delta.min(isize::MAX as usize) as isize)); + } + + /// Increase `max_width` by `delta` saturating on overflow. See `adjust_max_width`. + #[allow(unreachable_pub)] + pub fn increase_max_width(&mut self, delta: usize) { + self.adjust_max_width(delta.min(isize::MAX as usize) as isize); + } + + /// Adjust `max_width` by `delta` in a forked config (example: macro body at zero + /// indent). In particular: + /// + /// - Width options not set explicitly are recomputed from new `max_width` via + /// heuristics. + /// - Explicit widths will keep their value but capped at the new `max_width`. + /// + /// A later increase in `max_width` will _not_ restore a previously capped value. + fn adjust_max_width(&mut self, delta: isize) { + let max_width = self.max_width.2.saturating_add_signed(delta); + self.max_width.2 = max_width; + + self.array_width.2 = self.array_width.2.min(max_width); + self.attr_fn_like_width.2 = self.attr_fn_like_width.2.min(max_width); + self.chain_width.2 = self.chain_width.2.min(max_width); + self.fn_call_width.2 = self.fn_call_width.2.min(max_width); + self.single_line_if_else_max_width.2 = + self.single_line_if_else_max_width.2.min(max_width); + self.single_line_let_else_max_width.2 = + self.single_line_let_else_max_width.2.min(max_width); + self.struct_lit_width.2 = self.struct_lit_width.2.min(max_width); + self.struct_variant_width.2 = self.struct_variant_width.2.min(max_width); + + self.set_heuristics(); + } + fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) { let max_width = self.max_width.2; let get_width_value = | diff --git a/src/config/mod.rs b/src/config/mod.rs index 0180bb89841..05cd5610d91 100644 --- a/src/config/mod.rs +++ b/src/config/mod.rs @@ -689,6 +689,191 @@ mod test { assert_eq!(config.verbose(), Verbosity::Normal); } + #[test] + fn test_adjust_max_width() { + // Regression tests for #7147, where we didn't handle overflows / integral cast outside of + // range properly. + + // Reduce with huge amount saturates to zero instead of wraparound. + let mut config = Config::default(); + config.reduce_max_width(usize::MAX); + assert_eq!(config.max_width(), 0); + + // Increase with huge amount saturates instead of wraparound. + let mut config = Config::default(); + config.set().max_width(usize::MAX - 1); + config.increase_max_width(2); + assert_eq!(config.max_width(), usize::MAX); + } + + #[test] + fn test_reduce_max_width_heuristics_default() { + // Reducing `max_width` does not change unset width settings that participate in + // `use_small_heuristics` if the value of `max_width` is equal to the default value. + let toml = r#" + use_small_heuristics = "Default" + "#; + let mut config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), 60); + assert_eq!(config.attr_fn_like_width(), 70); + assert_eq!(config.chain_width(), 60); + assert_eq!(config.fn_call_width(), 60); + assert_eq!(config.single_line_if_else_max_width(), 50); + assert_eq!(config.single_line_let_else_max_width(), 50); + assert_eq!(config.struct_lit_width(), 18); + assert_eq!(config.struct_variant_width(), 35); + } + + #[test] + fn test_reduce_max_width_heuristics_default_small_max_width() { + // Reducing `max_width` does not change unset width settings that participate in + // `use_small_heuristics` if the value of `max_width` is less than the default value. + let toml = r#" + use_small_heuristics = "Default" + max_width = 50 + "#; + let mut config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), 60); + assert_eq!(config.attr_fn_like_width(), 70); + assert_eq!(config.chain_width(), 60); + assert_eq!(config.fn_call_width(), 60); + assert_eq!(config.single_line_if_else_max_width(), 50); + assert_eq!(config.single_line_let_else_max_width(), 50); + assert_eq!(config.struct_lit_width(), 18); + assert_eq!(config.struct_variant_width(), 35); + } + + #[test] + fn test_reduce_max_width_heuristics_default_large_max_width() { + // Reducing `max_width` scales unset width settings that participate in + // `use_small_heuristics` if the value of `max_width` is greater than the default value. + let toml = r#" + use_small_heuristics = "Default" + max_width = 200 + "#; + let mut config = Config::from_toml(toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), 114); + assert_eq!(config.attr_fn_like_width(), 133); + assert_eq!(config.chain_width(), 114); + assert_eq!(config.fn_call_width(), 114); + assert_eq!(config.single_line_if_else_max_width(), 95); + assert_eq!(config.single_line_let_else_max_width(), 95); + assert_eq!(config.struct_lit_width(), 34); + assert_eq!(config.struct_variant_width(), 67); + } + + #[test] + fn test_reduce_max_width_heuristics_off() { + // Reducing `max_width` leaves unset width settings that participate in + // `use_small_heuristics` unchanged if `use_small_heuristics` is set to `Off`. + for max_width in [50, 100, 200] { + let toml = formatdoc! {r#" + use_small_heuristics = "Off" + max_width = {max_width} + "#}; + let mut config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), usize::MAX); + assert_eq!(config.attr_fn_like_width(), usize::MAX); + assert_eq!(config.chain_width(), usize::MAX); + assert_eq!(config.fn_call_width(), usize::MAX); + assert_eq!(config.single_line_if_else_max_width(), 0); + assert_eq!(config.single_line_let_else_max_width(), 0); + assert_eq!(config.struct_lit_width(), 0); + assert_eq!(config.struct_variant_width(), 0); + } + } + + #[test] + fn test_reduce_max_width_heuristics_max() { + // Reducing `max_width` sets unset width settings that participate in + // `use_small_heuristics` to the adjusted value of `max_width` if `use_small_heuristics` is + // set to `Max`. + for max_width in [50, 100, 200] { + let toml = formatdoc! {r#" + use_small_heuristics = "Max" + max_width = {max_width} + "#}; + let mut config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), max_width - 8); + assert_eq!(config.attr_fn_like_width(), max_width - 8); + assert_eq!(config.chain_width(), max_width - 8); + assert_eq!(config.fn_call_width(), max_width - 8); + assert_eq!(config.single_line_if_else_max_width(), max_width - 8); + assert_eq!(config.single_line_let_else_max_width(), max_width - 8); + assert_eq!(config.struct_lit_width(), max_width - 8); + assert_eq!(config.struct_variant_width(), max_width - 8); + } + } + + #[test] + fn test_reduce_max_width_keeps_explicit_widths() { + // Reducing `max_width` preserves explicitly set width settings that participate in + // `use_small_heuristics` as long as they do not exceed the adjusted value of `max_width`. + for use_small_heuristics in ["Default", "Off", "Max"] { + for max_width in [50, 100, 200] { + let toml = formatdoc! {r#" + use_small_heuristics = "{use_small_heuristics}" + max_width = {max_width} + array_width = 5 + attr_fn_like_width = 10 + chain_width = 15 + fn_call_width = 20 + single_line_if_else_max_width = 25 + single_line_let_else_max_width = 30 + struct_lit_width = 35 + struct_variant_width = 40 + "#}; + let mut config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), 5); + assert_eq!(config.attr_fn_like_width(), 10); + assert_eq!(config.chain_width(), 15); + assert_eq!(config.fn_call_width(), 20); + assert_eq!(config.single_line_if_else_max_width(), 25); + assert_eq!(config.single_line_let_else_max_width(), 30); + assert_eq!(config.struct_lit_width(), 35); + assert_eq!(config.struct_variant_width(), 40); + } + } + } + + #[test] + fn test_reduce_max_width_caps_explicit_widths() { + // Reducing `max_width` caps explicitly set width settings that participate in + // `use_small_heuristics` if they are above the adjusted value of `max_width`. + for use_small_heuristics in ["Default", "Off", "Max"] { + for max_width in [50, 100, 200] { + let toml = formatdoc! {r#" + use_small_heuristics = "{use_small_heuristics}" + max_width = {max_width} + array_width = {max_width} + attr_fn_like_width = {max_width} + chain_width = {max_width} + fn_call_width = {max_width} + single_line_if_else_max_width = {max_width} + single_line_let_else_max_width = {max_width} + struct_lit_width = {max_width} + struct_variant_width = {max_width} + "#}; + let mut config = Config::from_toml(&toml, Path::new("./rustfmt.toml")).unwrap(); + config.reduce_max_width(8); + assert_eq!(config.array_width(), max_width - 8); + assert_eq!(config.attr_fn_like_width(), max_width - 8); + assert_eq!(config.chain_width(), max_width - 8); + assert_eq!(config.fn_call_width(), max_width - 8); + assert_eq!(config.single_line_if_else_max_width(), max_width - 8); + assert_eq!(config.single_line_let_else_max_width(), max_width - 8); + assert_eq!(config.struct_lit_width(), max_width - 8); + assert_eq!(config.struct_variant_width(), max_width - 8); + } + } + } + #[test] fn test_config_used_to_toml() { let config = Config::default(); diff --git a/src/macros.rs b/src/macros.rs index d5c27400106..421094b25df 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1365,15 +1365,13 @@ impl MacroBranch { } else { shape.indent.block_indent(&config) }; - let new_width = config.max_width() - body_indent.width(); - config.set().max_width(new_width); + config.reduce_max_width(body_indent.width()); // First try to format as items, then as statements. let new_body_snippet = match crate::format_snippet(&body_str, &config, true) { Some(new_body) => new_body, None => { - let new_width = new_width + config.tab_spaces(); - config.set().max_width(new_width); + config.increase_max_width(config.tab_spaces()); match crate::format_code_block(&body_str, &config, true) { Some(new_body) => new_body, None => { diff --git a/tests/source/issue-6651/chain_width.rs b/tests/source/issue-6651/chain_width.rs new file mode 100644 index 00000000000..fc1cac107bc --- /dev/null +++ b/tests/source/issue-6651/chain_width.rs @@ -0,0 +1,17 @@ +//@ rustfmt-format_macro_bodies: true +//@ rustfmt-chain_width: 72 + +macro_rules! inside_macro { + () => { + let value = aaaaaaaaaaaaaaaa + .bbbbbbbbbbbbbbbb() + .cccccccccccccccc() + .ddddddddddddd(); + }; +} + +fn outside_macro() { + if true { + let value = aaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbb().cccccccccccccccc().ddddddddddddd(); + } +} diff --git a/tests/source/issue-6651/custom_width.rs b/tests/source/issue-6651/custom_width.rs index 6bdfb562f53..85775a26ed4 100644 --- a/tests/source/issue-6651/custom_width.rs +++ b/tests/source/issue-6651/custom_width.rs @@ -19,3 +19,9 @@ macro_rules! long { m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, ddddddddddddddd); }; } + +macro_rules! too_long { + () => { + m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, dddddddddddddddd); + }; +} diff --git a/tests/target/issue-6651/chain_width.rs b/tests/target/issue-6651/chain_width.rs new file mode 100644 index 00000000000..569bd34adb0 --- /dev/null +++ b/tests/target/issue-6651/chain_width.rs @@ -0,0 +1,14 @@ +//@ rustfmt-format_macro_bodies: true +//@ rustfmt-chain_width: 72 + +macro_rules! inside_macro { + () => { + let value = aaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbb().cccccccccccccccc().ddddddddddddd(); + }; +} + +fn outside_macro() { + if true { + let value = aaaaaaaaaaaaaaaa.bbbbbbbbbbbbbbbb().cccccccccccccccc().ddddddddddddd(); + } +} diff --git a/tests/target/issue-6651/custom_width.rs b/tests/target/issue-6651/custom_width.rs index 6bdfb562f53..9eafd527142 100644 --- a/tests/target/issue-6651/custom_width.rs +++ b/tests/target/issue-6651/custom_width.rs @@ -19,3 +19,14 @@ macro_rules! long { m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, ddddddddddddddd); }; } + +macro_rules! too_long { + () => { + m!( + aaaaaaaaaaaaaaaa, + bbbbbbbbbbbbbbbb, + cccccccccccccccc, + dddddddddddddddd + ); + }; +}