Skip to content

Commit

Permalink
Rollup merge of rust-lang#74070 - eddyb:forall-tcx-providers, r=nikom…
Browse files Browse the repository at this point in the history
…atsakis

 Use for<'tcx> fn pointers in Providers, instead of having Providers<'tcx>.

In order to work around normalization-under-HRTB (for `provide!` in `rustc_metadata`), we ended up with this:
```rust
struct Providers<'tcx> {
    type_of: fn(TyCtxt<'tcx>, DefId) -> Ty<'tcx>,
    // ...
}
```
But what I initially wanted to do, IIRC, was this:
```rust
struct Providers {
    type_of: for<'tcx> fn(TyCtxt<'tcx>, DefId) -> Ty<'tcx>,
    // ...
}
```

This PR moves to the latter, for the simple reason that only the latter allows keeping a `Providers` value, or a subset of its `fn` pointer fields, around in a `static` or `thread_local!`, which can be really useful for custom drivers that override queries.
(@jyn514 and I came across a concrete usecase of that in `rustdoc`)

The `provide!` macro in `rustc_metadata` is fixed by making the query key/value types available as type aliases under `ty::query::query_{keys,values}`, not just associated types (this is the first commit).

r? @nikomatsakis
  • Loading branch information
Manishearth committed Jul 9, 2020
2 parents e7cdc83 + f07100a commit c2052bb
Show file tree
Hide file tree
Showing 74 changed files with 123 additions and 104 deletions.
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 fn provide(providers: &mut Providers) {
providers.target_features_whitelist = |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 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
4 changes: 2 additions & 2 deletions src/librustc_codegen_llvm/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,11 +241,11 @@ impl CodegenBackend for LlvmCodegenBackend {
Box::new(metadata::LlvmMetadataLoader)
}

fn provide(&self, providers: &mut ty::query::Providers<'_>) {
fn provide(&self, providers: &mut ty::query::Providers) {
attributes::provide(providers);
}

fn provide_extern(&self, providers: &mut ty::query::Providers<'_>) {
fn provide_extern(&self, providers: &mut ty::query::Providers) {
attributes::provide_extern(providers);
}

Expand Down
8 changes: 4 additions & 4 deletions src/librustc_codegen_ssa/back/symbol_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -161,9 +161,9 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
}

fn exported_symbols_provider_local(
tcx: TyCtxt<'_>,
tcx: TyCtxt<'tcx>,
cnum: CrateNum,
) -> &'tcx [(ExportedSymbol<'_>, SymbolExportLevel)] {
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
assert_eq!(cnum, LOCAL_CRATE);

if !tcx.sess.opts.output_types.should_codegen() {
Expand Down 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 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 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 @@ -853,7 +853,7 @@ impl CrateInfo {
}
}

pub fn provide_both(providers: &mut Providers<'_>) {
pub 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
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -138,12 +138,12 @@ pub struct CodegenResults {
pub crate_info: CrateInfo,
}

pub fn provide(providers: &mut Providers<'_>) {
pub fn provide(providers: &mut Providers) {
crate::back::symbol_export::provide(providers);
crate::base::provide_both(providers);
}

pub fn provide_extern(providers: &mut Providers<'_>) {
pub fn provide_extern(providers: &mut Providers) {
crate::back::symbol_export::provide_extern(providers);
crate::base::provide_both(providers);
}
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_codegen_ssa/traits/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,8 @@ pub trait CodegenBackend {
fn print_version(&self) {}

fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
fn provide(&self, _providers: &mut Providers<'_>);
fn provide_extern(&self, _providers: &mut Providers<'_>);
fn provide(&self, _providers: &mut Providers);
fn provide_extern(&self, _providers: &mut Providers);
fn codegen_crate<'tcx>(
&self,
tcx: TyCtxt<'tcx>,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_interface/interface.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ pub struct Compiler {
pub(crate) crate_name: Option<String>,
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
pub(crate) override_queries:
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
}

impl Compiler {
Expand Down Expand Up @@ -153,7 +153,7 @@ pub struct Config {
///
/// The second parameter is local providers and the third parameter is external providers.
pub override_queries:
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,

/// Registry of diagnostics codes.
pub registry: Registry,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_interface/passes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ pub fn prepare_outputs(
Ok(outputs)
}

pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
pub fn default_provide(providers: &mut ty::query::Providers) {
providers.analysis = analysis;
proc_macro_decls::provide(providers);
plugin::build::provide(providers);
Expand All @@ -740,7 +740,7 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
rustc_codegen_ssa::provide(providers);
}

pub fn default_provide_extern(providers: &mut ty::query::Providers<'_>) {
pub fn default_provide_extern(providers: &mut ty::query::Providers) {
rustc_metadata::provide_extern(providers);
rustc_codegen_ssa::provide_extern(providers);
}
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) 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 fn provide(providers: &mut Providers) {
providers.lint_levels = lint_levels;
}
2 changes: 1 addition & 1 deletion src/librustc_lint/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,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 fn provide(providers: &mut Providers) {
levels::provide(providers);
*providers = Providers { lint_mod, ..*providers };
}
Expand Down
13 changes: 5 additions & 8 deletions src/librustc_metadata/rmeta/decoder/cstore_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ use rustc_middle::middle::cstore::{CrateSource, CrateStore, EncodedMetadata};
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::query::QueryConfig;
use rustc_middle::ty::{self, TyCtxt};
use rustc_session::utils::NativeLibKind;
use rustc_session::{CrateDisambiguator, Session};
Expand All @@ -31,13 +30,11 @@ 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<$lt>(providers: &mut Providers<$lt>) {
// HACK(eddyb) `$lt: $lt` forces `$lt` to be early-bound, which
// allows the associated type in the return type to be normalized.
$(fn $name<$lt: $lt, T: IntoArgs>(
pub fn provide_extern(providers: &mut Providers) {
$(fn $name<$lt>(
$tcx: TyCtxt<$lt>,
def_id_arg: T,
) -> <ty::queries::$name<$lt> as QueryConfig<TyCtxt<$lt>>>::Value {
def_id_arg: ty::query::query_keys::$name<$lt>,
) -> ty::query::query_values::$name<$lt> {
let _prof_timer =
$tcx.prof.generic_activity("metadata_decode_entry");

Expand Down Expand Up @@ -243,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 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 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 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
6 changes: 3 additions & 3 deletions src/librustc_middle/ty/context.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1060,8 +1060,8 @@ impl<'tcx> TyCtxt<'tcx> {
pub fn create_global_ctxt(
s: &'tcx Session,
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
local_providers: ty::query::Providers<'tcx>,
extern_providers: ty::query::Providers<'tcx>,
local_providers: ty::query::Providers,
extern_providers: ty::query::Providers,
arena: &'tcx WorkerLocal<Arena<'tcx>>,
resolutions: ty::ResolverOutputs,
krate: &'tcx hir::Crate<'tcx>,
Expand Down 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 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) 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 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 fn provide(providers: &mut ty::query::Providers) {
context::provide(providers);
erase_regions::provide(providers);
layout::provide(providers);
Expand Down
44 changes: 33 additions & 11 deletions src/librustc_middle/ty/query/plumbing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -318,15 +318,34 @@ macro_rules! define_queries_inner {
}
}

#[allow(nonstandard_style)]
pub mod queries {
use std::marker::PhantomData;

$(#[allow(nonstandard_style)]
pub struct $name<$tcx> {
$(pub struct $name<$tcx> {
data: PhantomData<&$tcx ()>
})*
}

// HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
// below, but using type aliases instead of associated types, to bypass
// the limitations around normalizing under HRTB - for example, this:
// `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
// doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
// This is primarily used by the `provide!` macro in `rustc_metadata`.
#[allow(nonstandard_style, unused_lifetimes)]
pub mod query_keys {
use super::*;

$(pub type $name<$tcx> = $($K)*;)*
}
#[allow(nonstandard_style, unused_lifetimes)]
pub mod query_values {
use super::*;

$(pub type $name<$tcx> = $V;)*
}

$(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
type Key = $($K)*;
type Value = $V;
Expand Down Expand Up @@ -478,13 +497,16 @@ macro_rules! define_queries_inner {
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
}

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

// FIXME(eddyb) this macro (and others?) use `$tcx` and `'tcx` interchangeably.
// We should either not take `$tcx` at all and use `'tcx` everywhere, or use
// `$tcx` everywhere (even if that isn't necessary due to lack of hygiene).
macro_rules! define_queries_struct {
(tcx: $tcx:tt,
input: ($(([$($modifiers:tt)*] [$($attr:tt)*] [$name:ident]))*)) => {
Expand All @@ -494,8 +516,8 @@ macro_rules! define_queries_struct {
/// `DepGraph::try_mark_green()` and the query infrastructure.
pub(crate) on_disk_cache: OnDiskCache<'tcx>,

providers: IndexVec<CrateNum, Providers<$tcx>>,
fallback_extern_providers: Box<Providers<$tcx>>,
providers: IndexVec<CrateNum, Providers>,
fallback_extern_providers: Box<Providers>,

$($(#[$attr])* $name: QueryState<
TyCtxt<$tcx>,
Expand All @@ -505,8 +527,8 @@ macro_rules! define_queries_struct {

impl<$tcx> Queries<$tcx> {
pub(crate) fn new(
providers: IndexVec<CrateNum, Providers<$tcx>>,
fallback_extern_providers: Providers<$tcx>,
providers: IndexVec<CrateNum, Providers>,
fallback_extern_providers: Providers,
on_disk_cache: OnDiskCache<'tcx>,
) -> Self {
Queries {
Expand Down Expand Up @@ -539,11 +561,11 @@ macro_rules! define_queries_struct {
macro_rules! define_provider_struct {
(tcx: $tcx:tt,
input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
pub struct Providers<$tcx> {
$(pub $name: fn(TyCtxt<$tcx>, $K) -> $R,)*
pub struct Providers {
$(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
}

impl<$tcx> Default for Providers<$tcx> {
impl Default for Providers {
fn default() -> Self {
$(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
bug!("`tcx.{}({:?})` unsupported by its crate",
Expand Down
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 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 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 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
2 changes: 1 addition & 1 deletion src/librustc_mir/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub mod util;

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

pub fn provide(providers: &mut Providers<'_>) {
pub fn provide(providers: &mut Providers) {
borrow_check::provide(providers);
const_eval::provide(providers);
shim::provide(providers);
Expand Down
Loading

0 comments on commit c2052bb

Please sign in to comment.