Skip to content
Merged
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
59 changes: 0 additions & 59 deletions src/config/config_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -435,65 +435,6 @@ macro_rules! create_config {
)+
}

/// Reduces the maximum width of the configuration.
///
/// This method is intended to be used when creating a forked
/// configuration for a particular formatting context in which the
/// total available width needs to be reduced. This reduces the
/// size of max_width, and all other properties affected by small
/// heuristics, without treating those adjustments as overrides.
///
/// As an example use case, this method is used when formatting
/// arms within a declarative macro.
#[allow(unreachable_pub)]
pub fn reduce_max_width(&mut self, delta: usize) {
self.adjust_max_width(-(delta.min(isize::MAX as usize) as isize));
}

/// Increases the maximum width of the configuration.
///
/// This method is intended to be used when creating a forked
/// configuration for a particular formatting context in which the
/// total available width needs to be increased. This increases the
/// size of max_width, and all other properties affected by small
/// heuristics, without treating those adjustments as overrides.
///
/// As an example use case, this method is used when formatting
/// arms within a declarative macro.
#[allow(unreachable_pub)]
pub fn increase_max_width(&mut self, delta: usize) {
self.adjust_max_width(delta.min(isize::MAX as usize) as isize);
}

/// Adjusts the maximum width of the configuration.
///
/// This method is intended to be used when creating a forked
/// configuration for a particular formatting context in which the
/// total available width needs to be reduced. This adjusts the
/// size of max_width, and all other properties affected by small
/// heuristics, without treating those adjustments as overrides.
///
/// As an example use case, this method is used when formatting
/// arms within a declarative macro.
fn adjust_max_width(&mut self, delta: isize) {
// NB: we need to either saturate or report an error (or some kind of testable
// warning). I picked to saturate here.
let adjust = |value: usize| value.saturating_add_signed(delta);

self.array_width.2 = adjust(self.array_width.2);
self.attr_fn_like_width.2 = adjust(self.attr_fn_like_width.2);
self.chain_width.2 = adjust(self.chain_width.2);
self.fn_call_width.2 = adjust(self.fn_call_width.2);
self.single_line_if_else_max_width.2 = adjust(self.single_line_if_else_max_width.2);
self.single_line_let_else_max_width.2 =
adjust(self.single_line_let_else_max_width.2);
self.struct_lit_width.2 = adjust(self.struct_lit_width.2);
self.struct_variant_width.2 = adjust(self.struct_variant_width.2);

self.max_width.2 = adjust(self.max_width.2);
self.set_heuristics();
}

fn set_width_heuristics(&mut self, heuristics: WidthHeuristics) {
let max_width = self.max_width.2;
let get_width_value = |
Expand Down
17 changes: 0 additions & 17 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -689,23 +689,6 @@ 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_config_used_to_toml() {
let config = Config::default();
Expand Down
6 changes: 4 additions & 2 deletions src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1365,13 +1365,15 @@ impl MacroBranch {
} else {
shape.indent.block_indent(&config)
};
config.reduce_max_width(body_indent.width());
let new_width = config.max_width() - body_indent.width();
config.set().max_width(new_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 => {
config.increase_max_width(config.tab_spaces());
let new_width = new_width + config.tab_spaces();
config.set().max_width(new_width);
match crate::format_code_block(&body_str, &config, true) {
Some(new_body) => new_body,
None => {
Expand Down
7 changes: 1 addition & 6 deletions tests/target/issue-6651/custom_width.rs

@jieyouxu jieyouxu Nov 2, 2026 •

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

I think I'd like to have stronger test coverage for the reland.

(This is now more obvious on hindsight...)

View changes since the review

Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@ macro_rules! medium {

macro_rules! long {
() => {
m!(
aaaaaaaaaaaaaaaa,
bbbbbbbbbbbbbbbb,
cccccccccccccccc,
ddddddddddddddd
);
m!(aaaaaaaaaaaaaaaa, bbbbbbbbbbbbbbbb, cccccccccccccccc, ddddddddddddddd);
};
}
Loading