1
Fork 0

Use str and char's Debug impl to format literals

This commit is contained in:
David Tolnay 2022-03-26 13:08:50 -07:00
parent f1fa84ce95
commit f383134acc
No known key found for this signature in database
GPG key ID: F9BA143B95FF6D82
3 changed files with 16 additions and 16 deletions

View file

@ -663,16 +663,16 @@ impl server::Literal for Rustc<'_, '_> {
self.lit(token::Float, Symbol::intern(n), Some(sym::f64)) self.lit(token::Float, Symbol::intern(n), Some(sym::f64))
} }
fn string(&mut self, string: &str) -> Self::Literal { fn string(&mut self, string: &str) -> Self::Literal {
let mut escaped = String::new(); let quoted = format!("{:?}", string);
for ch in string.chars() { assert!(quoted.starts_with('"') && quoted.ends_with('"'));
escaped.extend(ch.escape_debug()); let symbol = &quoted[1..quoted.len() - 1];
} self.lit(token::Str, Symbol::intern(symbol), None)
self.lit(token::Str, Symbol::intern(&escaped), None)
} }
fn character(&mut self, ch: char) -> Self::Literal { fn character(&mut self, ch: char) -> Self::Literal {
let mut escaped = String::new(); let quoted = format!("{:?}", ch);
escaped.extend(ch.escape_unicode()); assert!(quoted.starts_with('\'') && quoted.ends_with('\''));
self.lit(token::Char, Symbol::intern(&escaped), None) let symbol = &quoted[1..quoted.len() - 1];
self.lit(token::Char, Symbol::intern(symbol), None)
} }
fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal { fn byte_string(&mut self, bytes: &[u8]) -> Self::Literal {
let string = bytes let string = bytes

View file

@ -21,13 +21,13 @@ fn test_display_literal() {
assert_eq!( assert_eq!(
Literal::string("a \t ❤ ' \" \u{1}").to_string(), Literal::string("a \t ❤ ' \" \u{1}").to_string(),
"\"a \\t ❤ \\' \\\" \\u{1}\"", "\"a \\t ❤ ' \\\" \\u{1}\"",
); );
assert_eq!(Literal::character('a').to_string(), "'\\u{61}'"); assert_eq!(Literal::character('a').to_string(), "'a'");
assert_eq!(Literal::character('\t').to_string(), "'\\u{9}'"); assert_eq!(Literal::character('\t').to_string(), "'\\t'");
assert_eq!(Literal::character('❤').to_string(), "'\\u{2764}'"); assert_eq!(Literal::character('❤').to_string(), "''");
assert_eq!(Literal::character('\'').to_string(), "'\\u{27}'"); assert_eq!(Literal::character('\'').to_string(), "'\\''");
assert_eq!(Literal::character('"').to_string(), "'\\u{22}'"); assert_eq!(Literal::character('"').to_string(), "'\"'");
assert_eq!(Literal::character('\u{1}').to_string(), "'\\u{1}'"); assert_eq!(Literal::character('\u{1}').to_string(), "'\\u{1}'");
} }

View file

@ -22,7 +22,7 @@ fn main() {
crate::Span::recover_proc_macro_span(0)))), crate::Span::recover_proc_macro_span(0)))),
crate::TokenStream::from(crate::TokenTree::Ident(crate::Ident::new("hello", crate::TokenStream::from(crate::TokenTree::Ident(crate::Ident::new("hello",
crate::Span::recover_proc_macro_span(1)))), crate::Span::recover_proc_macro_span(1)))),
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3d}', crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('=',
crate::Spacing::Alone))), crate::Spacing::Alone))),
crate::TokenStream::from(crate::TokenTree::Literal({ crate::TokenStream::from(crate::TokenTree::Literal({
let mut iter = let mut iter =
@ -35,7 +35,7 @@ fn main() {
::core::panicking::panic("internal error: entered unreachable code") ::core::panicking::panic("internal error: entered unreachable code")
} }
})), })),
crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new('\u{3b}', crate::TokenStream::from(crate::TokenTree::Punct(crate::Punct::new(';',
crate::Spacing::Alone)))].iter().cloned().collect::<crate::TokenStream>() crate::Spacing::Alone)))].iter().cloned().collect::<crate::TokenStream>()
} }
const _: () = const _: () =