1
Fork 0

Migrate "invalid literal suffix" diagnostic to diagnostic structs

This commit is contained in:
Xiretza 2022-09-15 10:12:09 +02:00
parent ab7c7dc7ce
commit 6ae7a30927
8 changed files with 83 additions and 65 deletions

View file

@ -24,7 +24,7 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Ident};
use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
use rustc_span::{Span, SpanSnippetError, Symbol, DUMMY_SP};
use std::ops::{Deref, DerefMut};
use std::mem::take;
@ -975,6 +975,30 @@ pub(crate) struct BinaryFloatLiteralNotSupported {
pub span: Span,
}
#[derive(Diagnostic)]
#[diag(parser::invalid_literal_suffix)]
pub(crate) struct InvalidLiteralSuffix {
#[primary_span]
#[label]
pub span: Span,
// FIXME(#100717)
pub kind: String,
pub suffix: Symbol,
}
#[derive(Diagnostic)]
#[diag(parser::invalid_literal_suffix_on_tuple_index)]
pub(crate) struct InvalidLiteralSuffixOnTupleIndex {
#[primary_span]
#[label]
pub span: Span,
pub suffix: Symbol,
#[help(parser::tuple_exception_line_1)]
#[help(parser::tuple_exception_line_2)]
#[help(parser::tuple_exception_line_3)]
pub exception: Option<()>,
}
#[derive(Diagnostic)]
#[diag(parser::non_string_abi_literal)]
pub(crate) struct NonStringAbiLiteral {

View file

@ -6,13 +6,13 @@ use super::diagnostics::{
FloatLiteralRequiresIntegerPart, FoundExprWouldBeStmt, IfExpressionMissingCondition,
IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment,
InvalidComparisonOperator, InvalidComparisonOperatorSub, InvalidInterpolatedExpression,
InvalidLogicalOperator, InvalidLogicalOperatorSub, LabeledLoopInBreak, LeftArrowOperator,
LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, MalformedLoopLabel,
MatchArmBodyWithoutBraces, MissingInInForLoop, MissingInInForLoopSub,
MissingSemicolonBeforeArray, NoFieldsForFnCall, NotAsNegationOperator,
NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields,
RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric, SnapshotParser,
StructLiteralNotAllowedHere, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
InvalidLiteralSuffix, InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator,
InvalidLogicalOperatorSub, LabeledLoopInBreak, LeftArrowOperator, LifetimeInBorrowExpression,
MacroInvocationWithQualifiedPath, MalformedLoopLabel, MatchArmBodyWithoutBraces,
MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray, NoFieldsForFnCall,
NotAsNegationOperator, NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse,
ParenthesesWithStructFields, RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric,
SnapshotParser, StructLiteralNotAllowedHere, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
UnexpectedTokenAfterLabelSugg,
};
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
@ -1158,7 +1158,9 @@ impl<'a> Parser<'a> {
}
let span = self.prev_token.span;
let field = ExprKind::Field(base, Ident::new(field, span));
self.expect_no_suffix(span, "a tuple index", suffix);
if let Some(suffix) = suffix {
self.expect_no_tuple_index_suffix(span, suffix);
}
self.mk_expr(lo.to(span), field)
}
@ -1829,11 +1831,13 @@ impl<'a> Parser<'a> {
// by lexer, so here we don't report it the second time.
LitError::LexerError => {}
LitError::InvalidSuffix => {
self.expect_no_suffix(
span,
&format!("{} {} literal", kind.article(), kind.descr()),
suffix,
);
if let Some(suffix) = suffix {
self.sess.emit_err(InvalidLiteralSuffix {
span,
kind: format!("{}", kind.descr()),
suffix,
});
}
}
LitError::InvalidIntSuffix => {
let suf = suffix.expect("suffix error with no suffix");
@ -1872,38 +1876,17 @@ impl<'a> Parser<'a> {
}
}
pub(super) fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<Symbol>) {
if let Some(suf) = suffix {
let mut err = if kind == "a tuple index"
&& [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suf)
{
// #59553: warn instead of reject out of hand to allow the fix to percolate
// through the ecosystem when people fix their macros
let mut err = self
.sess
.span_diagnostic
.struct_span_warn(sp, &format!("suffixes on {kind} are invalid"));
err.note(&format!(
"`{}` is *temporarily* accepted on tuple index fields as it was \
incorrectly accepted on stable for a few releases",
suf,
));
err.help(
"on proc macros, you'll want to use `syn::Index::from` or \
`proc_macro::Literal::*_unsuffixed` for code that will desugar \
to tuple field access",
);
err.note(
"see issue #60210 <https://github.com/rust-lang/rust/issues/60210> \
for more information",
);
err
} else {
self.struct_span_err(sp, &format!("suffixes on {kind} are invalid"))
.forget_guarantee()
};
err.span_label(sp, format!("invalid suffix `{suf}`"));
err.emit();
pub(super) fn expect_no_tuple_index_suffix(&self, span: Span, suffix: Symbol) {
if [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suffix) {
// #59553: warn instead of reject out of hand to allow the fix to percolate
// through the ecosystem when people fix their macros
self.sess.emit_warning(InvalidLiteralSuffixOnTupleIndex {
span,
suffix,
exception: Some(()),
});
} else {
self.sess.emit_err(InvalidLiteralSuffixOnTupleIndex { span, suffix, exception: None });
}
}

View file

@ -1147,7 +1147,9 @@ impl<'a> Parser<'a> {
fn parse_field_name(&mut self) -> PResult<'a, Ident> {
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
{
self.expect_no_suffix(self.token.span, "a tuple index", suffix);
if let Some(suffix) = suffix {
self.expect_no_tuple_index_suffix(self.token.span, suffix);
}
self.bump();
Ok(Ident::new(symbol, self.prev_token.span))
} else {