Skip to content

Commit f7f196c

Browse files
committed
add diagnostic for too many # after raw string literal
1 parent 87ed0b4 commit f7f196c

File tree

3 files changed

+40
-7
lines changed

3 files changed

+40
-7
lines changed

src/libsyntax/parse/lexer/mod.rs

Lines changed: 25 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use crate::symbol::{sym, Symbol};
55
use crate::parse::unescape;
66
use crate::parse::unescape_error_reporting::{emit_unescape_error, push_escaped_char};
77

8-
use errors::{FatalError, Diagnostic, DiagnosticBuilder};
8+
use errors::{Applicability, FatalError, Diagnostic, DiagnosticBuilder};
99
use syntax_pos::{BytePos, Pos, Span, NO_EXPANSION};
1010
use core::unicode::property::Pattern_White_Space;
1111

@@ -1172,12 +1172,6 @@ impl<'a> StringReader<'a> {
11721172
if self.is_eof() {
11731173
self.fail_unterminated_raw_string(start_bpos, hash_count);
11741174
}
1175-
// if self.ch_is('"') {
1176-
// content_end_bpos = self.pos;
1177-
// for _ in 0..hash_count {
1178-
// self.bump();
1179-
// if !self.ch_is('#') {
1180-
// continue 'outer;
11811175
let c = self.ch.unwrap();
11821176
match c {
11831177
'"' => {
@@ -1206,6 +1200,30 @@ impl<'a> StringReader<'a> {
12061200
}
12071201

12081202
self.bump();
1203+
if self.ch_is('#') {
1204+
let lo = self.pos;
1205+
while self.ch_is('#') {
1206+
self.bump();
1207+
}
1208+
1209+
let sp = self.mk_sp(start_bpos, self.pos);
1210+
let sp_beg = self.mk_sp(BytePos(start_bpos.0 + 1), BytePos(start_bpos.0 + 1 + hash_count as u32));
1211+
let sp_end = self.mk_sp(BytePos(lo.0 - hash_count as u32), self.pos);
1212+
1213+
let mut err = self.sess.span_diagnostic.struct_span_err(sp, "too many `#` when terminating raw string");
1214+
err.span_label(sp_beg, format!("The raw string has {} leading `#`...", hash_count));
1215+
err.span_label(sp_end, format!("...but is closed with {}.", self.pos.0 - lo.0 + hash_count as u32));
1216+
err.span_suggestion_hidden(
1217+
self.mk_sp(lo, self.pos),
1218+
"remove the unneeded `#`",
1219+
String::new(),
1220+
Applicability::MachineApplicable,
1221+
);
1222+
1223+
err.emit();
1224+
valid = false;
1225+
}
1226+
12091227
let symbol = if valid {
12101228
self.name_from_to(content_start_bpos, content_end_bpos)
12111229
} else {
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
fn main() {
2+
let foo = r##"bar"####; //~ERROR too many `#` when terminating raw string
3+
}
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
error: too many `#` when terminating raw string
2+
--> $DIR/raw-literal-too-many.rs:2:15
3+
|
4+
LL | let foo = r##"bar"####;
5+
| ^--^^^^^----
6+
| | |
7+
| | ...but is closed with 4.
8+
| The raw string has 2 leading `#`...
9+
= help: remove the unneeded `#`
10+
11+
error: aborting due to previous error
12+

0 commit comments

Comments
 (0)