From 8f2604dad1f85d1472cda238662a23f65be99ccb Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Mon, 23 Nov 2026 11:18:39 +0000 Subject: [PATCH 1/3] Avoid temporary vectors in derive trait checks Signed-off-by: Perfloop Agent --- rule-preprocessor/src/semantic.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 7f47818f2e..6c3eadb4b0 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -461,7 +461,7 @@ fn type_derives<'tcx>( .into_iter() .flatten() .filter(|&trait_def_id| { - let args = vec![ty; tcx.generics_of(trait_def_id).count()]; + let args = std::iter::repeat_n(ty, tcx.generics_of(trait_def_id).count()); infcx .type_implements_trait(trait_def_id, args, param_env) .must_apply_modulo_regions() From 3808ae86404be9c876650c3a2f0c25a1162d889e Mon Sep 17 00:00:00 2001 From: Perfloop Agent Date: Mon, 23 Nov 2026 12:04:37 +0000 Subject: [PATCH 2/3] Test allocation-free trait argument construction Signed-off-by: Perfloop Agent --- rule-preprocessor/src/semantic.rs | 108 +++++++++++++++++++++++++++++- 1 file changed, 107 insertions(+), 1 deletion(-) diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 6c3eadb4b0..38006b1e2c 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -461,7 +461,19 @@ fn type_derives<'tcx>( .into_iter() .flatten() .filter(|&trait_def_id| { - let args = std::iter::repeat_n(ty, tcx.generics_of(trait_def_id).count()); + let arity = tcx.generics_of(trait_def_id).count(); + #[cfg(test)] + let allocations = tests::allocation_count(); + let args = std::iter::repeat_n(ty, arity); + #[cfg(test)] + { + std::hint::black_box(&args); + assert_eq!( + tests::allocation_count(), + allocations, + "trait argument construction allocated" + ); + } infcx .type_implements_trait(trait_def_id, args, param_env) .must_apply_modulo_regions() @@ -744,3 +756,97 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { } } } + +#[cfg(test)] +mod tests { + extern crate rustc_session; + + use std::alloc::{GlobalAlloc, Layout, System}; + use std::cell::Cell; + use std::hint::black_box; + + thread_local! { + static ALLOCATIONS: Cell = const { Cell::new(0) }; + } + + struct CountingAllocator; + #[global_allocator] + static ALLOCATOR: CountingAllocator = CountingAllocator; + + unsafe impl GlobalAlloc for CountingAllocator { + unsafe fn alloc(&self, layout: Layout) -> *mut u8 { + let _ = ALLOCATIONS.try_with(|n| n.set(n.get() + 1)); + unsafe { System.alloc(layout) } + } + + unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { + let _ = ALLOCATIONS.try_with(|n| n.set(n.get() + 1)); + unsafe { System.alloc_zeroed(layout) } + } + + unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 { + let _ = ALLOCATIONS.try_with(|n| n.set(n.get() + 1)); + unsafe { System.realloc(ptr, layout, size) } + } + + unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { + unsafe { System.dealloc(ptr, layout) } + } + } + + pub(super) fn allocation_count() -> usize { + ALLOCATIONS.with(Cell::get) + } + + #[test] + #[cfg_attr( + debug_assertions, + ignore = "run with cargo test --release to observe argument allocations" + )] + fn trait_arguments_do_not_allocate() { + struct Callbacks(bool); + impl rustc_driver::Callbacks for Callbacks { + fn config(&mut self, config: &mut rustc_interface::interface::Config) { + config.input = rustc_session::config::Input::Str { + name: rustc_span::FileName::Custom("trait_arguments.rs".into()), + input: String::new(), + }; + } + + fn after_analysis( + &mut self, + _compiler: &rustc_interface::interface::Compiler, + tcx: rustc_middle::ty::TyCtxt<'_>, + ) -> rustc_driver::Compilation { + let before = allocation_count(); + let control = vec![tcx.types.i32; 1]; + black_box(&control); + assert!( + allocation_count() > before, + "allocation observer missed a vector" + ); + + black_box(super::type_derives( + tcx, + rustc_middle::ty::ParamEnv::empty(), + tcx.types.i32, + )); + self.0 = true; + rustc_driver::Compilation::Stop + } + } + + let mut callbacks = Callbacks(false); + rustc_driver::compiler_entrypoint( + &[ + "rustc".into(), + "--crate-name=trait_arguments".into(), + "--crate-type=lib".into(), + format!("--sysroot={}", super::get_sysroot().display()), + "-".into(), + ], + &mut callbacks, + ); + assert!(callbacks.0, "compiler did not run the trait check"); + } +} From a3fb7b4849c9ae39e64424e1752025f9f3583f0b Mon Sep 17 00:00:00 2001 From: Nuno Lopes Date: Fri, 27 Nov 2026 08:49:46 +0000 Subject: [PATCH 3/3] Remove allocation counting tests and related code Removed test-related code for allocation counting and assertions from semantic.rs. --- rule-preprocessor/src/semantic.rs | 105 ------------------------------ 1 file changed, 105 deletions(-) diff --git a/rule-preprocessor/src/semantic.rs b/rule-preprocessor/src/semantic.rs index 38006b1e2c..b704e0969c 100644 --- a/rule-preprocessor/src/semantic.rs +++ b/rule-preprocessor/src/semantic.rs @@ -462,18 +462,7 @@ fn type_derives<'tcx>( .flatten() .filter(|&trait_def_id| { let arity = tcx.generics_of(trait_def_id).count(); - #[cfg(test)] - let allocations = tests::allocation_count(); let args = std::iter::repeat_n(ty, arity); - #[cfg(test)] - { - std::hint::black_box(&args); - assert_eq!( - tests::allocation_count(), - allocations, - "trait argument construction allocated" - ); - } infcx .type_implements_trait(trait_def_id, args, param_env) .must_apply_modulo_regions() @@ -756,97 +745,3 @@ impl<'a, 'tcx> AstVisitor<'a, 'tcx> { } } } - -#[cfg(test)] -mod tests { - extern crate rustc_session; - - use std::alloc::{GlobalAlloc, Layout, System}; - use std::cell::Cell; - use std::hint::black_box; - - thread_local! { - static ALLOCATIONS: Cell = const { Cell::new(0) }; - } - - struct CountingAllocator; - #[global_allocator] - static ALLOCATOR: CountingAllocator = CountingAllocator; - - unsafe impl GlobalAlloc for CountingAllocator { - unsafe fn alloc(&self, layout: Layout) -> *mut u8 { - let _ = ALLOCATIONS.try_with(|n| n.set(n.get() + 1)); - unsafe { System.alloc(layout) } - } - - unsafe fn alloc_zeroed(&self, layout: Layout) -> *mut u8 { - let _ = ALLOCATIONS.try_with(|n| n.set(n.get() + 1)); - unsafe { System.alloc_zeroed(layout) } - } - - unsafe fn realloc(&self, ptr: *mut u8, layout: Layout, size: usize) -> *mut u8 { - let _ = ALLOCATIONS.try_with(|n| n.set(n.get() + 1)); - unsafe { System.realloc(ptr, layout, size) } - } - - unsafe fn dealloc(&self, ptr: *mut u8, layout: Layout) { - unsafe { System.dealloc(ptr, layout) } - } - } - - pub(super) fn allocation_count() -> usize { - ALLOCATIONS.with(Cell::get) - } - - #[test] - #[cfg_attr( - debug_assertions, - ignore = "run with cargo test --release to observe argument allocations" - )] - fn trait_arguments_do_not_allocate() { - struct Callbacks(bool); - impl rustc_driver::Callbacks for Callbacks { - fn config(&mut self, config: &mut rustc_interface::interface::Config) { - config.input = rustc_session::config::Input::Str { - name: rustc_span::FileName::Custom("trait_arguments.rs".into()), - input: String::new(), - }; - } - - fn after_analysis( - &mut self, - _compiler: &rustc_interface::interface::Compiler, - tcx: rustc_middle::ty::TyCtxt<'_>, - ) -> rustc_driver::Compilation { - let before = allocation_count(); - let control = vec![tcx.types.i32; 1]; - black_box(&control); - assert!( - allocation_count() > before, - "allocation observer missed a vector" - ); - - black_box(super::type_derives( - tcx, - rustc_middle::ty::ParamEnv::empty(), - tcx.types.i32, - )); - self.0 = true; - rustc_driver::Compilation::Stop - } - } - - let mut callbacks = Callbacks(false); - rustc_driver::compiler_entrypoint( - &[ - "rustc".into(), - "--crate-name=trait_arguments".into(), - "--crate-type=lib".into(), - format!("--sysroot={}", super::get_sysroot().display()), - "-".into(), - ], - &mut callbacks, - ); - assert!(callbacks.0, "compiler did not run the trait check"); - } -}