From 3aa31788b57da7a5eeee8184dccb00178b40121b Mon Sep 17 00:00:00 2001 From: Nicholas Nethercote Date: Wed, 14 Jan 2026 10:30:08 +1100 Subject: [PATCH] Remove `Deref`/`DerefMut` impl for `Providers`. It's described as a "backwards compatibility hack to keep the diff small". Removing it requires only a modest amount of churn, and the resulting code is clearer without the invisible derefs. --- compiler/rustc_codegen_gcc/src/lib.rs | 3 +- compiler/rustc_codegen_llvm/src/lib.rs | 2 +- .../src/back/symbol_export.rs | 18 ++++---- compiler/rustc_codegen_ssa/src/lib.rs | 6 +-- compiler/rustc_const_eval/src/lib.rs | 16 +++---- compiler/rustc_interface/src/passes.rs | 46 +++++++++---------- compiler/rustc_metadata/src/rmeta/mod.rs | 2 +- compiler/rustc_middle/src/util/mod.rs | 17 ------- compiler/rustc_mir_build/src/lib.rs | 12 ++--- compiler/rustc_mir_transform/src/lib.rs | 6 +-- compiler/rustc_monomorphize/src/collector.rs | 2 +- compiler/rustc_monomorphize/src/lib.rs | 2 +- .../rustc_monomorphize/src/partitioning.rs | 8 ++-- compiler/rustc_passes/src/lib.rs | 2 +- src/librustdoc/core.rs | 9 ++-- src/tools/miri/src/bin/miri.rs | 2 +- tests/ui-fulldeps/obtain-borrowck.rs | 6 +-- 17 files changed, 72 insertions(+), 87 deletions(-) diff --git a/compiler/rustc_codegen_gcc/src/lib.rs b/compiler/rustc_codegen_gcc/src/lib.rs index 96d3a0024f41..cf1be1806235 100644 --- a/compiler/rustc_codegen_gcc/src/lib.rs +++ b/compiler/rustc_codegen_gcc/src/lib.rs @@ -286,7 +286,8 @@ impl CodegenBackend for GccCodegenBackend { } fn provide(&self, providers: &mut Providers) { - providers.global_backend_features = |tcx, ()| gcc_util::global_gcc_features(tcx.sess) + providers.queries.global_backend_features = + |tcx, ()| gcc_util::global_gcc_features(tcx.sess) } fn codegen_crate(&self, tcx: TyCtxt<'_>) -> Box { diff --git a/compiler/rustc_codegen_llvm/src/lib.rs b/compiler/rustc_codegen_llvm/src/lib.rs index 438a74e0a091..e0007f69828d 100644 --- a/compiler/rustc_codegen_llvm/src/lib.rs +++ b/compiler/rustc_codegen_llvm/src/lib.rs @@ -264,7 +264,7 @@ impl CodegenBackend for LlvmCodegenBackend { } fn provide(&self, providers: &mut Providers) { - providers.global_backend_features = + providers.queries.global_backend_features = |tcx, ()| llvm_util::global_llvm_features(tcx.sess, false) } diff --git a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs index 27989f6f5ea2..59d0f5ee9d54 100644 --- a/compiler/rustc_codegen_ssa/src/back/symbol_export.rs +++ b/compiler/rustc_codegen_ssa/src/back/symbol_export.rs @@ -474,15 +474,15 @@ fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId) } pub(crate) fn provide(providers: &mut Providers) { - providers.reachable_non_generics = reachable_non_generics_provider; - providers.is_reachable_non_generic = is_reachable_non_generic_provider_local; - providers.exported_non_generic_symbols = exported_non_generic_symbols_provider_local; - providers.exported_generic_symbols = exported_generic_symbols_provider_local; - providers.upstream_monomorphizations = upstream_monomorphizations_provider; - providers.is_unreachable_local_definition = is_unreachable_local_definition_provider; - providers.upstream_drop_glue_for = upstream_drop_glue_for_provider; - providers.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider; - providers.wasm_import_module_map = wasm_import_module_map; + providers.queries.reachable_non_generics = reachable_non_generics_provider; + providers.queries.is_reachable_non_generic = is_reachable_non_generic_provider_local; + providers.queries.exported_non_generic_symbols = exported_non_generic_symbols_provider_local; + providers.queries.exported_generic_symbols = exported_generic_symbols_provider_local; + providers.queries.upstream_monomorphizations = upstream_monomorphizations_provider; + providers.queries.is_unreachable_local_definition = is_unreachable_local_definition_provider; + providers.queries.upstream_drop_glue_for = upstream_drop_glue_for_provider; + providers.queries.upstream_async_drop_glue_for = upstream_async_drop_glue_for_provider; + providers.queries.wasm_import_module_map = wasm_import_module_map; providers.extern_queries.is_reachable_non_generic = is_reachable_non_generic_provider_extern; providers.extern_queries.upstream_monomorphizations_for = upstream_monomorphizations_for_provider; diff --git a/compiler/rustc_codegen_ssa/src/lib.rs b/compiler/rustc_codegen_ssa/src/lib.rs index d7461c76ff03..8c39d04ef21d 100644 --- a/compiler/rustc_codegen_ssa/src/lib.rs +++ b/compiler/rustc_codegen_ssa/src/lib.rs @@ -266,9 +266,9 @@ pub enum CodegenErrors { pub fn provide(providers: &mut Providers) { crate::back::symbol_export::provide(providers); - crate::base::provide(providers); - crate::target_features::provide(providers); - crate::codegen_attrs::provide(providers); + crate::base::provide(&mut providers.queries); + crate::target_features::provide(&mut providers.queries); + crate::codegen_attrs::provide(&mut providers.queries); providers.queries.global_backend_features = |_tcx: TyCtxt<'_>, ()| vec![]; } diff --git a/compiler/rustc_const_eval/src/lib.rs b/compiler/rustc_const_eval/src/lib.rs index 2fce4b8c0566..9b1d4652212b 100644 --- a/compiler/rustc_const_eval/src/lib.rs +++ b/compiler/rustc_const_eval/src/lib.rs @@ -30,20 +30,20 @@ pub use self::errors::ReportErrorExt; rustc_fluent_macro::fluent_messages! { "../messages.ftl" } pub fn provide(providers: &mut Providers) { - const_eval::provide(providers); - providers.tag_for_variant = const_eval::tag_for_variant_provider; - providers.eval_to_const_value_raw = const_eval::eval_to_const_value_raw_provider; - providers.eval_to_allocation_raw = const_eval::eval_to_allocation_raw_provider; - providers.eval_static_initializer = const_eval::eval_static_initializer_provider; + const_eval::provide(&mut providers.queries); + providers.queries.tag_for_variant = const_eval::tag_for_variant_provider; + providers.queries.eval_to_const_value_raw = const_eval::eval_to_const_value_raw_provider; + providers.queries.eval_to_allocation_raw = const_eval::eval_to_allocation_raw_provider; + providers.queries.eval_static_initializer = const_eval::eval_static_initializer_provider; providers.hooks.const_caller_location = util::caller_location::const_caller_location_provider; - providers.eval_to_valtree = |tcx, ty::PseudoCanonicalInput { typing_env, value }| { + providers.queries.eval_to_valtree = |tcx, ty::PseudoCanonicalInput { typing_env, value }| { const_eval::eval_to_valtree(tcx, typing_env, value) }; providers.hooks.try_destructure_mir_constant_for_user_output = const_eval::try_destructure_mir_constant_for_user_output; - providers.valtree_to_const_val = + providers.queries.valtree_to_const_val = |tcx, cv| const_eval::valtree_to_const_value(tcx, ty::TypingEnv::fully_monomorphized(), cv); - providers.check_validity_requirement = |tcx, (init_kind, param_env_and_ty)| { + providers.queries.check_validity_requirement = |tcx, (init_kind, param_env_and_ty)| { util::check_validity_requirement(tcx, init_kind, param_env_and_ty) }; providers.hooks.validate_scalar_in_layout = diff --git a/compiler/rustc_interface/src/passes.rs b/compiler/rustc_interface/src/passes.rs index a0383b187de5..35ab202d4c27 100644 --- a/compiler/rustc_interface/src/passes.rs +++ b/compiler/rustc_interface/src/passes.rs @@ -880,36 +880,36 @@ pub fn write_interface<'tcx>(tcx: TyCtxt<'tcx>) { pub static DEFAULT_QUERY_PROVIDERS: LazyLock = LazyLock::new(|| { let providers = &mut Providers::default(); - providers.analysis = analysis; - providers.hir_crate = rustc_ast_lowering::lower_to_hir; - providers.resolver_for_lowering_raw = resolver_for_lowering_raw; - providers.stripped_cfg_items = |tcx, _| &tcx.resolutions(()).stripped_cfg_items[..]; - providers.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1; - providers.early_lint_checks = early_lint_checks; - providers.env_var_os = env_var_os; - limits::provide(providers); - proc_macro_decls::provide(providers); + providers.queries.analysis = analysis; + providers.queries.hir_crate = rustc_ast_lowering::lower_to_hir; + providers.queries.resolver_for_lowering_raw = resolver_for_lowering_raw; + providers.queries.stripped_cfg_items = |tcx, _| &tcx.resolutions(()).stripped_cfg_items[..]; + providers.queries.resolutions = |tcx, ()| tcx.resolver_for_lowering_raw(()).1; + providers.queries.early_lint_checks = early_lint_checks; + providers.queries.env_var_os = env_var_os; + limits::provide(&mut providers.queries); + proc_macro_decls::provide(&mut providers.queries); rustc_const_eval::provide(providers); - rustc_middle::hir::provide(providers); - rustc_borrowck::provide(providers); + rustc_middle::hir::provide(&mut providers.queries); + rustc_borrowck::provide(&mut providers.queries); rustc_incremental::provide(providers); rustc_mir_build::provide(providers); rustc_mir_transform::provide(providers); rustc_monomorphize::provide(providers); - rustc_privacy::provide(providers); + rustc_privacy::provide(&mut providers.queries); rustc_query_impl::provide(providers); - rustc_resolve::provide(providers); - rustc_hir_analysis::provide(providers); - rustc_hir_typeck::provide(providers); - ty::provide(providers); - traits::provide(providers); - solve::provide(providers); - rustc_passes::provide(providers); - rustc_traits::provide(providers); - rustc_ty_utils::provide(providers); + rustc_resolve::provide(&mut providers.queries); + rustc_hir_analysis::provide(&mut providers.queries); + rustc_hir_typeck::provide(&mut providers.queries); + ty::provide(&mut providers.queries); + traits::provide(&mut providers.queries); + solve::provide(&mut providers.queries); + rustc_passes::provide(&mut providers.queries); + rustc_traits::provide(&mut providers.queries); + rustc_ty_utils::provide(&mut providers.queries); rustc_metadata::provide(providers); - rustc_lint::provide(providers); - rustc_symbol_mangling::provide(providers); + rustc_lint::provide(&mut providers.queries); + rustc_symbol_mangling::provide(&mut providers.queries); rustc_codegen_ssa::provide(providers); *providers }); diff --git a/compiler/rustc_metadata/src/rmeta/mod.rs b/compiler/rustc_metadata/src/rmeta/mod.rs index 0de54cf87433..af6df0cd6eb6 100644 --- a/compiler/rustc_metadata/src/rmeta/mod.rs +++ b/compiler/rustc_metadata/src/rmeta/mod.rs @@ -585,6 +585,6 @@ const SYMBOL_OFFSET: u8 = 1; const SYMBOL_PREDEFINED: u8 = 2; pub fn provide(providers: &mut Providers) { - encoder::provide(providers); + encoder::provide(&mut providers.queries); decoder::provide(providers); } diff --git a/compiler/rustc_middle/src/util/mod.rs b/compiler/rustc_middle/src/util/mod.rs index 85519fb0a7d2..d5076a278eab 100644 --- a/compiler/rustc_middle/src/util/mod.rs +++ b/compiler/rustc_middle/src/util/mod.rs @@ -6,20 +6,3 @@ pub struct Providers { pub extern_queries: crate::query::ExternProviders, pub hooks: crate::hooks::Providers, } - -/// Backwards compatibility hack to keep the diff small. This -/// gives direct access to the `queries` field's fields, which -/// are what almost everything wants access to. -impl std::ops::DerefMut for Providers { - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.queries - } -} - -impl std::ops::Deref for Providers { - type Target = crate::query::Providers; - - fn deref(&self) -> &Self::Target { - &self.queries - } -} diff --git a/compiler/rustc_mir_build/src/lib.rs b/compiler/rustc_mir_build/src/lib.rs index 410ea791ac5c..91cc4dfa665f 100644 --- a/compiler/rustc_mir_build/src/lib.rs +++ b/compiler/rustc_mir_build/src/lib.rs @@ -23,12 +23,12 @@ use rustc_middle::util::Providers; rustc_fluent_macro::fluent_messages! { "../messages.ftl" } pub fn provide(providers: &mut Providers) { - providers.check_match = thir::pattern::check_match; - providers.lit_to_const = thir::constant::lit_to_const; - providers.closure_saved_names_of_captured_variables = + providers.queries.check_match = thir::pattern::check_match; + providers.queries.lit_to_const = thir::constant::lit_to_const; + providers.queries.closure_saved_names_of_captured_variables = builder::closure_saved_names_of_captured_variables; - providers.check_unsafety = check_unsafety::check_unsafety; - providers.check_tail_calls = check_tail_calls::check_tail_calls; - providers.thir_body = thir::cx::thir_body; + providers.queries.check_unsafety = check_unsafety::check_unsafety; + providers.queries.check_tail_calls = check_tail_calls::check_tail_calls; + providers.queries.thir_body = thir::cx::thir_body; providers.hooks.build_mir_inner_impl = builder::build_mir_inner_impl; } diff --git a/compiler/rustc_mir_transform/src/lib.rs b/compiler/rustc_mir_transform/src/lib.rs index c78b0adaf6f1..328a4b23c175 100644 --- a/compiler/rustc_mir_transform/src/lib.rs +++ b/compiler/rustc_mir_transform/src/lib.rs @@ -206,9 +206,9 @@ rustc_fluent_macro::fluent_messages! { "../messages.ftl" } pub fn provide(providers: &mut Providers) { coverage::query::provide(providers); - ffi_unwind_calls::provide(providers); - shim::provide(providers); - cross_crate_inline::provide(providers); + ffi_unwind_calls::provide(&mut providers.queries); + shim::provide(&mut providers.queries); + cross_crate_inline::provide(&mut providers.queries); providers.queries = query::Providers { mir_keys, mir_built, diff --git a/compiler/rustc_monomorphize/src/collector.rs b/compiler/rustc_monomorphize/src/collector.rs index 070db1ae6b5e..ce1704696918 100644 --- a/compiler/rustc_monomorphize/src/collector.rs +++ b/compiler/rustc_monomorphize/src/collector.rs @@ -1825,5 +1825,5 @@ pub(crate) fn collect_crate_mono_items<'tcx>( pub(crate) fn provide(providers: &mut Providers) { providers.hooks.should_codegen_locally = should_codegen_locally; - providers.items_of_instance = items_of_instance; + providers.queries.items_of_instance = items_of_instance; } diff --git a/compiler/rustc_monomorphize/src/lib.rs b/compiler/rustc_monomorphize/src/lib.rs index 5b4f74ca6a70..56c74b693fb7 100644 --- a/compiler/rustc_monomorphize/src/lib.rs +++ b/compiler/rustc_monomorphize/src/lib.rs @@ -52,5 +52,5 @@ fn custom_coerce_unsize_info<'tcx>( pub fn provide(providers: &mut Providers) { partitioning::provide(providers); - mono_checks::provide(providers); + mono_checks::provide(&mut providers.queries); } diff --git a/compiler/rustc_monomorphize/src/partitioning.rs b/compiler/rustc_monomorphize/src/partitioning.rs index 13d7f6a25f76..a86230e9ab22 100644 --- a/compiler/rustc_monomorphize/src/partitioning.rs +++ b/compiler/rustc_monomorphize/src/partitioning.rs @@ -1313,12 +1313,12 @@ fn dump_mono_items_stats<'tcx>( } pub(crate) fn provide(providers: &mut Providers) { - providers.collect_and_partition_mono_items = collect_and_partition_mono_items; + providers.queries.collect_and_partition_mono_items = collect_and_partition_mono_items; - providers.is_codegened_item = + providers.queries.is_codegened_item = |tcx, def_id| tcx.collect_and_partition_mono_items(()).all_mono_items.contains(&def_id); - providers.codegen_unit = |tcx, name| { + providers.queries.codegen_unit = |tcx, name| { tcx.collect_and_partition_mono_items(()) .codegen_units .iter() @@ -1326,7 +1326,7 @@ pub(crate) fn provide(providers: &mut Providers) { .unwrap_or_else(|| panic!("failed to find cgu with name {name:?}")) }; - providers.size_estimate = |tcx, instance| { + providers.queries.size_estimate = |tcx, instance| { match instance.def { // "Normal" functions size estimate: the number of // statements, plus one for the terminator. diff --git a/compiler/rustc_passes/src/lib.rs b/compiler/rustc_passes/src/lib.rs index 865f2a9c3b8b..d371589cba6c 100644 --- a/compiler/rustc_passes/src/lib.rs +++ b/compiler/rustc_passes/src/lib.rs @@ -9,7 +9,7 @@ #![feature(map_try_insert)] // tidy-alphabetical-end -use rustc_middle::util::Providers; +use rustc_middle::query::Providers; pub mod abi_test; mod check_attr; diff --git a/src/librustdoc/core.rs b/src/librustdoc/core.rs index fb2088963284..413163290ee3 100644 --- a/src/librustdoc/core.rs +++ b/src/librustdoc/core.rs @@ -297,14 +297,15 @@ pub(crate) fn create_config( override_queries: Some(|_sess, providers| { // We do not register late module lints, so this only runs `MissingDoc`. // Most lints will require typechecking, so just don't run them. - providers.lint_mod = |tcx, module_def_id| late_lint_mod(tcx, module_def_id, MissingDoc); + providers.queries.lint_mod = + |tcx, module_def_id| late_lint_mod(tcx, module_def_id, MissingDoc); // hack so that `used_trait_imports` won't try to call typeck - providers.used_trait_imports = |_, _| { + providers.queries.used_trait_imports = |_, _| { static EMPTY_SET: LazyLock> = LazyLock::new(UnordSet::default); &EMPTY_SET }; // In case typeck does end up being called, don't ICE in case there were name resolution errors - providers.typeck = move |tcx, def_id| { + providers.queries.typeck = move |tcx, def_id| { // Closures' tables come from their outermost function, // as they are part of the same "inference environment". // This avoids emitting errors for the parent twice (see similar code in `typeck_with_fallback`) @@ -316,7 +317,7 @@ pub(crate) fn create_config( let body = tcx.hir_body_owned_by(def_id); debug!("visiting body for {def_id:?}"); EmitIgnoredResolutionErrors::new(tcx).visit_body(body); - (rustc_interface::DEFAULT_QUERY_PROVIDERS.typeck)(tcx, def_id) + (rustc_interface::DEFAULT_QUERY_PROVIDERS.queries.typeck)(tcx, def_id) }; }), extra_symbols: Vec::new(), diff --git a/src/tools/miri/src/bin/miri.rs b/src/tools/miri/src/bin/miri.rs index 19fbf90246c9..9816061a8eca 100644 --- a/src/tools/miri/src/bin/miri.rs +++ b/src/tools/miri/src/bin/miri.rs @@ -305,7 +305,7 @@ impl rustc_driver::Callbacks for MiriDepCompilerCalls { config.override_queries = Some(|_, local_providers| { // We need to add #[used] symbols to exported_symbols for `lookup_link_section`. // FIXME handle this somehow in rustc itself to avoid this hack. - local_providers.exported_non_generic_symbols = |tcx, LocalCrate| { + local_providers.queries.exported_non_generic_symbols = |tcx, LocalCrate| { let reachable_set = tcx .with_stable_hashing_context(|hcx| tcx.reachable_set(()).to_sorted(&hcx, true)); tcx.arena.alloc_from_iter( diff --git a/tests/ui-fulldeps/obtain-borrowck.rs b/tests/ui-fulldeps/obtain-borrowck.rs index 08213fd75880..a562d0ccd3df 100644 --- a/tests/ui-fulldeps/obtain-borrowck.rs +++ b/tests/ui-fulldeps/obtain-borrowck.rs @@ -114,7 +114,7 @@ impl rustc_driver::Callbacks for CompilerCalls { } fn override_queries(_session: &Session, local: &mut Providers) { - local.mir_borrowck = mir_borrowck; + local.queries.mir_borrowck = mir_borrowck; } // Since mir_borrowck does not have access to any other state, we need to use a @@ -142,8 +142,8 @@ fn mir_borrowck<'tcx>(tcx: TyCtxt<'tcx>, def_id: LocalDefId) -> ProvidedValue<'t } }); let mut providers = Providers::default(); - rustc_borrowck::provide(&mut providers); - let original_mir_borrowck = providers.mir_borrowck; + rustc_borrowck::provide(&mut providers.queries); + let original_mir_borrowck = providers.queries.mir_borrowck; original_mir_borrowck(tcx, def_id) }