Skip to content

Commit

Permalink
try to improve imporper delimiter message
Browse files Browse the repository at this point in the history
  • Loading branch information
hellow554 committed May 29, 2019
1 parent 2045130 commit e63f19d
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 26 deletions.
44 changes: 24 additions & 20 deletions src/libsyntax/parse/lexer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)
}
Expand Down Expand Up @@ -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>
{
Expand Down Expand Up @@ -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();
Expand Down
7 changes: 4 additions & 3 deletions src/test/ui/parser/raw/raw-byte-string-literals.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -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

7 changes: 4 additions & 3 deletions src/test/ui/parser/raw/raw-str-delim.stderr
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit e63f19d

Please sign in to comment.