Skip to content

-Z unpretty=expanded leads to invalid syntax for unicode escapes in format strings #116799

Closed
@narpfel

Description

@narpfel

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

Metadata

Metadata

Assignees

Labels

C-bugCategory: This is a bug.E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions