diff options
| author | Jieyou Xu <jieyouxu@outlook.com> | 2025-08-15 22:41:43 +0800 |
|---|---|---|
| committer | Jieyou Xu <jieyouxu@outlook.com> | 2025-08-18 21:57:10 +0800 |
| commit | eb3441b25a7acbce8cec0571b3f8ec87cca8c349 (patch) | |
| tree | 4b41d32e44ff480c2332d466c8b6d588e4dce67e | |
| parent | d7f7443a9540761c7cc2540c4b7b07b31b6120e5 (diff) | |
| download | rust-eb3441b25a7acbce8cec0571b3f8ec87cca8c349.tar.gz rust-eb3441b25a7acbce8cec0571b3f8ec87cca8c349.zip | |
Add test coverage for proc-macro invalid tup index suffixes
3 files changed, 98 insertions, 0 deletions
diff --git a/tests/ui/parser/auxiliary/tuple-index-suffix-proc-macro-aux.rs b/tests/ui/parser/auxiliary/tuple-index-suffix-proc-macro-aux.rs new file mode 100644 index 00000000000..fa40ed948a6 --- /dev/null +++ b/tests/ui/parser/auxiliary/tuple-index-suffix-proc-macro-aux.rs @@ -0,0 +1,33 @@ +#![feature(proc_macro_quote, proc_macro_span)] + +extern crate proc_macro; + +use proc_macro::{Ident, Literal, Span, TokenStream, TokenTree, quote}; + +#[proc_macro] +pub fn bad_tup_indexing(input: TokenStream) -> TokenStream { + let tt = input.into_iter().next().unwrap(); + let TokenTree::Literal(indexing_expr) = tt else { + unreachable!(); + }; + quote! { (42,).$indexing_expr } +} + +// Expects {IDENT, COMMA, LITERAL} +#[proc_macro] +pub fn bad_tup_struct_indexing(input: TokenStream) -> TokenStream { + let mut input = input.into_iter(); + + let id_tt = input.next().unwrap(); + let _comma = input.next().unwrap(); + let tt = input.next().unwrap(); + + let TokenTree::Ident(ident) = id_tt else { + unreachable!("id"); + }; + let TokenTree::Literal(indexing_expr) = tt else { + unreachable!("lit"); + }; + + quote! { $ident.$indexing_expr } +} diff --git a/tests/ui/parser/tuple-index-suffix-proc-macro.rs b/tests/ui/parser/tuple-index-suffix-proc-macro.rs new file mode 100644 index 00000000000..feca6f9cdfb --- /dev/null +++ b/tests/ui/parser/tuple-index-suffix-proc-macro.rs @@ -0,0 +1,31 @@ +//! See #59418. +//! +//! Like `tuple-index-suffix.rs`, but exercises the proc-macro interaction. + +//@ proc-macro: tuple-index-suffix-proc-macro-aux.rs + +extern crate tuple_index_suffix_proc_macro_aux; +use tuple_index_suffix_proc_macro_aux as aux; + +fn main() { + struct TupStruct(i32); + let tup_struct = TupStruct(42); + + // #60186 carve outs `{i,u}{32,usize}` as non-lint pseudo-FCW warnings. + + aux::bad_tup_indexing!(0usize); + //~^ WARN suffixes on a tuple index are invalid + aux::bad_tup_struct_indexing!(tup_struct, 0isize); + //~^ WARN suffixes on a tuple index are invalid + + // Not part of the #60186 carve outs. + + aux::bad_tup_indexing!(0u8); + //~^ ERROR suffixes on a tuple index are invalid + aux::bad_tup_struct_indexing!(tup_struct, 0u64); + //~^ ERROR suffixes on a tuple index are invalid + + // NOTE: didn't bother with trying to figure out how to generate `struct P { 0u32: u32 }` using + // *only* `proc_macro` without help with `syn`/`quote`, looks like you can't with just + // `proc_macro::quote`? +} diff --git a/tests/ui/parser/tuple-index-suffix-proc-macro.stderr b/tests/ui/parser/tuple-index-suffix-proc-macro.stderr new file mode 100644 index 00000000000..c8bc3a4576b --- /dev/null +++ b/tests/ui/parser/tuple-index-suffix-proc-macro.stderr @@ -0,0 +1,34 @@ +warning: suffixes on a tuple index are invalid + --> $DIR/tuple-index-suffix-proc-macro.rs:16:28 + | +LL | aux::bad_tup_indexing!(0usize); + | ^^^^^^ invalid suffix `usize` + | + = help: `usize` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases + = 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 + = help: see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information + +warning: suffixes on a tuple index are invalid + --> $DIR/tuple-index-suffix-proc-macro.rs:18:47 + | +LL | aux::bad_tup_struct_indexing!(tup_struct, 0isize); + | ^^^^^^ invalid suffix `isize` + | + = help: `isize` is *temporarily* accepted on tuple index fields as it was incorrectly accepted on stable for a few releases + = 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 + = help: see issue #60210 <https://github.com/rust-lang/rust/issues/60210> for more information + +error: suffixes on a tuple index are invalid + --> $DIR/tuple-index-suffix-proc-macro.rs:23:28 + | +LL | aux::bad_tup_indexing!(0u8); + | ^^^ invalid suffix `u8` + +error: suffixes on a tuple index are invalid + --> $DIR/tuple-index-suffix-proc-macro.rs:25:47 + | +LL | aux::bad_tup_struct_indexing!(tup_struct, 0u64); + | ^^^^ invalid suffix `u64` + +error: aborting due to 2 previous errors; 2 warnings emitted + |
