Skip to content

String Debug implementation prints escaped single quotes #83046

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
marcianx opened this issue Mar 12, 2021 · 2 comments · Fixed by #83079
Closed

String Debug implementation prints escaped single quotes #83046

marcianx opened this issue Mar 12, 2021 · 2 comments · Fixed by #83079
Labels
C-bug Category: This is a bug.

Comments

@marcianx
Copy link

This is very minor.

println!("{:?}", "'");

(playground)

Expected output: "'"
Actual output: "\'"

Perhaps this was intentional, but I'm not sure why it would be. The closest issue I saw was #3752 which is ancient and was more obviously a bug since escapes were appearing in Display output and not just Debug.

This reproduces in the latest stable (1.50.0) and nightly (1.52.0-nightly) builds.

@marcianx marcianx added the C-bug Category: This is a bug. label Mar 12, 2021
@osa1
Copy link
Contributor

osa1 commented Mar 13, 2021

This is because the same format string method of char (escape_debug_ext) is used for Debug implementation of both str and char (and a few other internal types), so it needs to escake ' otherwise in char context it would generate incorrect Debug string. Adding a is_char argument to this method solves the issue. I tested this and it passes ui tests.

I checked the language spec https://doc.rust-lang.org/stable/reference/tokens.html#string-literals I don't think "\'" is a valid string, right? Am I missing anything?

@marcianx
Copy link
Author

Oh, wow. Until you mentioned it, I didn't even notice that this also affects char's Debug output as well so that println!("{:?}", '"'); prints '\"'. Thanks for fixing them both!

bors added a commit to rust-lang-ci/rust that referenced this issue Mar 26, 2021
Update char::escape_debug_ext to handle different escapes in strings and chars

Fixes rust-lang#83046

The program

    fn main() {
        println!("{:?}", '"');
        println!("{:?}", "'");
    }

would previously print

    '\"'
    "\'"

With this patch it now prints:

    '"'
    "'"
@bors bors closed this as completed in 819247f Mar 26, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Category: This is a bug.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants