Skip to content

Use escape_default() for strings in LitKind::token(). #50391

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

Merged
merged 2 commits into from
May 3, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 1 addition & 4 deletions src/libsyntax/attr.rs
Original file line number Diff line number Diff line change
@@ -1228,10 +1228,7 @@ impl LitKind {

match *self {
LitKind::Str(string, ast::StrStyle::Cooked) => {
let mut escaped = String::new();
for ch in string.as_str().chars() {
escaped.extend(ch.escape_unicode());
}
let escaped = string.as_str().escape_default();
Token::Literal(token::Lit::Str_(Symbol::intern(&escaped)), None)
}
LitKind::Str(string, ast::StrStyle::Raw(n)) => {
1 change: 1 addition & 0 deletions src/libsyntax/lib.rs
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@
#![feature(non_exhaustive)]
#![feature(const_atomic_usize_new)]
#![feature(rustc_attrs)]
#![feature(str_escape)]

#![recursion_limit="256"]

8 changes: 2 additions & 6 deletions src/libsyntax/parse/mod.rs
Original file line number Diff line number Diff line change
@@ -298,14 +298,10 @@ pub fn char_lit(lit: &str, diag: Option<(Span, &Handler)>) -> (char, isize) {
}
}

pub fn escape_default(s: &str) -> String {
s.chars().map(char::escape_default).flat_map(|x| x).collect()
}

/// Parse a string representing a string literal into its final form. Does
/// unescaping.
pub fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String {
debug!("parse_str_lit: given {}", escape_default(lit));
debug!("str_lit: given {}", lit.escape_default());
let mut res = String::with_capacity(lit.len());

let error = |i| format!("lexer should have rejected {} at {}", lit, i);
@@ -374,7 +370,7 @@ pub fn str_lit(lit: &str, diag: Option<(Span, &Handler)>) -> String {
/// Parse a string representing a raw string literal into its final form. The
/// only operation this does is convert embedded CRLF into a single LF.
pub fn raw_str_lit(lit: &str) -> String {
debug!("raw_str_lit: given {}", escape_default(lit));
debug!("raw_str_lit: given {}", lit.escape_default());
let mut res = String::with_capacity(lit.len());

let mut chars = lit.chars().peekable();
2 changes: 1 addition & 1 deletion src/libsyntax/print/pprust.rs
Original file line number Diff line number Diff line change
@@ -656,7 +656,7 @@ pub trait PrintState<'a> {
style: ast::StrStyle) -> io::Result<()> {
let st = match style {
ast::StrStyle::Cooked => {
(format!("\"{}\"", parse::escape_default(st)))
(format!("\"{}\"", st.escape_default()))
}
ast::StrStyle::Raw(n) => {
(format!("r{delim}\"{string}\"{delim}",