Skip to content

Commit ac47f6c

Browse files
committed
Add suffixes to LitError.
To avoid some unwrapping.
1 parent 25ed6e4 commit ac47f6c

File tree

2 files changed

+20
-22
lines changed

2 files changed

+20
-22
lines changed

Diff for: compiler/rustc_ast/src/util/literal.rs

+13-11
Original file line numberDiff line numberDiff line change
@@ -31,19 +31,21 @@ pub fn escape_byte_str_symbol(bytes: &[u8]) -> Symbol {
3131

3232
#[derive(Debug)]
3333
pub enum LitError {
34-
InvalidSuffix,
35-
InvalidIntSuffix,
36-
InvalidFloatSuffix,
37-
NonDecimalFloat(u32),
38-
IntTooLarge(u32),
34+
InvalidSuffix(Symbol),
35+
InvalidIntSuffix(Symbol),
36+
InvalidFloatSuffix(Symbol),
37+
NonDecimalFloat(u32), // u32 is the base
38+
IntTooLarge(u32), // u32 is the base
3939
}
4040

4141
impl LitKind {
4242
/// Converts literal token into a semantic literal.
4343
pub fn from_token_lit(lit: token::Lit) -> Result<LitKind, LitError> {
4444
let token::Lit { kind, symbol, suffix } = lit;
45-
if suffix.is_some() && !kind.may_have_suffix() {
46-
return Err(LitError::InvalidSuffix);
45+
if let Some(suffix) = suffix
46+
&& !kind.may_have_suffix()
47+
{
48+
return Err(LitError::InvalidSuffix(suffix));
4749
}
4850

4951
// For byte/char/string literals, chars and escapes have already been
@@ -271,12 +273,12 @@ fn filtered_float_lit(
271273
return Err(LitError::NonDecimalFloat(base));
272274
}
273275
Ok(match suffix {
274-
Some(suf) => LitKind::Float(
276+
Some(suffix) => LitKind::Float(
275277
symbol,
276-
ast::LitFloatType::Suffixed(match suf {
278+
ast::LitFloatType::Suffixed(match suffix {
277279
sym::f32 => ast::FloatTy::F32,
278280
sym::f64 => ast::FloatTy::F64,
279-
_ => return Err(LitError::InvalidFloatSuffix),
281+
_ => return Err(LitError::InvalidFloatSuffix(suffix)),
280282
}),
281283
),
282284
None => LitKind::Float(symbol, ast::LitFloatType::Unsuffixed),
@@ -317,7 +319,7 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
317319
// `1f64` and `2f32` etc. are valid float literals, and
318320
// `fxxx` looks more like an invalid float literal than invalid integer literal.
319321
_ if suf.as_str().starts_with('f') => return filtered_float_lit(symbol, suffix, base),
320-
_ => return Err(LitError::InvalidIntSuffix),
322+
_ => return Err(LitError::InvalidIntSuffix(suf)),
321323
},
322324
_ => ast::LitIntType::Unsuffixed,
323325
};

Diff for: compiler/rustc_session/src/errors.rs

+7-11
Original file line numberDiff line numberDiff line change
@@ -378,28 +378,24 @@ pub fn report_lit_error(
378378
valid.then(|| format!("0{}{}", base_char.to_ascii_lowercase(), &suffix[1..]))
379379
}
380380

381-
let token::Lit { kind, symbol, suffix } = lit;
382381
let dcx = &sess.dcx;
383382
match err {
384-
LitError::InvalidSuffix => {
385-
let suffix = suffix.unwrap();
386-
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix })
383+
LitError::InvalidSuffix(suffix) => {
384+
dcx.emit_err(InvalidLiteralSuffix { span, kind: lit.kind.descr(), suffix })
387385
}
388-
LitError::InvalidIntSuffix => {
389-
let suf = suffix.expect("suffix error with no suffix");
390-
let suf = suf.as_str();
386+
LitError::InvalidIntSuffix(suffix) => {
387+
let suf = suffix.as_str();
391388
if looks_like_width_suffix(&['i', 'u'], suf) {
392389
// If it looks like a width, try to be helpful.
393390
dcx.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() })
394-
} else if let Some(fixed) = fix_base_capitalisation(symbol.as_str(), suf) {
391+
} else if let Some(fixed) = fix_base_capitalisation(lit.symbol.as_str(), suf) {
395392
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed })
396393
} else {
397394
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
398395
}
399396
}
400-
LitError::InvalidFloatSuffix => {
401-
let suf = suffix.expect("suffix error with no suffix");
402-
let suf = suf.as_str();
397+
LitError::InvalidFloatSuffix(suffix) => {
398+
let suf = suffix.as_str();
403399
if looks_like_width_suffix(&['f'], suf) {
404400
// If it looks like a width, try to be helpful.
405401
dcx.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() })

0 commit comments

Comments
 (0)