about summary refs log tree commit diff
path: root/tests/ui/closures
diff options
context:
space:
mode:
authorJubilee <workingjubilee@gmail.com>2025-07-04 23:26:21 -0700
committerGitHub <noreply@github.com>2025-07-04 23:26:21 -0700
commitf10725218dbc9fabd1073ed3d29a0cbed17cfc00 (patch)
tree8629d4e1ea5ad34aa487b3979893fc24882dcac1 /tests/ui/closures
parent2f119daf4eb26fe73d8e5e651769977484f67328 (diff)
parentf12120d2bd237ffbf03c8de027513a8d0bc63616 (diff)
downloadrust-f10725218dbc9fabd1073ed3d29a0cbed17cfc00.tar.gz
rust-f10725218dbc9fabd1073ed3d29a0cbed17cfc00.zip
Rollup merge of #143202 - Kivooeo:tf18, r=tgross35
`tests/ui`: A New Order [18/N]

> [!NOTE]
>
> Intermediate commits are intended to help review, but will be squashed prior to merge.

Some `tests/ui/` housekeeping, to trim down number of tests directly under `tests/ui/`. Part of rust-lang/rust#133895.

r? `@tgross35`
Diffstat (limited to 'tests/ui/closures')
-rw-r--r--tests/ui/closures/many-closures.rs47
1 files changed, 47 insertions, 0 deletions
diff --git a/tests/ui/closures/many-closures.rs b/tests/ui/closures/many-closures.rs
new file mode 100644
index 00000000000..c96ef5544c2
--- /dev/null
+++ b/tests/ui/closures/many-closures.rs
@@ -0,0 +1,47 @@
+//! Test that the compiler can handle code bases with a high number of closures.
+//! This is particularly important for the MinGW toolchain which has a limit of
+//! 2^15 weak symbols per binary. This test creates 2^12 closures (256 functions
+//! with 16 closures each) to check the compiler handles this correctly.
+//!
+//! Regression test for <https://github.com/rust-lang/rust/issues/34793>.
+//! See also <https://github.com/rust-lang/rust/pull/34830>.
+
+//@ run-pass
+
+// Make sure we don't optimize anything away:
+//@ compile-flags: -C no-prepopulate-passes -Cpasses=name-anon-globals
+
+/// Macro for exponential expansion - creates 2^n copies of the given macro call
+macro_rules! go_bacterial {
+    ($mac:ident) => ($mac!());
+    ($mac:ident 1 $($t:tt)*) => (
+        go_bacterial!($mac $($t)*);
+        go_bacterial!($mac $($t)*);
+    )
+}
+
+/// Creates and immediately calls a closure
+macro_rules! create_closure {
+    () => {
+        (move || {})()
+    };
+}
+
+/// Creates a function containing 16 closures (2^4)
+macro_rules! create_function_with_closures {
+    () => {
+        {
+            fn function_with_closures() {
+                // Create 16 closures using exponential expansion: 2^4 = 16
+                go_bacterial!(create_closure 1 1 1 1);
+            }
+            let _ = function_with_closures();
+        }
+    }
+}
+
+fn main() {
+    // Create 2^8 = 256 functions, each containing 16 closures,
+    // resulting in 2^12 = 4096 closures total.
+    go_bacterial!(create_function_with_closures 1 1 1 1  1 1 1 1);
+}