forked from rust-lang/rust
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of rust-lang#70522 - rcoh:60762-raw-string-errors, r=pet…
…rochenkov Improve error messages for raw strings (rust-lang#60762) This diff improves error messages around raw strings in a few ways: - Catch extra trailing `#` in the parser. This can't be handled in the lexer because we could be in a macro that actually expects another # (see test) - Refactor & unify error handling in the lexer between ByteStrings and RawByteStrings - Detect potentially intended terminators (longest sequence of "#*" is suggested) Fixes rust-lang#60762 cc @estebank who reviewed the original (abandoned) PR for the same ticket. r? @Centril
- Loading branch information
Showing
22 changed files
with
385 additions
and
74 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,121 @@ | ||
#[cfg(test)] | ||
mod tests { | ||
use crate::*; | ||
|
||
fn check_raw_str( | ||
s: &str, | ||
expected: UnvalidatedRawStr, | ||
validated: Result<ValidatedRawStr, LexRawStrError>, | ||
) { | ||
let s = &format!("r{}", s); | ||
let mut cursor = Cursor::new(s); | ||
cursor.bump(); | ||
let tok = cursor.raw_double_quoted_string(0); | ||
assert_eq!(tok, expected); | ||
assert_eq!(tok.validate(), validated); | ||
} | ||
|
||
#[test] | ||
fn test_naked_raw_str() { | ||
check_raw_str( | ||
r#""abc""#, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 0, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Ok(ValidatedRawStr { n_hashes: 0 }), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_raw_no_start() { | ||
check_raw_str( | ||
r##""abc"#"##, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 0, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Ok(ValidatedRawStr { n_hashes: 0 }), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_too_many_terminators() { | ||
// this error is handled in the parser later | ||
check_raw_str( | ||
r###"#"abc"##"###, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 1, | ||
n_end_hashes: 1, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Ok(ValidatedRawStr { n_hashes: 1 }), | ||
); | ||
} | ||
|
||
#[test] | ||
fn test_unterminated() { | ||
check_raw_str( | ||
r#"#"abc"#, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 1, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Err(LexRawStrError::NoTerminator { | ||
expected: 1, | ||
found: 0, | ||
possible_terminator_offset: None, | ||
}), | ||
); | ||
check_raw_str( | ||
r###"##"abc"#"###, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 2, | ||
n_end_hashes: 1, | ||
valid_start: true, | ||
possible_terminator_offset: Some(7), | ||
}, | ||
Err(LexRawStrError::NoTerminator { | ||
expected: 2, | ||
found: 1, | ||
possible_terminator_offset: Some(7), | ||
}), | ||
); | ||
// We're looking for "# not just any # | ||
check_raw_str( | ||
r###"##"abc#"###, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 2, | ||
n_end_hashes: 0, | ||
valid_start: true, | ||
possible_terminator_offset: None, | ||
}, | ||
Err(LexRawStrError::NoTerminator { | ||
expected: 2, | ||
found: 0, | ||
possible_terminator_offset: None, | ||
}), | ||
) | ||
} | ||
|
||
#[test] | ||
fn test_invalid_start() { | ||
check_raw_str( | ||
r##"#~"abc"#"##, | ||
UnvalidatedRawStr { | ||
n_start_hashes: 1, | ||
n_end_hashes: 0, | ||
valid_start: false, | ||
possible_terminator_offset: None, | ||
}, | ||
Err(LexRawStrError::InvalidStarter), | ||
); | ||
} | ||
} |
Oops, something went wrong.