1
+ //! Test that the compiler can handle code bases with a high number of closures.
2
+ //! This is particularly important for the MinGW toolchain which has a limit of
3
+ //! 2^15 weak symbols per binary. This test creates 2^12 closures (256 functions
4
+ //! with 16 closures each) to check the compiler handles this correctly.
5
+ //!
6
+ //! Regression test for <https://github.com/rust-lang/rust/issues/34793>.
7
+ //! See also <https://github.com/rust-lang/rust/pull/34830>.
8
+
1
9
//@ run-pass
2
- // This test case tests whether we can handle code bases that contain a high
3
- // number of closures, something that needs special handling in the MingGW
4
- // toolchain.
5
- // See https://github.com/rust-lang/rust/issues/34793 for more information.
6
10
7
11
// Make sure we don't optimize anything away:
8
12
//@ compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
9
13
10
- // Expand something exponentially
14
+ /// Macro for exponential expansion - creates 2^n copies of the given macro call
11
15
macro_rules! go_bacterial {
12
16
( $mac: ident) => ( $mac!( ) ) ;
13
17
( $mac: ident 1 $( $t: tt) * ) => (
@@ -16,24 +20,28 @@ macro_rules! go_bacterial {
16
20
)
17
21
}
18
22
19
- macro_rules! mk_closure {
20
- ( ) => ( ( move || { } ) ( ) )
23
+ /// Creates and immediately calls a closure
24
+ macro_rules! create_closure {
25
+ ( ) => {
26
+ ( move || { } ) ( )
27
+ } ;
21
28
}
22
29
23
- macro_rules! mk_fn {
30
+ /// Creates a function containing 16 closures (2^4)
31
+ macro_rules! create_function_with_closures {
24
32
( ) => {
25
33
{
26
- fn function ( ) {
27
- // Make 16 closures
28
- go_bacterial!( mk_closure 1 1 1 1 ) ;
34
+ fn function_with_closures ( ) {
35
+ // Create 16 closures using exponential expansion: 2^4 = 16
36
+ go_bacterial!( create_closure 1 1 1 1 ) ;
29
37
}
30
- let _ = function ( ) ;
38
+ let _ = function_with_closures ( ) ;
31
39
}
32
40
}
33
41
}
34
42
35
43
fn main ( ) {
36
- // Make 2^8 functions, each containing 16 closures,
37
- // resulting in 2^12 closures overall .
38
- go_bacterial ! ( mk_fn 1 1 1 1 1 1 1 1 ) ;
44
+ // Create 2^8 = 256 functions, each containing 16 closures,
45
+ // resulting in 2^12 = 4096 closures total .
46
+ go_bacterial ! ( create_function_with_closures 1 1 1 1 1 1 1 1 ) ;
39
47
}
0 commit comments