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_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
.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_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(
if let Some(suffix) = suffix {
self.sess.emit_err(InvalidLiteralSuffix {
span,
&format!("{} {} literal", kind.article(), kind.descr()),
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)
{
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
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
self.sess.emit_warning(InvalidLiteralSuffixOnTupleIndex {
span,
suffix,
exception: Some(()),
});
} else {
self.struct_span_err(sp, &format!("suffixes on {kind} are invalid"))
.forget_guarantee()
};
err.span_label(sp, format!("invalid suffix `{suf}`"));
err.emit();
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 {

View file

@ -14,7 +14,7 @@ mod nonexistent_env {
mod erroneous_literal {
include!(concat!("NON_EXISTENT"suffix, "/data.rs"));
//~^ ERROR suffixes on a string literal are invalid
//~^ ERROR suffixes on string literals are invalid
}
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)
error: suffixes on a string literal are invalid
error: suffixes on string literals are invalid
--> $DIR/issue-55897.rs:16:22
|
LL | include!(concat!("NON_EXISTENT"suffix, "/data.rs"));

View file

@ -1,18 +1,18 @@
extern
"C"suffix //~ ERROR suffixes on a string literal are invalid
"C"suffix //~ ERROR suffixes on string literals are invalid
fn foo() {}
extern
"C"suffix //~ ERROR suffixes on a string literal are invalid
"C"suffix //~ ERROR suffixes on string literals are invalid
{}
fn main() {
""suffix; //~ ERROR suffixes on a string literal are invalid
b""suffix; //~ ERROR suffixes on a byte string literal are invalid
r#""#suffix; //~ ERROR suffixes on a string literal are invalid
br#""#suffix; //~ ERROR suffixes on a byte string literal are invalid
'a'suffix; //~ ERROR suffixes on a char literal are invalid
b'a'suffix; //~ ERROR suffixes on a byte literal are invalid
""suffix; //~ ERROR suffixes on string literals are invalid
b""suffix; //~ ERROR suffixes on byte string literals are invalid
r#""#suffix; //~ ERROR suffixes on string literals are invalid
br#""#suffix; //~ ERROR suffixes on byte string literals are invalid
'a'suffix; //~ ERROR suffixes on char literals are invalid
b'a'suffix; //~ ERROR suffixes on byte literals are invalid
1234u1024; //~ 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
|
LL | "C"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
|
LL | "C"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
|
LL | ""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
|
LL | b""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
|
LL | r#""#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
|
LL | br#""#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
|
LL | 'a'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
|
LL | b'a'suffix;