Skip to content

Commit 6630ead

Browse files
committed
add proc_macro test
1 parent 228f432 commit 6630ead

File tree

3 files changed

+41
-12
lines changed

3 files changed

+41
-12
lines changed

tests/ui/auxiliary/proc_macros.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ use proc_macro::token_stream::IntoIter;
99
use proc_macro::Delimiter::{self, Brace, Parenthesis};
1010
use proc_macro::Spacing::{self, Alone, Joint};
1111
use proc_macro::{Group, Ident, Literal, Punct, Span, TokenStream, TokenTree as TT};
12+
use syn::spanned::Spanned;
1213

1314
type Result<T> = core::result::Result<T, TokenStream>;
1415

@@ -82,6 +83,21 @@ pub fn external(input: TokenStream) -> TokenStream {
8283
}
8384
}
8485

86+
/// Takes an array repeat expression (i.e. [0_u32; 100]), and double its allocation size.
87+
#[proc_macro]
88+
pub fn make_it_big(input: TokenStream) -> TokenStream {
89+
let mut expr_repeat = syn::parse_macro_input!(input as syn::ExprRepeat);
90+
let len_span = expr_repeat.len.span();
91+
if let syn::Expr::Lit(expr_lit) = &mut *expr_repeat.len {
92+
if let syn::Lit::Int(lit_int) = &expr_lit.lit {
93+
let orig_val = lit_int.base10_parse::<usize>().expect("not a valid length parameter");
94+
let new_val = orig_val.saturating_mul(10);
95+
expr_lit.lit = syn::parse_quote_spanned!( len_span => #new_val);
96+
}
97+
}
98+
quote::quote! {#expr_repeat}.into()
99+
}
100+
85101
/// Copies all the tokens, replacing all their spans with the given span. Tokens can be escaped
86102
/// either by `#ident` or `#(tokens)`.
87103
fn write_with_span(s: Span, mut input: IntoIter, out: &mut TokenStream) -> Result<()> {

tests/ui/large_stack_arrays.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
1+
//@aux-build:proc_macros.rs
12
#![warn(clippy::large_stack_arrays)]
23
#![allow(clippy::large_enum_variant)]
34

5+
extern crate proc_macros;
6+
47
#[derive(Clone, Copy)]
58
struct S {
69
pub data: [u64; 32],
@@ -88,4 +91,6 @@ fn issue_12586() {
8891
let y = dummy!(vec![dummy![x, x, x, x, x]]);
8992
let y = dummy![[x, x, x, x, x]];
9093
//~^ ERROR: allocating a local array larger than 512000 bytes
94+
let y = proc_macros::make_it_big!([x; 1]);
95+
//~^ ERROR: allocating a local array larger than 512000 bytes
9196
}

tests/ui/large_stack_arrays.stderr

Lines changed: 20 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
error: allocating a local array larger than 512000 bytes
2-
--> tests/ui/large_stack_arrays.rs:29:14
2+
--> tests/ui/large_stack_arrays.rs:32:14
33
|
44
LL | let _x = [build(); 3];
55
| ^^^^^^^^^^^^
@@ -9,71 +9,71 @@ LL | let _x = [build(); 3];
99
= help: to override `-D warnings` add `#[allow(clippy::large_stack_arrays)]`
1010

1111
error: allocating a local array larger than 512000 bytes
12-
--> tests/ui/large_stack_arrays.rs:32:14
12+
--> tests/ui/large_stack_arrays.rs:35:14
1313
|
1414
LL | let _y = [build(), build(), build()];
1515
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
1616
|
1717
= help: consider allocating on the heap with `vec![build(), build(), build()].into_boxed_slice()`
1818

1919
error: allocating a local array larger than 512000 bytes
20-
--> tests/ui/large_stack_arrays.rs:38:9
20+
--> tests/ui/large_stack_arrays.rs:41:9
2121
|
2222
LL | [0u32; 20_000_000],
2323
| ^^^^^^^^^^^^^^^^^^
2424
|
2525
= help: consider allocating on the heap with `vec![0u32; 20_000_000].into_boxed_slice()`
2626

2727
error: allocating a local array larger than 512000 bytes
28-
--> tests/ui/large_stack_arrays.rs:40:9
28+
--> tests/ui/large_stack_arrays.rs:43:9
2929
|
3030
LL | [S { data: [0; 32] }; 5000],
3131
| ^^^^^^^^^^^^^^^^^^^^^^^^^^^
3232
|
3333
= help: consider allocating on the heap with `vec![S { data: [0; 32] }; 5000].into_boxed_slice()`
3434

3535
error: allocating a local array larger than 512000 bytes
36-
--> tests/ui/large_stack_arrays.rs:42:9
36+
--> tests/ui/large_stack_arrays.rs:45:9
3737
|
3838
LL | [Some(""); 20_000_000],
3939
| ^^^^^^^^^^^^^^^^^^^^^^
4040
|
4141
= help: consider allocating on the heap with `vec![Some(""); 20_000_000].into_boxed_slice()`
4242

4343
error: allocating a local array larger than 512000 bytes
44-
--> tests/ui/large_stack_arrays.rs:44:9
44+
--> tests/ui/large_stack_arrays.rs:47:9
4545
|
4646
LL | [E::T(0); 5000],
4747
| ^^^^^^^^^^^^^^^
4848
|
4949
= help: consider allocating on the heap with `vec![E::T(0); 5000].into_boxed_slice()`
5050

5151
error: allocating a local array larger than 512000 bytes
52-
--> tests/ui/large_stack_arrays.rs:46:9
52+
--> tests/ui/large_stack_arrays.rs:49:9
5353
|
5454
LL | [0u8; usize::MAX],
5555
| ^^^^^^^^^^^^^^^^^
5656
|
5757
= help: consider allocating on the heap with `vec![0u8; usize::MAX].into_boxed_slice()`
5858

5959
error: allocating a local array larger than 512000 bytes
60-
--> tests/ui/large_stack_arrays.rs:81:25
60+
--> tests/ui/large_stack_arrays.rs:84:25
6161
|
6262
LL | let y = vec![dummy![[x, x, x, x, x]]];
6363
| ^^^^^^^^^^^^^^^
6464
|
6565
= help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()`
6666

6767
error: allocating a local array larger than 512000 bytes
68-
--> tests/ui/large_stack_arrays.rs:84:13
68+
--> tests/ui/large_stack_arrays.rs:87:13
6969
|
7070
LL | let y = [x, x, dummy!(x), x, x];
7171
| ^^^^^^^^^^^^^^^^^^^^^^^
7272
|
7373
= help: consider allocating on the heap with `vec![x, x, dummy!(x), x, x].into_boxed_slice()`
7474

7575
error: allocating a local array larger than 512000 bytes
76-
--> tests/ui/large_stack_arrays.rs:67:13
76+
--> tests/ui/large_stack_arrays.rs:70:13
7777
|
7878
LL | [$a, $b, $a, $b]
7979
| ^^^^^^^^^^^^^^^^
@@ -84,12 +84,20 @@ LL | let y = dummy![x => x];
8484
= note: this error originates in the macro `dummy` (in Nightly builds, run with -Z macro-backtrace for more info)
8585

8686
error: allocating a local array larger than 512000 bytes
87-
--> tests/ui/large_stack_arrays.rs:89:20
87+
--> tests/ui/large_stack_arrays.rs:92:20
8888
|
8989
LL | let y = dummy![[x, x, x, x, x]];
9090
| ^^^^^^^^^^^^^^^
9191
|
9292
= help: consider allocating on the heap with `vec![x, x, x, x, x].into_boxed_slice()`
9393

94-
error: aborting due to 11 previous errors
94+
error: allocating a local array larger than 512000 bytes
95+
--> tests/ui/large_stack_arrays.rs:94:39
96+
|
97+
LL | let y = proc_macros::make_it_big!([x; 1]);
98+
| ^^^^^^
99+
|
100+
= help: consider allocating on the heap with `vec![x; 1].into_boxed_slice()`
101+
102+
error: aborting due to 12 previous errors
95103

0 commit comments

Comments
 (0)