Skip to content

Commit 6ae7a30

Browse files
committed
Migrate "invalid literal suffix" diagnostic to diagnostic structs
1 parent ab7c7dc commit 6ae7a30

File tree

8 files changed

+83
-65
lines changed

8 files changed

+83
-65
lines changed

compiler/rustc_error_messages/locales/en-US/parser.ftl

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,15 @@ parser_octal_float_literal_not_supported = octal float literal is not supported
215215
parser_binary_float_literal_not_supported = binary float literal is not supported
216216
parser_not_supported = not supported
217217
218+
parser_invalid_literal_suffix = suffixes on {$kind} literals are invalid
219+
.label = invalid suffix `{$suffix}`
220+
221+
parser_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are invalid
222+
.label = invalid suffix `{$suffix}`
223+
.tuple_exception_line_1 = `{$suffix}` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
224+
.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
225+
.tuple_exception_line_3 = see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information
226+
218227
parser_non_string_abi_literal = non-string ABI literal
219228
.suggestion = specify the ABI with a string literal
220229

compiler/rustc_parse/src/parser/diagnostics.rs

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ use rustc_macros::{Diagnostic, Subdiagnostic};
2424
use rustc_session::errors::ExprParenthesesNeeded;
2525
use rustc_span::source_map::Spanned;
2626
use rustc_span::symbol::{kw, sym, Ident};
27-
use rustc_span::{Span, SpanSnippetError, DUMMY_SP};
27+
use rustc_span::{Span, SpanSnippetError, Symbol, DUMMY_SP};
2828
use std::ops::{Deref, DerefMut};
2929

3030
use std::mem::take;
@@ -975,6 +975,30 @@ pub(crate) struct BinaryFloatLiteralNotSupported {
975975
pub span: Span,
976976
}
977977

978+
#[derive(Diagnostic)]
979+
#[diag(parser::invalid_literal_suffix)]
980+
pub(crate) struct InvalidLiteralSuffix {
981+
#[primary_span]
982+
#[label]
983+
pub span: Span,
984+
// FIXME(#100717)
985+
pub kind: String,
986+
pub suffix: Symbol,
987+
}
988+
989+
#[derive(Diagnostic)]
990+
#[diag(parser::invalid_literal_suffix_on_tuple_index)]
991+
pub(crate) struct InvalidLiteralSuffixOnTupleIndex {
992+
#[primary_span]
993+
#[label]
994+
pub span: Span,
995+
pub suffix: Symbol,
996+
#[help(parser::tuple_exception_line_1)]
997+
#[help(parser::tuple_exception_line_2)]
998+
#[help(parser::tuple_exception_line_3)]
999+
pub exception: Option<()>,
1000+
}
1001+
9781002
#[derive(Diagnostic)]
9791003
#[diag(parser::non_string_abi_literal)]
9801004
pub(crate) struct NonStringAbiLiteral {

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 28 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,13 @@ use super::diagnostics::{
66
FloatLiteralRequiresIntegerPart, FoundExprWouldBeStmt, IfExpressionMissingCondition,
77
IfExpressionMissingThenBlock, IfExpressionMissingThenBlockSub, InvalidBlockMacroSegment,
88
InvalidComparisonOperator, InvalidComparisonOperatorSub, InvalidInterpolatedExpression,
9-
InvalidLogicalOperator, InvalidLogicalOperatorSub, LabeledLoopInBreak, LeftArrowOperator,
10-
LifetimeInBorrowExpression, MacroInvocationWithQualifiedPath, MalformedLoopLabel,
11-
MatchArmBodyWithoutBraces, MissingInInForLoop, MissingInInForLoopSub,
12-
MissingSemicolonBeforeArray, NoFieldsForFnCall, NotAsNegationOperator,
13-
NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse, ParenthesesWithStructFields,
14-
RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric, SnapshotParser,
15-
StructLiteralNotAllowedHere, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
9+
InvalidLiteralSuffix, InvalidLiteralSuffixOnTupleIndex, InvalidLogicalOperator,
10+
InvalidLogicalOperatorSub, LabeledLoopInBreak, LeftArrowOperator, LifetimeInBorrowExpression,
11+
MacroInvocationWithQualifiedPath, MalformedLoopLabel, MatchArmBodyWithoutBraces,
12+
MissingInInForLoop, MissingInInForLoopSub, MissingSemicolonBeforeArray, NoFieldsForFnCall,
13+
NotAsNegationOperator, NotAsNegationOperatorSub, OuterAttributeNotAllowedOnIfElse,
14+
ParenthesesWithStructFields, RequireColonAfterLabeledExpression, ShiftInterpretedAsGeneric,
15+
SnapshotParser, StructLiteralNotAllowedHere, TildeAsUnaryOperator, UnexpectedTokenAfterLabel,
1616
UnexpectedTokenAfterLabelSugg,
1717
};
1818
use super::pat::{CommaRecoveryMode, RecoverColon, RecoverComma, PARAM_EXPECTED};
@@ -1158,7 +1158,9 @@ impl<'a> Parser<'a> {
11581158
}
11591159
let span = self.prev_token.span;
11601160
let field = ExprKind::Field(base, Ident::new(field, span));
1161-
self.expect_no_suffix(span, "a tuple index", suffix);
1161+
if let Some(suffix) = suffix {
1162+
self.expect_no_tuple_index_suffix(span, suffix);
1163+
}
11621164
self.mk_expr(lo.to(span), field)
11631165
}
11641166

