Skip to content

Commit ddd9993

Browse files
committed
Turn invalid index suffixes into hard errors
1 parent eb3441b commit ddd9993

File tree

9 files changed

+92
-151
lines changed

9 files changed

+92
-151
lines changed

compiler/rustc_parse/messages.ftl

Lines changed: 0 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -473,9 +473,6 @@ parse_invalid_label =
473473
474474
parse_invalid_literal_suffix_on_tuple_index = suffixes on a tuple index are invalid
475475
.label = invalid suffix `{$suffix}`
476-
.tuple_exception_line_1 = `{$suffix}` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
477-
.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
478-
.tuple_exception_line_3 = see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information
479476
480477
parse_invalid_logical_operator = `{$incorrect}` is not a logical operator
481478
.note = unlike in e.g., Python and PHP, `&&` and `||` are used for logical operators

compiler/rustc_parse/src/errors.rs

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1016,10 +1016,6 @@ pub(crate) struct InvalidLiteralSuffixOnTupleIndex {
10161016
#[label]
10171017
pub span: Span,
10181018
pub suffix: Symbol,
1019-
#[help(parse_tuple_exception_line_1)]
1020-
#[help(parse_tuple_exception_line_2)]
1021-
#[help(parse_tuple_exception_line_3)]
1022-
pub exception: bool,
10231019
}
10241020

10251021
#[derive(Diagnostic)]

compiler/rustc_parse/src/parser/expr.rs

Lines changed: 6 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1163,7 +1163,10 @@ impl<'a> Parser<'a> {
11631163
suffix,
11641164
}) => {
11651165
if let Some(suffix) = suffix {
1166-
self.expect_no_tuple_index_suffix(current.span, suffix);
1166+
self.dcx().emit_err(errors::InvalidLiteralSuffixOnTupleIndex {
1167+
span: current.span,
1168+
suffix,
1169+
});
11671170
}
11681171
match self.break_up_float(symbol, current.span) {
11691172
// 1e2
@@ -1239,7 +1242,8 @@ impl<'a> Parser<'a> {
12391242
suffix: Option<Symbol>,
12401243
) -> Box<Expr> {
12411244
if let Some(suffix) = suffix {
1242-
self.expect_no_tuple_index_suffix(ident_span, suffix);
1245+
self.dcx()
1246+
.emit_err(errors::InvalidLiteralSuffixOnTupleIndex { span: ident_span, suffix });
12431247
}
12441248
self.mk_expr(lo.to(ident_span), ExprKind::Field(base, Ident::new(field, ident_span)))
12451249
}
@@ -2225,24 +2229,6 @@ impl<'a> Parser<'a> {
22252229
})
22262230
}
22272231

2228-
pub(super) fn expect_no_tuple_index_suffix(&self, span: Span, suffix: Symbol) {
2229-
if [sym::i32, sym::u32, sym::isize, sym::usize].contains(&suffix) {
2230-
// #59553: warn instead of reject out of hand to allow the fix to percolate
2231-
// through the ecosystem when people fix their macros
2232-
self.dcx().emit_warn(errors::InvalidLiteralSuffixOnTupleIndex {
2233-
span,
2234-
suffix,
2235-
exception: true,
2236-
});
2237-
} else {
2238-
self.dcx().emit_err(errors::InvalidLiteralSuffixOnTupleIndex {
2239-
span,
2240-
suffix,
2241-
exception: false,
2242-
});
2243-
}
2244-
}
2245-
22462232
/// Matches `'-' lit | lit` (cf. `ast_validation::AstValidator::check_expr_within_pat`).
22472233
/// Keep this in sync with `Token::can_begin_literal_maybe_minus`.
22482234
pub fn parse_literal_maybe_minus(&mut self) -> PResult<'a, Box<Expr>> {

