From 16165abdf15dbeb7e7c9f23d1f3b94bd3b2f23d3 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Wed, 2 Dec 2026 14:00:56 +0000 Subject: [PATCH 1/3] Rust: Size the proc-macro server pool from the extraction threads Parallel extraction workers otherwise all queue on a single proc-macro server. Use up to four server processes, as each loads every proc-macro dylib. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- rust/extractor/src/config.rs | 9 +++++++-- rust/extractor/src/main.rs | 2 +- 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/rust/extractor/src/config.rs b/rust/extractor/src/config.rs index 3a6c034749df..641607c63819 100644 --- a/rust/extractor/src/config.rs +++ b/rust/extractor/src/config.rs @@ -24,6 +24,8 @@ use std::path::{Path, PathBuf}; use crate::toolchain::select_toolchain; +const MAX_PROC_MACRO_PROCESSES: usize = 4; + #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy)] #[serde(rename_all = "lowercase")] pub enum Compression { @@ -203,7 +205,8 @@ impl Config { } } - pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) { + /// Builds the rust-analyzer loading configuration for extracting on `threads` threads. + pub fn to_cargo_config(&self, dir: &AbsPath, threads: usize) -> (CargoConfig, LoadCargoConfig) { let sysroot = self.sysroot(dir); let mut extra_args = self.cargo_extra_args.clone(); // The fixed toolchain may be older than the project's declared minimum. @@ -257,7 +260,9 @@ impl Config { with_proc_macro_server: self.proc_macro_server_choice(dir), prefill_caches: false, num_worker_threads: 1, - proc_macro_processes: 1, + // each process serves one expansion at a time and loads every proc-macro dylib, + // so a few processes are shared by all extraction threads to bound memory + proc_macro_processes: threads.clamp(1, MAX_PROC_MACRO_PROCESSES), }, ) } diff --git a/rust/extractor/src/main.rs b/rust/extractor/src/main.rs index d47f4c9f7e26..97e25380460d 100644 --- a/rust/extractor/src/main.rs +++ b/rust/extractor/src/main.rs @@ -395,7 +395,6 @@ fn main() -> anyhow::Result<()> { let (workspaces, unmanaged) = Extractor::group_files(&manifests, &files); let cwd = cwd()?; log_project_toolchain(); - let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd); let library_mode = if config.extract_dependencies_as_source { SourceKind::Source } else { @@ -415,6 +414,7 @@ fn main() -> anyhow::Result<()> { SourceKind::Source, &RustAnalyzerNoSemantics::warning("no manifest found"), ); + let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd, threads); let mut dependencies = DependencyFiles::default(); From 0a034400e9e3b5428fa955aaafc30b9043bfdcf2 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Thu, 3 Dec 2026 10:12:09 +0000 Subject: [PATCH 2/3] Rust: Test proc-macro server pool sizing Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- rust/extractor/src/config.rs | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/rust/extractor/src/config.rs b/rust/extractor/src/config.rs index 641607c63819..594bd1b94774 100644 --- a/rust/extractor/src/config.rs +++ b/rust/extractor/src/config.rs @@ -309,3 +309,26 @@ fn join_path_buf(lhs: &AbsPath, rhs: &Path) -> AbsPathBuf { }; lhs.join(path) } + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn proc_macro_processes_follow_threads_up_to_cap() { + let dir = AbsPathBuf::assert_utf8(std::env::temp_dir()); + // explicit sysroot paths avoid toolchain discovery + let config = Config { + sysroot: Some(dir.clone().into()), + sysroot_src: Some(dir.clone().into()), + ..Default::default() + }; + for (threads, expected) in [(0, 1), (1, 1), (2, 2), (4, 4), (32, 4)] { + let (_, load_config) = config.to_cargo_config(&dir, threads); + assert_eq!( + load_config.proc_macro_processes, expected, + "{threads} threads" + ); + } + } +} From d484168cd30a0418e939bb7843b47d8a1f1f4081 Mon Sep 17 00:00:00 2001 From: Paolo Tranquilli Date: Fri, 4 Dec 2026 16:12:55 +0000 Subject: [PATCH 3/3] Rust: Use a single proc-macro server again Stateful proc macros like `enum_dispatch` keep process-global state across expansions, so spreading expansions over a pool of servers can lose generated items. Add an integration test exercising this pattern under parallel extraction. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- rust/extractor/src/config.rs | 32 +-- rust/extractor/src/main.rs | 2 +- .../proc-macro-state/Cargo.toml | 10 + .../proc-macro-state/busy0/Cargo.toml | 7 + .../proc-macro-state/busy0/src/lib.rs | 8 + .../proc-macro-state/busy1/Cargo.toml | 7 + .../proc-macro-state/busy1/src/lib.rs | 17 ++ .../proc-macro-state/busy2/Cargo.toml | 7 + .../proc-macro-state/busy2/src/lib.rs | 26 +++ .../proc-macro-state/busy3/Cargo.toml | 7 + .../proc-macro-state/busy3/src/lib.rs | 35 ++++ .../proc-macro-state/busy4/Cargo.toml | 7 + .../proc-macro-state/busy4/src/lib.rs | 44 +++++ .../proc-macro-state/busy5/Cargo.toml | 7 + .../proc-macro-state/busy5/src/lib.rs | 53 +++++ .../proc-macro-state/calls.expected | 8 + .../proc-macro-state/calls.ql | 5 + .../proc-macro-state/macros/Cargo.toml | 7 + .../proc-macro-state/macros/src/lib.rs | 89 +++++++++ .../proc-macro-state/src/lib.rs | 186 ++++++++++++++++++ .../proc-macro-state/test_proc_macro_state.py | 8 + 21 files changed, 541 insertions(+), 31 deletions(-) create mode 100644 rust/ql/integration-tests/proc-macro-state/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy0/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy0/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/busy1/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy1/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/busy2/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy2/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/busy3/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy3/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/busy4/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy4/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/busy5/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/busy5/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/calls.expected create mode 100644 rust/ql/integration-tests/proc-macro-state/calls.ql create mode 100644 rust/ql/integration-tests/proc-macro-state/macros/Cargo.toml create mode 100644 rust/ql/integration-tests/proc-macro-state/macros/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/src/lib.rs create mode 100644 rust/ql/integration-tests/proc-macro-state/test_proc_macro_state.py diff --git a/rust/extractor/src/config.rs b/rust/extractor/src/config.rs index 594bd1b94774..3a6c034749df 100644 --- a/rust/extractor/src/config.rs +++ b/rust/extractor/src/config.rs @@ -24,8 +24,6 @@ use std::path::{Path, PathBuf}; use crate::toolchain::select_toolchain; -const MAX_PROC_MACRO_PROCESSES: usize = 4; - #[derive(Debug, PartialEq, Eq, Default, Serialize, Deserialize, Clone, Copy)] #[serde(rename_all = "lowercase")] pub enum Compression { @@ -205,8 +203,7 @@ impl Config { } } - /// Builds the rust-analyzer loading configuration for extracting on `threads` threads. - pub fn to_cargo_config(&self, dir: &AbsPath, threads: usize) -> (CargoConfig, LoadCargoConfig) { + pub fn to_cargo_config(&self, dir: &AbsPath) -> (CargoConfig, LoadCargoConfig) { let sysroot = self.sysroot(dir); let mut extra_args = self.cargo_extra_args.clone(); // The fixed toolchain may be older than the project's declared minimum. @@ -260,9 +257,7 @@ impl Config { with_proc_macro_server: self.proc_macro_server_choice(dir), prefill_caches: false, num_worker_threads: 1, - // each process serves one expansion at a time and loads every proc-macro dylib, - // so a few processes are shared by all extraction threads to bound memory - proc_macro_processes: threads.clamp(1, MAX_PROC_MACRO_PROCESSES), + proc_macro_processes: 1, }, ) } @@ -309,26 +304,3 @@ fn join_path_buf(lhs: &AbsPath, rhs: &Path) -> AbsPathBuf { }; lhs.join(path) } - -#[cfg(test)] -mod tests { - use super::*; - - #[test] - fn proc_macro_processes_follow_threads_up_to_cap() { - let dir = AbsPathBuf::assert_utf8(std::env::temp_dir()); - // explicit sysroot paths avoid toolchain discovery - let config = Config { - sysroot: Some(dir.clone().into()), - sysroot_src: Some(dir.clone().into()), - ..Default::default() - }; - for (threads, expected) in [(0, 1), (1, 1), (2, 2), (4, 4), (32, 4)] { - let (_, load_config) = config.to_cargo_config(&dir, threads); - assert_eq!( - load_config.proc_macro_processes, expected, - "{threads} threads" - ); - } - } -} diff --git a/rust/extractor/src/main.rs b/rust/extractor/src/main.rs index 97e25380460d..d47f4c9f7e26 100644 --- a/rust/extractor/src/main.rs +++ b/rust/extractor/src/main.rs @@ -395,6 +395,7 @@ fn main() -> anyhow::Result<()> { let (workspaces, unmanaged) = Extractor::group_files(&manifests, &files); let cwd = cwd()?; log_project_toolchain(); + let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd); let library_mode = if config.extract_dependencies_as_source { SourceKind::Source } else { @@ -414,7 +415,6 @@ fn main() -> anyhow::Result<()> { SourceKind::Source, &RustAnalyzerNoSemantics::warning("no manifest found"), ); - let (cargo_config, load_cargo_config) = config.to_cargo_config(&cwd, threads); let mut dependencies = DependencyFiles::default(); diff --git a/rust/ql/integration-tests/proc-macro-state/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/Cargo.toml new file mode 100644 index 000000000000..cc5379c50937 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "proc_macro_state" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "macros" } + +[workspace] +members = ["macros", "busy0", "busy1", "busy2", "busy3", "busy4", "busy5"] diff --git a/rust/ql/integration-tests/proc-macro-state/busy0/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/busy0/Cargo.toml new file mode 100644 index 000000000000..0382a46ac81e --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy0/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "busy0" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "../macros" } diff --git a/rust/ql/integration-tests/proc-macro-state/busy0/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/busy0/src/lib.rs new file mode 100644 index 000000000000..cf8f043d132e --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy0/src/lib.rs @@ -0,0 +1,8 @@ +#[macros::slow] +pub fn f0() {} + +#[macros::slow] +pub fn f1() {} + +#[macros::slow] +pub fn f2() {} diff --git a/rust/ql/integration-tests/proc-macro-state/busy1/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/busy1/Cargo.toml new file mode 100644 index 000000000000..3171f585c646 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy1/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "busy1" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "../macros" } diff --git a/rust/ql/integration-tests/proc-macro-state/busy1/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/busy1/src/lib.rs new file mode 100644 index 000000000000..7fb3e341e395 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy1/src/lib.rs @@ -0,0 +1,17 @@ +#[macros::slow] +pub fn f0() {} + +#[macros::slow] +pub fn f1() {} + +#[macros::slow] +pub fn f2() {} + +#[macros::slow] +pub fn f3() {} + +#[macros::slow] +pub fn f4() {} + +#[macros::slow] +pub fn f5() {} diff --git a/rust/ql/integration-tests/proc-macro-state/busy2/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/busy2/Cargo.toml new file mode 100644 index 000000000000..a74d40bbdc94 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy2/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "busy2" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "../macros" } diff --git a/rust/ql/integration-tests/proc-macro-state/busy2/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/busy2/src/lib.rs new file mode 100644 index 000000000000..c4a250b9741a --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy2/src/lib.rs @@ -0,0 +1,26 @@ +#[macros::slow] +pub fn f0() {} + +#[macros::slow] +pub fn f1() {} + +#[macros::slow] +pub fn f2() {} + +#[macros::slow] +pub fn f3() {} + +#[macros::slow] +pub fn f4() {} + +#[macros::slow] +pub fn f5() {} + +#[macros::slow] +pub fn f6() {} + +#[macros::slow] +pub fn f7() {} + +#[macros::slow] +pub fn f8() {} diff --git a/rust/ql/integration-tests/proc-macro-state/busy3/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/busy3/Cargo.toml new file mode 100644 index 000000000000..19f47e28a1b0 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy3/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "busy3" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "../macros" } diff --git a/rust/ql/integration-tests/proc-macro-state/busy3/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/busy3/src/lib.rs new file mode 100644 index 000000000000..1bb890393e79 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy3/src/lib.rs @@ -0,0 +1,35 @@ +#[macros::slow] +pub fn f0() {} + +#[macros::slow] +pub fn f1() {} + +#[macros::slow] +pub fn f2() {} + +#[macros::slow] +pub fn f3() {} + +#[macros::slow] +pub fn f4() {} + +#[macros::slow] +pub fn f5() {} + +#[macros::slow] +pub fn f6() {} + +#[macros::slow] +pub fn f7() {} + +#[macros::slow] +pub fn f8() {} + +#[macros::slow] +pub fn f9() {} + +#[macros::slow] +pub fn f10() {} + +#[macros::slow] +pub fn f11() {} diff --git a/rust/ql/integration-tests/proc-macro-state/busy4/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/busy4/Cargo.toml new file mode 100644 index 000000000000..5aeb62fbf68c --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy4/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "busy4" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "../macros" } diff --git a/rust/ql/integration-tests/proc-macro-state/busy4/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/busy4/src/lib.rs new file mode 100644 index 000000000000..890fed433aa1 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy4/src/lib.rs @@ -0,0 +1,44 @@ +#[macros::slow] +pub fn f0() {} + +#[macros::slow] +pub fn f1() {} + +#[macros::slow] +pub fn f2() {} + +#[macros::slow] +pub fn f3() {} + +#[macros::slow] +pub fn f4() {} + +#[macros::slow] +pub fn f5() {} + +#[macros::slow] +pub fn f6() {} + +#[macros::slow] +pub fn f7() {} + +#[macros::slow] +pub fn f8() {} + +#[macros::slow] +pub fn f9() {} + +#[macros::slow] +pub fn f10() {} + +#[macros::slow] +pub fn f11() {} + +#[macros::slow] +pub fn f12() {} + +#[macros::slow] +pub fn f13() {} + +#[macros::slow] +pub fn f14() {} diff --git a/rust/ql/integration-tests/proc-macro-state/busy5/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/busy5/Cargo.toml new file mode 100644 index 000000000000..7d22300410c5 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy5/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "busy5" +version = "0.1.0" +edition = "2021" + +[dependencies] +macros = { path = "../macros" } diff --git a/rust/ql/integration-tests/proc-macro-state/busy5/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/busy5/src/lib.rs new file mode 100644 index 000000000000..b15326756922 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/busy5/src/lib.rs @@ -0,0 +1,53 @@ +#[macros::slow] +pub fn f0() {} + +#[macros::slow] +pub fn f1() {} + +#[macros::slow] +pub fn f2() {} + +#[macros::slow] +pub fn f3() {} + +#[macros::slow] +pub fn f4() {} + +#[macros::slow] +pub fn f5() {} + +#[macros::slow] +pub fn f6() {} + +#[macros::slow] +pub fn f7() {} + +#[macros::slow] +pub fn f8() {} + +#[macros::slow] +pub fn f9() {} + +#[macros::slow] +pub fn f10() {} + +#[macros::slow] +pub fn f11() {} + +#[macros::slow] +pub fn f12() {} + +#[macros::slow] +pub fn f13() {} + +#[macros::slow] +pub fn f14() {} + +#[macros::slow] +pub fn f15() {} + +#[macros::slow] +pub fn f16() {} + +#[macros::slow] +pub fn f17() {} diff --git a/rust/ql/integration-tests/proc-macro-state/calls.expected b/rust/ql/integration-tests/proc-macro-state/calls.expected new file mode 100644 index 000000000000..db385d41b100 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/calls.expected @@ -0,0 +1,8 @@ +| src/lib.rs:122:5:122:12 | e.get0() | ::get0 | +| src/lib.rs:131:5:131:12 | e.get1() | ::get1 | +| src/lib.rs:140:5:140:12 | e.get2() | ::get2 | +| src/lib.rs:149:5:149:12 | e.get3() | ::get3 | +| src/lib.rs:158:5:158:12 | e.get4() | ::get4 | +| src/lib.rs:167:5:167:12 | e.get5() | ::get5 | +| src/lib.rs:176:5:176:12 | e.get6() | ::get6 | +| src/lib.rs:185:5:185:12 | e.get7() | ::get7 | diff --git a/rust/ql/integration-tests/proc-macro-state/calls.ql b/rust/ql/integration-tests/proc-macro-state/calls.ql new file mode 100644 index 000000000000..d4a3e88aaaea --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/calls.ql @@ -0,0 +1,5 @@ +import rust + +from MethodCallExpr c +where c.getLocation().getFile().getRelativePath() = "src/lib.rs" +select c, c.getStaticTarget().getCanonicalPath() diff --git a/rust/ql/integration-tests/proc-macro-state/macros/Cargo.toml b/rust/ql/integration-tests/proc-macro-state/macros/Cargo.toml new file mode 100644 index 000000000000..d9f563d5285a --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/macros/Cargo.toml @@ -0,0 +1,7 @@ +[package] +name = "macros" +version = "0.1.0" +edition = "2021" + +[lib] +proc-macro = true diff --git a/rust/ql/integration-tests/proc-macro-state/macros/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/macros/src/lib.rs new file mode 100644 index 000000000000..e5421700a0fa --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/macros/src/lib.rs @@ -0,0 +1,89 @@ +//! Mimics `enum_dispatch`, which records trait definitions in process-global state when expanding +//! the trait attribute and reads them back when expanding the enum attribute. +extern crate proc_macro; + +use proc_macro::{Delimiter, TokenStream, TokenTree}; +use std::collections::BTreeMap; +use std::sync::Mutex; +use std::time::Duration; + +/// Maps trait names to their method name and signature. +static TRAIT_METHODS: Mutex> = Mutex::new(BTreeMap::new()); + +fn ident_after(tokens: &[TokenTree], keyword: &str) -> Option { + tokens + .windows(2) + .find(|w| matches!(&w[0], TokenTree::Ident(i) if i.to_string() == keyword)) + .map(|w| w[1].to_string()) +} + +fn body(tokens: &[TokenTree]) -> Vec { + tokens + .iter() + .find_map(|t| match t { + TokenTree::Group(g) if g.delimiter() == Delimiter::Brace => { + Some(g.stream().into_iter().collect()) + } + _ => None, + }) + .unwrap_or_default() +} + +/// Expects a trait with a single `fn name(&self) -> Type;` method. +#[proc_macro_attribute] +pub fn register(_: TokenStream, item: TokenStream) -> TokenStream { + pause(); + let tokens: Vec<_> = item.clone().into_iter().collect(); + let name = ident_after(&tokens, "trait").unwrap(); + let signature: Vec<_> = body(&tokens) + .into_iter() + .take_while(|t| !matches!(t, TokenTree::Punct(p) if p.as_char() == ';')) + .collect(); + let method = ident_after(&signature, "fn").unwrap(); + let signature = TokenStream::from_iter(signature).to_string(); + TRAIT_METHODS + .lock() + .unwrap() + .insert(name, (method, signature)); + item +} + +/// Implements the registered trait for an enum of single-field tuple variants, if this process +/// has seen the trait. +#[proc_macro_attribute] +pub fn dispatch(attr: TokenStream, item: TokenStream) -> TokenStream { + pause(); + let trait_name = attr.to_string(); + let Some((method, signature)) = TRAIT_METHODS.lock().unwrap().get(&trait_name).cloned() else { + return item; + }; + let tokens: Vec<_> = item.clone().into_iter().collect(); + let name = ident_after(&tokens, "enum").unwrap(); + let arms: String = body(&tokens) + .iter() + .filter_map(|t| match t { + TokenTree::Ident(variant) => Some(format!( + "{name}::{variant}(x) => {trait_name}::{method}(x)," + )), + _ => None, + }) + .collect(); + let mut ret = item; + ret.extend( + format!("impl {trait_name} for {name} {{ {signature} {{ match self {{ {arms} }} }} }}") + .parse::() + .unwrap(), + ); + ret +} + +/// Makes concurrent expansions overlap, so that a server pool would spread them across servers. +fn pause() { + std::thread::sleep(Duration::from_millis(20)); +} + +#[proc_macro_attribute] +pub fn slow(_: TokenStream, item: TokenStream) -> TokenStream { + pause(); + item +} diff --git a/rust/ql/integration-tests/proc-macro-state/src/lib.rs b/rust/ql/integration-tests/proc-macro-state/src/lib.rs new file mode 100644 index 000000000000..3fc12b554ac9 --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/src/lib.rs @@ -0,0 +1,186 @@ +#[macros::register] +pub trait Get0 { + fn get0(&self) -> u32; +} + +pub struct A0; +impl Get0 for A0 { + fn get0(&self) -> u32 { + 0 + } +} + +#[macros::register] +pub trait Get1 { + fn get1(&self) -> u32; +} + +pub struct A1; +impl Get1 for A1 { + fn get1(&self) -> u32 { + 1 + } +} + +#[macros::register] +pub trait Get2 { + fn get2(&self) -> u32; +} + +pub struct A2; +impl Get2 for A2 { + fn get2(&self) -> u32 { + 2 + } +} + +#[macros::register] +pub trait Get3 { + fn get3(&self) -> u32; +} + +pub struct A3; +impl Get3 for A3 { + fn get3(&self) -> u32 { + 3 + } +} + +#[macros::register] +pub trait Get4 { + fn get4(&self) -> u32; +} + +pub struct A4; +impl Get4 for A4 { + fn get4(&self) -> u32 { + 4 + } +} + +#[macros::register] +pub trait Get5 { + fn get5(&self) -> u32; +} + +pub struct A5; +impl Get5 for A5 { + fn get5(&self) -> u32 { + 5 + } +} + +#[macros::register] +pub trait Get6 { + fn get6(&self) -> u32; +} + +pub struct A6; +impl Get6 for A6 { + fn get6(&self) -> u32 { + 6 + } +} + +#[macros::register] +pub trait Get7 { + fn get7(&self) -> u32; +} + +pub struct A7; +impl Get7 for A7 { + fn get7(&self) -> u32 { + 7 + } +} + +// gives other crates time to take over the proc-macro server used for the traits +#[macros::slow] +pub fn pause0() {} + +#[macros::slow] +pub fn pause1() {} + +#[macros::slow] +pub fn pause2() {} + +#[macros::slow] +pub fn pause3() {} + +#[macros::slow] +pub fn pause4() {} + +#[macros::slow] +pub fn pause5() {} + +#[macros::dispatch(Get0)] +pub enum E0 { + A(A0), +} + +pub fn call0(e: E0) -> u32 { + e.get0() +} + +#[macros::dispatch(Get1)] +pub enum E1 { + A(A1), +} + +pub fn call1(e: E1) -> u32 { + e.get1() +} + +#[macros::dispatch(Get2)] +pub enum E2 { + A(A2), +} + +pub fn call2(e: E2) -> u32 { + e.get2() +} + +#[macros::dispatch(Get3)] +pub enum E3 { + A(A3), +} + +pub fn call3(e: E3) -> u32 { + e.get3() +} + +#[macros::dispatch(Get4)] +pub enum E4 { + A(A4), +} + +pub fn call4(e: E4) -> u32 { + e.get4() +} + +#[macros::dispatch(Get5)] +pub enum E5 { + A(A5), +} + +pub fn call5(e: E5) -> u32 { + e.get5() +} + +#[macros::dispatch(Get6)] +pub enum E6 { + A(A6), +} + +pub fn call6(e: E6) -> u32 { + e.get6() +} + +#[macros::dispatch(Get7)] +pub enum E7 { + A(A7), +} + +pub fn call7(e: E7) -> u32 { + e.get7() +} diff --git a/rust/ql/integration-tests/proc-macro-state/test_proc_macro_state.py b/rust/ql/integration-tests/proc-macro-state/test_proc_macro_state.py new file mode 100644 index 000000000000..f9e7f57d485b --- /dev/null +++ b/rust/ql/integration-tests/proc-macro-state/test_proc_macro_state.py @@ -0,0 +1,8 @@ +import pytest + + +# Stateful proc macros (like `enum_dispatch`) only work if all their expansions happen in the same +# proc-macro server process, even when crates are processed in parallel. +@pytest.mark.parametrize("threads", [1, 8]) +def test_proc_macro_state(codeql, rust, threads): + codeql.database.create(threads=threads)