From e63f19db8d97bee4e6bbd23d6a1995ccab98a245 Mon Sep 17 00:00:00 2001 From: Marcel Hellwig Date: Wed, 29 May 2019 13:01:31 +0200 Subject: [PATCH] try to improve imporper delimiter message --- src/libsyntax/parse/lexer/mod.rs | 44 ++++++++++--------- .../raw/raw-byte-string-literals.stderr | 7 +-- src/test/ui/parser/raw/raw-str-delim.stderr | 7 +-- 3 files changed, 32 insertions(+), 26 deletions(-) diff --git a/src/libsyntax/parse/lexer/mod.rs b/src/libsyntax/parse/lexer/mod.rs index c5a9a1f7ec627..a51def579bed1 100644 --- a/src/libsyntax/parse/lexer/mod.rs +++ b/src/libsyntax/parse/lexer/mod.rs @@ -201,6 +201,29 @@ impl<'a> StringReader<'a> { FatalError.raise(); } + fn fail_incorrect_raw_string_delimiter(&mut self, start: BytePos) -> ! { + loop { + match self.ch { + Some('#') | Some('"') => break, + _ => self.bump(), + } + } + let end = self.pos; + let span = self.mk_sp(start, end); + let mut err = self.sess.span_diagnostic.struct_span_fatal( + span, + "found invalid character; only `#` is allowed in raw string delimitation", + ); + err.span_suggestion_hidden( + span, + "replace with `#`", + format!("{}", "#".repeat((end.0 - start.0) as usize)), + Applicability::MachineApplicable, + ); + err.emit(); + FatalError.raise(); + } + fn fatal(&self, m: &str) -> FatalError { self.fatal_span(self.peek_span, m) } @@ -330,16 +353,6 @@ impl<'a> StringReader<'a> { self.err_span(self.mk_sp(from_pos, to_pos), m) } - /// 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(": "); - push_escaped_char(&mut m, c); - - self.fatal_span_(from_pos, to_pos, &m[..]) - } - fn struct_span_fatal(&self, from_pos: BytePos, to_pos: BytePos, m: &str) -> DiagnosticBuilder<'a> { @@ -1357,16 +1370,7 @@ impl<'a> StringReader<'a> { vec![self.mk_sp(self.pos, self.pos)] ), Some('"') => {}, - Some(c) => { - let last_bpos = self.pos; - self.fatal_span_char( - start_bpos, - last_bpos, - "found invalid character; only `#` is allowed \ - in raw string delimitation", - c - ).raise(); - } + Some(_) => self.fail_incorrect_raw_string_delimiter(self.pos), } self.bump(); diff --git a/src/test/ui/parser/raw/raw-byte-string-literals.stderr b/src/test/ui/parser/raw/raw-byte-string-literals.stderr index fb69d506166c1..2fc4d5ce88b95 100644 --- a/src/test/ui/parser/raw/raw-byte-string-literals.stderr +++ b/src/test/ui/parser/raw/raw-byte-string-literals.stderr @@ -4,11 +4,12 @@ error: raw byte string must be ASCII: \u{e9} LL | br"é"; | ^ -error: found invalid character; only `#` is allowed in raw string delimitation: ~ - --> $DIR/raw-byte-string-literals.rs:6:5 +error: found invalid character; only `#` is allowed in raw string delimitation + --> $DIR/raw-byte-string-literals.rs:6:9 | LL | br##~"a"~##; - | ^^^^ + | ^ + = help: replace with `#` error: aborting due to 2 previous errors diff --git a/src/test/ui/parser/raw/raw-str-delim.stderr b/src/test/ui/parser/raw/raw-str-delim.stderr index b86b9e90e73ad..4a9e4f7720d0f 100644 --- a/src/test/ui/parser/raw/raw-str-delim.stderr +++ b/src/test/ui/parser/raw/raw-str-delim.stderr @@ -1,8 +1,9 @@ -error: found invalid character; only `#` is allowed in raw string delimitation: ~ - --> $DIR/raw-str-delim.rs:2:5 +error: found invalid character; only `#` is allowed in raw string delimitation + --> $DIR/raw-str-delim.rs:2:7 | LL | r#~"#"~# - | ^^ + | ^ + = help: replace with `#` error: aborting due to previous error