Skip to content

Commit eb3441b

Browse files
committed
Add test coverage for proc-macro invalid tup index suffixes
1 parent d7f7443 commit eb3441b

File tree

3 files changed

+98
-0
lines changed

3 files changed

+98
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#![feature(proc_macro_quote, proc_macro_span)]
2+
3+
extern crate proc_macro;
4+
5+
use proc_macro::{Ident, Literal, Span, TokenStream, TokenTree, quote};
6+
7+
#[proc_macro]
8+
pub fn bad_tup_indexing(input: TokenStream) -> TokenStream {
9+
let tt = input.into_iter().next().unwrap();
10+
let TokenTree::Literal(indexing_expr) = tt else {
11+
unreachable!();
12+
};
13+
quote! { (42,).$indexing_expr }
14+
}
15+
16+
// Expects {IDENT, COMMA, LITERAL}
17+
#[proc_macro]
18+
pub fn bad_tup_struct_indexing(input: TokenStream) -> TokenStream {
19+
let mut input = input.into_iter();
20+
21+
let id_tt = input.next().unwrap();
22+
let _comma = input.next().unwrap();
23+
let tt = input.next().unwrap();
24+
25+
let TokenTree::Ident(ident) = id_tt else {
26+
unreachable!("id");
27+
};
28+
let TokenTree::Literal(indexing_expr) = tt else {
29+
unreachable!("lit");
30+
};
31+
32+
quote! { $ident.$indexing_expr }
33+
}
Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//! See #59418.
2+
//!
3+
//! Like `tuple-index-suffix.rs`, but exercises the proc-macro interaction.
4+
5+
//@ proc-macro: tuple-index-suffix-proc-macro-aux.rs
6+
7+
extern crate tuple_index_suffix_proc_macro_aux;
8+
use tuple_index_suffix_proc_macro_aux as aux;
9+
10+
fn main() {
11+
struct TupStruct(i32);
12+
let tup_struct = TupStruct(42);
13+
14+
// #60186 carve outs `{i,u}{32,usize}` as non-lint pseudo-FCW warnings.
15+
16+
aux::bad_tup_indexing!(0usize);
17+
//~^ WARN suffixes on a tuple index are invalid
18+
aux::bad_tup_struct_indexing!(tup_struct, 0isize);
19+
//~^ WARN suffixes on a tuple index are invalid
20+
21+
// Not part of the #60186 carve outs.
22+
23+
aux::bad_tup_indexing!(0u8);
24+
//~^ ERROR suffixes on a tuple index are invalid
25+
aux::bad_tup_struct_indexing!(tup_struct, 0u64);
26+
//~^ ERROR suffixes on a tuple index are invalid
27+
28+
// NOTE: didn't bother with trying to figure out how to generate `struct P { 0u32: u32 }` using
29+
// *only* `proc_macro` without help with `syn`/`quote`, looks like you can't with just
30+
// `proc_macro::quote`?
31+
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
warning: suffixes on a tuple index are invalid
2+
--> $DIR/tuple-index-suffix-proc-macro.rs:16:28
3+
|
4+
LL | aux::bad_tup_indexing!(0usize);
5+
| ^^^^^^ 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
10+
11+
warning: suffixes on a tuple index are invalid
12+
--> $DIR/tuple-index-suffix-proc-macro.rs:18:47
13+
|
14+
LL | aux::bad_tup_struct_indexing!(tup_struct, 0isize);
15+
| ^^^^^^ 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
20+
21+
error: suffixes on a tuple index are invalid
22+
--> $DIR/tuple-index-suffix-proc-macro.rs:23:28
23+
|
24+
LL | aux::bad_tup_indexing!(0u8);
25+
| ^^^ invalid suffix `u8`
26+
27+
error: suffixes on a tuple index are invalid
28+
--> $DIR/tuple-index-suffix-proc-macro.rs:25:47
29+
|
30+
LL | aux::bad_tup_struct_indexing!(tup_struct, 0u64);
31+
| ^^^^ invalid suffix `u64`
32+
33+
error: aborting due to 2 previous errors; 2 warnings emitted
34+

0 commit comments

Comments
 (0)