Skip to content

Commit c15f86b

Browse files
committed
Cleanup error messages, improve docstrings
1 parent 629e97a commit c15f86b

File tree

8 files changed

+48
-30
lines changed

8 files changed

+48
-30
lines changed

src/librustc_lexer/src/cursor.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ impl<'a> Cursor<'a> {
4141
/// If requested position doesn't exist, `EOF_CHAR` is returned.
4242
/// However, getting `EOF_CHAR` doesn't always mean actual end of file,
4343
/// it should be checked with `is_eof` method.
44-
pub(crate) fn nth_char(&self, n: usize) -> char {
44+
fn nth_char(&self, n: usize) -> char {
4545
self.chars().nth(n).unwrap_or(EOF_CHAR)
4646
}
4747

src/librustc_lexer/src/lib.rs

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -141,25 +141,41 @@ pub enum LiteralKind {
141141
RawByteStr(UnvalidatedRawStr),
142142
}
143143

144+
/// Represents something that looks like a raw string, but may have some
145+
/// problems. Use `.validate()` to convert it into something
146+
/// usable.
144147
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
145148
pub struct UnvalidatedRawStr {
149+
/// The prefix (`r###"`) is valid
146150
valid_start: bool,
151+
/// The number of leading `#`
147152
n_start_hashes: usize,
153+
/// The number of trailing `#`. `n_end_hashes` <= `n_start_hashes`
148154
n_end_hashes: usize,
155+
/// The offset starting at `r` or `br` where the user may have intended to end the string.
156+
/// Currently, it is the longest sequence of pattern `"#+"`.
149157
possible_terminator_offset: Option<usize>,
150158
}
151159

160+
/// Error produced validating a raw string. Represents cases like:
161+
/// - `r##~"abcde"##`: `LexRawStrError::InvalidStarter`
162+
/// - `r###"abcde"##`: `LexRawStrError::NoTerminator { expected: 3, found: 2, possible_terminator_offset: Some(11)`
163+
/// - Too many `#`s (>65536): `TooManyDelimiters`
152164
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
153165
pub enum LexRawStrError {
154-
/// Non # characters between `r` and `"` eg. `r#~"..`
166+
/// Non `#` characters exist between `r` and `"` eg. `r#~"..`
155167
InvalidStarter,
156-
/// The string was never terminated. `possible_terminator_offset` is the best guess of where they
168+
/// The string was never terminated. `possible_terminator_offset` is the number of characters after `r` or `br` where they
157169
/// may have intended to terminate it.
158170
NoTerminator { expected: usize, found: usize, possible_terminator_offset: Option<usize> },
159-
/// More than 65536 # signs
171+
/// More than 65536 `#`s exist.
160172
TooManyDelimiters,
161173
}
162174

175+
/// Raw String that contains a valid prefix (`#+"`) and postfix (`"#+`) where
176+
/// there are a matching number of `#` characters in both. Note that this will
177+
/// not consume extra trailing `#` characters: `r###"abcde"####` is lexed as a
178+
/// `ValidatedRawString { n_hashes: 3 }` followed by a `#` token.
163179
#[derive(Debug, Eq, PartialEq, Copy, Clone)]
164180
pub struct ValidatedRawStr {
165181
n_hashes: u16,
@@ -172,27 +188,26 @@ impl ValidatedRawStr {
172188
}
173189

174190
impl UnvalidatedRawStr {
175-
pub fn started(&self) -> bool {
176-
self.valid_start
177-
}
178-
179191
pub fn validate(self) -> Result<ValidatedRawStr, LexRawStrError> {
180192
if !self.valid_start {
181193
return Err(LexRawStrError::InvalidStarter);
182194
}
183195

196+
// Only up to 65535 `#`s are allowed in raw strings
184197
let n_start_safe: u16 =
185198
self.n_start_hashes.try_into().map_err(|_| LexRawStrError::TooManyDelimiters)?;
186-
match (self.n_start_hashes, self.n_end_hashes) {
187-
(n_start, n_end) if n_start > n_end => Err(LexRawStrError::NoTerminator {
188-
expected: n_start,
199+
200+
if self.n_start_hashes > self.n_end_hashes {
201+
Err(LexRawStrError::NoTerminator {
202+
expected: self.n_start_hashes,
189203
found: self.n_end_hashes,
190204
possible_terminator_offset: self.possible_terminator_offset,
191-
}),
192-
(n_start, n_end) => {
193-
debug_assert_eq!(n_start, n_end);
194-
Ok(ValidatedRawStr { n_hashes: n_start_safe })
195-
}
205+
})
206+
} else {
207+
// Since the lexer should never produce a literal with n_end > n_start, if n_start <= n_end,
208+
// they must be equal.
209+
debug_assert_eq!(self.n_start_hashes, self.n_end_hashes);
210+
Ok(ValidatedRawStr { n_hashes: n_start_safe })
196211
}
197212
}
198213
}
@@ -656,7 +671,7 @@ impl Cursor<'_> {
656671
false
657672
}
658673

659-
/// Eats the double-quoted string an UnvalidatedRawStr
674+
/// Eats the double-quoted string and returns an `UnvalidatedRawStr`.
660675
fn raw_double_quoted_string(&mut self, prefix_len: usize) -> UnvalidatedRawStr {
661676
debug_assert!(self.prev() == 'r');
662677
let mut valid_start: bool = false;

src/librustc_parse/lexer/mod.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -533,13 +533,12 @@ impl<'a> StringReader<'a> {
533533
}
534534

535535
if let Some(possible_offset) = possible_offset {
536-
let span = self.mk_sp(
537-
start + BytePos(possible_offset as u32),
538-
start + BytePos(possible_offset as u32) + BytePos(found_terminators as u32),
539-
);
536+
let lo = start + BytePos(possible_offset as u32);
537+
let hi = lo + BytePos(found_terminators as u32);
538+
let span = self.mk_sp(lo, hi);
540539
err.span_suggestion(
541540
span,
542-
"you might have intended to terminate the string here",
541+
"consider terminating the string here",
543542
"#".repeat(n_hashes),
544543
Applicability::MaybeIncorrect,
545544
);

src/librustc_parse/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#![feature(crate_visibility_modifier)]
55
#![feature(bindings_after_at)]
66
#![feature(try_blocks)]
7+
#![feature(or_patterns)]
78

89
use rustc_ast::ast;
910
use rustc_ast::token::{self, Nonterminal};

src/librustc_parse/parser/diagnostics.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -288,9 +288,12 @@ impl<'a> Parser<'a> {
288288

289289
fn check_too_many_raw_str_terminators(&mut self, err: &mut DiagnosticBuilder<'_>) -> bool {
290290
let prev_token_raw_str = match self.prev_token {
291-
Token { kind: TokenKind::Literal(Lit { kind: LitKind::StrRaw(n), .. }), .. } => Some(n),
292291
Token {
293-
kind: TokenKind::Literal(Lit { kind: LitKind::ByteStrRaw(n), .. }), ..
292+
kind:
293+
TokenKind::Literal(Lit {
294+
kind: LitKind::StrRaw(n) | LitKind::ByteStrRaw(n), ..
295+
}),
296+
..
294297
} => Some(n),
295298
_ => None,
296299
};
@@ -300,11 +303,11 @@ impl<'a> Parser<'a> {
300303
err.set_primary_message("too many `#` when terminating raw string");
301304
err.span_suggestion(
302305
self.token.span,
303-
"Remove the extra `#`",
306+
"remove the extra `#`",
304307
String::new(),
305308
Applicability::MachineApplicable,
306309
);
307-
err.note(&format!("The raw string started with {} `#`s", n_hashes));
310+
err.note(&format!("the raw string started with {} `#`s", n_hashes));
308311
return true;
309312
}
310313
}

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0748]: unterminated raw string
22
--> $DIR/raw-byte-string-eof.rs:2:5
33
|
44
LL | br##"a"#;
5-
| ^ - help: you might have intended to terminate the string here: `##`
5+
| ^ - help: consider terminating the string here: `##`
66
| |
77
| unterminated raw string
88
|

src/test/ui/parser/raw/raw-str-unbalanced.stderr

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@ error: too many `#` when terminating raw string
22
--> $DIR/raw-str-unbalanced.rs:3:9
33
|
44
LL | "##
5-
| ^ help: Remove the extra `#`
5+
| ^ help: remove the extra `#`
66
|
7-
= note: The raw string started with 1 `#`s
7+
= note: the raw string started with 1 `#`s
88

99
error: aborting due to previous error
1010

src/test/ui/parser/raw/raw_string.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error[E0748]: unterminated raw string
22
--> $DIR/raw_string.rs:2:13
33
|
44
LL | let x = r##"lol"#;
5-
| ^ - help: you might have intended to terminate the string here: `##`
5+
| ^ - help: consider terminating the string here: `##`
66
| |
77
| unterminated raw string
88
|

0 commit comments

Comments
 (0)