Skip to content

Commit a513bb2

Browse files
committed
Make report_lit_error return ErrorGuaranteed.
This will be helpful for subsequent commits.
1 parent 8b35f8e commit a513bb2

File tree

1 file changed

+23
-20
lines changed

1 file changed

+23
-20
lines changed

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

+23-20
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,8 @@ use std::num::NonZeroU32;
33
use rustc_ast::token;
44
use rustc_ast::util::literal::LitError;
55
use rustc_errors::{
6-
codes::*, DiagCtxt, DiagnosticBuilder, DiagnosticMessage, IntoDiagnostic, Level, MultiSpan,
6+
codes::*, DiagCtxt, DiagnosticBuilder, DiagnosticMessage, ErrorGuaranteed, IntoDiagnostic,
7+
Level, MultiSpan,
78
};
89
use rustc_macros::Diagnostic;
910
use rustc_span::{Span, Symbol};
@@ -344,7 +345,12 @@ pub(crate) struct BinaryFloatLiteralNotSupported {
344345
pub span: Span,
345346
}
346347

347-
pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span: Span) {
348+
pub fn report_lit_error(
349+
sess: &ParseSess,
350+
err: LitError,
351+
lit: token::Lit,
352+
span: Span,
353+
) -> ErrorGuaranteed {
348354
// Checks if `s` looks like i32 or u1234 etc.
349355
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
350356
s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
@@ -372,44 +378,41 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
372378
valid.then(|| format!("0{}{}", base_char.to_ascii_lowercase(), &suffix[1..]))
373379
}
374380

375-
let token::Lit { kind, symbol, suffix, .. } = lit;
381+
let token::Lit { kind, symbol, suffix } = lit;
376382
let dcx = &sess.dcx;
377383
match err {
378384
LitError::InvalidSuffix => {
379-
if let Some(suffix) = suffix {
380-
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix });
381-
}
385+
let suffix = suffix.unwrap();
386+
dcx.emit_err(InvalidLiteralSuffix { span, kind: kind.descr(), suffix })
382387
}
383388
LitError::InvalidIntSuffix => {
384389
let suf = suffix.expect("suffix error with no suffix");
385390
let suf = suf.as_str();
386391
if looks_like_width_suffix(&['i', 'u'], suf) {
387392
// If it looks like a width, try to be helpful.
388-
dcx.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() });
393+
dcx.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() })
389394
} else if let Some(fixed) = fix_base_capitalisation(symbol.as_str(), suf) {
390-
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed });
395+
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed })
391396
} else {
392-
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() });
397+
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
393398
}
394399
}
395400
LitError::InvalidFloatSuffix => {
396401
let suf = suffix.expect("suffix error with no suffix");
397402
let suf = suf.as_str();
398403
if looks_like_width_suffix(&['f'], suf) {
399404
// If it looks like a width, try to be helpful.
400-
dcx.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() });
405+
dcx.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() })
401406
} else {
402-
dcx.emit_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() });
407+
dcx.emit_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() })
403408
}
404409
}
405-
LitError::NonDecimalFloat(base) => {
406-
match base {
407-
16 => dcx.emit_err(HexadecimalFloatLiteralNotSupported { span }),
408-
8 => dcx.emit_err(OctalFloatLiteralNotSupported { span }),
409-
2 => dcx.emit_err(BinaryFloatLiteralNotSupported { span }),
410-
_ => unreachable!(),
411-
};
412-
}
410+
LitError::NonDecimalFloat(base) => match base {
411+
16 => dcx.emit_err(HexadecimalFloatLiteralNotSupported { span }),
412+
8 => dcx.emit_err(OctalFloatLiteralNotSupported { span }),
413+
2 => dcx.emit_err(BinaryFloatLiteralNotSupported { span }),
414+
_ => unreachable!(),
415+
},
413416
LitError::IntTooLarge(base) => {
414417
let max = u128::MAX;
415418
let limit = match base {
@@ -418,7 +421,7 @@ pub fn report_lit_error(sess: &ParseSess, err: LitError, lit: token::Lit, span:
418421
16 => format!("{max:#x}"),
419422
_ => format!("{max}"),
420423
};
421-
dcx.emit_err(IntLiteralTooLarge { span, limit });
424+
dcx.emit_err(IntLiteralTooLarge { span, limit })
422425
}
423426
}
424427
}

0 commit comments

Comments
 (0)