diff options
| author | kennytm <kennytm@gmail.com> | 2018-02-02 16:29:20 +0800 |
|---|---|---|
| committer | kennytm <kennytm@gmail.com> | 2018-02-02 22:48:46 +0800 |
| commit | b4b73a1d95814d56cdb34720d6fb01544a1e8d11 (patch) | |
| tree | 51e89553bb0944d312d067d4b4cc2daec3c48547 /src/libsyntax/parse | |
| parent | 2e3a8f51595e3af985425520f1cba1bce4423979 (diff) | |
| parent | b9441f2428f501de15dcb00af8da255a70302a0d (diff) | |
| download | rust-b4b73a1d95814d56cdb34720d6fb01544a1e8d11.tar.gz rust-b4b73a1d95814d56cdb34720d6fb01544a1e8d11.zip | |
Rollup merge of #47914 - etaoins:improve-char-escape-in-lexer-msg, r=petrochenkov Improve char escaping in lexer messages Currently ', " and \ are escaped as \', \" and \\ respectively. This leads to confusing messages such as `error: unknown start of token: \\` when encountering a single backslash. Fix by emitting printable ASCII characters directly. This will still escape \r, \n, \t and Unicode characters. Fixes #47902
Diffstat (limited to 'src/libsyntax/parse')
| -rw-r--r-- | src/libsyntax/parse/lexer/mod.rs | 31 |
1 files changed, 19 insertions, 12 deletions
diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index 0fd069b76aa..11ab84a5729 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -246,14 +246,27 @@ impl<'a> StringReader<'a> { self.err_span(self.mk_sp(from_pos, to_pos), m) } + /// Pushes a character to a message string for error reporting + fn push_escaped_char_for_msg(m: &mut String, c: char) { + match c { + '\u{20}'...'\u{7e}' => { + // Don't escape \, ' or " for user-facing messages + m.push(c); + } + _ => { + for c in c.escape_default() { + m.push(c); + } + } + } + } + /// Report a lexical error spanning [`from_pos`, `to_pos`), appending an /// escaped character to the error message fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> FatalError { let mut m = m.to_string(); m.push_str(": "); - for c in c.escape_default() { - m.push(c) - } + Self::push_escaped_char_for_msg(&mut m, c); self.fatal_span_(from_pos, to_pos, &m[..]) } fn struct_fatal_span_char(&self, @@ -264,9 +277,7 @@ impl<'a> StringReader<'a> { -> DiagnosticBuilder<'a> { let mut m = m.to_string(); m.push_str(": "); - for c in c.escape_default() { - m.push(c) - } + Self::push_escaped_char_for_msg(&mut m, c); self.sess.span_diagnostic.struct_span_fatal(self.mk_sp(from_pos, to_pos), &m[..]) } @@ -275,9 +286,7 @@ impl<'a> StringReader<'a> { fn err_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) { let mut m = m.to_string(); m.push_str(": "); - for c in c.escape_default() { - m.push(c) - } + Self::push_escaped_char_for_msg(&mut m, c); self.err_span_(from_pos, to_pos, &m[..]); } fn struct_err_span_char(&self, @@ -288,9 +297,7 @@ impl<'a> StringReader<'a> { -> DiagnosticBuilder<'a> { let mut m = m.to_string(); m.push_str(": "); - for c in c.escape_default() { - m.push(c) - } + Self::push_escaped_char_for_msg(&mut m, c); self.sess.span_diagnostic.struct_span_err(self.mk_sp(from_pos, to_pos), &m[..]) } |