@@ -1829,11 +1831,13 @@ impl<'a> Parser<'a> {
18291831
// by lexer, so here we don't report it the second time.
18301832
LitError::LexerError => {}
18311833
LitError::InvalidSuffix => {
1832-
self.expect_no_suffix(
1833-
span,
1834-
&format!("{} {} literal", kind.article(), kind.descr()),
1835-
suffix,
1836-
);
1834+
if let Some(suffix) = suffix {
1835+
self.sess.emit_err(InvalidLiteralSuffix {
1836+
span,
1837+
kind: format!("{}", kind.descr()),
1838+
suffix,
1839+
});
1840+
}
18371841
}
18381842
LitError::InvalidIntSuffix => {
18391843
let suf = suffix.expect("suffix error with no suffix");
@@ -1872,38 +1876,17 @@ impl<'a> Parser<'a> {
18721876
}
18731877
}
18741878

1875-
pub(super) fn expect_no_suffix(&self, sp: Span, kind: &str, suffix: Option<Symbol>) {
1876-
if let Some(suf) = suffix {
1877-
let mut err = if kind == "a tuple index"
1878-
&& [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suf)
1879-
{
1880-
// #59553: warn instead of reject out of hand to allow the fix to percolate
1881-
// through the ecosystem when people fix their macros
1882-
let mut err = self
1883-
.sess
1884-
.span_diagnostic
1885-
.struct_span_warn(sp, &format!("suffixes on {kind} are invalid"));
1886-
err.note(&format!(
1887-
"`{}` is *temporarily* accepted on tuple index fields as it was \
1888-
incorrectly accepted on stable for a few releases",
1889-
suf,
1890-
));
1891-
err.help(
1892-
"on proc macros, you'll want to use `syn::Index::from` or \
1893-
`proc_macro::Literal::*_unsuffixed` for code that will desugar \
1894-
to tuple field access",
1895-
);
1896-
err.note(
1897-
"see issue #60210 <https://github.com/rust-lang/rust/issues/60210> \
1898-
for more information",
1899-
);
1900-
err
1901-
} else {
1902-
self.struct_span_err(sp, &format!("suffixes on {kind} are invalid"))
1903-
.forget_guarantee()
1904-
};
1905-
err.span_label(sp, format!("invalid suffix `{suf}`"));
1906-
err.emit();
1879+
pub(super) fn expect_no_tuple_index_suffix(&self, span: Span, suffix: Symbol) {
1880+
if [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suffix) {
1881+
// #59553: warn instead of reject out of hand to allow the fix to percolate
1882+
// through the ecosystem when people fix their macros
1883+
self.sess.emit_warning(InvalidLiteralSuffixOnTupleIndex {
1884+
span,
1885+
suffix,
1886+
exception: Some(()),
1887+
});
1888+
} else {
1889+
self.sess.emit_err(InvalidLiteralSuffixOnTupleIndex { span, suffix, exception: None });
19071890
}
19081891
}
19091892

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1147,7 +1147,9 @@ impl<'a> Parser<'a> {
11471147
fn parse_field_name(&mut self) -> PResult<'a, Ident> {
11481148
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
11491149
{
1150-
self.expect_no_suffix(self.token.span, "a tuple index", suffix);
1150+
if let Some(suffix) = suffix {
1151+
self.expect_no_tuple_index_suffix(self.token.span, suffix);
1152+
}
11511153
self.bump();
11521154
Ok(Ident::new(symbol, self.prev_token.span))
11531155
} else {

src/test/ui/extenv/issue-55897.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ mod nonexistent_env {
1414

1515
mod erroneous_literal {
1616
include!(concat!("NON_EXISTENT"suffix, "/data.rs"));
17-
//~^ ERROR suffixes on a string literal are invalid
17+
//~^ ERROR suffixes on string literals are invalid
1818
}
1919

2020
fn main() {}

src/test/ui/extenv/issue-55897.stderr

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ LL | include!(concat!(env!("NON_EXISTENT"), "/data.rs"));
66
|
77
= note: this error originates in the macro `env` (in Nightly builds, run with -Z macro-backtrace for more info)
88

9-
error: suffixes on a string literal are invalid
9+
error: suffixes on string literals are invalid
1010
--> $DIR/issue-55897.rs:16:22
1111
|
1212
LL | include!(concat!("NON_EXISTENT"suffix, "/data.rs"));

src/test/ui/parser/bad-lit-suffixes.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
extern
2-
"C"suffix //~ ERROR suffixes on a string literal are invalid
2+
"C"suffix //~ ERROR suffixes on string literals are invalid
33
fn foo() {}
44

55
extern
6-
"C"suffix //~ ERROR suffixes on a string literal are invalid
6+
"C"suffix //~ ERROR suffixes on string literals are invalid
77
{}
88

99
fn main() {
10-
""suffix; //~ ERROR suffixes on a string literal are invalid
11-
b""suffix; //~ ERROR suffixes on a byte string literal are invalid
12-
r#""#suffix; //~ ERROR suffixes on a string literal are invalid
13-
br#""#suffix; //~ ERROR suffixes on a byte string literal are invalid
14-
'a'suffix; //~ ERROR suffixes on a char literal are invalid
15-
b'a'suffix; //~ ERROR suffixes on a byte literal are invalid
10+
""suffix; //~ ERROR suffixes on string literals are invalid
11+
b""suffix; //~ ERROR suffixes on byte string literals are invalid
12+
r#""#suffix; //~ ERROR suffixes on string literals are invalid
13+
br#""#suffix; //~ ERROR suffixes on byte string literals are invalid
14+
'a'suffix; //~ ERROR suffixes on char literals are invalid
15+
b'a'suffix; //~ ERROR suffixes on byte literals are invalid
1616

1717
1234u1024; //~ ERROR invalid width `1024` for integer literal
1818
1234i1024; //~ ERROR invalid width `1024` for integer literal

src/test/ui/parser/bad-lit-suffixes.stderr

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,46 +1,46 @@
1-
error: suffixes on a string literal are invalid
1+
error: suffixes on string literals are invalid
22
--> $DIR/bad-lit-suffixes.rs:2:5
33
|
44
LL | "C"suffix
55
| ^^^^^^^^^ invalid suffix `suffix`
66

7-
error: suffixes on a string literal are invalid
7+
error: suffixes on string literals are invalid
88
--> $DIR/bad-lit-suffixes.rs:6:5
99
|
1010
LL | "C"suffix
1111
| ^^^^^^^^^ invalid suffix `suffix`
1212

13-
error: suffixes on a string literal are invalid
13+
error: suffixes on string literals are invalid
1414
--> $DIR/bad-lit-suffixes.rs:10:5
1515
|
1616
LL | ""suffix;
1717
| ^^^^^^^^ invalid suffix `suffix`
1818

19-
error: suffixes on a byte string literal are invalid
19+
error: suffixes on byte string literals are invalid
2020
--> $DIR/bad-lit-suffixes.rs:11:5
2121
|
2222
LL | b""suffix;
2323
| ^^^^^^^^^ invalid suffix `suffix`
2424

25-
error: suffixes on a string literal are invalid
25+
error: suffixes on string literals are invalid
2626
--> $DIR/bad-lit-suffixes.rs:12:5
2727
|
2828
LL | r#""#suffix;
2929
| ^^^^^^^^^^^ invalid suffix `suffix`
3030

31-
error: suffixes on a byte string literal are invalid
31+
error: suffixes on byte string literals are invalid
3232
--> $DIR/bad-lit-suffixes.rs:13:5
3333
|
3434
LL | br#""#suffix;
3535
| ^^^^^^^^^^^^ invalid suffix `suffix`
3636

37-
error: suffixes on a char literal are invalid
37+
error: suffixes on char literals are invalid
3838
--> $DIR/bad-lit-suffixes.rs:14:5
3939
|
4040
LL | 'a'suffix;
4141
| ^^^^^^^^^ invalid suffix `suffix`
4242

43-
error: suffixes on a byte literal are invalid
43+
error: suffixes on byte literals are invalid
4444
--> $DIR/bad-lit-suffixes.rs:15:5
4545
|
4646
LL | b'a'suffix;

0 commit comments

Comments
 (0)