Closed
Description
I tried this code (Godbolt link):
pub fn main() {
println!("\x1B[1mHello, world!\x1B[0m");
println!("\u{1B}[1mHello, world!\u{1B}[0m");
println!("{}", "\x1B[1mHello, world!\x1B[0m");
}
I expected to see this happen: When running rustc -Z unpretty=expanded
, the format string literals for the first two println!
s should remain valid string literals.
Instead, this happened: This code is generated:
use ::std::prelude::rust_2015::*;
extern crate std;
pub fn main() {
{
::std::io::_print(format_args!("\u{{1b}}[1mHello, world!\u{{1b}}[0m\n"));
};
{
::std::io::_print(format_args!("\u{{1b}}[1mHello, world!\u{{1b}}[0m\n"));
};
{
::std::io::_print(format_args!("{0}\n",
"\x1B[1mHello, world!\x1B[0m"));
};
}
Note the double {{
and }}
in the \u
escapes.
The escape sequences in the last println!
are correctly preserved.
This is annoying because it makes it impossible to run rustfmt
on the macro-expanded source, which is helpful when trying to read macro-generated code.
Meta
rustc --version --verbose
:
rustc 1.75.0-nightly (42b1224e9 2023-10-15)
binary: rustc
commit-hash: 42b1224e9eb37177f608d3f6a6f2be2ee13902e4
commit-date: 2023-10-15
host: x86_64-unknown-linux-gnu
release: 1.75.0-nightly
LLVM version: 17.0.2