compiler/rustc_parse/src/parser/mod.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1333,7 +1333,10 @@ impl<'a> Parser<'a> {
13331333
if let token::Literal(token::Lit { kind: token::Integer, symbol, suffix }) = self.token.kind
13341334
{
13351335
if let Some(suffix) = suffix {
1336-
self.expect_no_tuple_index_suffix(self.token.span, suffix);
1336+
self.dcx().emit_err(errors::InvalidLiteralSuffixOnTupleIndex {
1337+
span: self.token.span,
1338+
suffix,
1339+
});
13371340
}
13381341
self.bump();
13391342
Ok(Ident::new(symbol, self.prev_token.span))

tests/ui/parser/auxiliary/tuple-index-suffix-proc-macro-aux.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,14 +18,14 @@ pub fn bad_tup_indexing(input: TokenStream) -> TokenStream {
1818
pub fn bad_tup_struct_indexing(input: TokenStream) -> TokenStream {
1919
let mut input = input.into_iter();
2020

21-
let id_tt = input.next().unwrap();
21+
let ident = input.next().unwrap();
2222
let _comma = input.next().unwrap();
23-
let tt = input.next().unwrap();
23+
let lit = input.next().unwrap();
2424

25-
let TokenTree::Ident(ident) = id_tt else {
25+
let TokenTree::Ident(ident) = ident else {
2626
unreachable!("id");
2727
};
28-
let TokenTree::Literal(indexing_expr) = tt else {
28+
let TokenTree::Literal(indexing_expr) = lit else {
2929
unreachable!("lit");
3030
};
3131

tests/ui/parser/tuple-index-suffix-proc-macro.rs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,12 +11,13 @@ fn main() {
1111
struct TupStruct(i32);
1212
let tup_struct = TupStruct(42);
1313

14-
// #60186 carve outs `{i,u}{32,usize}` as non-lint pseudo-FCW warnings.
14+
// Previously, #60186 had carve outs for `{i,u}{32,usize}` as non-lint pseudo-FCW warnings. Now,
15+
// they all hard error.
1516

1617
aux::bad_tup_indexing!(0usize);
17-
//~^ WARN suffixes on a tuple index are invalid
18+
//~^ ERROR suffixes on a tuple index are invalid
1819
aux::bad_tup_struct_indexing!(tup_struct, 0isize);
19-
//~^ WARN suffixes on a tuple index are invalid
20+
//~^ ERROR suffixes on a tuple index are invalid
2021

2122
// Not part of the #60186 carve outs.
2223

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,26 @@
1-
warning: suffixes on a tuple index are invalid
2-
--> $DIR/tuple-index-suffix-proc-macro.rs:16:28
1+
error: suffixes on a tuple index are invalid
2+
--> $DIR/tuple-index-suffix-proc-macro.rs:17:28
33
|
44
LL | aux::bad_tup_indexing!(0usize);
55
| ^^^^^^ invalid suffix `usize`
6-
|
7-
= help: `usize` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
8-
= 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
9-
= help: see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information
106

11-
warning: suffixes on a tuple index are invalid
12-
--> $DIR/tuple-index-suffix-proc-macro.rs:18:47
7+
error: suffixes on a tuple index are invalid
8+
--> $DIR/tuple-index-suffix-proc-macro.rs:19:47
139
|
1410
LL | aux::bad_tup_struct_indexing!(tup_struct, 0isize);
1511
| ^^^^^^ invalid suffix `isize`
16-
|
17-
= help: `isize` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases
18-
= 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
19-
= help: see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information
2012

2113
error: suffixes on a tuple index are invalid
22-
--> $DIR/tuple-index-suffix-proc-macro.rs:23:28
14+
--> $DIR/tuple-index-suffix-proc-macro.rs:24:28
2315
|
2416
LL | aux::bad_tup_indexing!(0u8);
2517
| ^^^ invalid suffix `u8`
2618

2719
error: suffixes on a tuple index are invalid
28-
--> $DIR/tuple-index-suffix-proc-macro.rs:25:47
20+
--> $DIR/tuple-index-suffix-proc-macro.rs:26:47
2921
|
3022
LL | aux::bad_tup_struct_indexing!(tup_struct, 0u64);
3123
| ^^^^ invalid suffix `u64`
3224

33-
error: aborting due to 2 previous errors; 2 warnings emitted
25+
error: aborting due to 4 previous errors
3426

tests/ui/parser/tuple-index-suffix.rs

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,37 +1,39 @@
1-
//! See #60210.
1+
//! Regression test for both the original regression in #59418 where invalid suffixes in indexing
2+
//! positions were accidentally accepted, and also for the removal of the temporary carve out that
3+
//! mitigated ecosystem impact following trying to reject #59418 (this was implemented as a FCW
4+
//! tracked in #60210).
25
//!
36
//! Check that we hard error on invalid suffixes in tuple indexing subexpressions and struct numeral
4-
//! field names, modulo carve-outs for `{i,u}{32,usize}` at warning level to mitigate ecosystem
5-
//! impact.
7+
//! field names.
68
79
struct X(i32,i32,i32);
810

911
fn main() {
1012
let tup_struct = X(1, 2, 3);
1113
let invalid_tup_struct_suffix = tup_struct.0suffix;
1214
//~^ ERROR suffixes on a tuple index are invalid
13-
let carve_out_tup_struct_suffix = tup_struct.0i32;
14-
//~^ WARN suffixes on a tuple index are invalid
15+
let previous_carve_out_tup_struct_suffix = tup_struct.0i32;
16+
//~^ ERROR suffixes on a tuple index are invalid
1517

1618
let tup = (1, 2, 3);
1719
let invalid_tup_suffix = tup.0suffix;
1820
//~^ ERROR suffixes on a tuple index are invalid
19-
let carve_out_tup_suffix = tup.0u32;
20-
//~^ WARN suffixes on a tuple index are invalid
21+
let previous_carve_out_tup_suffix = tup.0u32;
22+
//~^ ERROR suffixes on a tuple index are invalid
2123

2224
numeral_struct_field_name_suffix_invalid();
23-
numeral_struct_field_name_suffix_carve_out();
25+
numeral_struct_field_name_suffix_previous_carve_out();
2426
}
2527

26-
// Very limited carve outs as a ecosystem impact mitigation implemented in #60186. *Only*
27-
// `{i,u}{32,usize}` suffixes are temporarily accepted.
28-
fn carve_outs() {
29-
// Ok, only pseudo-FCW warnings.
28+
// Previously, there were very limited carve outs as a ecosystem impact mitigation implemented in
29+
// #60186. *Only* `{i,u}{32,usize}` suffixes were temporarily accepted. Now, they all hard error.
30+
fn previous_carve_outs() {
31+
// Previously temporarily accepted by a pseudo-FCW (#60210), now hard error.
3032

31-
let carve_out_i32 = (42,).0i32; //~ WARN suffixes on a tuple index are invalid
32-
let carve_out_i32 = (42,).0u32; //~ WARN suffixes on a tuple index are invalid
33-
let carve_out_isize = (42,).0isize; //~ WARN suffixes on a tuple index are invalid
34-
let carve_out_usize = (42,).0usize; //~ WARN suffixes on a tuple index are invalid
33+
let previous_carve_out_i32 = (42,).0i32; //~ ERROR suffixes on a tuple index are invalid
34+
let previous_carve_out_i32 = (42,).0u32; //~ ERROR suffixes on a tuple index are invalid
35+
let previous_carve_out_isize = (42,).0isize; //~ ERROR suffixes on a tuple index are invalid
36+
let previous_carve_out_usize = (42,).0usize; //~ ERROR suffixes on a tuple index are invalid
3537

3638
// Not part of the carve outs!
3739
let error_i8 = (42,).0i8; //~ ERROR suffixes on a tuple index are invalid
@@ -53,12 +55,12 @@ fn numeral_struct_field_name_suffix_invalid() {
5355
}
5456
}
5557

56-
fn numeral_struct_field_name_suffix_carve_out() {
58+
fn numeral_struct_field_name_suffix_previous_carve_out() {
5759
let carve_out_struct_name = X { 0u32: 0, 1: 1, 2: 2 };
58-
//~^ WARN suffixes on a tuple index are invalid
60+
//~^ ERROR suffixes on a tuple index are invalid
5961
match carve_out_struct_name {
6062
X { 0u32: _, .. } => {}
61-
//~^ WARN suffixes on a tuple index are invalid
63+
//~^ ERROR suffixes on a tuple index are invalid
6264
}
6365
}
6466

@@ -67,9 +69,9 @@ fn offset_of_suffix() {
6769
#[repr(C)]
6870
pub struct Struct<T>(u8, T);
6971

70-
// Carve outs
72+
// Previous pseudo-FCW carve outs
7173
assert_eq!(std::mem::offset_of!(Struct<u32>, 0usize), 0);
72-
//~^ WARN suffixes on a tuple index are invalid
74+
//~^ ERROR suffixes on a tuple index are invalid
7375

7476
// Not part of carve outs
7577
assert_eq!(std::mem::offset_of!(Struct<u32>, 0u8), 0);

0 commit comments

Comments
 (0)