Skip to content

Commit f383134

Browse files
committed
Use str and char's Debug impl to format literals
1 parent f1fa84c commit f383134

File tree

3 files changed

+16
-16
lines changed

3 files changed

+16
-16
lines changed

compiler/rustc_expand/src/proc_macro_server.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -663,16 +663,16 @@ impl server::Literal for Rustc<'_, '_> {
663663
self.lit(token::Float, Symbol::intern(n), Some(sym::f64))
664664
}
665665
fn string(&mut self, string: &str) -> Self::Literal {
666-
let mut escaped = String::new();
667-
for ch in string.chars() {
668-
escaped.extend(ch.escape_debug());
669-
}
670-
self.lit(token::Str, Symbol::intern(&escaped), None)
666+
let quoted = format!("{:?}", string);
667+
assert!(quoted.starts_with('"') && quoted.ends_with('"'));
668+
let symbol = &quoted[1..quoted.len() - 1];
669+
self.lit(token::Str, Symbol::intern(symbol), None)
671670
}
672671
fn character(&mut self, ch: char) -> Self::Literal {
673-
let mut escaped = String::new();
674-
escaped.extend(ch.escape_unicode());
675-
self.lit(token::Char, Symbol::intern(&escaped), None)
672+
let quoted = format!("{:?}", ch);
673+
assert!(quoted.starts_with('\'') && quoted.ends_with('\''));
674+
let symbol = &quoted[1..quoted.len() - 1];
675+
self.lit(token::Char, Symbol::intern(symbol), None)
676676
}
677677
fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal {
678678
let string = bytes

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

+6-6
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,13 @@ fn test_display_literal() {
2121

2222
assert_eq!(
2323
Literal::string("a \t ❤ ' \" \u{1}").to_string(),
24-
"\"a \\t ❤ \\' \\\" \\u{1}\"",
24+
"\"a \\t ❤ ' \\\" \\u{1}\"",
2525
);
26-
assert_eq!(Literal::character('a').to_string(), "'\\u{61}'");
27-
assert_eq!(Literal::character('\t').to_string(), "'\\u{9}'");
28-
assert_eq!(Literal::character('❤').to_string(), "'\\u{2764}'");
29-
assert_eq!(Literal::character('\'').to_string(), "'\\u{27}'");
30-
assert_eq!(Literal::character('"').to_string(), "'\\u{22}'");
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(), "'\"'");
3131
assert_eq!(Literal::character('\u{1}').to_string(), "'\\u{1}'");
3232
}
3333

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)