mirror of
https://github.com/rust-lang/rust.git
synced 2024-11-25 16:24:46 +00:00
Add suffixes to LitError
.
To avoid some unwrapping.
This commit is contained in:
parent
25ed6e43b0
commit
ac47f6c666
@ -31,19 +31,21 @@ pub fn escape_byte_str_symbol(bytes: &[u8]) -> Symbol {
|
|||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub enum LitError {
|
pub enum LitError {
|
||||||
InvalidSuffix,
|
InvalidSuffix(Symbol),
|
||||||
InvalidIntSuffix,
|
InvalidIntSuffix(Symbol),
|
||||||
InvalidFloatSuffix,
|
InvalidFloatSuffix(Symbol),
|
||||||
NonDecimalFloat(u32),
|
NonDecimalFloat(u32), // u32 is the base
|
||||||
IntTooLarge(u32),
|
IntTooLarge(u32), // u32 is the base
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LitKind {
|
impl LitKind {
|
||||||
/// Converts literal token into a semantic literal.
|
/// Converts literal token into a semantic literal.
|
||||||
pub fn from_token_lit(lit: token::Lit) -> Result<LitKind, LitError> {
|
pub fn from_token_lit(lit: token::Lit) -> Result<LitKind, LitError> {
|
||||||
let token::Lit { kind, symbol, suffix } = lit;
|
let token::Lit { kind, symbol, suffix } = lit;
|
||||||
if suffix.is_some() && !kind.may_have_suffix() {
|
if let Some(suffix) = suffix
|
||||||
return Err(LitError::InvalidSuffix);
|
&& !kind.may_have_suffix()
|
||||||
|
{
|
||||||
|
return Err(LitError::InvalidSuffix(suffix));
|
||||||
}
|
}
|
||||||
|
|
||||||
// For byte/char/string literals, chars and escapes have already been
|
// For byte/char/string literals, chars and escapes have already been
|
||||||
@ -271,12 +273,12 @@ fn filtered_float_lit(
|
|||||||
return Err(LitError::NonDecimalFloat(base));
|
return Err(LitError::NonDecimalFloat(base));
|
||||||
}
|
}
|
||||||
Ok(match suffix {
|
Ok(match suffix {
|
||||||
Some(suf) => LitKind::Float(
|
Some(suffix) => LitKind::Float(
|
||||||
symbol,
|
symbol,
|
||||||
ast::LitFloatType::Suffixed(match suf {
|
ast::LitFloatType::Suffixed(match suffix {
|
||||||
sym::f32 => ast::FloatTy::F32,
|
sym::f32 => ast::FloatTy::F32,
|
||||||
sym::f64 => ast::FloatTy::F64,
|
sym::f64 => ast::FloatTy::F64,
|
||||||
_ => return Err(LitError::InvalidFloatSuffix),
|
_ => return Err(LitError::InvalidFloatSuffix(suffix)),
|
||||||
}),
|
}),
|
||||||
),
|
),
|
||||||
None => LitKind::Float(symbol, ast::LitFloatType::Unsuffixed),
|
None => LitKind::Float(symbol, ast::LitFloatType::Unsuffixed),
|
||||||
@ -317,7 +319,7 @@ fn integer_lit(symbol: Symbol, suffix: Option<Symbol>) -> Result<LitKind, LitErr
|
|||||||
// `1f64` and `2f32` etc. are valid float literals, and
|
// `1f64` and `2f32` etc. are valid float literals, and
|
||||||
// `fxxx` looks more like an invalid float literal than invalid integer literal.
|
// `fxxx` looks more like an invalid float literal than invalid integer literal.
|
||||||
_ if suf.as_str().starts_with('f') => return filtered_float_lit(symbol, suffix, base),
|
_ if suf.as_str().starts_with('f') => return filtered_float_lit(symbol, suffix, base),
|
||||||
_ => return Err(LitError::InvalidIntSuffix),
|
_ => return Err(LitError::InvalidIntSuffix(suf)),
|
||||||
},
|
},
|
||||||
_ => ast::LitIntType::Unsuffixed,
|
_ => ast::LitIntType::Unsuffixed,
|
||||||
};
|
};
|
||||||
|
@ -378,28 +378,24 @@ pub fn report_lit_error(
|
|||||||
valid.then(|| format!("0{}{}", base_char.to_ascii_lowercase(), &suffix[1..]))
|
valid.then(|| format!("0{}{}", base_char.to_ascii_lowercase(), &suffix[1..]))
|
||||||
}
|
}
|
||||||
|
|
||||||
let token::Lit { kind, symbol, suffix } = lit;
|
|
||||||
let dcx = &sess.dcx;
|
let dcx = &sess.dcx;
|
||||||
match err {
|
match err {
|
||||||
LitError::InvalidSuffix => {
|
LitError::InvalidSuffix(suffix) => {
|
||||||
let suffix = suffix.unwrap();
|
dcx.emit_err(InvalidLiteralSuffix { span, kind: lit.kind.descr(), suffix })
|
||||||
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix })
|
|
||||||
}
|
}
|
||||||
LitError::InvalidIntSuffix => {
|
LitError::InvalidIntSuffix(suffix) => {
|
||||||
let suf = suffix.expect("suffix error with no suffix");
|
let suf = suffix.as_str();
|
||||||
let suf = suf.as_str();
|
|
||||||
if looks_like_width_suffix(&['i', 'u'], suf) {
|
if looks_like_width_suffix(&['i', 'u'], suf) {
|
||||||
// If it looks like a width, try to be helpful.
|
// If it looks like a width, try to be helpful.
|
||||||
dcx.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() })
|
dcx.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() })
|
||||||
} else if let Some(fixed) = fix_base_capitalisation(symbol.as_str(), suf) {
|
} else if let Some(fixed) = fix_base_capitalisation(lit.symbol.as_str(), suf) {
|
||||||
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed })
|
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed })
|
||||||
} else {
|
} else {
|
||||||
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
|
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
LitError::InvalidFloatSuffix => {
|
LitError::InvalidFloatSuffix(suffix) => {
|
||||||
let suf = suffix.expect("suffix error with no suffix");
|
let suf = suffix.as_str();
|
||||||
let suf = suf.as_str();
|
|
||||||
if looks_like_width_suffix(&['f'], suf) {
|
if looks_like_width_suffix(&['f'], suf) {
|
||||||
// If it looks like a width, try to be helpful.
|
// If it looks like a width, try to be helpful.
|
||||||
dcx.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() })
|
dcx.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() })
|
||||||
|
Loading…
Reference in New Issue
Block a user