Skip to content

Commit 366f8f3

Browse files
committed
Compute default query providers at compile-time
All of the `provide` functions are made `const`, allowing us to compute the default local and extern query providers entirely at compile-time. In addition to moving runtime work to compile-time, this will allow our internal documentation to show all query implementations, once #87038 is implemented.
1 parent a31431f commit 366f8f3

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

82 files changed

+142
-108
lines changed

compiler/rustc_codegen_ssa/src/back/symbol_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -357,7 +357,7 @@ fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: LocalDefId)
357357
!tcx.reachable_set(()).contains(&def_id)
358358
}
359359

360-
pub fn provide(providers: &mut Providers) {
360+
pub const fn provide(providers: &mut Providers) {
361361
providers.reachable_non_generics = reachable_non_generics_provider;
362362
providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
363363
providers.exported_symbols = exported_symbols_provider_local;
@@ -367,7 +367,7 @@ pub fn provide(providers: &mut Providers) {
367367
providers.wasm_import_module_map = wasm_import_module_map;
368368
}
369369

370-
pub fn provide_extern(providers: &mut Providers) {
370+
pub const fn provide_extern(providers: &mut Providers) {
371371
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
372372
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
373373
}

compiler/rustc_codegen_ssa/src/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -849,7 +849,7 @@ impl CrateInfo {
849849
}
850850
}
851851

852-
pub fn provide(providers: &mut Providers) {
852+
pub const fn provide(providers: &mut Providers) {
853853
providers.backend_optimization_level = |tcx, cratenum| {
854854
let for_speed = match tcx.sess.opts.optimize {
855855
// If globally no optimisation is done, #[optimize] has no effect.

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
#![feature(in_band_lifetimes)]
66
#![feature(nll)]
77
#![feature(associated_type_bounds)]
8+
#![feature(const_fn_fn_ptr_basics)]
9+
#![feature(const_mut_refs)]
810
#![recursion_limit = "256"]
911

1012
//! This crate contains codegen code that is used by all codegen backends (LLVM and others).
@@ -161,13 +163,13 @@ pub struct CodegenResults {
161163
pub crate_info: CrateInfo,
162164
}
163165

164-
pub fn provide(providers: &mut Providers) {
166+
pub const fn provide(providers: &mut Providers) {
165167
crate::back::symbol_export::provide(providers);
166168
crate::base::provide(providers);
167169
crate::target_features::provide(providers);
168170
}
169171

170-
pub fn provide_extern(providers: &mut Providers) {
172+
pub const fn provide_extern(providers: &mut Providers) {
171173
crate::back::symbol_export::provide_extern(providers);
172174
}
173175

compiler/rustc_codegen_ssa/src/target_features.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -245,7 +245,7 @@ pub fn supported_target_features(sess: &Session) -> &'static [(&'static str, Opt
245245
}
246246
}
247247

248-
pub(crate) fn provide(providers: &mut Providers) {
248+
pub(crate) const fn provide(providers: &mut Providers) {
249249
providers.supported_target_features = |tcx, cnum| {
250250
assert_eq!(cnum, LOCAL_CRATE);
251251
if tcx.sess.opts.actually_rustdoc {

compiler/rustc_interface/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![feature(bool_to_option)]
22
#![feature(box_patterns)]
3+
#![feature(const_fn_fn_ptr_basics)]
4+
#![feature(const_mut_refs)]
35
#![feature(internal_output_capture)]
46
#![feature(nll)]
57
#![feature(once_cell)]

compiler/rustc_interface/src/passes.rs

Lines changed: 27 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,6 @@ use std::any::Any;
4545
use std::cell::RefCell;
4646
use std::ffi::OsString;
4747
use std::io::{self, BufWriter, Write};
48-
use std::lazy::SyncLazy;
4948
use std::marker::PhantomPinned;
5049
use std::path::PathBuf;
5150
use std::pin::Pin;
@@ -737,35 +736,35 @@ pub fn prepare_outputs(
737736
Ok(outputs)
738737
}
739738

740-
pub static DEFAULT_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
741-
let providers = &mut Providers::default();
739+
pub const DEFAULT_QUERY_PROVIDERS: Providers = {
740+
let mut providers = Providers::default();
742741
providers.analysis = analysis;
743-
proc_macro_decls::provide(providers);
744-
plugin::build::provide(providers);
745-
rustc_middle::hir::provide(providers);
746-
mir::provide(providers);
747-
mir_build::provide(providers);
748-
rustc_privacy::provide(providers);
749-
typeck::provide(providers);
750-
ty::provide(providers);
751-
traits::provide(providers);
752-
rustc_passes::provide(providers);
753-
rustc_resolve::provide(providers);
754-
rustc_traits::provide(providers);
755-
rustc_ty_utils::provide(providers);
756-
rustc_metadata::provide(providers);
757-
rustc_lint::provide(providers);
758-
rustc_symbol_mangling::provide(providers);
759-
rustc_codegen_ssa::provide(providers);
760-
*providers
761-
});
762-
763-
pub static DEFAULT_EXTERN_QUERY_PROVIDERS: SyncLazy<Providers> = SyncLazy::new(|| {
764-
let mut extern_providers = *DEFAULT_QUERY_PROVIDERS;
742+
proc_macro_decls::provide(&mut providers);
743+
plugin::build::provide(&mut providers);
744+
rustc_middle::hir::provide(&mut providers);
745+
mir::provide(&mut providers);
746+
mir_build::provide(&mut providers);
747+
rustc_privacy::provide(&mut providers);
748+
typeck::provide(&mut providers);
749+
ty::provide(&mut providers);
750+
traits::provide(&mut providers);
751+
rustc_passes::provide(&mut providers);
752+
rustc_resolve::provide(&mut providers);
753+
rustc_traits::provide(&mut providers);
754+
rustc_ty_utils::provide(&mut providers);
755+
rustc_metadata::provide(&mut providers);
756+
rustc_lint::provide(&mut providers);
757+
rustc_symbol_mangling::provide(&mut providers);
758+
rustc_codegen_ssa::provide(&mut providers);
759+
providers
760+
};
761+
762+
pub const DEFAULT_EXTERN_QUERY_PROVIDERS: Providers = {
763+
let mut extern_providers = DEFAULT_QUERY_PROVIDERS;
765764
rustc_metadata::provide_extern(&mut extern_providers);
766765
rustc_codegen_ssa::provide_extern(&mut extern_providers);
767766
extern_providers
768-
});
767+
};
769768

770769
pub struct QueryContext<'tcx> {
771770
gcx: &'tcx GlobalCtxt<'tcx>,
@@ -808,10 +807,10 @@ pub fn create_global_ctxt<'tcx>(
808807
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
809808

810809
let codegen_backend = compiler.codegen_backend();
811-
let mut local_providers = *DEFAULT_QUERY_PROVIDERS;
810+
let mut local_providers = DEFAULT_QUERY_PROVIDERS;
812811
codegen_backend.provide(&mut local_providers);
813812

814-
let mut extern_providers = *DEFAULT_EXTERN_QUERY_PROVIDERS;
813+
let mut extern_providers = DEFAULT_EXTERN_QUERY_PROVIDERS;
815814
codegen_backend.provide(&mut extern_providers);
816815
codegen_backend.provide_extern(&mut extern_providers);
817816

compiler/rustc_interface/src/proc_macro_decls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,6 @@ impl<'v> ItemLikeVisitor<'v> for Finder<'_> {
3232
fn visit_foreign_item(&mut self, _foreign_item: &hir::ForeignItem<'_>) {}
3333
}
3434

35-
pub(crate) fn provide(providers: &mut Providers) {
35+
pub(crate) const fn provide(providers: &mut Providers) {
3636
*providers = Providers { proc_macro_decls_static, ..*providers };
3737
}

compiler/rustc_lint/src/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -698,6 +698,6 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
698698
}
699699
}
700700

701-
pub fn provide(providers: &mut Providers) {
701+
pub const fn provide(providers: &mut Providers) {
702702
providers.lint_levels = lint_levels;
703703
}

compiler/rustc_lint/src/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@
3737
#![feature(never_type)]
3838
#![feature(nll)]
3939
#![feature(control_flow_enum)]
40+
#![feature(const_fn_fn_ptr_basics)]
41+
#![feature(const_mut_refs)]
4042
#![recursion_limit = "256"]
4143

4244
#[macro_use]
@@ -96,7 +98,7 @@ pub use rustc_session::lint::Level::{self, *};
9698
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
9799
pub use rustc_session::lint::{LintArray, LintPass};
98100

99-
pub fn provide(providers: &mut Providers) {
101+
pub const fn provide(providers: &mut Providers) {
100102
levels::provide(providers);
101103
*providers = Providers { lint_mod, ..*providers };
102104
}

compiler/rustc_metadata/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,8 @@
88
#![feature(min_specialization)]
99
#![feature(try_blocks)]
1010
#![feature(never_type)]
11+
#![feature(const_fn_fn_ptr_basics)]
12+
#![feature(const_mut_refs)]
1113
#![recursion_limit = "256"]
1214

1315
extern crate proc_macro;

0 commit comments

Comments
 (0)