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

+2-2
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

+1-1
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

+4-2
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

+1-1
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

+2
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

+27-28
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

+1-1
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

+1-1
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

+3-1
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

+2
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;

compiler/rustc_metadata/src/rmeta/decoder/cstore_impl.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -30,7 +30,7 @@ use std::any::Any;
3030
macro_rules! provide {
3131
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
3232
$($name:ident => $compute:block)*) => {
33-
pub fn provide_extern(providers: &mut Providers) {
33+
pub const fn provide_extern(providers: &mut Providers) {
3434
$(fn $name<$lt>(
3535
$tcx: TyCtxt<$lt>,
3636
def_id_arg: ty::query::query_keys::$name<$lt>,
@@ -235,7 +235,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
235235
expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) }
236236
}
237237

238-
pub fn provide(providers: &mut Providers) {
238+
pub const fn provide(providers: &mut Providers) {
239239
// FIXME(#44234) - almost all of these queries have no sub-queries and
240240
// therefore no actual inputs, they're just reading tables calculated in
241241
// resolve! Does this work? Unsure! That's what the issue is about

compiler/rustc_middle/src/hir/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ impl<'tcx> TyCtxt<'tcx> {
129129
}
130130
}
131131

132-
pub fn provide(providers: &mut Providers) {
132+
pub const fn provide(providers: &mut Providers) {
133133
providers.parent_module_from_def_id = |tcx, id| {
134134
let hir = tcx.hir();
135135
hir.local_def_id(hir.get_module_parent_node(hir.local_def_id_to_hir_id(id)))

compiler/rustc_middle/src/lib.rs

+2
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,8 @@
4949
#![feature(iter_zip)]
5050
#![feature(thread_local_const_init)]
5151
#![feature(try_reserve)]
52+
#![feature(const_fn_fn_ptr_basics)]
53+
#![feature(const_mut_refs)]
5254
#![recursion_limit = "512"]
5355

5456
#[macro_use]

compiler/rustc_middle/src/middle/limits.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@ use rustc_span::symbol::{sym, Symbol};
1818

1919
use std::num::IntErrorKind;
2020

21-
pub fn provide(providers: &mut ty::query::Providers) {
21+
pub const fn provide(providers: &mut ty::query::Providers) {
2222
providers.limits = |tcx, ()| Limits {
2323
recursion_limit: get_recursion_limit(tcx.hir().krate_attrs(), tcx.sess),
2424
move_size_limit: get_limit(tcx.hir().krate_attrs(), tcx.sess, sym::move_size_limit, 0),

compiler/rustc_middle/src/middle/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,6 @@ pub mod region;
3333
pub mod resolve_lifetime;
3434
pub mod stability;
3535

36-
pub fn provide(providers: &mut crate::ty::query::Providers) {
36+
pub const fn provide(providers: &mut crate::ty::query::Providers) {
3737
limits::provide(providers);
3838
}

compiler/rustc_middle/src/ty/context.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2819,7 +2819,7 @@ fn ptr_eq<T, U>(t: *const T, u: *const U) -> bool {
28192819
t as *const () == u as *const ()
28202820
}
28212821

2822-
pub fn provide(providers: &mut ty::query::Providers) {
2822+
pub const fn provide(providers: &mut ty::query::Providers) {
28232823
providers.in_scope_traits_map = |tcx, id| tcx.hir_crate(()).trait_map.get(&id);
28242824
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
28252825
providers.crate_name = |tcx, id| {

compiler/rustc_middle/src/ty/erase_regions.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use crate::mir;
22
use crate::ty::fold::{TypeFoldable, TypeFolder};
33
use crate::ty::{self, Ty, TyCtxt, TypeFlags};
44

5-
pub(super) fn provide(providers: &mut ty::query::Providers) {
5+
pub(super) const fn provide(providers: &mut ty::query::Providers) {
66
*providers = ty::query::Providers { erase_regions_ty, ..*providers };
77
}
88

compiler/rustc_middle/src/ty/layout.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,7 @@ fn layout_raw<'tcx>(
242242
})
243243
}
244244

245-
pub fn provide(providers: &mut ty::query::Providers) {
245+
pub const fn provide(providers: &mut ty::query::Providers) {
246246
*providers = ty::query::Providers { layout_raw, ..*providers };
247247
}
248248

compiler/rustc_middle/src/ty/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1978,7 +1978,7 @@ pub fn ast_uint_ty(uty: UintTy) -> ast::UintTy {
19781978
}
19791979
}
19801980

1981-
pub fn provide(providers: &mut ty::query::Providers) {
1981+
pub const fn provide(providers: &mut ty::query::Providers) {
19821982
context::provide(providers);
19831983
erase_regions::provide(providers);
19841984
layout::provide(providers);

compiler/rustc_middle/src/ty/print/pretty.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -2348,6 +2348,6 @@ fn trimmed_def_paths(tcx: TyCtxt<'_>, (): ()) -> FxHashMap<DefId, Symbol> {
23482348
map
23492349
}
23502350

2351-
pub fn provide(providers: &mut ty::query::Providers) {
2351+
pub const fn provide(providers: &mut ty::query::Providers) {
23522352
*providers = ty::query::Providers { trimmed_def_paths, ..*providers };
23532353
}

compiler/rustc_middle/src/ty/query/mod.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -215,8 +215,11 @@ macro_rules! define_callbacks {
215215
) -> query_values::$name<'tcx>,)*
216216
}
217217

218-
impl Default for Providers {
219-
fn default() -> Self {
218+
219+
// FIXME: Make this an `impl const Default for Providers`
220+
// when `const_trait_impl` is no longer incomplete
221+
impl Providers {
222+
pub const fn default() -> Self {
220223
Providers {
221224
$($name: |_, key| bug!(
222225
"`tcx.{}({:?})` unsupported by its crate; \

compiler/rustc_middle/src/ty/util.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1084,6 +1084,6 @@ pub fn normalize_opaque_types(
10841084
val.fold_with(&mut visitor)
10851085
}
10861086

1087-
pub fn provide(providers: &mut ty::query::Providers) {
1087+
pub const fn provide(providers: &mut ty::query::Providers) {
10881088
*providers = ty::query::Providers { normalize_opaque_types, ..*providers }
10891089
}

compiler/rustc_middle/src/util/bug.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,6 @@ pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
4848
);
4949
}
5050

51-
pub fn provide(providers: &mut crate::ty::query::Providers) {
51+
pub const fn provide(providers: &mut crate::ty::query::Providers) {
5252
*providers = crate::ty::query::Providers { trigger_delay_span_bug, ..*providers };
5353
}

compiler/rustc_mir/src/borrow_check/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ crate struct Upvar<'tcx> {
8282

8383
const DEREF_PROJECTION: &[PlaceElem<'_>; 1] = &[ProjectionElem::Deref];
8484

85-
pub fn provide(providers: &mut Providers) {
85+
pub const fn provide(providers: &mut Providers) {
8686
*providers = Providers {
8787
mir_borrowck: |tcx, did| {
8888
if let Some(def) = ty::WithOptConstParam::try_lookup(did, tcx) {

compiler/rustc_mir/src/const_eval/fn_queries.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,7 @@ fn is_promotable_const_fn(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
102102
}
103103
}
104104

105-
pub fn provide(providers: &mut Providers) {
105+
pub const fn provide(providers: &mut Providers) {
106106
*providers = Providers {
107107
is_const_fn_raw,
108108
is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),

compiler/rustc_mir/src/lib.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,8 @@ Rust MIR: a lowered representation of Rust.
3030
#![feature(once_cell)]
3131
#![feature(control_flow_enum)]
3232
#![feature(try_reserve)]
33+
#![feature(const_fn_fn_ptr_basics)]
34+
#![feature(const_mut_refs)]
3335
#![recursion_limit = "256"]
3436

3537
#[macro_use]
@@ -48,7 +50,7 @@ pub mod util;
4850

4951
use rustc_middle::ty::query::Providers;
5052

51-
pub fn provide(providers: &mut Providers) {
53+
pub const fn provide(providers: &mut Providers) {
5254
borrow_check::provide(providers);
5355
const_eval::provide(providers);
5456
shim::provide(providers);

compiler/rustc_mir/src/monomorphize/partitioning/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -448,7 +448,7 @@ fn codegened_and_inlined_items<'tcx>(tcx: TyCtxt<'tcx>, (): ()) -> &'tcx DefIdSe
448448
tcx.arena.alloc(result)
449449
}
450450

451-
pub fn provide(providers: &mut Providers) {
451+
pub const fn provide(providers: &mut Providers) {
452452
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
453453
providers.codegened_and_inlined_items = codegened_and_inlined_items;
454454

compiler/rustc_mir/src/monomorphize/polymorphize.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::convert::TryInto;
2323
use std::ops::ControlFlow;
2424

2525
/// Provide implementations of queries relating to polymorphization analysis.
26-
pub fn provide(providers: &mut Providers) {
26+
pub const fn provide(providers: &mut Providers) {
2727
providers.unused_generic_params = unused_generic_params;
2828
}
2929

compiler/rustc_mir/src/shim.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use crate::util::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle
2323
use crate::util::expand_aggregate;
2424
use crate::util::patch::MirPatch;
2525

26-
pub fn provide(providers: &mut Providers) {
26+
pub const fn provide(providers: &mut Providers) {
2727
providers.mir_shims = make_shim;
2828
}
2929

compiler/rustc_mir/src/transform/check_packed_ref.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_span::symbol::sym;
99
use crate::transform::MirPass;
1010
use crate::util;
1111

12-
pub(crate) fn provide(providers: &mut Providers) {
12+
pub(crate) const fn provide(providers: &mut Providers) {
1313
*providers = Providers { unsafe_derive_on_repr_packed, ..*providers };
1414
}
1515

0 commit comments

Comments
 (0)