Skip to content

Commit f0141eb

Browse files
committed
Compute query::Providers almost entirely at compile-time.
1 parent 346aec9 commit f0141eb

Some content is hidden

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

77 files changed

+160
-110
lines changed

src/librustc_codegen_llvm/attributes.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -342,7 +342,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
342342
}
343343
}
344344

345-
pub fn provide(providers: &mut Providers) {
345+
pub const fn provide(providers: &mut Providers) {
346346
providers.supported_target_features = |tcx, cnum| {
347347
assert_eq!(cnum, LOCAL_CRATE);
348348
if tcx.sess.opts.actually_rustdoc {
@@ -360,7 +360,7 @@ pub fn provide(providers: &mut Providers) {
360360
provide_extern(providers);
361361
}
362362

363-
pub fn provide_extern(providers: &mut Providers) {
363+
pub const fn provide_extern(providers: &mut Providers) {
364364
providers.wasm_import_module_map = |tcx, cnum| {
365365
// Build up a map from DefId to a `NativeLib` structure, where
366366
// `NativeLib` internally contains information about

src/librustc_codegen_llvm/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,8 @@
77
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
88
#![feature(bool_to_option)]
99
#![feature(const_cstr_unchecked)]
10+
#![feature(const_fn)]
11+
#![feature(const_mut_refs)]
1012
#![feature(crate_visibility_modifier)]
1113
#![feature(extern_types)]
1214
#![feature(in_band_lifetimes)]

src/librustc_codegen_ssa/back/symbol_export.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -366,7 +366,7 @@ fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> b
366366
}
367367
}
368368

369-
pub fn provide(providers: &mut Providers) {
369+
pub const fn provide(providers: &mut Providers) {
370370
providers.reachable_non_generics = reachable_non_generics_provider;
371371
providers.is_reachable_non_generic = is_reachable_non_generic_provider_local;
372372
providers.exported_symbols = exported_symbols_provider_local;
@@ -375,7 +375,7 @@ pub fn provide(providers: &mut Providers) {
375375
providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
376376
}
377377

378-
pub fn provide_extern(providers: &mut Providers) {
378+
pub const fn provide_extern(providers: &mut Providers) {
379379
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
380380
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
381381
}

src/librustc_codegen_ssa/base.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -852,7 +852,7 @@ impl CrateInfo {
852852
}
853853
}
854854

855-
pub fn provide_both(providers: &mut Providers) {
855+
pub const fn provide_both(providers: &mut Providers) {
856856
providers.backend_optimization_level = |tcx, cratenum| {
857857
let for_speed = match tcx.sess.opts.optimize {
858858
// If globally no optimisation is done, #[optimize] has no effect.

src/librustc_codegen_ssa/lib.rs

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22
#![feature(bool_to_option)]
33
#![feature(box_patterns)]
4+
#![feature(const_fn)]
5+
#![feature(const_mut_refs)]
46
#![feature(try_blocks)]
57
#![feature(in_band_lifetimes)]
68
#![feature(nll)]
@@ -138,12 +140,12 @@ pub struct CodegenResults {
138140
pub crate_info: CrateInfo,
139141
}
140142

141-
pub fn provide(providers: &mut Providers) {
143+
pub const fn provide(providers: &mut Providers) {
142144
crate::back::symbol_export::provide(providers);
143145
crate::base::provide_both(providers);
144146
}
145147

146-
pub fn provide_extern(providers: &mut Providers) {
148+
pub const fn provide_extern(providers: &mut Providers) {
147149
crate::back::symbol_export::provide_extern(providers);
148150
crate::base::provide_both(providers);
149151
}

src/librustc_interface/lib.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![feature(bool_to_option)]
22
#![feature(box_syntax)]
3+
#![feature(const_fn)]
4+
#![feature(const_mut_refs)]
35
#![feature(set_stdio)]
46
#![feature(nll)]
57
#![feature(generator_trait)]
@@ -14,6 +16,7 @@ mod queries;
1416
pub mod util;
1517

1618
pub use interface::{run_compiler, Config};
19+
pub use passes::{DEFAULT_EXTERN_QUERY_PROVIDERS, DEFAULT_QUERY_PROVIDERS};
1720
pub use queries::Queries;
1821

1922
#[cfg(test)]

src/librustc_interface/passes.rs

Lines changed: 46 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ use rustc_middle::arena::Arena;
1919
use rustc_middle::dep_graph::DepGraph;
2020
use rustc_middle::middle;
2121
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
22+
use rustc_middle::ty::query::Providers;
2223
use rustc_middle::ty::steal::Steal;
2324
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
2425
use rustc_mir as mir;
@@ -719,31 +720,49 @@ pub fn prepare_outputs(
719720
Ok(outputs)
720721
}
721722

722-
pub fn default_provide(providers: &mut ty::query::Providers) {
723-
providers.analysis = analysis;
724-
proc_macro_decls::provide(providers);
725-
plugin::build::provide(providers);
726-
rustc_middle::hir::provide(providers);
727-
mir::provide(providers);
728-
mir_build::provide(providers);
729-
rustc_privacy::provide(providers);
730-
typeck::provide(providers);
731-
ty::provide(providers);
732-
traits::provide(providers);
733-
rustc_passes::provide(providers);
734-
rustc_resolve::provide(providers);
735-
rustc_traits::provide(providers);
736-
rustc_ty::provide(providers);
737-
rustc_metadata::provide(providers);
738-
rustc_lint::provide(providers);
739-
rustc_symbol_mangling::provide(providers);
740-
rustc_codegen_ssa::provide(providers);
741-
}
723+
pub const DEFAULT_QUERY_PROVIDERS: Providers = {
724+
// FIXME(eddyb) the `const fn` shouldn't be needed, but mutable references
725+
// were disallowed in constants even when not escaping as `&'static mut T`.
726+
const fn default_providers() -> Providers {
727+
let mut providers = &mut Providers::EMPTY;
728+
729+
providers.analysis = analysis;
730+
proc_macro_decls::provide(providers);
731+
plugin::build::provide(providers);
732+
rustc_middle::hir::provide(providers);
733+
mir::provide(providers);
734+
mir_build::provide(providers);
735+
rustc_privacy::provide(providers);
736+
typeck::provide(providers);
737+
ty::provide(providers);
738+
traits::provide(providers);
739+
rustc_passes::provide(providers);
740+
rustc_resolve::provide(providers);
741+
rustc_traits::provide(providers);
742+
rustc_ty::provide(providers);
743+
rustc_metadata::provide(providers);
744+
rustc_lint::provide(providers);
745+
rustc_symbol_mangling::provide(providers);
746+
rustc_codegen_ssa::provide(providers);
747+
748+
*providers
749+
}
750+
default_providers()
751+
};
742752

743-
pub fn default_provide_extern(providers: &mut ty::query::Providers) {
744-
rustc_metadata::provide_extern(providers);
745-
rustc_codegen_ssa::provide_extern(providers);
746-
}
753+
pub const DEFAULT_EXTERN_QUERY_PROVIDERS: Providers = {
754+
// FIXME(eddyb) the `const fn` shouldn't be needed, but mutable references
755+
// were disallowed in constants even when not escaping as `&'static mut T`.
756+
const fn default_extern_providers() -> Providers {
757+
let providers = &mut DEFAULT_QUERY_PROVIDERS;
758+
759+
rustc_metadata::provide_extern(providers);
760+
rustc_codegen_ssa::provide_extern(providers);
761+
762+
*providers
763+
}
764+
default_extern_providers()
765+
};
747766

748767
pub struct QueryContext<'tcx>(&'tcx GlobalCtxt<'tcx>);
749768

@@ -780,12 +799,11 @@ pub fn create_global_ctxt<'tcx>(
780799
let query_result_on_disk_cache = rustc_incremental::load_query_result_cache(sess);
781800

782801
let codegen_backend = compiler.codegen_backend();
783-
let mut local_providers = ty::query::Providers::default();
784-
default_provide(&mut local_providers);
802+
let mut local_providers = DEFAULT_QUERY_PROVIDERS;
785803
codegen_backend.provide(&mut local_providers);
786804

787-
let mut extern_providers = local_providers;
788-
default_provide_extern(&mut extern_providers);
805+
let mut extern_providers = DEFAULT_EXTERN_QUERY_PROVIDERS;
806+
codegen_backend.provide(&mut extern_providers);
789807
codegen_backend.provide_extern(&mut extern_providers);
790808

791809
if let Some(callback) = compiler.override_queries {

src/librustc_interface/proc_macro_decls.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,6 @@ impl<'v> ItemLikeVisitor<'v> for Finder {
3535
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
3636
}
3737

38-
pub(crate) fn provide(providers: &mut Providers) {
38+
pub(crate) const fn provide(providers: &mut Providers) {
3939
*providers = Providers { proc_macro_decls_static, ..*providers };
4040
}

src/librustc_lint/levels.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -571,6 +571,6 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
571571
}
572572
}
573573

574-
pub fn provide(providers: &mut Providers) {
574+
pub const fn provide(providers: &mut Providers) {
575575
providers.lint_levels = lint_levels;
576576
}

src/librustc_lint/lib.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@
2929
#![cfg_attr(test, feature(test))]
3030
#![feature(bool_to_option)]
3131
#![feature(box_syntax)]
32+
#![feature(const_fn)]
33+
#![feature(const_mut_refs)]
3234
#![feature(crate_visibility_modifier)]
3335
#![feature(iter_order_by)]
3436
#![feature(never_type)]
@@ -88,7 +90,7 @@ pub use rustc_session::lint::Level::{self, *};
8890
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
8991
pub use rustc_session::lint::{LintArray, LintPass};
9092

91-
pub fn provide(providers: &mut Providers) {
93+
pub const fn provide(providers: &mut Providers) {
9294
levels::provide(providers);
9395
*providers = Providers { lint_mod, ..*providers };
9496
}

src/librustc_metadata/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,7 @@
11
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
22
#![feature(bool_to_option)]
3+
#![feature(const_fn)]
4+
#![feature(const_mut_refs)]
35
#![feature(core_intrinsics)]
46
#![feature(crate_visibility_modifier)]
57
#![feature(drain_filter)]

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

Lines changed: 2 additions & 2 deletions
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>,
@@ -240,7 +240,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
240240
crate_extern_paths => { cdata.source().paths().cloned().collect() }
241241
}
242242

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

src/librustc_middle/hir/map/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1067,6 +1067,6 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
10671067
}
10681068
}
10691069

1070-
pub fn provide(providers: &mut Providers) {
1070+
pub const fn provide(providers: &mut Providers) {
10711071
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
10721072
}

src/librustc_middle/hir/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<'tcx> TyCtxt<'tcx> {
6262
}
6363
}
6464

65-
pub fn provide(providers: &mut Providers) {
65+
pub const fn provide(providers: &mut Providers) {
6666
providers.parent_module_from_def_id = |tcx, id| {
6767
let hir = tcx.hir();
6868
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id)))

src/librustc_middle/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@
2929
#![feature(box_syntax)]
3030
#![cfg_attr(bootstrap, feature(const_if_match))]
3131
#![feature(const_fn)]
32+
#![feature(const_mut_refs)]
3233
#![feature(const_panic)]
3334
#![feature(const_transmute)]
3435
#![feature(core_intrinsics)]

src/librustc_middle/ty/context.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2699,7 +2699,7 @@ fn ptr_eq<T, U>(t: *const T, u: *const U) -> bool {
26992699
t as *const () == u as *const ()
27002700
}
27012701

2702-
pub fn provide(providers: &mut ty::query::Providers) {
2702+
pub const fn provide(providers: &mut ty::query::Providers) {
27032703
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id);
27042704
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
27052705
providers.crate_name = |tcx, id| {

src/librustc_middle/ty/erase_regions.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use crate::ty::fold::{TypeFoldable, TypeFolder};
22
use crate::ty::{self, Ty, TyCtxt, TypeFlags};
33

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

src/librustc_middle/ty/layout.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -210,7 +210,7 @@ fn layout_raw<'tcx>(
210210
})
211211
}
212212

213-
pub fn provide(providers: &mut ty::query::Providers) {
213+
pub const fn provide(providers: &mut ty::query::Providers) {
214214
*providers = ty::query::Providers { layout_raw, ..*providers };
215215
}
216216

src/librustc_middle/ty/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2966,7 +2966,7 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
29662966
None
29672967
}
29682968

2969-
pub fn provide(providers: &mut ty::query::Providers) {
2969+
pub const fn provide(providers: &mut ty::query::Providers) {
29702970
context::provide(providers);
29712971
erase_regions::provide(providers);
29722972
layout::provide(providers);

src/librustc_middle/ty/query/plumbing.rs

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -496,11 +496,6 @@ macro_rules! define_queries_inner {
496496
tcx: $tcx,
497497
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
498498
}
499-
500-
impl Copy for Providers {}
501-
impl Clone for Providers {
502-
fn clone(&self) -> Self { *self }
503-
}
504499
}
505500
}
506501

@@ -565,14 +560,19 @@ macro_rules! define_provider_struct {
565560
$(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
566561
}
567562

568-
impl Default for Providers {
569-
fn default() -> Self {
563+
impl Copy for Providers {}
564+
impl Clone for Providers {
565+
fn clone(&self) -> Self { *self }
566+
}
567+
568+
impl Providers {
569+
pub const EMPTY: Self = {
570570
$(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
571571
bug!("`tcx.{}({:?})` unsupported by its crate",
572572
stringify!($name), key);
573573
})*
574574
Providers { $($name),* }
575-
}
575+
};
576576
}
577577
};
578578
}

src/librustc_middle/util/bug.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,6 +47,6 @@ pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
4747
);
4848
}
4949

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

src/librustc_mir/borrow_check/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ crate struct Upvar {
8686

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

89-
pub fn provide(providers: &mut Providers) {
89+
pub const fn provide(providers: &mut Providers) {
9090
*providers = Providers { mir_borrowck, ..*providers };
9191
}
9292

src/librustc_mir/const_eval/fn_queries.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ fn const_fn_is_allowed_fn_ptr(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
156156
&& tcx.lookup_const_stability(def_id).map(|stab| stab.allow_const_fn_ptr).unwrap_or(false)
157157
}
158158

159-
pub fn provide(providers: &mut Providers) {
159+
pub const fn provide(providers: &mut Providers) {
160160
*providers = Providers {
161161
is_const_fn_raw,
162162
is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),

src/librustc_mir/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ Rust MIR: a lowered representation of Rust.
1212
#![feature(const_fn)]
1313
#![cfg_attr(bootstrap, feature(const_if_match))]
1414
#![cfg_attr(bootstrap, feature(const_loop))]
15+
#![feature(const_mut_refs)]
1516
#![feature(const_panic)]
1617
#![feature(crate_visibility_modifier)]
1718
#![feature(decl_macro)]
@@ -47,7 +48,7 @@ pub mod util;
4748

4849
use rustc_middle::ty::query::Providers;
4950

50-
pub fn provide(providers: &mut Providers) {
51+
pub const fn provide(providers: &mut Providers) {
5152
borrow_check::provide(providers);
5253
const_eval::provide(providers);
5354
shim::provide(providers);

src/librustc_mir/monomorphize/partitioning.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -994,7 +994,7 @@ fn collect_and_partition_mono_items(
994994
(tcx.arena.alloc(mono_items), codegen_units)
995995
}
996996

997-
pub fn provide(providers: &mut Providers) {
997+
pub const fn provide(providers: &mut Providers) {
998998
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;
999999

10001000
providers.is_codegened_item = |tcx, def_id| {

0 commit comments

Comments
 (0)