Skip to content

Commit a203482

Browse files
committed
Inline and remove validate_int_literal.
It has a single callsite, and is fairly small. The `Float` match arm already has base-specific checking inline, so this makes things more consistent.
1 parent d963686 commit a203482

File tree

2 files changed

+18
-23
lines changed

2 files changed

+18
-23
lines changed

compiler/rustc_lexer/src/lib.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -203,13 +203,13 @@ pub enum RawStrError {
203203
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord)]
204204
pub enum Base {
205205
/// Literal starts with "0b".
206-
Binary,
206+
Binary = 2,
207207
/// Literal starts with "0o".
208-
Octal,
209-
/// Literal starts with "0x".
210-
Hexadecimal,
208+
Octal = 8,
211209
/// Literal doesn't contain a prefix.
212-
Decimal,
210+
Decimal = 10,
211+
/// Literal starts with "0x".
212+
Hexadecimal = 16,
213213
}
214214

215215
/// `rustc` allows files to have a shebang, e.g. "#!/usr/bin/rustrun",

compiler/rustc_parse/src/lexer/mod.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,19 @@ impl<'a> StringReader<'a> {
437437
.emit();
438438
(token::Integer, sym::integer(0))
439439
} else {
440-
self.validate_int_literal(base, start, end);
440+
if matches!(base, Base::Binary | Base::Octal) {
441+
let base = base as u32;
442+
let s = self.str_from_to(start + BytePos(2), end);
443+
for (idx, c) in s.char_indices() {
444+
if c != '_' && c.to_digit(base).is_none() {
445+
self.err_span_(
446+
start + BytePos::from_usize(2 + idx),
447+
start + BytePos::from_usize(2 + idx + c.len_utf8()),
448+
&format!("invalid digit for a base {} literal", base),
449+
);
450+
}
451+
}
452+
}
441453
(token::Integer, self.symbol_from_to(start, end))
442454
}
443455
}
@@ -683,23 +695,6 @@ impl<'a> StringReader<'a> {
683695
});
684696
(kind, Symbol::intern(lit_content))
685697
}
686-
687-
fn validate_int_literal(&self, base: Base, content_start: BytePos, content_end: BytePos) {
688-
let base = match base {
689-
Base::Binary => 2,
690-
Base::Octal => 8,
691-
_ => return,
692-
};
693-
let s = self.str_from_to(content_start + BytePos(2), content_end);
694-
for (idx, c) in s.char_indices() {
695-
let idx = idx as u32;
696-
if c != '_' && c.to_digit(base).is_none() {
697-
let lo = content_start + BytePos(2 + idx);
698-
let hi = content_start + BytePos(2 + idx + c.len_utf8() as u32);
699-
self.err_span_(lo, hi, &format!("invalid digit for a base {} literal", base));
700-
}
701-
}
702-
}
703698
}
704699

705700
pub fn nfc_normalize(string: &str) -> Symbol {

0 commit comments

Comments
 (0)