Skip to content

Commit 8b35f8e

Browse files
committed
Remove LitError::LexerError.
`cook_lexer_literal` can emit an error about an invalid int literal but then return a non-`Err` token. And then `integer_lit` has to account for this to avoid printing a redundant error message. This commit changes `cook_lexer_literal` to return `Err` in that case. Then `integer_lit` doesn't need the special case, and `LitError::LexerError` can be removed.
1 parent ee9c7c9 commit 8b35f8e

File tree

3 files changed

+18
-26
lines changed

3 files changed

+18
-26
lines changed

compiler/rustc_ast/src/util/literal.rs

+3-8
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ pub fn escape_byte_str_symbol(bytes: &[u8]) -> Symbol {
3131

3232
#[derive(Debug)]
3333
pub enum LitError {
34-
LexerError,
3534
InvalidSuffix,
3635
InvalidIntSuffix,
3736
InvalidFloatSuffix,
@@ -324,11 +323,7 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
324323
};
325324

326325
let s = &s[if base != 10 { 2 } else { 0 }..];
327-
u128::from_str_radix(s, base).map(|i| LitKind::Int(i.into(), ty)).map_err(|_| {
328-
// Small bases are lexed as if they were base 10, e.g, the string
329-
// might be `0b10201`. This will cause the conversion above to fail,
330-
// but these kinds of errors are already reported by the lexer.
331-
let from_lexer = base < 10 && s.chars().any(|c| c.to_digit(10).is_some_and(|d| d >= base));
332-
if from_lexer { LitError::LexerError } else { LitError::IntTooLarge(base) }
333-
})
326+
u128::from_str_radix(s, base)
327+
.map(|i| LitKind::Int(i.into(), ty))
328+
.map_err(|_| LitError::IntTooLarge(base))
334329
}

compiler/rustc_parse/src/lexer/mod.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ use rustc_session::lint::builtin::{
1616
};
1717
use rustc_session::lint::BuiltinLintDiagnostics;
1818
use rustc_session::parse::ParseSess;
19-
use rustc_span::symbol::{sym, Symbol};
19+
use rustc_span::symbol::Symbol;
2020
use rustc_span::{edition::Edition, BytePos, Pos, Span};
2121

2222
mod diagnostics;
@@ -478,26 +478,26 @@ impl<'sess, 'src> StringReader<'sess, 'src> {
478478
}
479479
}
480480
rustc_lexer::LiteralKind::Int { base, empty_int } => {
481+
let mut kind = token::Integer;
481482
if empty_int {
482483
let span = self.mk_sp(start, end);
483484
self.dcx().emit_err(errors::NoDigitsLiteral { span });
484-
(token::Integer, sym::integer(0))
485-
} else {
486-
if matches!(base, Base::Binary | Base::Octal) {
487-
let base = base as u32;
488-
let s = self.str_from_to(start + BytePos(2), end);
489-
for (idx, c) in s.char_indices() {
490-
let span = self.mk_sp(
491-
start + BytePos::from_usize(2 + idx),
492-
start + BytePos::from_usize(2 + idx + c.len_utf8()),
493-
);
494-
if c != '_' && c.to_digit(base).is_none() {
495-
self.dcx().emit_err(errors::InvalidDigitLiteral { span, base });
496-
}
485+
kind = token::Err;
486+
} else if matches!(base, Base::Binary | Base::Octal) {
487+
let base = base as u32;
488+
let s = self.str_from_to(start + BytePos(2), end);
489+
for (idx, c) in s.char_indices() {
490+
let span = self.mk_sp(
491+
start + BytePos::from_usize(2 + idx),
492+
start + BytePos::from_usize(2 + idx + c.len_utf8()),
493+
);
494+
if c != '_' && c.to_digit(base).is_none() {
495+
self.dcx().emit_err(errors::InvalidDigitLiteral { span, base });
496+
kind = token::Err;
497497
}
498498
}
499-
(token::Integer, self.symbol_from_to(start, end))
500499
}
500+
(kind, self.symbol_from_to(start, end))
501501
}
502502
rustc_lexer::LiteralKind::Float { base, empty_exponent } => {
503503
if empty_exponent {

compiler/rustc_session/src/errors.rs

-3
Original file line numberDiff line numberDiff line change
@@ -375,9 +375,6 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
375375
let token::Lit { kind, symbol, suffix, .. } = lit;
376376
let dcx = &sess.dcx;
377377
match err {
378-
// `LexerError` is an error, but it was already reported
379-
// by lexer, so here we don't report it the second time.
380-
LitError::LexerError => {}
381378
LitError::InvalidSuffix => {
382379
if let Some(suffix) = suffix {
383380
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix });

0 commit comments

Comments
 (0)