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

@ -215,6 +215,15 @@ parser_octal_float_literal_not_supported = octal float literal is not supported
parser_binary_float_literal_not_supported = binary float literal is not supported parser_binary_float_literal_not_supported = binary float literal is not supported
parser_not_supported = not supported parser_not_supported = not supported
parser_invalid_literal_suffix = suffixes on {$kind} literals are invalid
.label = invalid suffix `{$suffix}`
parser_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are invalid
.label = invalid suffix `{$suffix}`
.tuple_exception_line_1 = `{$suffix}` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
.tuple_exception_line_2 = 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
.tuple_exception_line_3 = see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information
parser_non_string_abi_literal = non-string ABI literal parser_non_string_abi_literal = non-string ABI literal
.suggestion = specify the ABI with a string literal .suggestion = specify the ABI with a string literal

View file

@ -24,7 +24,7 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
use rustc_session::errors::ExprParenthesesNeeded; use rustc_session::errors::ExprParenthesesNeeded;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use rustc_span::symbol::{kw, sym, Ident}; 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::ops::{Deref, DerefMut};
use std::mem::take; use std::mem::take;
@ -975,6 +975,30 @@ pub(crate) struct BinaryFloatLiteralNotSupported {
pub span: Span, 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)] #[derive(Diagnostic)]
#[diag(parser::non_string_abi_literal)] #[diag(parser::non_string_abi_literal)]
pub(crate) struct NonStringAbiLiteral { pub(crate) struct NonStringAbiLiteral {

View file

@ -6,13 +6,13 @@ use super::diagnostics::{
FloatLiteralRequiresIntegerPart, FoundExprWouldBeStmt, IfExpressionMissingCondition, FloatLiteralRequiresIntegerPart, FoundExprWouldBeStmt, IfExpressionMissingCondition,
IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment, IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment,
InvalidComparisonOperator, InvalidComparisonOperatorSub, InvalidInterpolatedExpression, InvalidComparisonOperator, InvalidComparisonOperatorSub, InvalidInterpolatedExpression,
InvalidLogicalOperator, InvalidLogicalOperatorSub, LabeledLoopInBreak, LeftArrowOperator, InvalidLiteralSuffix, InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator,
LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, MalformedLoopLabel, InvalidLogicalOperatorSub, LabeledLoopInBreak, LeftArrowOperator, LifetimeInBorrowExpression,
MatchArmBodyWithoutBraces, MissingInInForLoop, MissingInInForLoopSub, MacroInvocationWithQualifiedPath, MalformedLoopLabel, MatchArmBodyWithoutBraces,
MissingSemicolonBeforeArray, NoFieldsForFnCall, NotAsNegationOperator, MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray, NoFieldsForFnCall,
NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields, NotAsNegationOperator, NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse,
RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric, SnapshotParser, ParenthesesWithStructFields, RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric,
StructLiteralNotAllowedHere, TildeAsUnaryOperator, UnexpectedTokenAfterLabel, SnapshotParser, StructLiteralNotAllowedHere, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
UnexpectedTokenAfterLabelSugg, UnexpectedTokenAfterLabelSugg,
}; };
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED}; use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
@ -1158,7 +1158,9 @@ impl<'a> Parser<'a> {
} }
let span = self.prev_token.span; let span = self.prev_token.span;
let field = ExprKind::Field(base, Ident::new(field, 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) 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. // by lexer, so here we don't report it the second time.
LitError::LexerError => {} LitError::LexerError => {}
LitError::InvalidSuffix => { LitError::InvalidSuffix => {
self.expect_no_suffix( if let Some(suffix) = suffix {
span, self.sess.emit_err(InvalidLiteralSuffix {
&format!("{} {} literal", kind.article(), kind.descr()), span,
suffix, kind: format!("{}", kind.descr()),
); suffix,
});
}
} }
LitError::InvalidIntSuffix => { LitError::InvalidIntSuffix => {
let suf = suffix.expect("suffix error with no suffix"); 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>) { pub(super) fn expect_no_tuple_index_suffix(&self, span: Span, suffix: Symbol) {
if let Some(suf) = suffix { if [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suffix) {
let mut err = if kind == "a tuple index" // #59553: warn instead of reject out of hand to allow the fix to percolate
&& [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suf) // through the ecosystem when people fix their macros
{ self.sess.emit_warning(InvalidLiteralSuffixOnTupleIndex {
// #59553: warn instead of reject out of hand to allow the fix to percolate span,
// through the ecosystem when people fix their macros suffix,
let mut err = self exception: Some(()),
.sess });
.span_diagnostic } else {
.struct_span_warn(sp, &format!("suffixes on {kind} are invalid")); self.sess.emit_err(InvalidLiteralSuffixOnTupleIndex { span, suffix, exception: None });
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();
} }
} }

View file

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

View file

@ -14,7 +14,7 @@ mod nonexistent_env {
mod erroneous_literal { mod erroneous_literal {
include!(concat!("NON_EXISTENT"suffix, "/data.rs")); include!(concat!("NON_EXISTENT"suffix, "/data.rs"));
//~^ ERROR suffixes on a string literal are invalid //~^ ERROR suffixes on string literals are invalid
} }
fn main() {} fn main() {}

View file

@ -6,7 +6,7 @@ LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
| |
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info) = note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
error: suffixes on a string literal are invalid error: suffixes on string literals are invalid
--> $DIR/issue-55897.rs:16:22 --> $DIR/issue-55897.rs:16:22
| |
LL | include!(concat!("NON_EXISTENT"suffix, "/data.rs")); LL | include!(concat!("NON_EXISTENT"suffix, "/data.rs"));

View file

@ -1,18 +1,18 @@
extern extern
"C"suffix //~ ERROR suffixes on a string literal are invalid "C"suffix //~ ERROR suffixes on string literals are invalid
fn foo() {} fn foo() {}
extern extern
"C"suffix //~ ERROR suffixes on a string literal are invalid "C"suffix //~ ERROR suffixes on string literals are invalid
{} {}
fn main() { fn main() {
""suffix; //~ ERROR suffixes on a string literal are invalid ""suffix; //~ ERROR suffixes on string literals are invalid
b""suffix; //~ ERROR suffixes on a byte string literal are invalid b""suffix; //~ ERROR suffixes on byte string literals are invalid
r#""#suffix; //~ ERROR suffixes on a string literal are invalid r#""#suffix; //~ ERROR suffixes on string literals are invalid
br#""#suffix; //~ ERROR suffixes on a byte string literal are invalid br#""#suffix; //~ ERROR suffixes on byte string literals are invalid
'a'suffix; //~ ERROR suffixes on a char literal are invalid 'a'suffix; //~ ERROR suffixes on char literals are invalid
b'a'suffix; //~ ERROR suffixes on a byte literal are invalid b'a'suffix; //~ ERROR suffixes on byte literals are invalid
1234u1024; //~ ERROR invalid width `1024` for integer literal 1234u1024; //~ ERROR invalid width `1024` for integer literal
1234i1024; //~ ERROR invalid width `1024` for integer literal 1234i1024; //~ ERROR invalid width `1024` for integer literal

View file

@ -1,46 +1,46 @@
error: suffixes on a string literal are invalid error: suffixes on string literals are invalid
--> $DIR/bad-lit-suffixes.rs:2:5 --> $DIR/bad-lit-suffixes.rs:2:5
| |
LL | "C"suffix LL | "C"suffix
| ^^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a string literal are invalid error: suffixes on string literals are invalid
--> $DIR/bad-lit-suffixes.rs:6:5 --> $DIR/bad-lit-suffixes.rs:6:5
| |
LL | "C"suffix LL | "C"suffix
| ^^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a string literal are invalid error: suffixes on string literals are invalid
--> $DIR/bad-lit-suffixes.rs:10:5 --> $DIR/bad-lit-suffixes.rs:10:5
| |
LL | ""suffix; LL | ""suffix;
| ^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^ invalid suffix `suffix`
error: suffixes on a byte string literal are invalid error: suffixes on byte string literals are invalid
--> $DIR/bad-lit-suffixes.rs:11:5 --> $DIR/bad-lit-suffixes.rs:11:5
| |
LL | b""suffix; LL | b""suffix;
| ^^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a string literal are invalid error: suffixes on string literals are invalid
--> $DIR/bad-lit-suffixes.rs:12:5 --> $DIR/bad-lit-suffixes.rs:12:5
| |
LL | r#""#suffix; LL | r#""#suffix;
| ^^^^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a byte string literal are invalid error: suffixes on byte string literals are invalid
--> $DIR/bad-lit-suffixes.rs:13:5 --> $DIR/bad-lit-suffixes.rs:13:5
| |
LL | br#""#suffix; LL | br#""#suffix;
| ^^^^^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a char literal are invalid error: suffixes on char literals are invalid
--> $DIR/bad-lit-suffixes.rs:14:5 --> $DIR/bad-lit-suffixes.rs:14:5
| |
LL | 'a'suffix; LL | 'a'suffix;
| ^^^^^^^^^ invalid suffix `suffix` | ^^^^^^^^^ invalid suffix `suffix`
error: suffixes on a byte literal are invalid error: suffixes on byte literals are invalid
--> $DIR/bad-lit-suffixes.rs:15:5 --> $DIR/bad-lit-suffixes.rs:15:5
| |
LL | b'a'suffix; LL | b'a'suffix;