Skip to content

Commit 1c191b2

Browse files
Add note for unterminated raw string error
1 parent 4a316e7 commit 1c191b2

File tree

3 files changed

+47
-8
lines changed

3 files changed

+47
-8
lines changed

src/libsyntax/parse/lexer/mod.rs

+25-8
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,18 @@ impl<'a> StringReader<'a> {
132132
self.advance_token()?;
133133
Ok(ret_val)
134134
}
135+
136+
fn fail_unterminated_raw_string(&self, pos: BytePos, hash_count: usize) {
137+
let mut err = self.struct_span_fatal(pos, pos, "unterminated raw string");
138+
err.span_label(self.mk_sp(pos, pos), "unterminated raw string");
139+
if hash_count > 0 {
140+
err.note(&format!("this raw string should be terminated with `\"{}`",
141+
"#".repeat(hash_count)));
142+
}
143+
err.emit();
144+
FatalError.raise();
145+
}
146+
135147
fn fatal(&self, m: &str) -> FatalError {
136148
self.fatal_span(self.peek_span, m)
137149
}
@@ -269,6 +281,15 @@ impl<'a> StringReader<'a> {
269281
Self::push_escaped_char_for_msg(&mut m, c);
270282
self.fatal_span_(from_pos, to_pos, &m[..])
271283
}
284+
285+
fn struct_span_fatal(&self,
286+
from_pos: BytePos,
287+
to_pos: BytePos,
288+
m: &str)
289+
-> DiagnosticBuilder<'a> {
290+
self.sess.span_diagnostic.struct_span_fatal(self.mk_sp(from_pos, to_pos), m)
291+
}
292+
272293
fn struct_fatal_span_char(&self,
273294
from_pos: BytePos,
274295
to_pos: BytePos,
@@ -1404,8 +1425,7 @@ impl<'a> StringReader<'a> {
14041425
}
14051426

14061427
if self.is_eof() {
1407-
let last_bpos = self.pos;
1408-
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string").raise();
1428+
self.fail_unterminated_raw_string(start_bpos, hash_count);
14091429
} else if !self.ch_is('"') {
14101430
let last_bpos = self.pos;
14111431
let curr_char = self.ch.unwrap();
@@ -1421,8 +1441,7 @@ impl<'a> StringReader<'a> {
14211441
let mut valid = true;
14221442
'outer: loop {
14231443
if self.is_eof() {
1424-
let last_bpos = self.pos;
1425-
self.fatal_span_(start_bpos, last_bpos, "unterminated raw string").raise();
1444+
self.fail_unterminated_raw_string(start_bpos, hash_count);
14261445
}
14271446
// if self.ch_is('"') {
14281447
// content_end_bpos = self.pos;
@@ -1636,8 +1655,7 @@ impl<'a> StringReader<'a> {
16361655
}
16371656

16381657
if self.is_eof() {
1639-
let pos = self.pos;
1640-
self.fatal_span_(start_bpos, pos, "unterminated raw string").raise();
1658+
self.fail_unterminated_raw_string(start_bpos, hash_count);
16411659
} else if !self.ch_is('"') {
16421660
let pos = self.pos;
16431661
let ch = self.ch.unwrap();
@@ -1653,8 +1671,7 @@ impl<'a> StringReader<'a> {
16531671
'outer: loop {
16541672
match self.ch {
16551673
None => {
1656-
let pos = self.pos;
1657-
self.fatal_span_(start_bpos, pos, "unterminated raw string").raise()
1674+
self.fail_unterminated_raw_string(start_bpos, hash_count);
16581675
}
16591676
Some('"') => {
16601677
content_end_bpos = self.pos;

src/test/ui/raw_string.rs

+14
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
// Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2+
// file at the top-level directory of this distribution and at
3+
// http://rust-lang.org/COPYRIGHT.
4+
//
5+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6+
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8+
// option. This file may not be copied, modified, or distributed
9+
// except according to those terms.
10+
11+
fn main() {
12+
let x = r##"lol"#;
13+
//~^ ERROR unterminated raw string
14+
}

src/test/ui/raw_string.stderr

+8
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
error: unterminated raw string
2+
--> $DIR/raw_string.rs:12:13
3+
|
4+
LL | let x = r##"lol"#;
5+
| ^ unterminated raw string
6+
|
7+
= note: this raw string should be terminated with `"##`
8+

0 commit comments

Comments
 (0)