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+
19//@ 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.
610
711// Make sure we don't optimize anything away:
812//@ compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
913
10- // Expand something exponentially
14+ /// Macro for exponential expansion - creates 2^n copies of the given macro call
1115macro_rules! go_bacterial {
1216 ( $mac: ident) => ( $mac!( ) ) ;
1317 ( $mac: ident 1 $( $t: tt) * ) => (
@@ -16,24 +20,28 @@ macro_rules! go_bacterial {
1620 )
1721}
1822
19- macro_rules! mk_closure {
20- ( ) => ( ( move || { } ) ( ) )
23+ /// Creates and immediately calls a closure
24+ macro_rules! create_closure {
25+ ( ) => {
26+ ( move || { } ) ( )
27+ } ;
2128}
2229
23- macro_rules! mk_fn {
30+ /// Creates a function containing 16 closures (2^4)
31+ macro_rules! create_function_with_closures {
2432 ( ) => {
2533 {
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 ) ;
2937 }
30- let _ = function ( ) ;
38+ let _ = function_with_closures ( ) ;
3139 }
3240 }
3341}
3442
3543fn 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 ) ;
3947}
0 commit comments