Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 = |
Expand Down
185 changes: 185 additions & 0 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand Down
6 changes: 2 additions & 4 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 => {
Expand Down
17 changes: 17 additions & 0 deletions tests/source/issue-6651/chain_width.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
6 changes: 6 additions & 0 deletions tests/source/issue-6651/custom_width.rs
Comment thread
csmulhern marked this conversation as resolved.
Resolved
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,9 @@ macro_rules! long {
m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, ddddddddddddddd);
};
}

macro_rules! too_long {
() => {
m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, dddddddddddddddd);
};
}

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

NB: I was looking at the logic here a bit more closely. I know obviously that the ui tests here won't demonstrate "ah the warning went away" (that's #6933), which is the point for this PR.

So my understanding is that, there's 3 cases we need to consider:

  1. Explicit width never set, i.e. default config
  2. Explicit width configured, but:
    • (A) > recomputed (reduced) max width, so it has to be capped to recomputed max width.
    • (B) <= recomputed (reduced) max width, so fits in recomputed max width.

Here:

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes, that's right. The only change from this PR should now be the warning.

  1. default.rs covers this.

  2. Covered by custom_width.rs and chain_width.rs:

    • A. custom_width.rs sets fn_call_width to 76, which is capped to 74 inside the macro. long fits within 74, so it stays on one line. The original Fixes adjustment of max_width within macros #6651 cut the width to 68 and wrapped it. too_long is over max_width, so it wraps regardless.

    • B. chain_width.rs was the piece missing in the original PR that covers this. It sets chain_width to 72 and uses a 70 character expression, so the expression stays on one line inside and outside the macro (unlike Fixes adjustment of max_width within macros #6651 which reduced chain_width and wrapped the macro version).

14 changes: 14 additions & 0 deletions tests/target/issue-6651/chain_width.rs
Original file line number Diff line number Diff line change
@@ -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();
}
}
11 changes: 11 additions & 0 deletions tests/target/issue-6651/custom_width.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,3 +19,14 @@ macro_rules! long {
m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, ddddddddddddddd);
};
}

macro_rules! too_long {
() => {
m!(
aaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccc,
dddddddddddddddd
);
};
}
Loading