Skip to content

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

Closed

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

Activity

  1. added
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 16, 2023
  2. added
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
    E-mediumCall for participation: Medium difficulty. Experience needed to fix: Intermediate.
    and removed
    E-easyCall for participation: Easy difficulty. Experience needed to fix: Not much. Good first issue.
    on Oct 16, 2023
  3. Noratrieb commented on Oct 16, 2023

    @Noratrieb
    Member

    I haven't looked at it, but this is probably going wrong somewhere in compiler/rustc_ast_pretty when printing FormatArgs.

  4. narpfel commented on Oct 16, 2023

    @narpfel
    ContributorAuthor

    Looks like you’re right:

    FormatArgsPiece::Literal(s) => {
    for c in s.as_str().escape_debug() {
    template.push(c);
    if let '{' | '}' = c {
    template.push(c);
    }
    }
    }

    As I understand this, it duplicates curlies regardless of whether they’re part of an escape sequence.

  5. narpfel commented on Oct 16, 2023

    @narpfel
    ContributorAuthor

    @rustbot claim

  6. removed
    needs-triageThis issue may need triage. Remove when done. See docs forge.rust-lang.org/release/issue-triaging
    on Oct 16, 2023
  7. added a commit that references this issue on Oct 16, 2023
    05e2056
  8. added a commit that references this issue on Oct 17, 2023
    321817a
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

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