Skip to content

Commit 17016af

Browse files
Improve error messages around invalid literals in attribute arguments
Signed-off-by: Jonathan Brouwer <[email protected]>
1 parent 6151eb6 commit 17016af

File tree

2 files changed

+35
-28
lines changed

2 files changed

+35
-28
lines changed

compiler/rustc_attr_parsing/src/parser.rs

Lines changed: 21 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use rustc_errors::PResult;
1414
use rustc_hir::{self as hir, AttrPath};
1515
use rustc_parse::exp;
1616
use rustc_parse::parser::{Parser, PathStyle, token_descr};
17-
use rustc_session::errors::report_lit_error;
17+
use rustc_session::errors::{create_lit_error, report_lit_error};
1818
use rustc_session::parse::ParseSess;
1919
use rustc_span::{ErrorGuaranteed, Ident, Span, Symbol, sym};
2020
use thin_vec::ThinVec;
@@ -379,22 +379,25 @@ struct MetaItemListParserContext<'a, 'sess> {
379379

380380
impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
381381
fn parse_unsuffixed_meta_item_lit(&mut self) -> PResult<'sess, MetaItemLit> {
382-
let uninterpolated_span = self.parser.token_uninterpolated_span();
383382
let Some(token_lit) = self.parser.eat_token_lit() else {
384383
return self.parser.handle_missing_lit(Parser::mk_meta_item_lit_char);
385384
};
385+
self.unsuffixed_meta_item_from_lit(token_lit)
386+
}
386387

388+
fn unsuffixed_meta_item_from_lit(
389+
&mut self,
390+
token_lit: token::Lit,
391+
) -> PResult<'sess, MetaItemLit> {
387392
let lit = match MetaItemLit::from_token_lit(token_lit, self.parser.prev_token.span) {
388393
Ok(lit) => lit,
389394
Err(err) => {
390-
let guar =
391-
report_lit_error(&self.parser.psess, err, token_lit, uninterpolated_span);
392-
// Pack possible quotes and prefixes from the original literal into
393-
// the error literal's symbol so they can be pretty-printed faithfully.
394-
let suffixless_lit = token::Lit::new(token_lit.kind, token_lit.symbol, None);
395-
let symbol = Symbol::intern(&suffixless_lit.to_string());
396-
let token_lit = token::Lit::new(token::Err(guar), symbol, token_lit.suffix);
397-
MetaItemLit::from_token_lit(token_lit, uninterpolated_span).unwrap()
395+
return Err(create_lit_error(
396+
&self.parser.psess,
397+
err,
398+
token_lit,
399+
self.parser.prev_token_uninterpolated_span(),
400+
));
398401
}
399402
};
400403

@@ -448,14 +451,14 @@ impl<'a, 'sess> MetaItemListParserContext<'a, 'sess> {
448451
}
449452

450453
fn parse_meta_item_inner(&mut self) -> PResult<'sess, MetaItemOrLitParser<'static>> {
451-
match self.parse_unsuffixed_meta_item_lit() {
452-
Ok(lit) => return Ok(MetaItemOrLitParser::Lit(lit)),
453-
Err(err) => err.cancel(), // we provide a better error below
454-
}
455-
456-
match self.parse_attr_item() {
457-
Ok(mi) => return Ok(MetaItemOrLitParser::MetaItemParser(mi)),
458-
Err(err) => err.cancel(), // we provide a better error below
454+
if let Some(token_lit) = self.parser.eat_token_lit() {
455+
// If a literal token is parsed, we commit to parsing a MetaItemLit for better errors
456+
return Ok(MetaItemOrLitParser::Lit(self.unsuffixed_meta_item_from_lit(token_lit)?));
457+
} else {
458+
match self.parse_attr_item() {
459+
Ok(item) => return Ok(MetaItemOrLitParser::MetaItemParser(item)),
460+
Err(err) => err.cancel(), // we provide a better error below
461+
}
459462
}
460463

461464
let mut err = InvalidMetaItem {

compiler/rustc_session/src/errors.rs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,10 @@ pub fn report_lit_error(
384384
lit: token::Lit,
385385
span: Span,
386386
) -> ErrorGuaranteed {
387+
create_lit_error(psess, err, lit, span).emit()
388+
}
389+
390+
pub fn create_lit_error(psess: &ParseSess, err: LitError, lit: token::Lit, span: Span) -> Diag<'_> {
387391
// Checks if `s` looks like i32 or u1234 etc.
388392
fn looks_like_width_suffix(first_chars: &[char], s: &str) -> bool {
389393
s.len() > 1 && s.starts_with(first_chars) && s[1..].chars().all(|c| c.is_ascii_digit())
@@ -414,32 +418,32 @@ pub fn report_lit_error(
414418
let dcx = psess.dcx();
415419
match err {
416420
LitError::InvalidSuffix(suffix) => {
417-
dcx.emit_err(InvalidLiteralSuffix { span, kind: lit.kind.descr(), suffix })
421+
dcx.create_err(InvalidLiteralSuffix { span, kind: lit.kind.descr(), suffix })
418422
}
419423
LitError::InvalidIntSuffix(suffix) => {
420424
let suf = suffix.as_str();
421425
if looks_like_width_suffix(&['i', 'u'], suf) {
422426
// If it looks like a width, try to be helpful.
423-
dcx.emit_err(InvalidIntLiteralWidth { span, width: suf[1..].into() })
427+
dcx.create_err(InvalidIntLiteralWidth { span, width: suf[1..].into() })
424428
} else if let Some(fixed) = fix_base_capitalisation(lit.symbol.as_str(), suf) {
425-
dcx.emit_err(InvalidNumLiteralBasePrefix { span, fixed })
429+
dcx.create_err(InvalidNumLiteralBasePrefix { span, fixed })
426430
} else {
427-
dcx.emit_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
431+
dcx.create_err(InvalidNumLiteralSuffix { span, suffix: suf.to_string() })
428432
}
429433
}
430434
LitError::InvalidFloatSuffix(suffix) => {
431435
let suf = suffix.as_str();
432436
if looks_like_width_suffix(&['f'], suf) {
433437
// If it looks like a width, try to be helpful.
434-
dcx.emit_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() })
438+
dcx.create_err(InvalidFloatLiteralWidth { span, width: suf[1..].to_string() })
435439
} else {
436-
dcx.emit_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() })
440+
dcx.create_err(InvalidFloatLiteralSuffix { span, suffix: suf.to_string() })
437441
}
438442
}
439443
LitError::NonDecimalFloat(base) => match base {
440-
16 => dcx.emit_err(HexadecimalFloatLiteralNotSupported { span }),
441-
8 => dcx.emit_err(OctalFloatLiteralNotSupported { span }),
442-
2 => dcx.emit_err(BinaryFloatLiteralNotSupported { span }),
444+
16 => dcx.create_err(HexadecimalFloatLiteralNotSupported { span }),
445+
8 => dcx.create_err(OctalFloatLiteralNotSupported { span }),
446+
2 => dcx.create_err(BinaryFloatLiteralNotSupported { span }),
443447
_ => unreachable!(),
444448
},
445449
LitError::IntTooLarge(base) => {
@@ -450,7 +454,7 @@ pub fn report_lit_error(
450454
16 => format!("{max:#x}"),
451455
_ => format!("{max}"),
452456
};
453-
dcx.emit_err(IntLiteralTooLarge { span, limit })
457+
dcx.create_err(IntLiteralTooLarge { span, limit })
454458
}
455459
}
456460
}

0 commit comments

Comments
 (0)