Skip to content

Commit a838952

Browse files
committed
Remove unescape_byte_literal.
It's easy to just use `unescape_literal` + `byte_from_char`.
1 parent a203482 commit a838952

File tree

4 files changed

+20
-43
lines changed

4 files changed

+20
-43
lines changed

compiler/rustc_ast/src/util/literal.rs

+11-18
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,9 @@
22
33
use crate::ast::{self, Lit, LitKind};
44
use crate::token::{self, Token};
5-
6-
use rustc_lexer::unescape::{unescape_byte, unescape_char};
7-
use rustc_lexer::unescape::{unescape_byte_literal, unescape_literal, Mode};
5+
use rustc_lexer::unescape::{byte_from_char, unescape_byte, unescape_char, unescape_literal, Mode};
86
use rustc_span::symbol::{kw, sym, Symbol};
97
use rustc_span::Span;
10-
118
use std::ascii;
129

1310
pub enum LitError {
@@ -109,13 +106,11 @@ impl LitKind {
109106
let s = symbol.as_str();
110107
let mut buf = Vec::with_capacity(s.len());
111108
let mut error = Ok(());
112-
unescape_byte_literal(&s, Mode::ByteStr, &mut |_, unescaped_byte| {
113-
match unescaped_byte {
114-
Ok(c) => buf.push(c),
115-
Err(err) => {
116-
if err.is_fatal() {
117-
error = Err(LitError::LexerError);
118-
}
109+
unescape_literal(&s, Mode::ByteStr, &mut |_, c| match c {
110+
Ok(c) => buf.push(byte_from_char(c)),
111+
Err(err) => {
112+
if err.is_fatal() {
113+
error = Err(LitError::LexerError);
119114
}
120115
}
121116
});
@@ -127,13 +122,11 @@ impl LitKind {
127122
let bytes = if s.contains('\r') {
128123
let mut buf = Vec::with_capacity(s.len());
129124
let mut error = Ok(());
130-
unescape_byte_literal(&s, Mode::RawByteStr, &mut |_, unescaped_byte| {
131-
match unescaped_byte {
132-
Ok(c) => buf.push(c),
133-
Err(err) => {
134-
if err.is_fatal() {
135-
error = Err(LitError::LexerError);
136-
}
125+
unescape_literal(&s, Mode::RawByteStr, &mut |_, c| match c {
126+
Ok(c) => buf.push(byte_from_char(c)),
127+
Err(err) => {
128+
if err.is_fatal() {
129+
error = Err(LitError::LexerError);
137130
}
138131
}
139132
});

compiler/rustc_lexer/src/unescape.rs

+2-14
Original file line numberDiff line numberDiff line change
@@ -93,19 +93,6 @@ where
9393
}
9494
}
9595

96-
/// Takes a contents of a byte, byte string or raw byte string (without quotes)
97-
/// and produces a sequence of bytes or errors.
98-
/// Values are returned through invoking of the provided callback.
99-
pub fn unescape_byte_literal<F>(src: &str, mode: Mode, callback: &mut F)
100-
where
101-
F: FnMut(Range<usize>, Result<u8, EscapeError>),
102-
{
103-
debug_assert!(mode.is_byte());
104-
unescape_literal(src, mode, &mut |range, result| {
105-
callback(range, result.map(byte_from_char));
106-
})
107-
}
108-
10996
/// Takes a contents of a char literal (without quotes), and returns an
11097
/// unescaped char or an error
11198
pub fn unescape_char(src: &str) -> Result<char, (usize, EscapeError)> {
@@ -351,7 +338,8 @@ where
351338
}
352339
}
353340

354-
fn byte_from_char(c: char) -> u8 {
341+
#[inline]
342+
pub fn byte_from_char(c: char) -> u8 {
355343
let res = c as u32;
356344
debug_assert!(res <= u8::MAX as u32, "guaranteed because of Mode::ByteStr");
357345
res as u8

compiler/rustc_lexer/src/unescape/tests.rs

+5-7
Original file line numberDiff line numberDiff line change
@@ -246,10 +246,10 @@ fn test_unescape_byte_good() {
246246
fn test_unescape_byte_str_good() {
247247
fn check(literal_text: &str, expected: &[u8]) {
248248
let mut buf = Ok(Vec::with_capacity(literal_text.len()));
249-
unescape_byte_literal(literal_text, Mode::ByteStr, &mut |range, c| {
249+
unescape_literal(literal_text, Mode::ByteStr, &mut |range, c| {
250250
if let Ok(b) = &mut buf {
251251
match c {
252-
Ok(c) => b.push(c),
252+
Ok(c) => b.push(byte_from_char(c)),
253253
Err(e) => buf = Err((range, e)),
254254
}
255255
}
@@ -280,15 +280,13 @@ fn test_unescape_raw_str() {
280280

281281
#[test]
282282
fn test_unescape_raw_byte_str() {
283-
fn check(literal: &str, expected: &[(Range<usize>, Result<u8, EscapeError>)]) {
283+
fn check(literal: &str, expected: &[(Range<usize>, Result<char, EscapeError>)]) {
284284
let mut unescaped = Vec::with_capacity(literal.len());
285-
unescape_byte_literal(literal, Mode::RawByteStr, &mut |range, res| {
286-
unescaped.push((range, res))
287-
});
285+
unescape_literal(literal, Mode::RawByteStr, &mut |range, res| unescaped.push((range, res)));
288286
assert_eq!(unescaped, expected);
289287
}
290288

291289
check("\r", &[(0..1, Err(EscapeError::BareCarriageReturnInRawString))]);
292290
check("🦀", &[(0..4, Err(EscapeError::NonAsciiCharInByte))]);
293-
check("🦀a", &[(0..4, Err(EscapeError::NonAsciiCharInByte)), (4..5, Ok(byte_from_char('a')))]);
291+
check("🦀a", &[(0..4, Err(EscapeError::NonAsciiCharInByte)), (4..5, Ok('a'))]);
294292
}

src/tools/rust-analyzer/crates/syntax/src/validation.rs

+2-4
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,7 @@
55
mod block;
66

77
use rowan::Direction;
8-
use rustc_lexer::unescape::{
9-
self, unescape_byte, unescape_byte_literal, unescape_char, unescape_literal, Mode,
10-
};
8+
use rustc_lexer::unescape::{self, unescape_byte, unescape_char, unescape_literal, Mode};
119

1210
use crate::{
1311
algo,
@@ -143,7 +141,7 @@ fn validate_literal(literal: ast::Literal, acc: &mut Vec<SyntaxError>) {
143141
ast::LiteralKind::ByteString(s) => {
144142
if !s.is_raw() {
145143
if let Some(without_quotes) = unquote(text, 2, '"') {
146-
unescape_byte_literal(without_quotes, Mode::ByteStr, &mut |range, char| {
144+
unescape_literal(without_quotes, Mode::ByteStr, &mut |range, char| {
147145
if let Err(err) = char {
148146
push_err(2, (range.start, err));
149147
}

0 commit comments

Comments
 (0)