Make report_lit_error return ErrorGuaranteed.

This will be helpful for subsequent commits.
This commit is contained in:
Nicholas Nethercote 2024-02-14 19:30:12 +11:00
parent 8b35f8e41e
commit a513bb20c3

View File

@ -3,7 +3,8 @@ use std::num::NonZeroU32;
use rustc_ast::token;
use rustc_ast::util::literal::LitError;
use rustc_errors::{
codes::*, DiagCtxt, DiagnosticBuilder, DiagnosticMessage, IntoDiagnostic, Level, MultiSpan,
codes::*, DiagCtxt, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, IntoDiagnostic,
Level, MultiSpan,
};
use rustc_macros::Diagnostic;
use rustc_span::{Span, Symbol};
@ -344,7 +345,12 @@ pub(crate) struct BinaryFloatLiteralNotSupported {
pub span: Span,
}
pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span: Span) {
pub fn report_lit_error(
sess: &ParseSess,
err: LitError,
lit: token::Lit,
span: Span,
) -> ErrorGuaranteed {
// Checks if `s` looks like i32 or u1234 etc.
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
@ -372,24 +378,23 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
valid.then(|| format!("0{}{}", base_char.to_ascii_lowercase(), &suffix[1..]))
}
let token::Lit { kind, symbol, suffix, .. } = lit;
let token::Lit { kind, symbol, suffix } = lit;
let dcx = &sess.dcx;
match err {
LitError::InvalidSuffix => {
if let Some(suffix) = suffix {
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix });
}
let suffix = suffix.unwrap();
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix })
}
LitError::InvalidIntSuffix => {
let suf = suffix.expect("suffix error with no suffix");
let suf = suf.as_str();
if looks_like_width_suffix(&['i', 'u'], suf) {
// 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) {
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed });
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed })
} else {
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() });
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
}
}
LitError::InvalidFloatSuffix => {
@ -397,19 +402,17 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
let suf = suf.as_str();
if looks_like_width_suffix(&['f'], suf) {
// 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() })
} else {
dcx.emit_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() });
dcx.emit_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() })
}
}
LitError::NonDecimalFloat(base) => {
match base {
16 => dcx.emit_err(HexadecimalFloatLiteralNotSupported { span }),
8 => dcx.emit_err(OctalFloatLiteralNotSupported { span }),
2 => dcx.emit_err(BinaryFloatLiteralNotSupported { span }),
_ => unreachable!(),
};
}
LitError::NonDecimalFloat(base) => match base {
16 => dcx.emit_err(HexadecimalFloatLiteralNotSupported { span }),
8 => dcx.emit_err(OctalFloatLiteralNotSupported { span }),
2 => dcx.emit_err(BinaryFloatLiteralNotSupported { span }),
_ => unreachable!(),
},
LitError::IntTooLarge(base) => {
let max = u128::MAX;
let limit = match base {
@ -418,7 +421,7 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
16 => format!("{max:#x}"),
_ => format!("{max}"),
};
dcx.emit_err(IntLiteralTooLarge { span, limit });
dcx.emit_err(IntLiteralTooLarge { span, limit })
}
}
}