Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compute query::Providers almost entirely at compile-time. #74283

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/attributes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -342,7 +342,7 @@ pub fn from_fn_attrs(cx: &CodegenCx<'ll, 'tcx>, llfn: &'ll Value, instance: ty::
}
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
providers.supported_target_features = |tcx, cnum| {
assert_eq!(cnum, LOCAL_CRATE);
if tcx.sess.opts.actually_rustdoc {
Expand All @@ -360,7 +360,7 @@ pub fn provide(providers: &mut Providers) {
provide_extern(providers);
}

pub fn provide_extern(providers: &mut Providers) {
pub const fn provide_extern(providers: &mut Providers) {
providers.wasm_import_module_map = |tcx, cnum| {
// Build up a map from DefId to a `NativeLib` structure, where
// `NativeLib` internally contains information about
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(bool_to_option)]
#![feature(const_cstr_unchecked)]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(crate_visibility_modifier)]
#![feature(extern_types)]
#![feature(in_band_lifetimes)]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -366,7 +366,7 @@ fn is_unreachable_local_definition_provider(tcx: TyCtxt<'_>, def_id: DefId) -> b
}
}

pub fn provide(providers: &mut Providers) {
pub const 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_symbols = exported_symbols_provider_local;
Expand All @@ -375,7 +375,7 @@ pub fn provide(providers: &mut Providers) {
providers.upstream_drop_glue_for = upstream_drop_glue_for_provider;
}

pub fn provide_extern(providers: &mut Providers) {
pub const fn provide_extern(providers: &mut Providers) {
providers.is_reachable_non_generic = is_reachable_non_generic_provider_extern;
providers.upstream_monomorphizations_for = upstream_monomorphizations_for_provider;
}
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_codegen_ssa/base.rs
Original file line number Diff line number Diff line change
Expand Up @@ -852,7 +852,7 @@ impl CrateInfo {
}
}

pub fn provide_both(providers: &mut Providers) {
pub const fn provide_both(providers: &mut Providers) {
providers.backend_optimization_level = |tcx, cratenum| {
let for_speed = match tcx.sess.opts.optimize {
// If globally no optimisation is done, #[optimize] has no effect.
Expand Down
6 changes: 4 additions & 2 deletions src/librustc_codegen_ssa/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(bool_to_option)]
#![feature(box_patterns)]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(try_blocks)]
#![feature(in_band_lifetimes)]
#![feature(nll)]
Expand Down Expand Up @@ -138,12 +140,12 @@ pub struct CodegenResults {
pub crate_info: CrateInfo,
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
crate::back::symbol_export::provide(providers);
crate::base::provide_both(providers);
}

pub fn provide_extern(providers: &mut Providers) {
pub const fn provide_extern(providers: &mut Providers) {
crate::back::symbol_export::provide_extern(providers);
crate::base::provide_both(providers);
}
Expand Down
3 changes: 3 additions & 0 deletions src/librustc_interface/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![feature(bool_to_option)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(set_stdio)]
#![feature(nll)]
#![feature(generator_trait)]
Expand All @@ -14,6 +16,7 @@ mod queries;
pub mod util;

pub use interface::{run_compiler, Config};
pub use passes::{DEFAULT_EXTERN_QUERY_PROVIDERS, DEFAULT_QUERY_PROVIDERS};
pub use queries::Queries;

#[cfg(test)]
Expand Down
74 changes: 46 additions & 28 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ use rustc_middle::arena::Arena;
use rustc_middle::dep_graph::DepGraph;
use rustc_middle::middle;
use rustc_middle::middle::cstore::{CrateStore, MetadataLoader, MetadataLoaderDyn};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::steal::Steal;
use rustc_middle::ty::{self, GlobalCtxt, ResolverOutputs, TyCtxt};
use rustc_mir as mir;
Expand Down Expand Up @@ -719,31 +720,49 @@ pub fn prepare_outputs(
Ok(outputs)
}

pub fn default_provide(providers: &mut ty::query::Providers) {
providers.analysis = analysis;
proc_macro_decls::provide(providers);
plugin::build::provide(providers);
rustc_middle::hir::provide(providers);
mir::provide(providers);
mir_build::provide(providers);
rustc_privacy::provide(providers);
typeck::provide(providers);
ty::provide(providers);
traits::provide(providers);
rustc_passes::provide(providers);
rustc_resolve::provide(providers);
rustc_traits::provide(providers);
rustc_ty::provide(providers);
rustc_metadata::provide(providers);
rustc_lint::provide(providers);
rustc_symbol_mangling::provide(providers);
rustc_codegen_ssa::provide(providers);
}
pub const DEFAULT_QUERY_PROVIDERS: Providers = {
// FIXME(eddyb) the `const fn` shouldn't be needed, but mutable references
Copy link
Member

@RalfJung RalfJung Jul 13, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This reads as if it should be a fn instead, but I think what you actually mean is that it should be directly inlined in DEFAULT_QUERY_PROVIDERS instead?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed, maybe I should say "separate function" or something like that.

// were disallowed in constants even when not escaping as `&'static mut T`.
const fn default_providers() -> Providers {
let mut providers = &mut Providers::EMPTY;

providers.analysis = analysis;
proc_macro_decls::provide(providers);
plugin::build::provide(providers);
rustc_middle::hir::provide(providers);
mir::provide(providers);
mir_build::provide(providers);
rustc_privacy::provide(providers);
typeck::provide(providers);
ty::provide(providers);
traits::provide(providers);
rustc_passes::provide(providers);
rustc_resolve::provide(providers);
rustc_traits::provide(providers);
rustc_ty::provide(providers);
rustc_metadata::provide(providers);
rustc_lint::provide(providers);
rustc_symbol_mangling::provide(providers);
rustc_codegen_ssa::provide(providers);

*providers
}
default_providers()
};

pub fn default_provide_extern(providers: &mut ty::query::Providers) {
rustc_metadata::provide_extern(providers);
rustc_codegen_ssa::provide_extern(providers);
}
pub const DEFAULT_EXTERN_QUERY_PROVIDERS: Providers = {
// FIXME(eddyb) the `const fn` shouldn't be needed, but mutable references
// were disallowed in constants even when not escaping as `&'static mut T`.
const fn default_extern_providers() -> Providers {
let providers = &mut DEFAULT_QUERY_PROVIDERS;

rustc_metadata::provide_extern(providers);
rustc_codegen_ssa::provide_extern(providers);

*providers
}
default_extern_providers()
};

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

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

let codegen_backend = compiler.codegen_backend();
let mut local_providers = ty::query::Providers::default();
default_provide(&mut local_providers);
let mut local_providers = DEFAULT_QUERY_PROVIDERS;
codegen_backend.provide(&mut local_providers);

let mut extern_providers = local_providers;
default_provide_extern(&mut extern_providers);
let mut extern_providers = DEFAULT_EXTERN_QUERY_PROVIDERS;
codegen_backend.provide(&mut extern_providers);
codegen_backend.provide_extern(&mut extern_providers);

if let Some(callback) = compiler.override_queries {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_interface/proc_macro_decls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,6 @@ impl<'v> ItemLikeVisitor<'v> for Finder {
fn visit_impl_item(&mut self, _impl_item: &hir::ImplItem<'_>) {}
}

pub(crate) fn provide(providers: &mut Providers) {
pub(crate) const fn provide(providers: &mut Providers) {
*providers = Providers { proc_macro_decls_static, ..*providers };
}
2 changes: 1 addition & 1 deletion src/librustc_lint/levels.rs
Original file line number Diff line number Diff line change
Expand Up @@ -571,6 +571,6 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelMapBuilder<'_, 'tcx> {
}
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
providers.lint_levels = lint_levels;
}
4 changes: 3 additions & 1 deletion src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@
#![cfg_attr(test, feature(test))]
#![feature(bool_to_option)]
#![feature(box_syntax)]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(crate_visibility_modifier)]
#![feature(iter_order_by)]
#![feature(never_type)]
Expand Down Expand Up @@ -88,7 +90,7 @@ pub use rustc_session::lint::Level::{self, *};
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
pub use rustc_session::lint::{LintArray, LintPass};

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
levels::provide(providers);
*providers = Providers { lint_mod, ..*providers };
}
Expand Down
2 changes: 2 additions & 0 deletions src/librustc_metadata/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#![doc(html_root_url = "https://doc.rust-lang.org/nightly/")]
#![feature(bool_to_option)]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(core_intrinsics)]
#![feature(crate_visibility_modifier)]
#![feature(drain_filter)]
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_metadata/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ use std::any::Any;
macro_rules! provide {
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
$($name:ident => $compute:block)*) => {
pub fn provide_extern(providers: &mut Providers) {
pub const fn provide_extern(providers: &mut Providers) {
$(fn $name<$lt>(
$tcx: TyCtxt<$lt>,
def_id_arg: ty::query::query_keys::$name<$lt>,
Expand Down Expand Up @@ -240,7 +240,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
crate_extern_paths => { cdata.source().paths().cloned().collect() }
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
// FIXME(#44234) - almost all of these queries have no sub-queries and
// therefore no actual inputs, they're just reading tables calculated in
// resolve! Does this work? Unsure! That's what the issue is about
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1067,6 +1067,6 @@ fn hir_id_to_string(map: &Map<'_>, id: HirId) -> String {
}
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
providers.def_kind = |tcx, def_id| tcx.hir().def_kind(def_id.expect_local());
}
2 changes: 1 addition & 1 deletion src/librustc_middle/hir/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ impl<'tcx> TyCtxt<'tcx> {
}
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
providers.parent_module_from_def_id = |tcx, id| {
let hir = tcx.hir();
hir.local_def_id(hir.get_module_parent_node(hir.as_local_hir_id(id)))
Expand Down
1 change: 1 addition & 0 deletions src/librustc_middle/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
#![feature(box_syntax)]
#![cfg_attr(bootstrap, feature(const_if_match))]
#![feature(const_fn)]
#![feature(const_mut_refs)]
#![feature(const_panic)]
#![feature(const_transmute)]
#![feature(core_intrinsics)]
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2699,7 +2699,7 @@ fn ptr_eq<T, U>(t: *const T, u: *const U) -> bool {
t as *const () == u as *const ()
}

pub fn provide(providers: &mut ty::query::Providers) {
pub const fn provide(providers: &mut ty::query::Providers) {
providers.in_scope_traits_map = |tcx, id| tcx.gcx.trait_map.get(&id);
providers.module_exports = |tcx, id| tcx.gcx.export_map.get(&id).map(|v| &v[..]);
providers.crate_name = |tcx, id| {
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/erase_regions.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::ty::fold::{TypeFoldable, TypeFolder};
use crate::ty::{self, Ty, TyCtxt, TypeFlags};

pub(super) fn provide(providers: &mut ty::query::Providers) {
pub(super) const fn provide(providers: &mut ty::query::Providers) {
*providers = ty::query::Providers { erase_regions_ty, ..*providers };
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/layout.rs
Original file line number Diff line number Diff line change
Expand Up @@ -210,7 +210,7 @@ fn layout_raw<'tcx>(
})
}

pub fn provide(providers: &mut ty::query::Providers) {
pub const fn provide(providers: &mut ty::query::Providers) {
*providers = ty::query::Providers { layout_raw, ..*providers };
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_middle/ty/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2966,7 +2966,7 @@ pub fn is_impl_trait_defn(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> {
None
}

pub fn provide(providers: &mut ty::query::Providers) {
pub const fn provide(providers: &mut ty::query::Providers) {
context::provide(providers);
erase_regions::provide(providers);
layout::provide(providers);
Expand Down
16 changes: 8 additions & 8 deletions src/librustc_middle/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,11 +496,6 @@ macro_rules! define_queries_inner {
tcx: $tcx,
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
}

impl Copy for Providers {}
impl Clone for Providers {
fn clone(&self) -> Self { *self }
}
}
}

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

impl Default for Providers {
fn default() -> Self {
impl Copy for Providers {}
impl Clone for Providers {
fn clone(&self) -> Self { *self }
}

impl Providers {
pub const EMPTY: Self = {
$(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
bug!("`tcx.{}({:?})` unsupported by its crate",
stringify!($name), key);
})*
Providers { $($name),* }
}
};
}
};
}
2 changes: 1 addition & 1 deletion src/librustc_middle/util/bug.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,6 @@ pub fn trigger_delay_span_bug(tcx: TyCtxt<'_>, key: rustc_hir::def_id::DefId) {
);
}

pub fn provide(providers: &mut crate::ty::query::Providers) {
pub const fn provide(providers: &mut crate::ty::query::Providers) {
*providers = crate::ty::query::Providers { trigger_delay_span_bug, ..*providers };
}
2 changes: 1 addition & 1 deletion src/librustc_mir/borrow_check/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ crate struct Upvar {

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

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
*providers = Providers { mir_borrowck, ..*providers };
}

Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/const_eval/fn_queries.rs
Original file line number Diff line number Diff line change
Expand Up @@ -156,7 +156,7 @@ fn const_fn_is_allowed_fn_ptr(tcx: TyCtxt<'_>, def_id: DefId) -> bool {
&& tcx.lookup_const_stability(def_id).map(|stab| stab.allow_const_fn_ptr).unwrap_or(false)
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
*providers = Providers {
is_const_fn_raw,
is_const_impl_raw: |tcx, def_id| is_const_impl_raw(tcx, def_id.expect_local()),
Expand Down
3 changes: 2 additions & 1 deletion src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ Rust MIR: a lowered representation of Rust.
#![feature(const_fn)]
#![cfg_attr(bootstrap, feature(const_if_match))]
#![cfg_attr(bootstrap, feature(const_loop))]
#![feature(const_mut_refs)]
#![feature(const_panic)]
#![feature(crate_visibility_modifier)]
#![feature(decl_macro)]
Expand Down Expand Up @@ -47,7 +48,7 @@ pub mod util;

use rustc_middle::ty::query::Providers;

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
borrow_check::provide(providers);
const_eval::provide(providers);
shim::provide(providers);
Expand Down
2 changes: 1 addition & 1 deletion src/librustc_mir/monomorphize/partitioning.rs
Original file line number Diff line number Diff line change
Expand Up @@ -994,7 +994,7 @@ fn collect_and_partition_mono_items(
(tcx.arena.alloc(mono_items), codegen_units)
}

pub fn provide(providers: &mut Providers) {
pub const fn provide(providers: &mut Providers) {
providers.collect_and_partition_mono_items = collect_and_partition_mono_items;

providers.is_codegened_item = |tcx, def_id| {
Expand Down
Loading