Skip to content

Commit c2052bb

Browse files
authored
Rollup merge of rust-lang#74070 - eddyb:forall-tcx-providers, r=nikomatsakis
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
2 parents e7cdc83 + f07100a commit c2052bb

Some content is hidden

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

74 files changed

+123
-104
lines changed

src/librustc_codegen_llvm/attributes.rs

+2-2
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 fn provide(providers: &mut Providers) {
346346
providers.target_features_whitelist = |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 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

+2-2
Original file line numberDiff line numberDiff line change
@@ -241,11 +241,11 @@ impl CodegenBackend for LlvmCodegenBackend {
241241
Box::new(metadata::LlvmMetadataLoader)
242242
}
243243

244-
fn provide(&self, providers: &mut ty::query::Providers<'_>) {
244+
fn provide(&self, providers: &mut ty::query::Providers) {
245245
attributes::provide(providers);
246246
}
247247

248-
fn provide_extern(&self, providers: &mut ty::query::Providers<'_>) {
248+
fn provide_extern(&self, providers: &mut ty::query::Providers) {
249249
attributes::provide_extern(providers);
250250
}
251251

src/librustc_codegen_ssa/back/symbol_export.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -161,9 +161,9 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
161161
}
162162

163163
fn exported_symbols_provider_local(
164-
tcx: TyCtxt<'_>,
164+
tcx: TyCtxt<'tcx>,
165165
cnum: CrateNum,
166-
) -> &'tcx [(ExportedSymbol<'_>, SymbolExportLevel)] {
166+
) -> &'tcx [(ExportedSymbol<'tcx>, SymbolExportLevel)] {
167167
assert_eq!(cnum, LOCAL_CRATE);
168168

169169
if !tcx.sess.opts.output_types.should_codegen() {
@@ -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 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 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

+1-1
Original file line numberDiff line numberDiff line change
@@ -853,7 +853,7 @@ impl CrateInfo {
853853
}
854854
}
855855

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

src/librustc_codegen_ssa/lib.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -138,12 +138,12 @@ pub struct CodegenResults {
138138
pub crate_info: CrateInfo,
139139
}
140140

141-
pub fn provide(providers: &mut Providers<'_>) {
141+
pub fn provide(providers: &mut Providers) {
142142
crate::back::symbol_export::provide(providers);
143143
crate::base::provide_both(providers);
144144
}
145145

146-
pub fn provide_extern(providers: &mut Providers<'_>) {
146+
pub fn provide_extern(providers: &mut Providers) {
147147
crate::back::symbol_export::provide_extern(providers);
148148
crate::base::provide_both(providers);
149149
}

src/librustc_codegen_ssa/traits/backend.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -55,8 +55,8 @@ pub trait CodegenBackend {
5555
fn print_version(&self) {}
5656

5757
fn metadata_loader(&self) -> Box<MetadataLoaderDyn>;
58-
fn provide(&self, _providers: &mut Providers<'_>);
59-
fn provide_extern(&self, _providers: &mut Providers<'_>);
58+
fn provide(&self, _providers: &mut Providers);
59+
fn provide_extern(&self, _providers: &mut Providers);
6060
fn codegen_crate<'tcx>(
6161
&self,
6262
tcx: TyCtxt<'tcx>,

src/librustc_interface/interface.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ pub struct Compiler {
3838
pub(crate) crate_name: Option<String>,
3939
pub(crate) register_lints: Option<Box<dyn Fn(&Session, &mut LintStore) + Send + Sync>>,
4040
pub(crate) override_queries:
41-
Option<fn(&Session, &mut ty::query::Providers<'_>, &mut ty::query::Providers<'_>)>,
41+
Option<fn(&Session, &mut ty::query::Providers, &mut ty::query::Providers)>,
4242
}
4343

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

158158
/// Registry of diagnostics codes.
159159
pub registry: Registry,

src/librustc_interface/passes.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ pub fn prepare_outputs(
719719
Ok(outputs)
720720
}
721721

722-
pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
722+
pub fn default_provide(providers: &mut ty::query::Providers) {
723723
providers.analysis = analysis;
724724
proc_macro_decls::provide(providers);
725725
plugin::build::provide(providers);
@@ -740,7 +740,7 @@ pub fn default_provide(providers: &mut ty::query::Providers<'_>) {
740740
rustc_codegen_ssa::provide(providers);
741741
}
742742

743-
pub fn default_provide_extern(providers: &mut ty::query::Providers<'_>) {
743+
pub fn default_provide_extern(providers: &mut ty::query::Providers) {
744744
rustc_metadata::provide_extern(providers);
745745
rustc_codegen_ssa::provide_extern(providers);
746746
}

src/librustc_interface/proc_macro_decls.rs

+1-1
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) fn provide(providers: &mut Providers) {
3939
*providers = Providers { proc_macro_decls_static, ..*providers };
4040
}

src/librustc_lint/levels.rs

+1-1
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 fn provide(providers: &mut Providers) {
575575
providers.lint_levels = lint_levels;
576576
}

src/librustc_lint/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ pub use rustc_session::lint::Level::{self, *};
8888
pub use rustc_session::lint::{BufferedEarlyLint, FutureIncompatibleInfo, Lint, LintId};
8989
pub use rustc_session::lint::{LintArray, LintPass};
9090

91-
pub fn provide(providers: &mut Providers<'_>) {
91+
pub fn provide(providers: &mut Providers) {
9292
levels::provide(providers);
9393
*providers = Providers { lint_mod, ..*providers };
9494
}

src/librustc_metadata/rmeta/decoder/cstore_impl.rs

+5-8
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ use rustc_middle::middle::cstore::{CrateSource, CrateStore, EncodedMetadata};
1717
use rustc_middle::middle::exported_symbols::ExportedSymbol;
1818
use rustc_middle::middle::stability::DeprecationEntry;
1919
use rustc_middle::ty::query::Providers;
20-
use rustc_middle::ty::query::QueryConfig;
2120
use rustc_middle::ty::{self, TyCtxt};
2221
use rustc_session::utils::NativeLibKind;
2322
use rustc_session::{CrateDisambiguator, Session};
@@ -31,13 +30,11 @@ use std::any::Any;
3130
macro_rules! provide {
3231
(<$lt:tt> $tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
3332
$($name:ident => $compute:block)*) => {
34-
pub fn provide_extern<$lt>(providers: &mut Providers<$lt>) {
35-
// HACK(eddyb) `$lt: $lt` forces `$lt` to be early-bound, which
36-
// allows the associated type in the return type to be normalized.
37-
$(fn $name<$lt: $lt, T: IntoArgs>(
33+
pub fn provide_extern(providers: &mut Providers) {
34+
$(fn $name<$lt>(
3835
$tcx: TyCtxt<$lt>,
39-
def_id_arg: T,
40-
) -> <ty::queries::$name<$lt> as QueryConfig<TyCtxt<$lt>>>::Value {
36+
def_id_arg: ty::query::query_keys::$name<$lt>,
37+
) -> ty::query::query_values::$name<$lt> {
4138
let _prof_timer =
4239
$tcx.prof.generic_activity("metadata_decode_entry");
4340

@@ -243,7 +240,7 @@ provide! { <'tcx> tcx, def_id, other, cdata,
243240
crate_extern_paths => { cdata.source().paths().cloned().collect() }
244241
}
245242

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

src/librustc_middle/hir/map/mod.rs

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

+1-1
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 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/ty/context.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -1060,8 +1060,8 @@ impl<'tcx> TyCtxt<'tcx> {
10601060
pub fn create_global_ctxt(
10611061
s: &'tcx Session,
10621062
lint_store: Lrc<dyn Any + sync::Send + sync::Sync>,
1063-
local_providers: ty::query::Providers<'tcx>,
1064-
extern_providers: ty::query::Providers<'tcx>,
1063+
local_providers: ty::query::Providers,
1064+
extern_providers: ty::query::Providers,
10651065
arena: &'tcx WorkerLocal<Arena<'tcx>>,
10661066
resolutions: ty::ResolverOutputs,
10671067
krate: &'tcx hir::Crate<'tcx>,
@@ -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 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

+1-1
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) fn provide(providers: &mut ty::query::Providers) {
55
*providers = ty::query::Providers { erase_regions_ty, ..*providers };
66
}
77

src/librustc_middle/ty/layout.rs

+1-1
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 fn provide(providers: &mut ty::query::Providers) {
214214
*providers = ty::query::Providers { layout_raw, ..*providers };
215215
}
216216

src/librustc_middle/ty/mod.rs

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

+33-11
Original file line numberDiff line numberDiff line change
@@ -318,15 +318,34 @@ macro_rules! define_queries_inner {
318318
}
319319
}
320320

321+
#[allow(nonstandard_style)]
321322
pub mod queries {
322323
use std::marker::PhantomData;
323324

324-
$(#[allow(nonstandard_style)]
325-
pub struct $name<$tcx> {
325+
$(pub struct $name<$tcx> {
326326
data: PhantomData<&$tcx ()>
327327
})*
328328
}
329329

330+
// HACK(eddyb) this is like the `impl QueryConfig for queries::$name`
331+
// below, but using type aliases instead of associated types, to bypass
332+
// the limitations around normalizing under HRTB - for example, this:
333+
// `for<'tcx> fn(...) -> <queries::$name<'tcx> as QueryConfig<TyCtxt<'tcx>>>::Value`
334+
// doesn't currently normalize to `for<'tcx> fn(...) -> query_values::$name<'tcx>`.
335+
// This is primarily used by the `provide!` macro in `rustc_metadata`.
336+
#[allow(nonstandard_style, unused_lifetimes)]
337+
pub mod query_keys {
338+
use super::*;
339+
340+
$(pub type $name<$tcx> = $($K)*;)*
341+
}
342+
#[allow(nonstandard_style, unused_lifetimes)]
343+
pub mod query_values {
344+
use super::*;
345+
346+
$(pub type $name<$tcx> = $V;)*
347+
}
348+
330349
$(impl<$tcx> QueryConfig<TyCtxt<$tcx>> for queries::$name<$tcx> {
331350
type Key = $($K)*;
332351
type Value = $V;
@@ -478,13 +497,16 @@ macro_rules! define_queries_inner {
478497
input: ($(([$($modifiers)*] [$name] [$($K)*] [$V]))*)
479498
}
480499

481-
impl<$tcx> Copy for Providers<$tcx> {}
482-
impl<$tcx> Clone for Providers<$tcx> {
500+
impl Copy for Providers {}
501+
impl Clone for Providers {
483502
fn clone(&self) -> Self { *self }
484503
}
485504
}
486505
}
487506

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

497-
providers: IndexVec<CrateNum, Providers<$tcx>>,
498-
fallback_extern_providers: Box<Providers<$tcx>>,
519+
providers: IndexVec<CrateNum, Providers>,
520+
fallback_extern_providers: Box<Providers>,
499521

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

506528
impl<$tcx> Queries<$tcx> {
507529
pub(crate) fn new(
508-
providers: IndexVec<CrateNum, Providers<$tcx>>,
509-
fallback_extern_providers: Providers<$tcx>,
530+
providers: IndexVec<CrateNum, Providers>,
531+
fallback_extern_providers: Providers,
510532
on_disk_cache: OnDiskCache<'tcx>,
511533
) -> Self {
512534
Queries {
@@ -539,11 +561,11 @@ macro_rules! define_queries_struct {
539561
macro_rules! define_provider_struct {
540562
(tcx: $tcx:tt,
541563
input: ($(([$($modifiers:tt)*] [$name:ident] [$K:ty] [$R:ty]))*)) => {
542-
pub struct Providers<$tcx> {
543-
$(pub $name: fn(TyCtxt<$tcx>, $K) -> $R,)*
564+
pub struct Providers {
565+
$(pub $name: for<$tcx> fn(TyCtxt<$tcx>, $K) -> $R,)*
544566
}
545567

546-
impl<$tcx> Default for Providers<$tcx> {
568+
impl Default for Providers {
547569
fn default() -> Self {
548570
$(fn $name<$tcx>(_: TyCtxt<$tcx>, key: $K) -> $R {
549571
bug!("`tcx.{}({:?})` unsupported by its crate",

src/librustc_middle/util/bug.rs

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

+1-1
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 fn provide(providers: &mut Providers) {
9090
*providers = Providers { mir_borrowck, ..*providers };
9191
}
9292

src/librustc_mir/const_eval/fn_queries.rs

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

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ pub mod util;
4747

4848
use rustc_middle::ty::query::Providers;
4949

50-
pub fn provide(providers: &mut Providers<'_>) {
50+
pub fn provide(providers: &mut Providers) {
5151
borrow_check::provide(providers);
5252
const_eval::provide(providers);
5353
shim::provide(providers);

0 commit comments

Comments
 (0)