1//! "Hooks" let you write `tcx` methods in downstream crates and call them in this crate, reducing
2//! the amount of code that needs to be in this crate (which is already very big). This is somewhat
3//! similar to queries, but queries come with a lot of machinery for caching and incremental
4//! compilation, whereas hooks are just plain function pointers without any of the query magic.
56use rustc_hir::def_id::{DefId, DefPathHash};
7use rustc_session::StableCrateId;
8use rustc_span::def_id::{CrateNum, LocalDefId};
9use rustc_span::{ExpnHash, ExpnId};
1011use crate::mir;
12use crate::ty::{Ty, TyCtxt};
1314macro_rules!declare_hooks {
15 ($($(#[$attr:meta])*hook $name:ident($($arg:ident: $K:ty),*) -> $V:ty;)*) => {
1617impl<'tcx> TyCtxt<'tcx> {
18 $(
19 $(#[$attr])*
20#[inline(always)]
21pub fn $name(self, $($arg: $K,)*) -> $V
22{
23 (self.hooks.$name)(self, $($arg,)*)
24 }
25 )*
26 }
2728pub struct Providers {
29 $(pub $name: for<'tcx> fn(
30TyCtxt<'tcx>,
31 $($arg: $K,)*
32 ) -> $V,)*
33 }
3435impl Default for Providers {
36fn default() -> Self {
37 Providers {
38 $($name: |_, $($arg,)*| default_hook(stringify!($name), &($($arg,)*))),*
39 }
40 }
41 }
4243impl Copy for Providers {}
44impl Clone for Providers {
45fn clone(&self) -> Self { *self }
46 }
47 };
48}
4950impl<'tcx> TyCtxt<'tcx> {
#[doc =
r" Tries to destructure an `mir::Const` ADT or array into its variant index"]
#[doc =
r" and its field values. This should only be used for pretty printing."]
#[inline(always)]
pub fn try_destructure_mir_constant_for_user_output(self,
val: mir::ConstValue, ty: Ty<'tcx>)
-> Option<mir::DestructuredConstant<'tcx>> {
(self.hooks.try_destructure_mir_constant_for_user_output)(self, val,
ty)
}
#[doc = r" Getting a &core::panic::Location referring to a span."]
#[inline(always)]
pub fn const_caller_location(self, file: rustc_span::Symbol, line: u32,
col: u32) -> mir::ConstValue {
(self.hooks.const_caller_location)(self, file, line, col)
}
#[doc =
r" Returns `true` if this def is a function-like thing that is eligible for"]
#[doc = r" coverage instrumentation under `-Cinstrument-coverage`."]
#[doc = r""]
#[doc =
r" (Eligible functions might nevertheless be skipped for other reasons.)"]
#[inline(always)]
pub fn is_eligible_for_coverage(self, key: LocalDefId) -> bool {
(self.hooks.is_eligible_for_coverage)(self, key)
}
#[doc =
r" Imports all `SourceFile`s from the given crate into the current session."]
#[doc =
r" This normally happens automatically when we decode a `Span` from"]
#[doc = r" that crate's metadata - however, the incr comp cache needs"]
#[doc = r" to trigger this manually when decoding a foreign `Span`"]
#[inline(always)]
pub fn import_source_files(self, key: CrateNum) -> () {
(self.hooks.import_source_files)(self, key)
}
#[inline(always)]
pub fn expn_hash_to_expn_id(self, cnum: CrateNum, index_guess: u32,
hash: ExpnHash) -> ExpnId {
(self.hooks.expn_hash_to_expn_id)(self, cnum, index_guess, hash)
}
#[doc =
r" Converts a `DefPathHash` to its corresponding `DefId` in the current compilation"]
#[doc =
r" session, if it still exists. This is used during incremental compilation to"]
#[doc = r" turn a deserialized `DefPathHash` into its current `DefId`."]
#[doc = r" Will fetch a DefId from a DefPathHash for a foreign crate."]
#[inline(always)]
pub fn def_path_hash_to_def_id_extern(self, hash: DefPathHash,
stable_crate_id: StableCrateId) -> Option<DefId> {
(self.hooks.def_path_hash_to_def_id_extern)(self, hash,
stable_crate_id)
}
#[doc =
r" Returns `true` if we should codegen an instance in the local crate, or returns `false` if we"]
#[doc =
r" can just link to the upstream crate and therefore don't need a mono item."]
#[doc = r""]
#[doc =
r" Note: this hook isn't called within `rustc_middle` but #127779 suggests it's a hook instead"]
#[doc =
r" of a normal function because external tools might want to override it."]
#[inline(always)]
pub fn should_codegen_locally(self, instance: crate::ty::Instance<'tcx>)
-> bool {
(self.hooks.should_codegen_locally)(self, instance)
}
#[inline(always)]
pub fn alloc_self_profile_query_strings(self) -> () {
(self.hooks.alloc_self_profile_query_strings)(self)
}
#[doc = r" Saves and writes the DepGraph to the file system."]
#[doc = r""]
#[doc =
r" This function saves both the dep-graph and the query result cache,"]
#[doc = r" and drops the result cache."]
#[doc = r""]
#[doc =
r" This function should only run after all queries have completed."]
#[doc =
r" Trying to execute a query afterwards would attempt to read the result cache we just dropped."]
#[inline(always)]
pub fn save_dep_graph(self) -> () { (self.hooks.save_dep_graph)(self) }
#[inline(always)]
pub fn query_key_hash_verify_all(self) -> () {
(self.hooks.query_key_hash_verify_all)(self)
}
#[doc = r" Ensure the given scalar is valid for the given type."]
#[doc = r" This checks non-recursive runtime validity."]
#[inline(always)]
pub fn validate_scalar_in_layout(self, scalar: crate::ty::ScalarInt,
ty: Ty<'tcx>) -> bool {
(self.hooks.validate_scalar_in_layout)(self, scalar, ty)
}
#[doc =
r" **Do not call this directly; call the `mir_built` query instead.**"]
#[doc = r""]
#[doc =
r" Creates the MIR for a given `DefId`, including unreachable code."]
#[inline(always)]
pub fn build_mir_inner_impl(self, def: LocalDefId) -> mir::Body<'tcx> {
(self.hooks.build_mir_inner_impl)(self, def)
}
}
pub struct Providers {
pub try_destructure_mir_constant_for_user_output: for<'tcx> fn(TyCtxt<'tcx>,
val: mir::ConstValue, ty: Ty<'tcx>)
-> Option<mir::DestructuredConstant<'tcx>>,
pub const_caller_location: for<'tcx> fn(TyCtxt<'tcx>,
file: rustc_span::Symbol, line: u32, col: u32) -> mir::ConstValue,
pub is_eligible_for_coverage: for<'tcx> fn(TyCtxt<'tcx>, key: LocalDefId)
-> bool,
pub import_source_files: for<'tcx> fn(TyCtxt<'tcx>, key: CrateNum) -> (),
pub expn_hash_to_expn_id: for<'tcx> fn(TyCtxt<'tcx>, cnum: CrateNum,
index_guess: u32, hash: ExpnHash) -> ExpnId,
pub def_path_hash_to_def_id_extern: for<'tcx> fn(TyCtxt<'tcx>,
hash: DefPathHash, stable_crate_id: StableCrateId) -> Option<DefId>,
pub should_codegen_locally: for<'tcx> fn(TyCtxt<'tcx>,
instance: crate::ty::Instance<'tcx>) -> bool,
pub alloc_self_profile_query_strings: for<'tcx> fn(TyCtxt<'tcx>) -> (),
pub save_dep_graph: for<'tcx> fn(TyCtxt<'tcx>) -> (),
pub query_key_hash_verify_all: for<'tcx> fn(TyCtxt<'tcx>) -> (),
pub validate_scalar_in_layout: for<'tcx> fn(TyCtxt<'tcx>,
scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool,
pub build_mir_inner_impl: for<'tcx> fn(TyCtxt<'tcx>, def: LocalDefId)
-> mir::Body<'tcx>,
}
impl Default for Providers {
fn default() -> Self {
Providers {
try_destructure_mir_constant_for_user_output: |_, val, ty|
default_hook("try_destructure_mir_constant_for_user_output",
&(val, ty)),
const_caller_location: |_, file, line, col|
default_hook("const_caller_location", &(file, line, col)),
is_eligible_for_coverage: |_, key|
default_hook("is_eligible_for_coverage", &(key,)),
import_source_files: |_, key|
default_hook("import_source_files", &(key,)),
expn_hash_to_expn_id: |_, cnum, index_guess, hash|
default_hook("expn_hash_to_expn_id",
&(cnum, index_guess, hash)),
def_path_hash_to_def_id_extern: |_, hash, stable_crate_id|
default_hook("def_path_hash_to_def_id_extern",
&(hash, stable_crate_id)),
should_codegen_locally: |_, instance|
default_hook("should_codegen_locally", &(instance,)),
alloc_self_profile_query_strings: |_|
default_hook("alloc_self_profile_query_strings", &()),
save_dep_graph: |_| default_hook("save_dep_graph", &()),
query_key_hash_verify_all: |_|
default_hook("query_key_hash_verify_all", &()),
validate_scalar_in_layout: |_, scalar, ty|
default_hook("validate_scalar_in_layout", &(scalar, ty)),
build_mir_inner_impl: |_, def|
default_hook("build_mir_inner_impl", &(def,)),
}
}
}
impl Copy for Providers {}
impl Clone for Providers {
fn clone(&self) -> Self { *self }
}declare_hooks! {
51/// Tries to destructure an `mir::Const` ADT or array into its variant index
52 /// and its field values. This should only be used for pretty printing.
53hook try_destructure_mir_constant_for_user_output(val: mir::ConstValue, ty: Ty<'tcx>) -> Option<mir::DestructuredConstant<'tcx>>;
5455/// Getting a &core::panic::Location referring to a span.
56hook const_caller_location(file: rustc_span::Symbol, line: u32, col: u32) -> mir::ConstValue;
5758/// Returns `true` if this def is a function-like thing that is eligible for
59 /// coverage instrumentation under `-Cinstrument-coverage`.
60 ///
61 /// (Eligible functions might nevertheless be skipped for other reasons.)
62hook is_eligible_for_coverage(key: LocalDefId) -> bool;
6364/// Imports all `SourceFile`s from the given crate into the current session.
65 /// This normally happens automatically when we decode a `Span` from
66 /// that crate's metadata - however, the incr comp cache needs
67 /// to trigger this manually when decoding a foreign `Span`
68hook import_source_files(key: CrateNum) -> ();
6970 hook expn_hash_to_expn_id(
71 cnum: CrateNum,
72 index_guess: u32,
73 hash: ExpnHash74 ) -> ExpnId;
7576/// Converts a `DefPathHash` to its corresponding `DefId` in the current compilation
77 /// session, if it still exists. This is used during incremental compilation to
78 /// turn a deserialized `DefPathHash` into its current `DefId`.
79 /// Will fetch a DefId from a DefPathHash for a foreign crate.
80hook def_path_hash_to_def_id_extern(hash: DefPathHash, stable_crate_id: StableCrateId) -> Option<DefId>;
8182/// Returns `true` if we should codegen an instance in the local crate, or returns `false` if we
83 /// can just link to the upstream crate and therefore don't need a mono item.
84 ///
85 /// Note: this hook isn't called within `rustc_middle` but #127779 suggests it's a hook instead
86 /// of a normal function because external tools might want to override it.
87hook should_codegen_locally(instance: crate::ty::Instance<'tcx>) -> bool;
8889 hook alloc_self_profile_query_strings() -> ();
9091/// Saves and writes the DepGraph to the file system.
92 ///
93 /// This function saves both the dep-graph and the query result cache,
94 /// and drops the result cache.
95 ///
96 /// This function should only run after all queries have completed.
97 /// Trying to execute a query afterwards would attempt to read the result cache we just dropped.
98hook save_dep_graph() -> ();
99100 hook query_key_hash_verify_all() -> ();
101102/// Ensure the given scalar is valid for the given type.
103 /// This checks non-recursive runtime validity.
104hook validate_scalar_in_layout(scalar: crate::ty::ScalarInt, ty: Ty<'tcx>) -> bool;
105106/// **Do not call this directly; call the `mir_built` query instead.**
107 ///
108 /// Creates the MIR for a given `DefId`, including unreachable code.
109hook build_mir_inner_impl(def: LocalDefId) -> mir::Body<'tcx>;
110}111112#[cold]
113fn default_hook(name: &str, args: &dyn std::fmt::Debug) -> ! {
114crate::util::bug::bug_fmt(format_args!("`tcx.{0}{1:?}` cannot be called as `{0}` was never assigned to a provider function",
name, args))bug!(
115"`tcx.{name}{args:?}` cannot be called as `{name}` was never assigned to a provider function"
116)117}