-
Notifications
You must be signed in to change notification settings - Fork 12.8k
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
Conversation
r? @m-ou-se (rust-highfive has picked a reviewer for you, use r? to override) |
I'll update docs of |
This comment has been minimized.
This comment has been minimized.
I'll add a test, which directory should I put it? |
This comment has been minimized.
This comment has been minimized.
library/core/src/char/methods.rs
Outdated
self, | ||
escape_grapheme_extended: bool, | ||
escape_single_quote: bool, | ||
escape_double_quote: bool, |
There was a problem hiding this comment.
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?
This comment has been minimized.
This comment has been minimized.
I don't have time to tweak this more today, I'll get back to this later. |
library/core/src/fmt/mod.rs
Outdated
@@ -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); |
There was a problem hiding this comment.
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 '
.
library/core/src/fmt/mod.rs
Outdated
@@ -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) { |
There was a problem hiding this comment.
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 "
.
I believe this should currently be completely backwards compatible, other than For some reason |
library/core/src/char/methods.rs
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
library/core/src/str/mod.rs
Outdated
@@ -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)) |
There was a problem hiding this comment.
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.
library/core/src/str/mod.rs
Outdated
@@ -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) |
There was a problem hiding this comment.
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.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
@m-ou-se This is ready for reviews now. I'll squash the commits once all is done. |
This seems like a great idea. I think 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. |
☔ The latest upstream changes (presumably #83301) made this pull request unmergeable. Please resolve the merge conflicts. |
@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 |
Ping @m-ou-se |
Thanks! @bors r+ |
📌 Commit 52db9708dbb5fbfde615fa2c64c92656b6e610ac has been approved by |
…vs. chars Fixes rust-lang#83046 The program fn main() { println!("{:?}", '"'); println!("{:?}", "'"); } would previously print '\"' "\'" With this patch it now prints: '"' "'"
@m-ou-se Thanks for the review. I squashed the commits, could you approve again please? |
@bors r+ |
📌 Commit 819247f has been approved by |
☀️ Test successful - checks-actions |
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.
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
The change in [1] is now in stable. [1]: rust-lang/rust#83079
Fixes #83046
The program
would previously print
With this patch it now prints: