2019-11-24 01:10:12 +03:00
|
|
|
use std::any::Any;
|
2023-02-05 16:28:12 +04:00
|
|
|
use std::mem;
|
2025-02-03 06:44:41 +03:00
|
|
|
use std::sync::Arc;
|
2015-11-25 00:00:26 +02:00
|
|
|
|
2025-07-31 11:00:40 +02:00
|
|
|
use rustc_hir::attrs::Deprecation;
|
2025-07-04 22:21:31 +03:00
|
|
|
use rustc_hir::def::{CtorKind, DefKind};
|
2022-04-15 19:27:53 +02:00
|
|
|
use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE};
|
2020-03-21 04:39:48 +01:00
|
|
|
use rustc_hir::definitions::{DefKey, DefPath, DefPathHash};
|
2022-05-22 11:16:14 -07:00
|
|
|
use rustc_middle::arena::ArenaAllocatable;
|
2024-05-08 18:10:16 +10:00
|
|
|
use rustc_middle::bug;
|
2021-12-21 11:24:43 +08:00
|
|
|
use rustc_middle::metadata::ModChild;
|
2020-03-29 17:19:48 +02:00
|
|
|
use rustc_middle::middle::exported_symbols::ExportedSymbol;
|
2022-05-22 11:16:14 -07:00
|
|
|
use rustc_middle::middle::stability::DeprecationEntry;
|
2023-09-22 16:38:31 +00:00
|
|
|
use rustc_middle::query::{ExternProviders, LocalCrate};
|
2021-08-29 21:41:46 +03:00
|
|
|
use rustc_middle::ty::fast_reject::SimplifiedType;
|
2023-02-22 02:03:29 +04:00
|
|
|
use rustc_middle::ty::{self, TyCtxt};
|
2023-09-22 16:38:31 +00:00
|
|
|
use rustc_middle::util::Providers;
|
2024-01-04 21:09:13 +03:00
|
|
|
use rustc_session::cstore::{CrateStore, ExternCrate};
|
2021-06-08 18:36:30 +02:00
|
|
|
use rustc_session::{Session, StableCrateId};
|
2024-03-26 12:31:41 +00:00
|
|
|
use rustc_span::hygiene::ExpnId;
|
2024-12-13 10:29:23 +11:00
|
|
|
use rustc_span::{Span, Symbol, kw};
|
2015-11-25 00:00:26 +02:00
|
|
|
|
2022-05-22 11:16:14 -07:00
|
|
|
use super::{Decodable, DecodeContext, DecodeIterator};
|
2019-11-24 01:10:12 +03:00
|
|
|
use crate::creader::{CStore, LoadedMacro};
|
2023-01-21 00:59:19 +04:00
|
|
|
use crate::rmeta::AttrFlags;
|
2023-02-05 16:28:12 +04:00
|
|
|
use crate::rmeta::table::IsDefault;
|
2019-02-08 20:50:17 +09:00
|
|
|
use crate::{foreign_modules, native_libs};
|
2022-05-22 11:16:14 -07:00
|
|
|
|
|
|
|
|
trait ProcessQueryValue<'tcx, T> {
|
|
|
|
|
fn process_decoded(self, _tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> T;
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 23:22:40 -04:00
|
|
|
impl<T> ProcessQueryValue<'_, T> for T {
|
2022-05-23 19:50:29 -07:00
|
|
|
#[inline(always)]
|
2024-08-28 23:22:40 -04:00
|
|
|
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> T {
|
2022-05-22 11:16:14 -07:00
|
|
|
self
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-08-28 23:22:40 -04:00
|
|
|
impl<'tcx, T> ProcessQueryValue<'tcx, ty::EarlyBinder<'tcx, T>> for T {
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> ty::EarlyBinder<'tcx, T> {
|
|
|
|
|
ty::EarlyBinder::bind(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 11:16:14 -07:00
|
|
|
impl<T> ProcessQueryValue<'_, T> for Option<T> {
|
2022-05-23 19:50:29 -07:00
|
|
|
#[inline(always)]
|
2022-05-22 11:16:14 -07:00
|
|
|
fn process_decoded(self, _tcx: TyCtxt<'_>, err: impl Fn() -> !) -> T {
|
|
|
|
|
if let Some(value) = self { value } else { err() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'tcx, T: ArenaAllocatable<'tcx>> ProcessQueryValue<'tcx, &'tcx T> for Option<T> {
|
2022-05-23 19:50:29 -07:00
|
|
|
#[inline(always)]
|
2022-05-22 11:16:14 -07:00
|
|
|
fn process_decoded(self, tcx: TyCtxt<'tcx>, err: impl Fn() -> !) -> &'tcx T {
|
|
|
|
|
if let Some(value) = self { tcx.arena.alloc(value) } else { err() }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<T, E> ProcessQueryValue<'_, Result<Option<T>, E>> for Option<T> {
|
2022-05-23 19:50:29 -07:00
|
|
|
#[inline(always)]
|
2022-05-22 11:16:14 -07:00
|
|
|
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Result<Option<T>, E> {
|
|
|
|
|
Ok(self)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'tcx, &'tcx [T]>
|
|
|
|
|
for Option<DecodeIterator<'a, 'tcx, T>>
|
|
|
|
|
{
|
2022-05-23 19:50:29 -07:00
|
|
|
#[inline(always)]
|
2024-08-29 20:49:39 -04:00
|
|
|
fn process_decoded(self, tcx: TyCtxt<'tcx>, err: impl Fn() -> !) -> &'tcx [T] {
|
|
|
|
|
if let Some(iter) = self { tcx.arena.alloc_from_iter(iter) } else { err() }
|
2022-05-22 11:16:14 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-07-12 13:07:16 -04:00
|
|
|
impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>>
|
|
|
|
|
ProcessQueryValue<'tcx, Option<&'tcx [T]>> for Option<DecodeIterator<'a, 'tcx, T>>
|
|
|
|
|
{
|
|
|
|
|
#[inline(always)]
|
|
|
|
|
fn process_decoded(self, tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> Option<&'tcx [T]> {
|
|
|
|
|
if let Some(iter) = self { Some(&*tcx.arena.alloc_from_iter(iter)) } else { None }
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-22 11:16:14 -07:00
|
|
|
impl ProcessQueryValue<'_, Option<DeprecationEntry>> for Option<Deprecation> {
|
2022-05-23 19:50:29 -07:00
|
|
|
#[inline(always)]
|
2022-05-22 11:16:14 -07:00
|
|
|
fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<DeprecationEntry> {
|
|
|
|
|
self.map(DeprecationEntry::external)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-02-17 21:33:23 +01:00
|
|
|
macro_rules! provide_one {
|
2022-08-11 21:35:33 -05:00
|
|
|
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table }) => {
|
2022-02-17 21:33:23 +01:00
|
|
|
provide_one! {
|
2022-08-11 21:35:33 -05:00
|
|
|
$tcx, $def_id, $other, $cdata, $name => {
|
2022-05-22 11:16:14 -07:00
|
|
|
$cdata
|
|
|
|
|
.root
|
|
|
|
|
.tables
|
|
|
|
|
.$name
|
|
|
|
|
.get($cdata, $def_id.index)
|
|
|
|
|
.map(|lazy| lazy.decode(($cdata, $tcx)))
|
|
|
|
|
.process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)))
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2023-02-05 16:28:12 +04:00
|
|
|
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_defaulted_array }) => {
|
|
|
|
|
provide_one! {
|
|
|
|
|
$tcx, $def_id, $other, $cdata, $name => {
|
|
|
|
|
let lazy = $cdata.root.tables.$name.get($cdata, $def_id.index);
|
2024-08-28 23:22:40 -04:00
|
|
|
let value = if lazy.is_default() {
|
|
|
|
|
&[] as &[_]
|
|
|
|
|
} else {
|
|
|
|
|
$tcx.arena.alloc_from_iter(lazy.decode(($cdata, $tcx)))
|
|
|
|
|
};
|
|
|
|
|
value.process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)))
|
2023-02-05 16:28:12 +04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-08-11 21:35:33 -05:00
|
|
|
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_direct }) => {
|
2022-05-22 11:16:14 -07:00
|
|
|
provide_one! {
|
2022-08-11 21:35:33 -05:00
|
|
|
$tcx, $def_id, $other, $cdata, $name => {
|
2022-05-22 11:16:14 -07:00
|
|
|
// We don't decode `table_direct`, since it's not a Lazy, but an actual value
|
|
|
|
|
$cdata
|
|
|
|
|
.root
|
|
|
|
|
.tables
|
|
|
|
|
.$name
|
|
|
|
|
.get($cdata, $def_id.index)
|
|
|
|
|
.process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name)))
|
2022-02-17 21:33:23 +01:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
};
|
2022-08-11 21:35:33 -05:00
|
|
|
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => $compute:block) => {
|
|
|
|
|
fn $name<'tcx>(
|
|
|
|
|
$tcx: TyCtxt<'tcx>,
|
2023-05-18 03:39:35 +02:00
|
|
|
def_id_arg: rustc_middle::query::queries::$name::Key<'tcx>,
|
|
|
|
|
) -> rustc_middle::query::queries::$name::ProvidedValue<'tcx> {
|
2022-02-17 21:33:23 +01:00
|
|
|
let _prof_timer =
|
|
|
|
|
$tcx.prof.generic_activity(concat!("metadata_decode_entry_", stringify!($name)));
|
|
|
|
|
|
|
|
|
|
#[allow(unused_variables)]
|
|
|
|
|
let ($def_id, $other) = def_id_arg.into_args();
|
|
|
|
|
assert!(!$def_id.is_local());
|
|
|
|
|
|
|
|
|
|
// External query providers call `crate_hash` in order to register a dependency
|
|
|
|
|
// on the crate metadata. The exception is `crate_hash` itself, which obviously
|
|
|
|
|
// doesn't need to do this (and can't, as it would cause a query cycle).
|
2023-09-15 15:39:11 +02:00
|
|
|
use rustc_middle::dep_graph::dep_kinds;
|
|
|
|
|
if dep_kinds::$name != dep_kinds::crate_hash && $tcx.dep_graph.is_fully_enabled() {
|
2025-01-30 16:20:09 +11:00
|
|
|
$tcx.ensure_ok().crate_hash($def_id.krate);
|
2022-02-17 21:33:23 +01:00
|
|
|
}
|
|
|
|
|
|
2023-09-09 16:02:11 +02:00
|
|
|
let cdata = rustc_data_structures::sync::FreezeReadGuard::map(CStore::from_tcx($tcx), |c| {
|
2022-12-08 10:53:20 +00:00
|
|
|
c.get_crate_data($def_id.krate).cdata
|
|
|
|
|
});
|
|
|
|
|
let $cdata = crate::creader::CrateMetadataRef {
|
|
|
|
|
cdata: &cdata,
|
|
|
|
|
cstore: &CStore::from_tcx($tcx),
|
|
|
|
|
};
|
2022-02-17 21:33:23 +01:00
|
|
|
|
|
|
|
|
$compute
|
|
|
|
|
}
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
|
2016-09-29 02:30:53 +03:00
|
|
|
macro_rules! provide {
|
2022-08-11 21:35:33 -05:00
|
|
|
($tcx:ident, $def_id:ident, $other:ident, $cdata:ident,
|
2022-02-17 21:33:23 +01:00
|
|
|
$($name:ident => { $($compute:tt)* })*) => {
|
2023-09-22 16:38:31 +00:00
|
|
|
fn provide_extern(providers: &mut ExternProviders) {
|
2022-02-17 21:33:23 +01:00
|
|
|
$(provide_one! {
|
2022-08-11 21:35:33 -05:00
|
|
|
$tcx, $def_id, $other, $cdata, $name => { $($compute)* }
|
2016-09-29 02:30:53 +03:00
|
|
|
})*
|
|
|
|
|
|
2021-05-30 17:24:54 +02:00
|
|
|
*providers = ExternProviders {
|
2016-09-29 02:30:53 +03:00
|
|
|
$($name,)*
|
|
|
|
|
..*providers
|
|
|
|
|
};
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2017-08-30 11:40:02 -07:00
|
|
|
// small trait to work around different signature queries all being defined via
|
|
|
|
|
// the macro above.
|
|
|
|
|
trait IntoArgs {
|
2022-03-15 16:30:30 +01:00
|
|
|
type Other;
|
|
|
|
|
fn into_args(self) -> (DefId, Self::Other);
|
2017-08-28 15:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-30 11:40:02 -07:00
|
|
|
impl IntoArgs for DefId {
|
2022-03-15 16:30:30 +01:00
|
|
|
type Other = ();
|
|
|
|
|
fn into_args(self) -> (DefId, ()) {
|
|
|
|
|
(self, ())
|
2017-08-30 11:40:02 -07:00
|
|
|
}
|
2017-08-28 15:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-30 11:40:02 -07:00
|
|
|
impl IntoArgs for CrateNum {
|
2022-03-15 16:30:30 +01:00
|
|
|
type Other = ();
|
|
|
|
|
fn into_args(self) -> (DefId, ()) {
|
|
|
|
|
(self.as_def_id(), ())
|
2017-08-30 11:40:02 -07:00
|
|
|
}
|
2017-08-28 15:55:32 -07:00
|
|
|
}
|
|
|
|
|
|
2017-08-30 11:40:02 -07:00
|
|
|
impl IntoArgs for (CrateNum, DefId) {
|
2022-03-15 16:30:30 +01:00
|
|
|
type Other = DefId;
|
2017-08-30 11:40:02 -07:00
|
|
|
fn into_args(self) -> (DefId, DefId) {
|
|
|
|
|
(self.0.as_def_id(), self.1)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2024-06-16 21:35:16 -04:00
|
|
|
impl<'tcx> IntoArgs for ty::InstanceKind<'tcx> {
|
2022-03-15 16:30:30 +01:00
|
|
|
type Other = ();
|
|
|
|
|
fn into_args(self) -> (DefId, ()) {
|
|
|
|
|
(self.def_id(), ())
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
impl IntoArgs for (CrateNum, SimplifiedType) {
|
|
|
|
|
type Other = SimplifiedType;
|
|
|
|
|
fn into_args(self) -> (DefId, SimplifiedType) {
|
|
|
|
|
(self.0.as_def_id(), self.1)
|
2021-10-01 17:08:06 +00:00
|
|
|
}
|
2017-08-30 11:40:02 -07:00
|
|
|
}
|
|
|
|
|
|
2022-08-11 21:35:33 -05:00
|
|
|
provide! { tcx, def_id, other, cdata,
|
2024-08-28 23:22:40 -04:00
|
|
|
explicit_item_bounds => { table_defaulted_array }
|
2025-01-22 21:07:54 +00:00
|
|
|
explicit_item_self_bounds => { table_defaulted_array }
|
2022-02-17 21:33:23 +01:00
|
|
|
explicit_predicates_of => { table }
|
|
|
|
|
generics_of => { table }
|
2023-02-05 16:28:12 +04:00
|
|
|
inferred_outlives_of => { table_defaulted_array }
|
2024-08-29 12:53:30 -04:00
|
|
|
explicit_super_predicates_of => { table_defaulted_array }
|
|
|
|
|
explicit_implied_predicates_of => { table_defaulted_array }
|
2022-02-17 21:33:23 +01:00
|
|
|
type_of => { table }
|
2024-08-28 23:22:40 -04:00
|
|
|
type_alias_is_lazy => { table_direct }
|
2022-02-17 21:33:23 +01:00
|
|
|
variances_of => { table }
|
|
|
|
|
fn_sig => { table }
|
2022-04-27 10:22:08 +02:00
|
|
|
codegen_fn_attrs => { table }
|
2024-02-10 21:26:48 +00:00
|
|
|
impl_trait_header => { table }
|
2022-02-17 21:33:23 +01:00
|
|
|
const_param_default => { table }
|
2022-05-29 20:15:34 +02:00
|
|
|
object_lifetime_default => { table }
|
2022-02-17 21:33:23 +01:00
|
|
|
thir_abstract_const => { table }
|
|
|
|
|
optimized_mir => { table }
|
|
|
|
|
mir_for_ctfe => { table }
|
2025-10-23 12:27:56 -04:00
|
|
|
trivial_const => { table }
|
2023-06-18 07:55:04 +00:00
|
|
|
closure_saved_names_of_captured_variables => { table }
|
2023-10-19 21:46:28 +00:00
|
|
|
mir_coroutine_witnesses => { table }
|
2022-02-17 21:33:23 +01:00
|
|
|
promoted_mir => { table }
|
|
|
|
|
def_span => { table }
|
|
|
|
|
def_ident_span => { table }
|
|
|
|
|
lookup_stability => { table }
|
|
|
|
|
lookup_const_stability => { table }
|
2022-04-27 18:14:19 +04:00
|
|
|
lookup_default_body_stability => { table }
|
2022-02-17 21:33:23 +01:00
|
|
|
lookup_deprecation_entry => { table }
|
2022-08-15 14:11:11 -05:00
|
|
|
params_in_repr => { table }
|
2023-11-24 00:49:02 +03:00
|
|
|
def_kind => { cdata.def_kind(def_id.index) }
|
2022-02-17 22:00:03 +01:00
|
|
|
impl_parent => { table }
|
2023-06-01 06:14:06 +00:00
|
|
|
defaultness => { table_direct }
|
2022-06-15 20:54:43 +10:00
|
|
|
constness => { table_direct }
|
2024-10-20 19:49:11 +00:00
|
|
|
const_conditions => { table }
|
2024-11-19 20:30:58 +00:00
|
|
|
explicit_implied_const_bounds => { table_defaulted_array }
|
2024-01-23 15:23:22 +00:00
|
|
|
coerce_unsized_info => {
|
|
|
|
|
Ok(cdata
|
|
|
|
|
.root
|
|
|
|
|
.tables
|
|
|
|
|
.coerce_unsized_info
|
|
|
|
|
.get(cdata, def_id.index)
|
|
|
|
|
.map(|lazy| lazy.decode((cdata, tcx)))
|
|
|
|
|
.process_decoded(tcx, || panic!("{def_id:?} does not have coerce_unsized_info"))) }
|
2022-02-18 18:53:47 +01:00
|
|
|
mir_const_qualif => { table }
|
2022-02-18 19:09:02 +01:00
|
|
|
rendered_const => { table }
|
2024-07-12 13:07:16 -04:00
|
|
|
rendered_precise_capturing_args => { table }
|
2022-05-22 11:16:14 -07:00
|
|
|
asyncness => { table_direct }
|
2025-04-08 12:23:07 +10:00
|
|
|
fn_arg_idents => { table }
|
2023-12-21 01:43:10 +00:00
|
|
|
coroutine_kind => { table_direct }
|
2024-02-10 22:58:23 +00:00
|
|
|
coroutine_for_closure => { table }
|
2024-09-10 11:39:46 -04:00
|
|
|
coroutine_by_move_body_def_id => { table }
|
2023-10-10 08:52:21 +00:00
|
|
|
eval_static_initializer => {
|
2023-10-09 12:20:26 +00:00
|
|
|
Ok(cdata
|
|
|
|
|
.root
|
|
|
|
|
.tables
|
2023-10-10 08:52:21 +00:00
|
|
|
.eval_static_initializer
|
2023-10-09 12:20:26 +00:00
|
|
|
.get(cdata, def_id.index)
|
|
|
|
|
.map(|lazy| lazy.decode((cdata, tcx)))
|
2023-10-10 08:52:21 +00:00
|
|
|
.unwrap_or_else(|| panic!("{def_id:?} does not have eval_static_initializer")))
|
2023-10-09 12:20:26 +00:00
|
|
|
}
|
2022-03-10 19:07:27 +01:00
|
|
|
trait_def => { table }
|
2024-08-29 20:49:39 -04:00
|
|
|
deduced_param_attrs => {
|
|
|
|
|
// FIXME: `deduced_param_attrs` has some sketchy encoding settings,
|
|
|
|
|
// where we don't encode unless we're optimizing, doing codegen,
|
|
|
|
|
// and not incremental (see `encoder.rs`). I don't think this is right!
|
|
|
|
|
cdata
|
|
|
|
|
.root
|
|
|
|
|
.tables
|
|
|
|
|
.deduced_param_attrs
|
|
|
|
|
.get(cdata, def_id.index)
|
|
|
|
|
.map(|lazy| {
|
|
|
|
|
&*tcx.arena.alloc_from_iter(lazy.decode((cdata, tcx)))
|
|
|
|
|
})
|
|
|
|
|
.unwrap_or_default()
|
|
|
|
|
}
|
2024-10-02 23:16:31 -04:00
|
|
|
opaque_ty_origin => { table }
|
2023-07-17 18:12:49 +00:00
|
|
|
assumed_wf_types_for_rpitit => { table }
|
2022-12-28 04:16:36 +00:00
|
|
|
collect_return_position_impl_trait_in_trait_tys => {
|
2022-09-23 00:56:55 +00:00
|
|
|
Ok(cdata
|
|
|
|
|
.root
|
|
|
|
|
.tables
|
|
|
|
|
.trait_impl_trait_tys
|
|
|
|
|
.get(cdata, def_id.index)
|
|
|
|
|
.map(|lazy| lazy.decode((cdata, tcx)))
|
2022-12-19 10:31:55 +01:00
|
|
|
.process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys")))
|
2023-02-02 20:37:02 +00:00
|
|
|
}
|
2022-02-17 21:33:23 +01:00
|
|
|
|
2025-07-13 02:52:03 +08:00
|
|
|
associated_types_for_impl_traits_in_trait_or_impl => { table }
|
2022-10-26 16:38:32 -03:00
|
|
|
|
2022-08-28 00:10:06 +03:00
|
|
|
visibility => { cdata.get_visibility(def_id.index) }
|
2016-09-29 02:30:53 +03:00
|
|
|
adt_def => { cdata.get_adt_def(def_id.index, tcx) }
|
2025-03-27 12:08:00 +00:00
|
|
|
adt_destructor => { table }
|
2025-03-27 12:46:04 +00:00
|
|
|
adt_async_destructor => { table }
|
2022-04-05 23:46:44 +03:00
|
|
|
associated_item_def_ids => {
|
2023-04-10 16:44:54 +03:00
|
|
|
tcx.arena.alloc_from_iter(cdata.get_associated_item_or_field_def_ids(def_id.index))
|
2022-04-05 23:46:44 +03:00
|
|
|
}
|
2022-08-04 22:03:16 +02:00
|
|
|
associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) }
|
2024-09-23 18:07:42 -04:00
|
|
|
inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) }
|
2024-10-17 01:14:01 +02:00
|
|
|
attrs_for_def => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) }
|
2017-05-04 12:45:56 -05:00
|
|
|
is_mir_available => { cdata.is_item_mir_available(def_id.index) }
|
2020-10-28 13:12:49 +00:00
|
|
|
is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) }
|
2024-08-28 23:22:40 -04:00
|
|
|
cross_crate_inlinable => { table_direct }
|
2017-06-14 22:49:07 -07:00
|
|
|
|
2018-12-01 17:27:12 +01:00
|
|
|
dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) }
|
2024-01-04 21:09:13 +03:00
|
|
|
is_private_dep => { cdata.private_dep }
|
2018-05-12 16:36:53 -07:00
|
|
|
is_panic_runtime => { cdata.root.panic_runtime }
|
|
|
|
|
is_compiler_builtins => { cdata.root.compiler_builtins }
|
|
|
|
|
has_global_allocator => { cdata.root.has_global_allocator }
|
2023-04-25 00:08:35 +02:00
|
|
|
has_alloc_error_handler => { cdata.root.has_alloc_error_handler }
|
2018-09-06 21:24:33 +02:00
|
|
|
has_panic_handler => { cdata.root.has_panic_handler }
|
2018-05-12 16:36:53 -07:00
|
|
|
is_profiler_runtime => { cdata.root.profiler_runtime }
|
2022-06-08 21:32:17 +01:00
|
|
|
required_panic_strategy => { cdata.root.required_panic_strategy }
|
2021-09-09 13:31:19 +01:00
|
|
|
panic_in_drop_strategy => { cdata.root.panic_in_drop_strategy }
|
2024-01-04 21:09:13 +03:00
|
|
|
extern_crate => { cdata.extern_crate.map(|c| &*tcx.arena.alloc(c)) }
|
2018-05-12 16:36:53 -07:00
|
|
|
is_no_builtins => { cdata.root.no_builtins }
|
2019-01-29 07:24:32 +02:00
|
|
|
symbol_mangling_version => { cdata.root.symbol_mangling_version }
|
2024-06-07 15:58:05 -04:00
|
|
|
specialization_enabled_in => { cdata.root.specialization_enabled_in }
|
2018-02-27 19:28:21 +01:00
|
|
|
reachable_non_generics => {
|
|
|
|
|
let reachable_non_generics = tcx
|
2025-06-25 14:19:57 +00:00
|
|
|
.exported_non_generic_symbols(cdata.cnum)
|
2018-02-27 19:28:21 +01:00
|
|
|
.iter()
|
2022-04-02 22:27:33 +01:00
|
|
|
.filter_map(|&(exported_symbol, export_info)| {
|
2018-02-27 19:28:21 +01:00
|
|
|
if let ExportedSymbol::NonGeneric(def_id) = exported_symbol {
|
2022-04-02 22:27:33 +01:00
|
|
|
Some((def_id, export_info))
|
2018-02-27 19:28:21 +01:00
|
|
|
} else {
|
|
|
|
|
None
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
.collect();
|
|
|
|
|
|
2020-03-27 20:26:20 +01:00
|
|
|
reachable_non_generics
|
2018-02-27 19:28:21 +01:00
|
|
|
}
|
2022-01-31 19:55:34 +01:00
|
|
|
native_libraries => { cdata.get_native_libraries(tcx.sess).collect() }
|
|
|
|
|
foreign_modules => { cdata.get_foreign_modules(tcx.sess).map(|m| (m.def_id, m)).collect() }
|
2023-05-21 09:40:00 -05:00
|
|
|
crate_hash => { cdata.root.header.hash }
|
2019-11-24 18:12:02 +03:00
|
|
|
crate_host_hash => { cdata.host_hash }
|
2023-05-21 09:40:00 -05:00
|
|
|
crate_name => { cdata.root.header.name }
|
2024-10-25 01:05:59 -07:00
|
|
|
num_extern_def_ids => { cdata.num_def_ids() }
|
2017-08-30 11:40:02 -07:00
|
|
|
|
2018-03-13 11:58:53 -07:00
|
|
|
extra_filename => { cdata.root.extra_filename.clone() }
|
|
|
|
|
|
2023-05-23 13:21:22 +00:00
|
|
|
traits => { tcx.arena.alloc_from_iter(cdata.get_traits()) }
|
2023-02-05 23:05:46 +04:00
|
|
|
trait_impls_in_crate => { tcx.arena.alloc_from_iter(cdata.get_trait_impls()) }
|
2022-01-06 15:13:22 +08:00
|
|
|
implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) }
|
2024-09-23 18:07:42 -04:00
|
|
|
crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) }
|
2017-08-30 14:48:57 -07:00
|
|
|
|
2024-01-04 21:09:13 +03:00
|
|
|
dep_kind => { cdata.dep_kind }
|
2021-12-23 16:12:34 +08:00
|
|
|
module_children => {
|
2022-11-22 20:01:44 +03:00
|
|
|
tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess))
|
2017-08-31 08:07:39 -07:00
|
|
|
}
|
2023-11-17 23:40:04 +00:00
|
|
|
lib_features => { cdata.get_lib_features() }
|
2022-07-13 15:10:19 +01:00
|
|
|
stability_implications => {
|
|
|
|
|
cdata.get_stability_implications(tcx).iter().copied().collect()
|
|
|
|
|
}
|
2023-03-10 22:39:14 +01:00
|
|
|
stripped_cfg_items => { cdata.get_stripped_cfg_items(cdata.cnum, tcx) }
|
2024-03-05 04:29:12 +00:00
|
|
|
intrinsic_raw => { cdata.get_intrinsic(def_id.index) }
|
2022-03-18 17:02:32 +01:00
|
|
|
defined_lang_items => { cdata.get_lang_items(tcx) }
|
2020-03-27 20:26:20 +01:00
|
|
|
diagnostic_items => { cdata.get_diagnostic_items() }
|
2018-12-01 16:57:29 +01:00
|
|
|
missing_lang_items => { cdata.get_missing_lang_items(tcx) }
|
2017-08-31 11:12:05 -07:00
|
|
|
|
2017-08-31 11:30:22 -07:00
|
|
|
missing_extern_crate_item => {
|
2024-01-04 21:09:13 +03:00
|
|
|
matches!(cdata.extern_crate, Some(extern_crate) if !extern_crate.is_direct())
|
2017-08-31 11:30:22 -07:00
|
|
|
}
|
2017-08-31 12:08:29 -07:00
|
|
|
|
2025-02-03 06:44:41 +03:00
|
|
|
used_crate_source => { Arc::clone(&cdata.source) }
|
2022-04-25 18:02:43 -07:00
|
|
|
debugger_visualizers => { cdata.get_debugger_visualizers() }
|
2017-09-20 20:42:49 +02:00
|
|
|
|
2024-09-30 21:07:36 +03:00
|
|
|
exportable_items => { tcx.arena.alloc_from_iter(cdata.get_exportable_items()) }
|
|
|
|
|
stable_order_of_exportable_impls => { tcx.arena.alloc(cdata.get_stable_order_of_exportable_impls().collect()) }
|
2025-06-25 14:19:57 +00:00
|
|
|
exported_non_generic_symbols => { cdata.exported_non_generic_symbols(tcx) }
|
|
|
|
|
exported_generic_symbols => { cdata.exported_generic_symbols(tcx) }
|
2020-06-27 14:54:19 -07:00
|
|
|
|
|
|
|
|
crate_extern_paths => { cdata.source().paths().cloned().collect() }
|
2020-10-05 15:32:25 -04:00
|
|
|
expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) }
|
2025-01-21 21:07:20 +00:00
|
|
|
default_field => { cdata.get_default_field(def_id.index) }
|
2023-01-21 00:59:19 +04:00
|
|
|
is_doc_hidden => { cdata.get_attr_flags(def_id.index).contains(AttrFlags::IS_DOC_HIDDEN) }
|
2022-02-01 20:30:32 +08:00
|
|
|
doc_link_resolutions => { tcx.arena.alloc(cdata.get_doc_link_resolutions(def_id.index)) }
|
|
|
|
|
doc_link_traits_in_scope => {
|
|
|
|
|
tcx.arena.alloc_from_iter(cdata.get_doc_link_traits_in_scope(def_id.index))
|
|
|
|
|
}
|
2025-03-12 11:23:20 +00:00
|
|
|
anon_const_kind => { table }
|
2016-09-29 02:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
2021-12-31 18:15:52 +08:00
|
|
|
pub(in crate::rmeta) fn provide(providers: &mut Providers) {
|
2024-03-26 12:06:07 +00:00
|
|
|
provide_cstore_hooks(providers);
|
2023-09-22 16:38:31 +00:00
|
|
|
providers.queries = rustc_middle::query::Providers {
|
2021-06-06 11:30:08 +02:00
|
|
|
allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(),
|
2023-04-25 00:08:35 +02:00
|
|
|
alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(),
|
2023-03-13 22:22:59 +00:00
|
|
|
is_private_dep: |_tcx, LocalCrate| false,
|
2022-07-12 13:52:35 -07:00
|
|
|
native_library: |tcx, id| {
|
2017-08-30 14:48:57 -07:00
|
|
|
tcx.native_libraries(id.krate)
|
|
|
|
|
.iter()
|
2023-11-21 20:07:32 +01:00
|
|
|
.filter(|lib| native_libs::relevant_lib(tcx.sess, lib))
|
2018-02-10 14:28:17 -08:00
|
|
|
.find(|lib| {
|
2022-02-19 00:48:49 +01:00
|
|
|
let Some(fm_id) = lib.foreign_module else {
|
|
|
|
|
return false;
|
2018-02-10 14:28:17 -08:00
|
|
|
};
|
2020-10-27 15:01:03 +01:00
|
|
|
let map = tcx.foreign_modules(id.krate);
|
2020-10-27 20:17:48 +01:00
|
|
|
map.get(&fm_id)
|
2018-02-10 14:28:17 -08:00
|
|
|
.expect("failed to find foreign module")
|
|
|
|
|
.foreign_items
|
|
|
|
|
.contains(&id)
|
|
|
|
|
})
|
2017-08-30 14:48:57 -07:00
|
|
|
},
|
2023-07-15 10:17:37 +00:00
|
|
|
native_libraries: native_libs::collect,
|
2023-07-15 10:09:36 +00:00
|
|
|
foreign_modules: foreign_modules::collect,
|
2017-09-07 08:13:41 -07:00
|
|
|
|
2018-11-27 02:59:49 +00:00
|
|
|
// Returns a map from a sufficiently visible external item (i.e., an
|
2017-08-31 11:30:22 -07:00
|
|
|
// external item that is visible from at least one local module) to a
|
|
|
|
|
// sufficiently visible parent (considering modules that re-export the
|
|
|
|
|
// external item to be parents).
|
2021-05-11 14:16:48 +02:00
|
|
|
visible_parent_map: |tcx, ()| {
|
2017-08-31 11:30:22 -07:00
|
|
|
use std::collections::hash_map::Entry;
|
|
|
|
|
use std::collections::vec_deque::VecDeque;
|
|
|
|
|
|
2018-07-21 22:15:11 +03:00
|
|
|
let mut visible_parent_map: DefIdMap<DefId> = Default::default();
|
2022-07-24 21:25:35 +00:00
|
|
|
// This is a secondary visible_parent_map, storing the DefId of
|
|
|
|
|
// parents that re-export the child as `_` or module parents
|
|
|
|
|
// which are `#[doc(hidden)]`. Since we prefer paths that don't
|
|
|
|
|
// do this, merge this map at the end, only if we're missing
|
|
|
|
|
// keys from the former.
|
|
|
|
|
// This is a rudimentary check that does not catch all cases,
|
|
|
|
|
// just the easiest.
|
2023-01-17 12:05:01 +01:00
|
|
|
let mut fallback_map: Vec<(DefId, DefId)> = Default::default();
|
2017-08-31 11:30:22 -07:00
|
|
|
|
2017-12-12 19:05:12 -06:00
|
|
|
// Issue 46112: We want the map to prefer the shortest
|
|
|
|
|
// paths when reporting the path to an item. Therefore we
|
|
|
|
|
// build up the map via a breadth-first search (BFS),
|
|
|
|
|
// which naturally yields minimal-length paths.
|
|
|
|
|
//
|
|
|
|
|
// Note that it needs to be a BFS over the whole forest of
|
|
|
|
|
// crates, not just each individual crate; otherwise you
|
|
|
|
|
// only get paths that are locally minimal with respect to
|
|
|
|
|
// whatever crate we happened to encounter first in this
|
|
|
|
|
// traversal, but not globally minimal across all crates.
|
|
|
|
|
let bfs_queue = &mut VecDeque::new();
|
2017-12-19 15:04:02 +01:00
|
|
|
|
2024-06-06 09:45:50 +00:00
|
|
|
for &cnum in tcx.crates(()) {
|
2017-08-31 11:30:22 -07:00
|
|
|
// Ignore crates without a corresponding local `extern crate` item.
|
|
|
|
|
if tcx.missing_extern_crate_item(cnum) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
2022-04-15 19:27:53 +02:00
|
|
|
bfs_queue.push_back(cnum.as_def_id());
|
2017-12-12 19:05:12 -06:00
|
|
|
}
|
|
|
|
|
|
2021-12-21 11:24:43 +08:00
|
|
|
let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| {
|
2021-12-18 20:07:58 +08:00
|
|
|
if !child.vis.is_public() {
|
2021-09-17 09:13:16 +02:00
|
|
|
return;
|
|
|
|
|
}
|
2017-08-31 11:30:22 -07:00
|
|
|
|
2021-12-18 20:07:58 +08:00
|
|
|
if let Some(def_id) = child.res.opt_def_id() {
|
|
|
|
|
if child.ident.name == kw::Underscore {
|
2023-01-17 12:05:01 +01:00
|
|
|
fallback_map.push((def_id, parent));
|
2021-11-30 17:18:48 -08:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2023-01-21 00:59:19 +04:00
|
|
|
if tcx.is_doc_hidden(parent) {
|
2023-01-17 12:05:01 +01:00
|
|
|
fallback_map.push((def_id, parent));
|
2022-07-24 21:25:35 +00:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2021-12-18 20:07:58 +08:00
|
|
|
match visible_parent_map.entry(def_id) {
|
2021-09-17 09:13:16 +02:00
|
|
|
Entry::Occupied(mut entry) => {
|
|
|
|
|
// If `child` is defined in crate `cnum`, ensure
|
|
|
|
|
// that it is mapped to a parent in `cnum`.
|
2021-12-18 20:07:58 +08:00
|
|
|
if def_id.is_local() && entry.get().is_local() {
|
2021-09-05 23:37:15 +03:00
|
|
|
entry.insert(parent);
|
|
|
|
|
}
|
2017-08-31 11:30:22 -07:00
|
|
|
}
|
2021-09-17 09:13:16 +02:00
|
|
|
Entry::Vacant(entry) => {
|
|
|
|
|
entry.insert(parent);
|
2025-07-04 22:21:31 +03:00
|
|
|
if child.res.module_like_def_id().is_some() {
|
2021-12-18 20:07:58 +08:00
|
|
|
bfs_queue.push_back(def_id);
|
|
|
|
|
}
|
2021-09-17 09:13:16 +02:00
|
|
|
}
|
2021-09-05 23:37:15 +03:00
|
|
|
}
|
2021-09-17 09:13:16 +02:00
|
|
|
}
|
|
|
|
|
};
|
2017-08-31 11:30:22 -07:00
|
|
|
|
2021-09-17 09:13:16 +02:00
|
|
|
while let Some(def) = bfs_queue.pop_front() {
|
2021-12-23 16:12:34 +08:00
|
|
|
for child in tcx.module_children(def).iter() {
|
2021-09-17 09:13:16 +02:00
|
|
|
add_child(bfs_queue, child, def);
|
2017-08-31 11:30:22 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-24 21:25:35 +00:00
|
|
|
// Fill in any missing entries with the less preferable path.
|
|
|
|
|
// If this path re-exports the child as `_`, we still use this
|
|
|
|
|
// path in a diagnostic that suggests importing `::*`.
|
2023-01-17 12:05:01 +01:00
|
|
|
|
2021-11-30 17:18:48 -08:00
|
|
|
for (child, parent) in fallback_map {
|
|
|
|
|
visible_parent_map.entry(child).or_insert(parent);
|
|
|
|
|
}
|
|
|
|
|
|
2022-01-31 19:55:34 +01:00
|
|
|
visible_parent_map
|
2017-08-31 11:30:22 -07:00
|
|
|
},
|
|
|
|
|
|
2025-02-03 06:44:41 +03:00
|
|
|
dependency_formats: |tcx, ()| Arc::new(crate::dependency_format::calculate(tcx)),
|
2023-03-13 22:22:59 +00:00
|
|
|
has_global_allocator: |tcx, LocalCrate| CStore::from_tcx(tcx).has_global_allocator(),
|
2023-04-25 00:08:35 +02:00
|
|
|
has_alloc_error_handler: |tcx, LocalCrate| CStore::from_tcx(tcx).has_alloc_error_handler(),
|
2021-05-11 14:50:54 +02:00
|
|
|
postorder_cnums: |tcx, ()| {
|
2025-05-29 12:45:04 +02:00
|
|
|
tcx.arena.alloc_from_iter(
|
|
|
|
|
CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE).into_iter(),
|
|
|
|
|
)
|
2019-11-24 18:12:02 +03:00
|
|
|
},
|
2024-06-06 09:45:50 +00:00
|
|
|
crates: |tcx, ()| {
|
2023-03-23 20:44:48 +04:00
|
|
|
// The list of loaded crates is now frozen in query cache,
|
|
|
|
|
// so make sure cstore is not mutably accessed from here on.
|
2023-09-09 16:02:11 +02:00
|
|
|
tcx.untracked().cstore.freeze();
|
2023-02-22 18:08:44 +04:00
|
|
|
tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).iter_crate_data().map(|(cnum, _)| cnum))
|
|
|
|
|
},
|
2024-02-14 21:40:45 +03:00
|
|
|
used_crates: |tcx, ()| {
|
|
|
|
|
// The list of loaded crates is now frozen in query cache,
|
|
|
|
|
// so make sure cstore is not mutably accessed from here on.
|
|
|
|
|
tcx.untracked().cstore.freeze();
|
|
|
|
|
tcx.arena.alloc_from_iter(
|
|
|
|
|
CStore::from_tcx(tcx)
|
|
|
|
|
.iter_crate_data()
|
|
|
|
|
.filter_map(|(cnum, data)| data.used().then_some(cnum)),
|
|
|
|
|
)
|
|
|
|
|
},
|
2023-09-22 16:38:31 +00:00
|
|
|
..providers.queries
|
2017-06-11 21:16:26 -07:00
|
|
|
};
|
2023-09-22 16:38:31 +00:00
|
|
|
provide_extern(&mut providers.extern_queries);
|
2016-09-29 02:30:53 +03:00
|
|
|
}
|
|
|
|
|
|
2019-11-24 01:10:12 +03:00
|
|
|
impl CStore {
|
2022-10-25 20:15:15 +04:00
|
|
|
pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> {
|
|
|
|
|
self.get_crate_data(def.krate).get_ctor(def.index)
|
2021-05-07 22:42:12 -07:00
|
|
|
}
|
|
|
|
|
|
2023-08-09 20:28:00 +08:00
|
|
|
pub fn load_macro_untracked(&self, id: DefId, tcx: TyCtxt<'_>) -> LoadedMacro {
|
|
|
|
|
let sess = tcx.sess;
|
2019-10-09 16:43:47 +02:00
|
|
|
let _prof_timer = sess.prof.generic_activity("metadata_load_macro");
|
|
|
|
|
|
2016-11-05 20:30:40 +00:00
|
|
|
let data = self.get_crate_data(id.krate);
|
2019-11-17 18:54:22 +03:00
|
|
|
if data.root.is_proc_macro_crate() {
|
2024-10-26 18:51:15 +03:00
|
|
|
LoadedMacro::ProcMacro(data.load_proc_macro(id.index, tcx))
|
|
|
|
|
} else {
|
|
|
|
|
LoadedMacro::MacroDef {
|
|
|
|
|
def: data.get_macro(id.index, sess),
|
2021-12-21 20:11:36 +08:00
|
|
|
ident: data.item_ident(id.index, sess),
|
|
|
|
|
attrs: data.get_item_attrs(id.index, sess).collect(),
|
2024-10-26 18:51:15 +03:00
|
|
|
span: data.get_span(id.index, sess),
|
|
|
|
|
edition: data.root.edition,
|
|
|
|
|
}
|
|
|
|
|
}
|
2016-10-28 06:52:45 +00:00
|
|
|
}
|
|
|
|
|
|
2023-03-23 20:15:04 +04:00
|
|
|
pub fn def_span_untracked(&self, def_id: DefId, sess: &Session) -> Span {
|
2019-10-14 17:20:50 -07:00
|
|
|
self.get_crate_data(def_id.krate).get_span(def_id.index, sess)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 20:15:04 +04:00
|
|
|
pub fn def_kind_untracked(&self, def: DefId) -> DefKind {
|
2021-07-12 21:20:16 +02:00
|
|
|
self.get_crate_data(def.krate).def_kind(def.index)
|
|
|
|
|
}
|
|
|
|
|
|
2023-03-23 20:15:04 +04:00
|
|
|
pub fn expn_that_defined_untracked(&self, def_id: DefId, sess: &Session) -> ExpnId {
|
|
|
|
|
self.get_crate_data(def_id.krate).get_expn_that_defined(def_id.index, sess)
|
2020-03-17 11:45:02 -04:00
|
|
|
}
|
2020-12-02 23:39:05 +03:00
|
|
|
|
2021-06-01 14:10:14 +02:00
|
|
|
/// Only public-facing way to traverse all the definitions in a non-local crate.
|
|
|
|
|
/// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>.
|
|
|
|
|
/// See <https://github.com/rust-lang/rust/pull/85889> for context.
|
|
|
|
|
pub fn num_def_ids_untracked(&self, cnum: CrateNum) -> usize {
|
|
|
|
|
self.get_crate_data(cnum).num_def_ids()
|
|
|
|
|
}
|
|
|
|
|
|
Implement span quoting for proc-macros
This PR implements span quoting, allowing proc-macros to produce spans
pointing *into their own crate*. This is used by the unstable
`proc_macro::quote!` macro, allowing us to get error messages like this:
```
error[E0412]: cannot find type `MissingType` in this scope
--> $DIR/auxiliary/span-from-proc-macro.rs:37:20
|
LL | pub fn error_from_attribute(_args: TokenStream, _input: TokenStream) -> TokenStream {
| ----------------------------------------------------------------------------------- in this expansion of procedural macro `#[error_from_attribute]`
...
LL | field: MissingType
| ^^^^^^^^^^^ not found in this scope
|
::: $DIR/span-from-proc-macro.rs:8:1
|
LL | #[error_from_attribute]
| ----------------------- in this macro invocation
```
Here, `MissingType` occurs inside the implementation of the proc-macro
`#[error_from_attribute]`. Previosuly, this would always result in a
span pointing at `#[error_from_attribute]`
This will make many proc-macro-related error message much more useful -
when a proc-macro generates code containing an error, users will get an
error message pointing directly at that code (within the macro
definition), instead of always getting a span pointing at the macro
invocation site.
This is implemented as follows:
* When a proc-macro crate is being *compiled*, it causes the `quote!`
macro to get run. This saves all of the sapns in the input to `quote!`
into the metadata of *the proc-macro-crate* (which we are currently
compiling). The `quote!` macro then expands to a call to
`proc_macro::Span::recover_proc_macro_span(id)`, where `id` is an
opaque identifier for the span in the crate metadata.
* When the same proc-macro crate is *run* (e.g. it is loaded from disk
and invoked by some consumer crate), the call to
`proc_macro::Span::recover_proc_macro_span` causes us to load the span
from the proc-macro crate's metadata. The proc-macro then produces a
`TokenStream` containing a `Span` pointing into the proc-macro crate
itself.
The recursive nature of 'quote!' can be difficult to understand at
first. The file `src/test/ui/proc-macro/quote-debug.stdout` shows
the output of the `quote!` macro, which should make this eaier to
understand.
This PR also supports custom quoting spans in custom quote macros (e.g.
the `quote` crate). All span quoting goes through the
`proc_macro::quote_span` method, which can be called by a custom quote
macro to perform span quoting. An example of this usage is provided in
`src/test/ui/proc-macro/auxiliary/custom-quote.rs`
Custom quoting currently has a few limitations:
In order to quote a span, we need to generate a call to
`proc_macro::Span::recover_proc_macro_span`. However, proc-macros
support renaming the `proc_macro` crate, so we can't simply hardcode
this path. Previously, the `quote_span` method used the path
`crate::Span` - however, this only works when it is called by the
builtin `quote!` macro in the same crate. To support being called from
arbitrary crates, we need access to the name of the `proc_macro` crate
to generate a path. This PR adds an additional argument to `quote_span`
to specify the name of the `proc_macro` crate. Howver, this feels kind
of hacky, and we may want to change this before stabilizing anything
quote-related.
Additionally, using `quote_span` currently requires enabling the
`proc_macro_internals` feature. The builtin `quote!` macro
has an `#[allow_internal_unstable]` attribute, but this won't work for
custom quote implementations. This will likely require some additional
tricks to apply `allow_internal_unstable` to the span of
`proc_macro::Span::recover_proc_macro_span`.
2020-08-02 19:52:16 -04:00
|
|
|
pub fn get_proc_macro_quoted_span_untracked(
|
|
|
|
|
&self,
|
|
|
|
|
cnum: CrateNum,
|
|
|
|
|
id: usize,
|
|
|
|
|
sess: &Session,
|
|
|
|
|
) -> Span {
|
|
|
|
|
self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess)
|
|
|
|
|
}
|
2024-01-04 21:09:13 +03:00
|
|
|
|
2024-01-04 16:57:24 +03:00
|
|
|
pub fn set_used_recursively(&mut self, cnum: CrateNum) {
|
|
|
|
|
let cmeta = self.get_crate_data_mut(cnum);
|
|
|
|
|
if !cmeta.used {
|
|
|
|
|
cmeta.used = true;
|
|
|
|
|
let dependencies = mem::take(&mut cmeta.dependencies);
|
|
|
|
|
for &dep_cnum in &dependencies {
|
|
|
|
|
self.set_used_recursively(dep_cnum);
|
|
|
|
|
}
|
|
|
|
|
self.get_crate_data_mut(cnum).dependencies = dependencies;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2025-07-03 13:41:54 +01:00
|
|
|
/// Track how an extern crate has been loaded. Called after resolving an import in the local crate.
|
2025-07-15 10:48:17 +01:00
|
|
|
///
|
|
|
|
|
/// * the `name` is for [`Self::set_resolved_extern_crate_name`] saving `--extern name=`
|
|
|
|
|
/// * `extern_crate` is for diagnostics
|
2025-07-03 13:41:54 +01:00
|
|
|
pub(crate) fn update_extern_crate(
|
|
|
|
|
&mut self,
|
|
|
|
|
cnum: CrateNum,
|
2025-07-15 10:48:17 +01:00
|
|
|
name: Symbol,
|
2025-07-03 13:41:54 +01:00
|
|
|
extern_crate: ExternCrate,
|
|
|
|
|
) {
|
|
|
|
|
debug_assert_eq!(
|
|
|
|
|
extern_crate.dependency_of, LOCAL_CRATE,
|
|
|
|
|
"this function should not be called on transitive dependencies"
|
|
|
|
|
);
|
2025-07-15 10:48:17 +01:00
|
|
|
self.set_resolved_extern_crate_name(name, cnum);
|
2025-07-03 13:41:54 +01:00
|
|
|
self.update_transitive_extern_crate_diagnostics(cnum, extern_crate);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// `CrateMetadata` uses `ExternCrate` only for diagnostics
|
|
|
|
|
fn update_transitive_extern_crate_diagnostics(
|
|
|
|
|
&mut self,
|
|
|
|
|
cnum: CrateNum,
|
|
|
|
|
extern_crate: ExternCrate,
|
|
|
|
|
) {
|
2024-01-04 21:09:13 +03:00
|
|
|
let cmeta = self.get_crate_data_mut(cnum);
|
2025-07-03 13:41:54 +01:00
|
|
|
if cmeta.update_extern_crate_diagnostics(extern_crate) {
|
2024-01-04 21:09:13 +03:00
|
|
|
// Propagate the extern crate info to dependencies if it was updated.
|
|
|
|
|
let extern_crate = ExternCrate { dependency_of: cnum, ..extern_crate };
|
2024-01-04 16:57:24 +03:00
|
|
|
let dependencies = mem::take(&mut cmeta.dependencies);
|
2024-01-04 21:09:13 +03:00
|
|
|
for &dep_cnum in &dependencies {
|
2025-07-03 13:41:54 +01:00
|
|
|
self.update_transitive_extern_crate_diagnostics(dep_cnum, extern_crate);
|
2024-01-04 21:09:13 +03:00
|
|
|
}
|
|
|
|
|
self.get_crate_data_mut(cnum).dependencies = dependencies;
|
|
|
|
|
}
|
|
|
|
|
}
|
2018-07-31 17:23:29 -06:00
|
|
|
}
|
|
|
|
|
|
2019-11-24 01:10:12 +03:00
|
|
|
impl CrateStore for CStore {
|
2019-11-24 18:12:02 +03:00
|
|
|
fn as_any(&self) -> &dyn Any {
|
|
|
|
|
self
|
2018-07-31 17:23:29 -06:00
|
|
|
}
|
2022-12-07 14:31:50 +00:00
|
|
|
fn untracked_as_any(&mut self) -> &mut dyn Any {
|
|
|
|
|
self
|
|
|
|
|
}
|
2018-07-31 17:23:29 -06:00
|
|
|
|
2021-07-12 21:20:16 +02:00
|
|
|
fn crate_name(&self, cnum: CrateNum) -> Symbol {
|
2023-05-21 09:40:00 -05:00
|
|
|
self.get_crate_data(cnum).root.header.name
|
2018-07-31 17:23:29 -06:00
|
|
|
}
|
|
|
|
|
|
2021-07-12 21:20:16 +02:00
|
|
|
fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId {
|
2021-06-08 18:36:30 +02:00
|
|
|
self.get_crate_data(cnum).root.stable_crate_id
|
2018-07-31 17:23:29 -06:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Returns the `DefKey` for a given `DefId`. This indicates the
|
|
|
|
|
/// parent `DefId` as well as some idea of what kind of data the
|
|
|
|
|
/// `DefId` refers to.
|
|
|
|
|
fn def_key(&self, def: DefId) -> DefKey {
|
|
|
|
|
self.get_crate_data(def.krate).def_key(def.index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn def_path(&self, def: DefId) -> DefPath {
|
|
|
|
|
self.get_crate_data(def.krate).def_path(def.index)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
fn def_path_hash(&self, def: DefId) -> DefPathHash {
|
|
|
|
|
self.get_crate_data(def.krate).def_path_hash(def.index)
|
|
|
|
|
}
|
2024-03-26 12:06:07 +00:00
|
|
|
}
|
2021-12-21 14:01:10 -05:00
|
|
|
|
2024-03-26 12:06:07 +00:00
|
|
|
fn provide_cstore_hooks(providers: &mut Providers) {
|
2024-03-26 15:53:05 +00:00
|
|
|
providers.hooks.def_path_hash_to_def_id_extern = |tcx, hash, stable_crate_id| {
|
|
|
|
|
// If this is a DefPathHash from an upstream crate, let the CrateStore map
|
|
|
|
|
// it to a DefId.
|
2025-01-31 16:52:39 +11:00
|
|
|
let cstore = CStore::from_tcx(tcx);
|
2024-03-26 16:52:06 +00:00
|
|
|
let cnum = *tcx
|
|
|
|
|
.untracked()
|
|
|
|
|
.stable_crate_ids
|
|
|
|
|
.read()
|
|
|
|
|
.get(&stable_crate_id)
|
|
|
|
|
.unwrap_or_else(|| bug!("uninterned StableCrateId: {stable_crate_id:?}"));
|
|
|
|
|
assert_ne!(cnum, LOCAL_CRATE);
|
2025-09-26 18:14:31 +02:00
|
|
|
let def_index = cstore.get_crate_data(cnum).def_path_hash_to_def_index(hash)?;
|
|
|
|
|
Some(DefId { krate: cnum, index: def_index })
|
2024-03-26 15:53:05 +00:00
|
|
|
};
|
|
|
|
|
|
2024-03-26 12:31:41 +00:00
|
|
|
providers.hooks.expn_hash_to_expn_id = |tcx, cnum, index_guess, hash| {
|
2025-01-31 16:52:39 +11:00
|
|
|
let cstore = CStore::from_tcx(tcx);
|
2024-03-26 12:31:41 +00:00
|
|
|
cstore.get_crate_data(cnum).expn_hash_to_expn_id(tcx.sess, index_guess, hash)
|
|
|
|
|
};
|
2024-03-26 12:06:07 +00:00
|
|
|
providers.hooks.import_source_files = |tcx, cnum| {
|
2025-01-31 16:52:39 +11:00
|
|
|
let cstore = CStore::from_tcx(tcx);
|
2024-03-26 12:06:07 +00:00
|
|
|
let cdata = cstore.get_crate_data(cnum);
|
2022-08-06 23:00:49 +02:00
|
|
|
for file_index in 0..cdata.root.source_map.size() {
|
2024-03-26 12:06:07 +00:00
|
|
|
cdata.imported_source_file(file_index as u32, tcx.sess);
|
2022-08-06 23:00:49 +02:00
|
|
|
}
|
2024-03-26 12:06:07 +00:00
|
|
|
};
|
2017-05-18 20:56:25 +03:00
|
|
|
}
|