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
18 changes: 17 additions & 1 deletion src/macros.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ use std::collections::HashMap;
use std::panic::{AssertUnwindSafe, catch_unwind};

use rustc_ast::ast;
use rustc_ast::token::{Delimiter, Token, TokenKind};
use rustc_ast::token::{Delimiter, LitKind, Token, TokenKind};
use rustc_ast::tokenstream::{TokenStream, TokenStreamIter, TokenTree};
use rustc_ast_pretty::pprust;
use rustc_span::{BytePos, DUMMY_SP, Ident, Pos, Span, Symbol};
Expand Down Expand Up @@ -299,6 +299,22 @@ fn rewrite_macro_inner(

match style {
Delimiter::Parenthesis => {
if context.config.style_edition() >= StyleEdition::Edition2027 {
// Handle special case where there is one argument,
// a string literal containing a newline.
if let [MacroArg::Expr(expr)] = arg_vec.as_slice() {
if let ast::ExprKind::Lit(lit) = expr.kind {
if lit.kind == LitKind::Str && lit.symbol.as_str().contains("\n") {
let rw = format!("{}(\"{}\")", macro_name, lit.symbol);
return match position {
MacroPosition::Item => Ok(format!("{};", rw)),
_ => Ok(rw),
};
}
}
}
Comment on lines +303 to +315

@jieyouxu jieyouxu Oct 4, 2026 •

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.

Remark: this feels very special-cased and somewhat strange to me. I agree the existing formatting doesn't seem great, but I'm not convinced special-casing here makes sense either.

View changes since the review

}

// Handle special case: `vec!(expr; expr)`
if vec_with_semi {
handle_vec_semi(context, shape, arg_vec, macro_name, style, mac.span())
Expand Down
33 changes: 33 additions & 0 deletions tests/source/issue_5029.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// rustfmt-style_edition: 2027

pub const FOO: &str = foo!("
foo
bar
");

pub const BAR: &str = foo!(
"
foo
bar
"
);

pub const BAZ: &str = foo!("
foo bar
");

fn main() {
foo!("
foo bar
");

let bytes1 = hex!("
00010203 04050607
08090a0b 0c0d0e0f
");

let bytes2 = hex!(
"00010203 04050607" // first half
"08090a0b 0c0d0e0f" // second half
);
}
31 changes: 31 additions & 0 deletions tests/target/issue_5029.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
// rustfmt-style_edition: 2027

pub const FOO: &str = foo!("
foo
bar
");

pub const BAR: &str = foo!("
foo
bar
");

pub const BAZ: &str = foo!("
foo bar
");

fn main() {
foo!("
foo bar
");

let bytes1 = hex!("
00010203 04050607
08090a0b 0c0d0e0f
");

let bytes2 = hex!(
"00010203 04050607" // first half
"08090a0b 0c0d0e0f" // second half
);
}
Loading