Skip to content

Commit 2d1496a

Browse files
authored
Rollup merge of #95343 - dtolnay:literals, r=petrochenkov
Reduce unnecessary escaping in proc_macro::Literal::character/string I noticed that https://doc.rust-lang.org/proc_macro/struct.Literal.html#method.character is producing unreadable literals that make macro-expanded code unnecessarily hard to read. Since the proc macro server was using `escape_unicode()`, every char is escaped using `\u{…}` regardless of whether there is any need to do so. For example `Literal::character('=')` would previously produce `'\u{3d}'` which unnecessarily obscures the meaning when reading the macro-expanded code. I've changed Literal::string also in this PR because `str`'s `Debug` impl is also smarter than just calling `escape_debug` on every char. For example `Literal::string("ferris's")` would previously produce `"ferris\'s"` but will now produce `"ferris's"`.
2 parents 9816892 + f383134 commit 2d1496a

File tree

3 files changed

+21
-10
lines changed

3 files changed

+21
-10
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -658,16 +658,16 @@ impl server::Literal for Rustc<'_, '_> {
658658
self.lit(token::Float, Symbol::intern(n), Some(sym::f64))
659659
}
660660
fn string(&mut self, string: &str) -> Self::Literal {
661-
let mut escaped = String::new();
662-
for ch in string.chars() {
663-
escaped.extend(ch.escape_debug());
664-
}
665-
self.lit(token::Str, Symbol::intern(&escaped), None)
661+
let quoted = format!("{:?}", string);
662+
assert!(quoted.starts_with('"') && quoted.ends_with('"'));
663+
let symbol = &quoted[1..quoted.len() - 1];
664+
self.lit(token::Str, Symbol::intern(symbol), None)
666665
}
667666
fn character(&mut self, ch: char) -> Self::Literal {
668-
let mut escaped = String::new();
669-
escaped.extend(ch.escape_unicode());
670-
self.lit(token::Char, Symbol::intern(&escaped), None)
667+
let quoted = format!("{:?}", ch);
668+
assert!(quoted.starts_with('\'') && quoted.ends_with('\''));
669+
let symbol = &quoted[1..quoted.len() - 1];
670+
self.lit(token::Char, Symbol::intern(symbol), None)
671671
}
672672
fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal {
673673
let string = bytes

src/test/ui/proc-macro/auxiliary/api/parse.rs

+11
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,17 @@ fn test_display_literal() {
1818
Literal::f64_unsuffixed(1e100).to_string(),
1919
"10000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000.0",
2020
);
21+
22+
assert_eq!(
23+
Literal::string("a \t ❤ ' \" \u{1}").to_string(),
24+
"\"a \\t ❤ ' \\\" \\u{1}\"",
25+
);
26+
assert_eq!(Literal::character('a').to_string(), "'a'");
27+
assert_eq!(Literal::character('\t').to_string(), "'\\t'");
28+
assert_eq!(Literal::character('❤').to_string(), "'❤'");
29+
assert_eq!(Literal::character('\'').to_string(), "'\\''");
30+
assert_eq!(Literal::character('"').to_string(), "'\"'");
31+
assert_eq!(Literal::character('\u{1}').to_string(), "'\\u{1}'");
2132
}
2233

2334
fn test_parse_literal() {

src/test/ui/proc-macro/quote-debug.stdout

+2-2
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() {
2222
crate::Span::recover_proc_macro_span(0)))),
2323
crate::TokenStream::from(crate::TokenTree::Ident(crate::Ident::new("hello",
2424
crate::Span::recover_proc_macro_span(1)))),
25-
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3d}',
25+
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('=',
2626
crate::Spacing::Alone))),
2727
crate::TokenStream::from(crate::TokenTree::Literal({
2828
let mut iter =
@@ -35,7 +35,7 @@ fn main() {
3535
::core::panicking::panic("internal error: entered unreachable code")
3636
}
3737
})),
38-
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3b}',
38+
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new(';',
3939
crate::Spacing::Alone)))].iter().cloned().collect::<crate::TokenStream>()
4040
}
4141
const _: () =

0 commit comments

Comments
 (0)