Skip to content

Commit 94cb5e8

Browse files
committed
Small cleanups in unescaping code.
- Rename `unescape_raw_str_or_raw_byte_str` as `unescape_raw_str_or_byte_str`, which is more accurate. - Remove the unused `Mode::in_single_quotes` method. - Make some assertions more precise, and add a missing one to `unescape_char_or_byte`. - Change all the assertions to `debug_assert!`, because this code is reasonably hot, and the assertions aren't required for memory safety, and any violations are likely to be sufficiently obvious that normal tests will trigger them.
1 parent c91c647 commit 94cb5e8

File tree

1 file changed

+11
-14
lines changed

1 file changed

+11
-14
lines changed

compiler/rustc_lexer/src/unescape.rs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -93,7 +93,7 @@ where
9393
// NOTE: Raw strings do not perform any explicit character escaping, here we
9494
// only translate CRLF to LF and produce errors on bare CR.
9595
Mode::RawStr | Mode::RawByteStr => {
96-
unescape_raw_str_or_byte_str(literal_text, mode, callback)
96+
unescape_raw_str_or_raw_byte_str(literal_text, mode, callback)
9797
}
9898
}
9999
}
@@ -105,7 +105,7 @@ pub fn unescape_byte_literal<F>(literal_text: &str, mode: Mode, callback: &mut F
105105
where
106106
F: FnMut(Range<usize>, Result<u8, EscapeError>),
107107
{
108-
assert!(mode.is_bytes());
108+
debug_assert!(mode.is_bytes());
109109
unescape_literal(literal_text, mode, &mut |range, result| {
110110
callback(range, result.map(byte_from_char));
111111
})
@@ -129,7 +129,7 @@ pub fn unescape_byte(literal_text: &str) -> Result<u8, (usize, EscapeError)> {
129129
}
130130

131131
/// What kind of literal do we parse.
132-
#[derive(Debug, Clone, Copy)]
132+
#[derive(Debug, Clone, Copy, PartialEq)]
133133
pub enum Mode {
134134
Char,
135135
Str,
@@ -140,17 +140,13 @@ pub enum Mode {
140140
}
141141

142142
impl Mode {
143-
pub fn in_single_quotes(self) -> bool {
143+
pub fn in_double_quotes(self) -> bool {
144144
match self {
145-
Mode::Char | Mode::Byte => true,
146-
Mode::Str | Mode::ByteStr | Mode::RawStr | Mode::RawByteStr => false,
145+
Mode::Str | Mode::ByteStr | Mode::RawStr | Mode::RawByteStr => true,
146+
Mode::Char | Mode::Byte => false,
147147
}
148148
}
149149

150-
pub fn in_double_quotes(self) -> bool {
151-
!self.in_single_quotes()
152-
}
153-
154150
pub fn is_bytes(self) -> bool {
155151
match self {
156152
Mode::Byte | Mode::ByteStr | Mode::RawByteStr => true,
@@ -263,6 +259,7 @@ fn ascii_check(first_char: char, mode: Mode) -> Result<char, EscapeError> {
263259
}
264260

265261
fn unescape_char_or_byte(chars: &mut Chars<'_>, mode: Mode) -> Result<char, EscapeError> {
262+
debug_assert!(mode == Mode::Char || mode == Mode::Byte);
266263
let first_char = chars.next().ok_or(EscapeError::ZeroChars)?;
267264
let res = match first_char {
268265
'\\' => scan_escape(chars, mode),
@@ -282,7 +279,7 @@ fn unescape_str_or_byte_str<F>(src: &str, mode: Mode, callback: &mut F)
282279
where
283280
F: FnMut(Range<usize>, Result<char, EscapeError>),
284281
{
285-
assert!(mode.in_double_quotes());
282+
debug_assert!(mode == Mode::Str || mode == Mode::ByteStr);
286283
let initial_len = src.len();
287284
let mut chars = src.chars();
288285
while let Some(first_char) = chars.next() {
@@ -344,11 +341,11 @@ where
344341
/// sequence of characters or errors.
345342
/// NOTE: Raw strings do not perform any explicit character escaping, here we
346343
/// only translate CRLF to LF and produce errors on bare CR.
347-
fn unescape_raw_str_or_byte_str<F>(literal_text: &str, mode: Mode, callback: &mut F)
344+
fn unescape_raw_str_or_raw_byte_str<F>(literal_text: &str, mode: Mode, callback: &mut F)
348345
where
349346
F: FnMut(Range<usize>, Result<char, EscapeError>),
350347
{
351-
assert!(mode.in_double_quotes());
348+
debug_assert!(mode == Mode::RawStr || mode == Mode::RawByteStr);
352349
let initial_len = literal_text.len();
353350

354351
let mut chars = literal_text.chars();
@@ -368,7 +365,7 @@ where
368365

369366
fn byte_from_char(c: char) -> u8 {
370367
let res = c as u32;
371-
assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
368+
debug_assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
372369
res as u8
373370
}
374371

0 commit comments

Comments
 (0)