Skip to content

fix: formatting macro with braces delimiter - #7031

Open
dswij wants to merge 1 commit into
rust-lang:mainfrom
dswij:issue-6747
Open

fix: formatting macro with braces delimiter#7031
dswij wants to merge 1 commit into
rust-lang:mainfrom
dswij:issue-6747

Conversation

@dswij

@dswij dswij commented Aug 16, 2026

Copy link
Copy Markdown
Member

Fixes #6747.

This PR makes it so that trim_left_preserve_layout does nothing when the first line contains a string start.

e.g., this will now not be formatted

macro! {"
...
"};

I'm not too satisfied with this PR; I think the formatting for macros with braces {,} needs to be reworked. I'm opening this PR as a starting point for that goal.

@rustbot rustbot added the S-waiting-on-review Status: awaiting review from the assignee but also interested parties. label Aug 16, 2026
Comment thread src/macros.rs
// For macro invocations with braces, always put a space between
// the `macro_name!` and `{ /* macro_body */ }` but skip modifying
// anything in between the braces (for now).
// the `macro_name!` and `{ /* macro_body */ }`.

@dswij dswij Aug 16, 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.

This is not correct. We do modify it by trimming (sometimes).

View changes since the review

Comment thread src/utils.rs
Comment on lines -611 to -637
/// e.g.
///
/// ```rust,compile_fail
/// foo!{
/// x,
/// y,
/// foo(
/// a,
/// b,
/// c,
/// ),
/// }
/// ```
///
/// will become
///
/// ```rust,compile_fail
/// foo!{
/// x,
/// y,
/// foo(
/// a,
/// b,
/// c,
/// ),
/// }
/// ```

@dswij dswij Aug 16, 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.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Yes, we traditionally don't format macro calls that are written with {} delimiters.

Instead of completely removing the example let's correct it.

Comment thread src/utils.rs
Comment on lines +619 to +623
// If a macro delimiter and a string start, `"`, is in the same line, then skip trimming
// altogether.
if first_line_kind == FullCodeCharKind::StartString {
return Some(orig.to_string());
}

@ytmimi ytmimi Aug 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

How do we know that we're being called in the context of formatting a macro?

trim_left_preserve_layout also gets called when formatting comments.

View changes since the review

Comment thread src/utils.rs
Comment on lines -647 to +628
let mut veto_trim = false;
let mut vetoed = false;

@ytmimi ytmimi Aug 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Why did we rename veto_trim -> vetoed?

Does vetoed represent something else now?

View changes since the review

@ytmimi ytmimi Aug 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If the source and target are the same, then you only need the target file to check for idempotence.

View changes since the review

Comment thread src/utils.rs
Comment on lines -690 to +682
.map(
|&(trimmed, ref line, prefix_space_width)| match prefix_space_width {
_ if !trimmed => line.to_owned(),
Some(original_indent_width) => {
let new_indent_width = indent.width()
+ original_indent_width.saturating_sub(min_prefix_space_width);
let new_indent = Indent::from_width(config, new_indent_width);
format!("{}{}", new_indent.to_string(config), line)
}
None => String::new(),
},
)
.map(|&(trimmed, ref line, prefix_space_width)| {
if !trimmed {
return line.to_owned();
}

if let Some(original_indent_width) = prefix_space_width {
let new_indent_width = indent.width()
+ original_indent_width.saturating_sub(min_prefix_space_width);
let new_indent = Indent::from_width(config, new_indent_width);
return format!("{}{}", new_indent.to_string(config), line);
}

String::new()
})

@ytmimi ytmimi Aug 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Was this meaningfully changed?

View changes since the review

Comment thread src/utils.rs
Comment on lines -662 to +648
let line = if veto_trim || new_veto_trim_value {
veto_trim = new_veto_trim_value;
trimmed = false;
line

if vetoed || new_veto_trim_value {
vetoed = new_veto_trim_value;
trimmed_lines.push((false, line, prefix_space_width));
} else {
line.trim().to_owned()
};
trimmed_lines.push((trimmed, line, prefix_space_width));
trimmed_lines.push((true, line.trim().to_owned(), prefix_space_width));
}

@ytmimi ytmimi Aug 19, 2026

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

So we didn't need to redefine line?

View changes since the review

@ytmimi

ytmimi commented Aug 19, 2026

Copy link
Copy Markdown
Contributor

@rustbot author

@rustbot rustbot added S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author. and removed S-waiting-on-review Status: awaiting review from the assignee but also interested parties. labels Aug 19, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

S-waiting-on-author Status: awaiting some action (such as code changes or more information) from the author.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Strings incorrectly unindented with braced macro

3 participants