LocalCrate key

This commit is contained in:
Michael Goulet 2023-03-13 22:22:59 +00:00
parent dcaf956de0
commit d213114cb5
11 changed files with 33 additions and 21 deletions

View File

@ -10,6 +10,7 @@ use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrFlags;
use rustc_middle::middle::exported_symbols::{
metadata_symbol_name, ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
};
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::subst::{GenericArgKind, SubstsRef};
use rustc_middle::ty::Instance;
@ -41,7 +42,7 @@ pub fn crates_export_threshold(crate_types: &[CrateType]) -> SymbolExportLevel {
}
}
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, (): ()) -> DefIdMap<SymbolExportInfo> {
fn reachable_non_generics_provider(tcx: TyCtxt<'_>, _: LocalCrate) -> DefIdMap<SymbolExportInfo> {
if !tcx.sess.opts.output_types.should_codegen() {
return Default::default();
}
@ -168,7 +169,7 @@ fn is_reachable_non_generic_provider_extern(tcx: TyCtxt<'_>, def_id: DefId) -> b
fn exported_symbols_provider_local(
tcx: TyCtxt<'_>,
(): (),
_: LocalCrate,
) -> &[(ExportedSymbol<'_>, SymbolExportInfo)] {
if !tcx.sess.opts.output_types.should_codegen() {
return &[];

View File

@ -13,6 +13,7 @@ use rustc_middle::arena::ArenaAllocatable;
use rustc_middle::metadata::ModChild;
use rustc_middle::middle::exported_symbols::ExportedSymbol;
use rustc_middle::middle::stability::DeprecationEntry;
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::fast_reject::SimplifiedType;
use rustc_middle::ty::query::{ExternProviders, Providers};
use rustc_middle::ty::{self, TyCtxt};
@ -367,7 +368,7 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
*providers = Providers {
allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(),
alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(),
is_private_dep: |_tcx, ()| false,
is_private_dep: |_tcx, LocalCrate| false,
native_library: |tcx, id| {
tcx.native_libraries(id.krate)
.iter()
@ -383,8 +384,8 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
.contains(&id)
})
},
native_libraries: |tcx, ()| native_libs::collect(tcx),
foreign_modules: |tcx, ()| {
native_libraries: |tcx, LocalCrate| native_libs::collect(tcx),
foreign_modules: |tcx, LocalCrate| {
foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect()
},
@ -482,8 +483,8 @@ pub(in crate::rmeta) fn provide(providers: &mut Providers) {
},
dependency_formats: |tcx, ()| Lrc::new(crate::dependency_format::calculate(tcx)),
has_global_allocator: |tcx, ()| CStore::from_tcx(tcx).has_global_allocator(),
has_alloc_error_handler: |tcx, ()| CStore::from_tcx(tcx).has_alloc_error_handler(),
has_global_allocator: |tcx, LocalCrate| CStore::from_tcx(tcx).has_global_allocator(),
has_alloc_error_handler: |tcx, LocalCrate| CStore::from_tcx(tcx).has_alloc_error_handler(),
postorder_cnums: |tcx, ()| {
tcx.arena
.alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE))

View File

@ -24,6 +24,7 @@ use rustc_middle::middle::exported_symbols::{
metadata_symbol_name, ExportedSymbol, SymbolExportInfo,
};
use rustc_middle::mir::interpret;
use rustc_middle::query::LocalCrate;
use rustc_middle::traits::specialization_graph;
use rustc_middle::ty::codec::TyEncoder;
use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams, TreatProjections};
@ -2240,7 +2241,7 @@ pub fn provide(providers: &mut Providers) {
.get(&def_id)
.expect("no traits in scope for a doc link")
},
traits_in_crate: |tcx, ()| {
traits_in_crate: |tcx, LocalCrate| {
let mut traits = Vec::new();
for id in tcx.hir().items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) {
@ -2252,7 +2253,7 @@ pub fn provide(providers: &mut Providers) {
traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id));
tcx.arena.alloc_slice(&traits)
},
trait_impls_in_crate: |tcx, ()| {
trait_impls_in_crate: |tcx, LocalCrate| {
let mut trait_impls = Vec::new();
for id in tcx.hir().items() {
if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. })

View File

@ -1,4 +1,5 @@
use crate::hir::{ModuleItems, Owner};
use crate::query::LocalCrate;
use crate::ty::TyCtxt;
use rustc_ast as ast;
use rustc_data_structures::fingerprint::Fingerprint;
@ -1131,7 +1132,7 @@ impl<'hir> intravisit::Map<'hir> for Map<'hir> {
}
}
pub(super) fn crate_hash(tcx: TyCtxt<'_>, (): ()) -> Svh {
pub(super) fn crate_hash(tcx: TyCtxt<'_>, _: LocalCrate) -> Svh {
let krate = tcx.hir_crate(());
let hir_body_hash = krate.opt_hir_hash.expect("HIR hash missing while computing crate hash");

View File

@ -13,6 +13,10 @@ use rustc_query_system::query::{DefaultCacheSelector, SingleCacheSelector, VecCa
use rustc_span::symbol::{Ident, Symbol};
use rustc_span::{Span, DUMMY_SP};
/// Placeholder for `CrateNum`'s "local" counterpart
#[derive(Copy, Clone, Debug)]
pub struct LocalCrate;
/// The `Key` trait controls what types can legally be used as the key
/// for a query.
pub trait Key: Sized {
@ -115,11 +119,11 @@ impl Key for CrateNum {
}
impl AsLocalKey for CrateNum {
type LocalKey = ();
type LocalKey = LocalCrate;
#[inline(always)]
fn as_local_key(&self) -> Option<Self::LocalKey> {
(*self == LOCAL_CRATE).then_some(())
(*self == LOCAL_CRATE).then_some(LocalCrate)
}
}

View File

@ -8,7 +8,7 @@ use crate::ty::{self, print::describe_as_module, TyCtxt};
use rustc_span::def_id::LOCAL_CRATE;
mod keys;
pub use keys::{AsLocalKey, Key};
pub use keys::{AsLocalKey, Key, LocalCrate};
// Each of these queries corresponds to a function pointer field in the
// `Providers` struct for requesting a value of that type, and a method

View File

@ -15,6 +15,7 @@ use crate::mir::interpret::{self, Allocation, ConstAllocation};
use crate::mir::{
Body, BorrowCheckResult, Field, Local, Place, PlaceElem, ProjectionKind, Promoted,
};
use crate::query::LocalCrate;
use crate::thir::Thir;
use crate::traits;
use crate::traits::solve;
@ -2519,10 +2520,10 @@ pub fn provide(providers: &mut ty::query::Providers) {
providers.extern_mod_stmt_cnum =
|tcx, id| tcx.resolutions(()).extern_crate_map.get(&id).cloned();
providers.is_panic_runtime =
|tcx, ()| tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::panic_runtime);
|tcx, LocalCrate| tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::panic_runtime);
providers.is_compiler_builtins =
|tcx, ()| tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::compiler_builtins);
providers.has_panic_handler = |tcx, ()| {
|tcx, LocalCrate| tcx.sess.contains_name(tcx.hir().krate_attrs(), sym::compiler_builtins);
providers.has_panic_handler = |tcx, LocalCrate| {
// We want to check if the panic handler was defined in this crate
tcx.lang_items().panic_impl().map_or(false, |did| did.is_local())
};

View File

@ -1,5 +1,6 @@
use rustc_hir::def_id::{LocalDefId, LOCAL_CRATE};
use rustc_middle::mir::*;
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::layout;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{self, TyCtxt};
@ -121,7 +122,7 @@ fn has_ffi_unwind_calls(tcx: TyCtxt<'_>, local_def_id: LocalDefId) -> bool {
tainted
}
fn required_panic_strategy(tcx: TyCtxt<'_>, (): ()) -> Option<PanicStrategy> {
fn required_panic_strategy(tcx: TyCtxt<'_>, _: LocalCrate) -> Option<PanicStrategy> {
if tcx.is_panic_runtime(LOCAL_CRATE) {
return Some(tcx.sess.panic_strategy());
}

View File

@ -5,8 +5,8 @@ use rustc_data_structures::fx::FxHashSet;
use rustc_expand::base::resolve_path;
use rustc_hir as hir;
use rustc_hir::HirId;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_middle::{query::LocalCrate, ty::query::Providers};
use rustc_span::{sym, DebuggerVisualizerFile, DebuggerVisualizerType};
use std::sync::Arc;
@ -67,7 +67,7 @@ fn check_for_debugger_visualizer(
}
/// Traverses and collects the debugger visualizers for a specific crate.
fn debugger_visualizers(tcx: TyCtxt<'_>, (): ()) -> Vec<DebuggerVisualizerFile> {
fn debugger_visualizers(tcx: TyCtxt<'_>, _: LocalCrate) -> Vec<DebuggerVisualizerFile> {
// Initialize the collector.
let mut debugger_visualizers = FxHashSet::default();

View File

@ -12,6 +12,7 @@
use rustc_ast as ast;
use rustc_hir::diagnostic_items::DiagnosticItems;
use rustc_hir::OwnerId;
use rustc_middle::query::LocalCrate;
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::TyCtxt;
use rustc_span::def_id::{DefId, LOCAL_CRATE};
@ -62,7 +63,7 @@ fn extract(attrs: &[ast::Attribute]) -> Option<Symbol> {
}
/// Traverse and collect the diagnostic items in the current
fn diagnostic_items(tcx: TyCtxt<'_>, (): ()) -> DiagnosticItems {
fn diagnostic_items(tcx: TyCtxt<'_>, _: LocalCrate) -> DiagnosticItems {
// Initialize the collector.
let mut diagnostic_items = DiagnosticItems::default();

View File

@ -29,6 +29,7 @@ use rustc_middle::{
ExportedSymbol, SymbolExportInfo, SymbolExportKind, SymbolExportLevel,
},
ty::{query::ExternProviders, TyCtxt},
query::LocalCrate,
};
use rustc_session::{config::CrateType, search_paths::PathKind, CtfeBacktrace};
@ -107,7 +108,7 @@ impl rustc_driver::Callbacks for MiriBeRustCompilerCalls {
config.override_queries = Some(|_, local_providers, _| {
// `exported_symbols` and `reachable_non_generics` provided by rustc always returns
// an empty result if `tcx.sess.opts.output_types.should_codegen()` is false.
local_providers.exported_symbols = |tcx, ()| {
local_providers.exported_symbols = |tcx, LocalCrate| {
let reachable_set = tcx.with_stable_hashing_context(|hcx| {
tcx.reachable_set(()).to_sorted(&hcx, true)
});