Skip to content
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

Update char::escape_debug_ext to handle different escapes in strings and chars #83079

Merged
merged 1 commit into from
Mar 26, 2021

Conversation

osa1
Copy link
Contributor

@osa1 osa1 commented Mar 13, 2021

Fixes #83046

The program

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

would previously print

'\"'
"\'"

With this patch it now prints:

'"'
"'"

@rust-highfive
Copy link
Collaborator

r? @m-ou-se

(rust-highfive has picked a reviewer for you, use r? to override)

@rust-highfive rust-highfive added the S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. label Mar 13, 2021
@osa1 osa1 changed the title Update char::escape_debug_ext to handle different escapes in strings … Update char::escape_debug_ext to handle different escapes in strings and chars Mar 13, 2021
@osa1
Copy link
Contributor Author

osa1 commented Mar 13, 2021

I'll update docs of escape_debug_ext and escape_debug.

@osa1 osa1 marked this pull request as draft March 13, 2021 08:30
@rust-log-analyzer

This comment has been minimized.

@osa1 osa1 marked this pull request as ready for review March 13, 2021 14:16
@osa1 osa1 requested a review from m-ou-se March 13, 2021 14:17
@osa1
Copy link
Contributor Author

osa1 commented Mar 13, 2021

I'll add a test, which directory should I put it?

@rust-log-analyzer

This comment has been minimized.

self,
escape_grapheme_extended: bool,
escape_single_quote: bool,
escape_double_quote: bool,
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should I introduce a struct for these bool args?

@rust-log-analyzer

This comment has been minimized.

@osa1
Copy link
Contributor Author

osa1 commented Mar 13, 2021

I don't have time to tweak this more today, I'll get back to this later.

@@ -2054,7 +2054,7 @@ impl Debug for str {
f.write_char('"')?;
let mut from = 0;
for (i, c) in self.char_indices() {
let esc = c.escape_debug();
let esc = c.escape_debug_ext(true, false, true);
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug output of str now escapes " but not '.

@@ -2080,7 +2080,7 @@ impl Display for str {
impl Debug for char {
fn fmt(&self, f: &mut Formatter<'_>) -> Result {
f.write_char('\'')?;
for c in self.escape_debug() {
for c in self.escape_debug_ext(true, true, false) {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Debug output of char not escapes ' but not ".

@osa1
Copy link
Contributor Author

osa1 commented Mar 14, 2021

I believe this should currently be completely backwards compatible, other than Debug outputs for str and char.

For some reason x.py tests skips the tests and I couldn't find a way to force it to run, so I'll rely on CI to re-run the previously-failing tests.

@@ -458,7 +465,7 @@ impl char {
#[stable(feature = "char_escape_debug", since = "1.20.0")]
#[inline]
pub fn escape_debug(self) -> EscapeDebug {
self.escape_debug_ext(true)
self.escape_debug_ext(true, true, true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Escapes both ' and ", as before.

@@ -2342,7 +2342,7 @@ impl str {
EscapeDebug {
inner: chars
.next()
.map(|first| first.escape_debug_ext(true))
.map(|first| first.escape_debug_ext(true, true, true))
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Escapes both ' and ", as before.

@@ -2460,7 +2460,7 @@ impl_fn_for_zst! {

#[derive(Clone)]
struct CharEscapeDebugContinue impl Fn = |c: char| -> char::EscapeDebug {
c.escape_debug_ext(false)
c.escape_debug_ext(false, true, true)
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Escapes both ' and ", as before.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@rust-log-analyzer

This comment has been minimized.

@osa1
Copy link
Contributor Author

osa1 commented Mar 15, 2021

@m-ou-se This is ready for reviews now. I'll squash the commits once all is done.

@joshtriplett
Copy link
Member

This seems like a great idea. I think escape_debug_ext having three bool args right next to each other is error-prone, though. Would you consider adding an enum instead?

enum EscapeChars {
    EscapeSingleQuote,
    EscapeDoubleQuote,
    EscapeBothQuotes,
}

impl EscapeChars {
    fn escape_single_quote(&self) -> bool {
        match self {
            ...
        }
    }

    fn escape_double_quote(&self) -> bool {
        match self {
            ...
        }
    }
}

I'm hoping that'll produce generated code that's just as efficient.

@bors
Copy link
Contributor

bors commented Mar 19, 2021

☔ The latest upstream changes (presumably #83301) made this pull request unmergeable. Please resolve the merge conflicts.

@osa1
Copy link
Contributor Author

osa1 commented Mar 21, 2021

@joshtriplett I didn't quite understand how that enum would work, but I added a struct for to give names to the bool args.

I'll squash the commits before merge.

Ping @m-ou-se

@osa1
Copy link
Contributor Author

osa1 commented Mar 26, 2021

Ping @m-ou-se

@m-ou-se
Copy link
Member

m-ou-se commented Mar 26, 2021

Thanks!

@bors r+

@bors
Copy link
Contributor

bors commented Mar 26, 2021

📌 Commit 52db9708dbb5fbfde615fa2c64c92656b6e610ac has been approved by m-ou-se

@bors bors added S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. and removed S-waiting-on-review Status: Awaiting review from the assignee but also interested parties. labels Mar 26, 2021
…vs. chars

Fixes rust-lang#83046

The program

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

would previously print

    '\"'
    "\'"

With this patch it now prints:

    '"'
    "'"
@osa1
Copy link
Contributor Author

osa1 commented Mar 26, 2021

@m-ou-se Thanks for the review. I squashed the commits, could you approve again please?

@m-ou-se
Copy link
Member

m-ou-se commented Mar 26, 2021

@bors r+

@bors
Copy link
Contributor

bors commented Mar 26, 2021

📌 Commit 819247f has been approved by m-ou-se

@bors
Copy link
Contributor

bors commented Mar 26, 2021

⌛ Testing commit 819247f with merge 4137088...

@bors
Copy link
Contributor

bors commented Mar 26, 2021

☀️ Test successful - checks-actions
Approved by: m-ou-se
Pushing 4137088 to master...

@bors bors added the merged-by-bors This PR was explicitly merged by bors. label Mar 26, 2021
@bors bors merged commit 4137088 into rust-lang:master Mar 26, 2021
@rustbot rustbot added this to the 1.53.0 milestone Mar 26, 2021
@osa1 osa1 deleted the issue83046 branch March 26, 2021 12:00
vickenty added a commit to vickenty/lang-c that referenced this pull request Apr 8, 2021
Free lunch is over, Debug representation for str has changed
(rust-lang/rust#83079). Roll our own version based on the stable
`escape_debug()` method instead.

Also spell out all the enums while at it.
sharksforarms pushed a commit to sharksforarms/deku that referenced this pull request Apr 13, 2021
Run tarpaulin for code coverage on stable rust instead of nightly.
Nightly rust has our test cases failing due to the changed debug
printing of strings with single quotes "'" so our test cases using
should_panic(expected = <error message>) are failing.

See rust-lang/rust#83079
utkarshkukreti added a commit to utkarshkukreti/wn.rs that referenced this pull request Jul 10, 2021
The change in [1] is now in stable.

[1]: rust-lang/rust#83079
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

String Debug implementation prints escaped single quotes
8 participants