Skip to content

Commit e63f19d

Browse files
committed
try to improve imporper delimiter message
1 parent 2045130 commit e63f19d

File tree

3 files changed

+32
-26
lines changed

3 files changed

+32
-26
lines changed

src/libsyntax/parse/lexer/mod.rs

+24-20
Original file line numberDiff line numberDiff line change
@@ -201,6 +201,29 @@ impl<'a> StringReader<'a> {
201201
FatalError.raise();
202202
}
203203

204+
fn fail_incorrect_raw_string_delimiter(&mut self, start: BytePos) -> ! {
205+
loop {
206+
match self.ch {
207+
Some('#') | Some('"') => break,
208+
_ => self.bump(),
209+
}
210+
}
211+
let end = self.pos;
212+
let span = self.mk_sp(start, end);
213+
let mut err = self.sess.span_diagnostic.struct_span_fatal(
214+
span,
215+
"found invalid character; only `#` is allowed in raw string delimitation",
216+
);
217+
err.span_suggestion_hidden(
218+
span,
219+
"replace with `#`",
220+
format!("{}", "#".repeat((end.0 - start.0) as usize)),
221+
Applicability::MachineApplicable,
222+
);
223+
err.emit();
224+
FatalError.raise();
225+
}
226+
204227
fn fatal(&self, m: &str) -> FatalError {
205228
self.fatal_span(self.peek_span, m)
206229
}
@@ -330,16 +353,6 @@ impl<'a> StringReader<'a> {
330353
self.err_span(self.mk_sp(from_pos, to_pos), m)
331354
}
332355

333-
/// Report a lexical error spanning [`from_pos`, `to_pos`), appending an
334-
/// escaped character to the error message
335-
fn fatal_span_char(&self, from_pos: BytePos, to_pos: BytePos, m: &str, c: char) -> FatalError {
336-
let mut m = m.to_string();
337-
m.push_str(": ");
338-
push_escaped_char(&mut m, c);
339-
340-
self.fatal_span_(from_pos, to_pos, &m[..])
341-
}
342-
343356
fn struct_span_fatal(&self, from_pos: BytePos, to_pos: BytePos, m: &str)
344357
-> DiagnosticBuilder<'a>
345358
{
@@ -1357,16 +1370,7 @@ impl<'a> StringReader<'a> {
13571370
vec![self.mk_sp(self.pos, self.pos)]
13581371
),
13591372
Some('"') => {},
1360-
Some(c) => {
1361-
let last_bpos = self.pos;
1362-
self.fatal_span_char(
1363-
start_bpos,
1364-
last_bpos,
1365-
"found invalid character; only `#` is allowed \
1366-
in raw string delimitation",
1367-
c
1368-
).raise();
1369-
}
1373+
Some(_) => self.fail_incorrect_raw_string_delimiter(self.pos),
13701374
}
13711375

13721376
self.bump();

src/test/ui/parser/raw/raw-byte-string-literals.stderr

+4-3
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,12 @@ error: raw byte string must be ASCII: \u{e9}
44
LL | br"é";
55
| ^
66

7-
error: found invalid character; only `#` is allowed in raw string delimitation: ~
8-
--> $DIR/raw-byte-string-literals.rs:6:5
7+
error: found invalid character; only `#` is allowed in raw string delimitation
8+
--> $DIR/raw-byte-string-literals.rs:6:9
99
|
1010
LL | br##~"a"~##;
11-
| ^^^^
11+
| ^
12+
= help: replace with `#`
1213

1314
error: aborting due to 2 previous errors
1415

+4-3
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,9 @@
1-
error: found invalid character; only `#` is allowed in raw string delimitation: ~
2-
--> $DIR/raw-str-delim.rs:2:5
1+
error: found invalid character; only `#` is allowed in raw string delimitation
2+
--> $DIR/raw-str-delim.rs:2:7
33
|
44
LL | r#~"#"~#
5-
| ^^
5+
| ^
6+
= help: replace with `#`
67

78
error: aborting due to previous error
89

0 commit comments

Comments
 (0)