Skip to content

Commit 6be2e56

Browse files
committed
Use unescape_unicode for raw C string literals.
They can't contain `\x` escapes, which means they can't contain high bytes, which means we can used `unescape_unicode` instead of `unescape_mixed` to unescape them. This avoids unnecessary used of `MixedUnit`.
1 parent 86f371e commit 6be2e56

File tree

2 files changed

+15
-20
lines changed

2 files changed

+15
-20
lines changed

compiler/rustc_lexer/src/unescape.rs

+14-19
Original file line numberDiff line numberDiff line change
@@ -97,7 +97,13 @@ where
9797
}
9898
Str | ByteStr => unescape_non_raw_common(src, mode, callback),
9999
RawStr | RawByteStr => check_raw_common(src, mode, callback),
100-
CStr | RawCStr => unreachable!(),
100+
RawCStr => check_raw_common(src, mode, &mut |r, mut result| {
101+
if let Ok('\0') = result {
102+
result = Err(EscapeError::NulInCStr);
103+
}
104+
callback(r, result)
105+
}),
106+
CStr => unreachable!(),
101107
}
102108
}
103109

@@ -141,24 +147,13 @@ where
141147
F: FnMut(Range<usize>, Result<MixedUnit, EscapeError>),
142148
{
143149
match mode {
144-
CStr => {
145-
unescape_non_raw_common(src, mode, &mut |r, mut result| {
146-
if let Ok(MixedUnit::Char('\0')) = result {
147-
result = Err(EscapeError::NulInCStr);
148-
}
149-
callback(r, result)
150-
});
151-
}
152-
RawCStr => {
153-
check_raw_common(src, mode, &mut |r, mut result| {
154-
if let Ok('\0') = result {
155-
result = Err(EscapeError::NulInCStr);
156-
}
157-
// High bytes aren't possible in raw strings.
158-
callback(r, result.map(MixedUnit::Char))
159-
});
160-
}
161-
Char | Byte | Str | RawStr | ByteStr | RawByteStr => unreachable!(),
150+
CStr => unescape_non_raw_common(src, mode, &mut |r, mut result| {
151+
if let Ok(MixedUnit::Char('\0')) = result {
152+
result = Err(EscapeError::NulInCStr);
153+
}
154+
callback(r, result)
155+
}),
156+
Char | Byte | Str | RawStr | ByteStr | RawByteStr | RawCStr => unreachable!(),
162157
}
163158
}
164159

compiler/rustc_parse/src/lexer/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -472,7 +472,7 @@ impl<'sess, 'src> StringReader<'sess, 'src> {
472472
if let Some(n_hashes) = n_hashes {
473473
let n = u32::from(n_hashes);
474474
let kind = token::CStrRaw(n_hashes);
475-
self.cook_mixed(kind, Mode::RawCStr, start, end, 3 + n, 1 + n) // cr##" "##
475+
self.cook_unicode(kind, Mode::RawCStr, start, end, 3 + n, 1 + n) // cr##" "##
476476
} else {
477477
self.report_raw_str_error(start, 2);
478478
}

0 commit comments

Comments
 (0)