]>
Commit | Line | Data |
---|---|---|
60c5eb7d | 1 | use crate::creader::{CStore, LoadedMacro}; |
dfeec247 | 2 | use crate::foreign_modules; |
9fa01778 | 3 | use crate::native_libs; |
92a42be0 | 4 | |
3dfed10e | 5 | use rustc_ast as ast; |
923072b8 | 6 | use rustc_attr::Deprecation; |
5099ac24 | 7 | use rustc_hir::def::{CtorKind, DefKind, Res}; |
04454e1e | 8 | use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, LOCAL_CRATE}; |
ba9703b0 | 9 | use rustc_hir::definitions::{DefKey, DefPath, DefPathHash}; |
923072b8 | 10 | use rustc_middle::arena::ArenaAllocatable; |
5099ac24 | 11 | use rustc_middle::metadata::ModChild; |
ba9703b0 | 12 | use rustc_middle::middle::exported_symbols::ExportedSymbol; |
923072b8 | 13 | use rustc_middle::middle::stability::DeprecationEntry; |
5099ac24 | 14 | use rustc_middle::ty::fast_reject::SimplifiedType; |
3c0e092e | 15 | use rustc_middle::ty::query::{ExternProviders, Providers}; |
17df50a5 | 16 | use rustc_middle::ty::{self, TyCtxt, Visibility}; |
5099ac24 | 17 | use rustc_session::cstore::{CrateSource, CrateStore}; |
136023e0 XL |
18 | use rustc_session::{Session, StableCrateId}; |
19 | use rustc_span::hygiene::{ExpnHash, ExpnId}; | |
1b1a35ee | 20 | use rustc_span::source_map::{Span, Spanned}; |
a2a8927a | 21 | use rustc_span::symbol::{kw, Symbol}; |
92a42be0 | 22 | |
dfeec247 | 23 | use rustc_data_structures::sync::Lrc; |
8bb4bdeb | 24 | use std::any::Any; |
92a42be0 | 25 | |
923072b8 FG |
26 | use super::{Decodable, DecodeContext, DecodeIterator}; |
27 | ||
28 | trait ProcessQueryValue<'tcx, T> { | |
29 | fn process_decoded(self, _tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> T; | |
30 | } | |
31 | ||
32 | impl<T> ProcessQueryValue<'_, Option<T>> for Option<T> { | |
33 | #[inline(always)] | |
34 | fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<T> { | |
35 | self | |
36 | } | |
37 | } | |
38 | ||
39 | impl<T> ProcessQueryValue<'_, T> for Option<T> { | |
40 | #[inline(always)] | |
41 | fn process_decoded(self, _tcx: TyCtxt<'_>, err: impl Fn() -> !) -> T { | |
42 | if let Some(value) = self { value } else { err() } | |
43 | } | |
44 | } | |
45 | ||
46 | impl<'tcx, T: ArenaAllocatable<'tcx>> ProcessQueryValue<'tcx, &'tcx T> for Option<T> { | |
47 | #[inline(always)] | |
48 | fn process_decoded(self, tcx: TyCtxt<'tcx>, err: impl Fn() -> !) -> &'tcx T { | |
49 | if let Some(value) = self { tcx.arena.alloc(value) } else { err() } | |
50 | } | |
51 | } | |
52 | ||
53 | impl<T, E> ProcessQueryValue<'_, Result<Option<T>, E>> for Option<T> { | |
54 | #[inline(always)] | |
55 | fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Result<Option<T>, E> { | |
56 | Ok(self) | |
57 | } | |
58 | } | |
59 | ||
60 | impl<'a, 'tcx, T: Copy + Decodable<DecodeContext<'a, 'tcx>>> ProcessQueryValue<'tcx, &'tcx [T]> | |
61 | for Option<DecodeIterator<'a, 'tcx, T>> | |
62 | { | |
63 | #[inline(always)] | |
64 | fn process_decoded(self, tcx: TyCtxt<'tcx>, _err: impl Fn() -> !) -> &'tcx [T] { | |
65 | if let Some(iter) = self { tcx.arena.alloc_from_iter(iter) } else { &[] } | |
66 | } | |
67 | } | |
68 | ||
69 | impl ProcessQueryValue<'_, Option<DeprecationEntry>> for Option<Deprecation> { | |
70 | #[inline(always)] | |
71 | fn process_decoded(self, _tcx: TyCtxt<'_>, _err: impl Fn() -> !) -> Option<DeprecationEntry> { | |
72 | self.map(DeprecationEntry::external) | |
73 | } | |
74 | } | |
75 | ||
5e7ed085 | 76 | macro_rules! provide_one { |
f2b60f7d | 77 | ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table }) => { |
5e7ed085 | 78 | provide_one! { |
f2b60f7d | 79 | $tcx, $def_id, $other, $cdata, $name => { |
923072b8 FG |
80 | $cdata |
81 | .root | |
82 | .tables | |
83 | .$name | |
84 | .get($cdata, $def_id.index) | |
85 | .map(|lazy| lazy.decode(($cdata, $tcx))) | |
86 | .process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name))) | |
87 | } | |
88 | } | |
89 | }; | |
f2b60f7d | 90 | ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => { table_direct }) => { |
923072b8 | 91 | provide_one! { |
f2b60f7d | 92 | $tcx, $def_id, $other, $cdata, $name => { |
923072b8 FG |
93 | // We don't decode `table_direct`, since it's not a Lazy, but an actual value |
94 | $cdata | |
95 | .root | |
96 | .tables | |
97 | .$name | |
98 | .get($cdata, $def_id.index) | |
99 | .process_decoded($tcx, || panic!("{:?} does not have a {:?}", $def_id, stringify!($name))) | |
5e7ed085 FG |
100 | } |
101 | } | |
102 | }; | |
f2b60f7d FG |
103 | ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, $name:ident => $compute:block) => { |
104 | fn $name<'tcx>( | |
105 | $tcx: TyCtxt<'tcx>, | |
106 | def_id_arg: ty::query::query_keys::$name<'tcx>, | |
107 | ) -> ty::query::query_values::$name<'tcx> { | |
5e7ed085 FG |
108 | let _prof_timer = |
109 | $tcx.prof.generic_activity(concat!("metadata_decode_entry_", stringify!($name))); | |
110 | ||
111 | #[allow(unused_variables)] | |
112 | let ($def_id, $other) = def_id_arg.into_args(); | |
113 | assert!(!$def_id.is_local()); | |
114 | ||
115 | // External query providers call `crate_hash` in order to register a dependency | |
116 | // on the crate metadata. The exception is `crate_hash` itself, which obviously | |
117 | // doesn't need to do this (and can't, as it would cause a query cycle). | |
118 | use rustc_middle::dep_graph::DepKind; | |
119 | if DepKind::$name != DepKind::crate_hash && $tcx.dep_graph.is_fully_enabled() { | |
120 | $tcx.ensure().crate_hash($def_id.krate); | |
121 | } | |
122 | ||
123 | let $cdata = CStore::from_tcx($tcx).get_crate_data($def_id.krate); | |
124 | ||
125 | $compute | |
126 | } | |
127 | }; | |
128 | } | |
129 | ||
8bb4bdeb | 130 | macro_rules! provide { |
f2b60f7d | 131 | ($tcx:ident, $def_id:ident, $other:ident, $cdata:ident, |
5e7ed085 | 132 | $($name:ident => { $($compute:tt)* })*) => { |
3c0e092e | 133 | pub fn provide_extern(providers: &mut ExternProviders) { |
5e7ed085 | 134 | $(provide_one! { |
f2b60f7d | 135 | $tcx, $def_id, $other, $cdata, $name => { $($compute)* } |
8bb4bdeb XL |
136 | })* |
137 | ||
3c0e092e | 138 | *providers = ExternProviders { |
8bb4bdeb XL |
139 | $($name,)* |
140 | ..*providers | |
141 | }; | |
142 | } | |
9e0c209e | 143 | } |
8bb4bdeb | 144 | } |
9e0c209e | 145 | |
ea8adc8c XL |
146 | // small trait to work around different signature queries all being defined via |
147 | // the macro above. | |
148 | trait IntoArgs { | |
5e7ed085 FG |
149 | type Other; |
150 | fn into_args(self) -> (DefId, Self::Other); | |
ea8adc8c XL |
151 | } |
152 | ||
153 | impl IntoArgs for DefId { | |
5e7ed085 FG |
154 | type Other = (); |
155 | fn into_args(self) -> (DefId, ()) { | |
156 | (self, ()) | |
dfeec247 | 157 | } |
ea8adc8c XL |
158 | } |
159 | ||
160 | impl IntoArgs for CrateNum { | |
5e7ed085 FG |
161 | type Other = (); |
162 | fn into_args(self) -> (DefId, ()) { | |
163 | (self.as_def_id(), ()) | |
dfeec247 | 164 | } |
ea8adc8c XL |
165 | } |
166 | ||
167 | impl IntoArgs for (CrateNum, DefId) { | |
5e7ed085 | 168 | type Other = DefId; |
dfeec247 XL |
169 | fn into_args(self) -> (DefId, DefId) { |
170 | (self.0.as_def_id(), self.1) | |
171 | } | |
ea8adc8c XL |
172 | } |
173 | ||
a2a8927a | 174 | impl<'tcx> IntoArgs for ty::InstanceDef<'tcx> { |
5e7ed085 FG |
175 | type Other = (); |
176 | fn into_args(self) -> (DefId, ()) { | |
177 | (self.def_id(), ()) | |
178 | } | |
179 | } | |
180 | ||
181 | impl IntoArgs for (CrateNum, SimplifiedType) { | |
182 | type Other = SimplifiedType; | |
183 | fn into_args(self) -> (DefId, SimplifiedType) { | |
184 | (self.0.as_def_id(), self.1) | |
c295e0f8 XL |
185 | } |
186 | } | |
187 | ||
f2b60f7d | 188 | provide! { tcx, def_id, other, cdata, |
5e7ed085 FG |
189 | explicit_item_bounds => { table } |
190 | explicit_predicates_of => { table } | |
191 | generics_of => { table } | |
192 | inferred_outlives_of => { table } | |
193 | super_predicates_of => { table } | |
194 | type_of => { table } | |
195 | variances_of => { table } | |
196 | fn_sig => { table } | |
04454e1e | 197 | codegen_fn_attrs => { table } |
5e7ed085 FG |
198 | impl_trait_ref => { table } |
199 | const_param_default => { table } | |
f2b60f7d | 200 | object_lifetime_default => { table } |
5e7ed085 FG |
201 | thir_abstract_const => { table } |
202 | optimized_mir => { table } | |
203 | mir_for_ctfe => { table } | |
204 | promoted_mir => { table } | |
205 | def_span => { table } | |
206 | def_ident_span => { table } | |
207 | lookup_stability => { table } | |
208 | lookup_const_stability => { table } | |
f2b60f7d | 209 | lookup_default_body_stability => { table } |
5e7ed085 | 210 | lookup_deprecation_entry => { table } |
2b03887a | 211 | params_in_repr => { table } |
5e7ed085 | 212 | unused_generic_params => { table } |
923072b8 | 213 | opt_def_kind => { table_direct } |
5e7ed085 | 214 | impl_parent => { table } |
923072b8 FG |
215 | impl_polarity => { table_direct } |
216 | impl_defaultness => { table_direct } | |
217 | constness => { table_direct } | |
5e7ed085 FG |
218 | coerce_unsized_info => { table } |
219 | mir_const_qualif => { table } | |
220 | rendered_const => { table } | |
923072b8 | 221 | asyncness => { table_direct } |
5e7ed085 FG |
222 | fn_arg_names => { table } |
223 | generator_kind => { table } | |
224 | trait_def => { table } | |
2b03887a | 225 | deduced_param_attrs => { table } |
487cf647 FG |
226 | is_type_alias_impl_trait => { |
227 | debug_assert_eq!(tcx.def_kind(def_id), DefKind::OpaqueTy); | |
228 | cdata | |
229 | .root | |
230 | .tables | |
231 | .is_type_alias_impl_trait | |
232 | .get(cdata, def_id.index) | |
233 | .is_some() | |
234 | } | |
f25598a0 | 235 | collect_return_position_impl_trait_in_trait_tys => { |
2b03887a FG |
236 | Ok(cdata |
237 | .root | |
238 | .tables | |
239 | .trait_impl_trait_tys | |
240 | .get(cdata, def_id.index) | |
241 | .map(|lazy| lazy.decode((cdata, tcx))) | |
f25598a0 | 242 | .process_decoded(tcx, || panic!("{def_id:?} does not have trait_impl_trait_tys"))) |
2b03887a | 243 | } |
5e7ed085 | 244 | |
f2b60f7d | 245 | visibility => { cdata.get_visibility(def_id.index) } |
8bb4bdeb XL |
246 | adt_def => { cdata.get_adt_def(def_id.index, tcx) } |
247 | adt_destructor => { | |
248 | let _ = cdata; | |
29967ef6 | 249 | tcx.calculate_dtor(def_id, |_,_| Ok(())) |
92a42be0 | 250 | } |
04454e1e FG |
251 | associated_item_def_ids => { |
252 | tcx.arena.alloc_from_iter(cdata.get_associated_item_def_ids(def_id.index, tcx.sess)) | |
253 | } | |
f2b60f7d | 254 | associated_item => { cdata.get_associated_item(def_id.index, tcx.sess) } |
dc9dc135 | 255 | inherent_impls => { cdata.get_inherent_implementations_for_type(tcx, def_id.index) } |
cc61c64b | 256 | is_foreign_item => { cdata.is_foreign_item(def_id.index) } |
a2a8927a | 257 | item_attrs => { tcx.arena.alloc_from_iter(cdata.get_item_attrs(def_id.index, tcx.sess)) } |
7cac9316 | 258 | is_mir_available => { cdata.is_item_mir_available(def_id.index) } |
5869c6ff | 259 | is_ctfe_mir_available => { cdata.is_ctfe_mir_available(def_id.index) } |
041b39d2 | 260 | |
dc9dc135 | 261 | dylib_dependency_formats => { cdata.get_dylib_dependency_formats(tcx) } |
17df50a5 | 262 | is_private_dep => { cdata.private_dep } |
94b46f34 XL |
263 | is_panic_runtime => { cdata.root.panic_runtime } |
264 | is_compiler_builtins => { cdata.root.compiler_builtins } | |
265 | has_global_allocator => { cdata.root.has_global_allocator } | |
487cf647 | 266 | has_alloc_error_handler => { cdata.root.has_alloc_error_handler } |
b7449926 | 267 | has_panic_handler => { cdata.root.has_panic_handler } |
94b46f34 | 268 | is_profiler_runtime => { cdata.root.profiler_runtime } |
064997fb | 269 | required_panic_strategy => { cdata.root.required_panic_strategy } |
c295e0f8 | 270 | panic_in_drop_strategy => { cdata.root.panic_in_drop_strategy } |
0531ce1d | 271 | extern_crate => { |
dc9dc135 XL |
272 | let r = *cdata.extern_crate.lock(); |
273 | r.map(|c| &*tcx.arena.alloc(c)) | |
0531ce1d | 274 | } |
94b46f34 | 275 | is_no_builtins => { cdata.root.no_builtins } |
dc9dc135 | 276 | symbol_mangling_version => { cdata.root.symbol_mangling_version } |
0531ce1d XL |
277 | reachable_non_generics => { |
278 | let reachable_non_generics = tcx | |
279 | .exported_symbols(cdata.cnum) | |
280 | .iter() | |
04454e1e | 281 | .filter_map(|&(exported_symbol, export_info)| { |
0531ce1d | 282 | if let ExportedSymbol::NonGeneric(def_id) = exported_symbol { |
04454e1e | 283 | Some((def_id, export_info)) |
0531ce1d XL |
284 | } else { |
285 | None | |
286 | } | |
287 | }) | |
288 | .collect(); | |
289 | ||
f9f354fc | 290 | reachable_non_generics |
0531ce1d | 291 | } |
5099ac24 FG |
292 | native_libraries => { cdata.get_native_libraries(tcx.sess).collect() } |
293 | foreign_modules => { cdata.get_foreign_modules(tcx.sess).map(|m| (m.def_id, m)).collect() } | |
94b46f34 | 294 | crate_hash => { cdata.root.hash } |
60c5eb7d | 295 | crate_host_hash => { cdata.host_hash } |
17df50a5 | 296 | crate_name => { cdata.root.name } |
ea8adc8c | 297 | |
83c7162d XL |
298 | extra_filename => { cdata.root.extra_filename.clone() } |
299 | ||
a2a8927a | 300 | traits_in_crate => { tcx.arena.alloc_from_iter(cdata.get_traits()) } |
a2a8927a | 301 | implementations_of_trait => { cdata.get_implementations_of_trait(tcx, other) } |
5e7ed085 | 302 | crate_incoherent_impls => { cdata.get_incoherent_impls(tcx, other) } |
ea8adc8c | 303 | |
0531ce1d XL |
304 | dep_kind => { |
305 | let r = *cdata.dep_kind.lock(); | |
306 | r | |
307 | } | |
5099ac24 | 308 | module_children => { |
487cf647 | 309 | tcx.arena.alloc_from_iter(cdata.get_module_children(def_id.index, tcx.sess)) |
ea8adc8c | 310 | } |
dc9dc135 | 311 | defined_lib_features => { cdata.get_lib_features(tcx) } |
064997fb FG |
312 | stability_implications => { |
313 | cdata.get_stability_implications(tcx).iter().copied().collect() | |
314 | } | |
923072b8 | 315 | is_intrinsic => { cdata.get_is_intrinsic(def_id.index) } |
5e7ed085 | 316 | defined_lang_items => { cdata.get_lang_items(tcx) } |
f9f354fc | 317 | diagnostic_items => { cdata.get_diagnostic_items() } |
dc9dc135 | 318 | missing_lang_items => { cdata.get_missing_lang_items(tcx) } |
ea8adc8c | 319 | |
ea8adc8c | 320 | missing_extern_crate_item => { |
29967ef6 | 321 | let r = matches!(*cdata.extern_crate.borrow(), Some(extern_crate) if !extern_crate.is_direct()); |
0531ce1d | 322 | r |
ea8adc8c XL |
323 | } |
324 | ||
5099ac24 | 325 | used_crate_source => { Lrc::clone(&cdata.source) } |
04454e1e | 326 | debugger_visualizers => { cdata.get_debugger_visualizers() } |
ea8adc8c | 327 | |
e74abb32 XL |
328 | exported_symbols => { |
329 | let syms = cdata.exported_symbols(tcx); | |
330 | ||
331 | // FIXME rust-lang/rust#64319, rust-lang/rust#64872: We want | |
332 | // to block export of generics from dylibs, but we must fix | |
333 | // rust-lang/rust#65890 before we can do that robustly. | |
334 | ||
ba9703b0 | 335 | syms |
e74abb32 | 336 | } |
f035d41b XL |
337 | |
338 | crate_extern_paths => { cdata.source().paths().cloned().collect() } | |
29967ef6 | 339 | expn_that_defined => { cdata.get_expn_that_defined(def_id.index, tcx.sess) } |
04454e1e | 340 | generator_diagnostic_data => { cdata.get_generator_diagnostic_data(tcx, def_id.index) } |
041b39d2 XL |
341 | } |
342 | ||
a2a8927a | 343 | pub(in crate::rmeta) fn provide(providers: &mut Providers) { |
ea8adc8c XL |
344 | // FIXME(#44234) - almost all of these queries have no sub-queries and |
345 | // therefore no actual inputs, they're just reading tables calculated in | |
346 | // resolve! Does this work? Unsure! That's what the issue is about | |
041b39d2 | 347 | *providers = Providers { |
136023e0 | 348 | allocator_kind: |tcx, ()| CStore::from_tcx(tcx).allocator_kind(), |
487cf647 | 349 | alloc_error_handler_kind: |tcx, ()| CStore::from_tcx(tcx).alloc_error_handler_kind(), |
17df50a5 XL |
350 | is_private_dep: |_tcx, cnum| { |
351 | assert_eq!(cnum, LOCAL_CRATE); | |
352 | false | |
ea8adc8c | 353 | }, |
f2b60f7d | 354 | native_library: |tcx, id| { |
ea8adc8c XL |
355 | tcx.native_libraries(id.krate) |
356 | .iter() | |
357 | .filter(|lib| native_libs::relevant_lib(&tcx.sess, lib)) | |
0531ce1d | 358 | .find(|lib| { |
5e7ed085 FG |
359 | let Some(fm_id) = lib.foreign_module else { |
360 | return false; | |
0531ce1d | 361 | }; |
29967ef6 XL |
362 | let map = tcx.foreign_modules(id.krate); |
363 | map.get(&fm_id) | |
0531ce1d XL |
364 | .expect("failed to find foreign module") |
365 | .foreign_items | |
366 | .contains(&id) | |
367 | }) | |
ea8adc8c XL |
368 | }, |
369 | native_libraries: |tcx, cnum| { | |
370 | assert_eq!(cnum, LOCAL_CRATE); | |
5099ac24 | 371 | native_libs::collect(tcx) |
0531ce1d XL |
372 | }, |
373 | foreign_modules: |tcx, cnum| { | |
374 | assert_eq!(cnum, LOCAL_CRATE); | |
5099ac24 | 375 | foreign_modules::collect(tcx).into_iter().map(|m| (m.def_id, m)).collect() |
ea8adc8c | 376 | }, |
ea8adc8c | 377 | |
0731742a | 378 | // Returns a map from a sufficiently visible external item (i.e., an |
ea8adc8c XL |
379 | // external item that is visible from at least one local module) to a |
380 | // sufficiently visible parent (considering modules that re-export the | |
381 | // external item to be parents). | |
17df50a5 | 382 | visible_parent_map: |tcx, ()| { |
ea8adc8c | 383 | use std::collections::hash_map::Entry; |
dfeec247 | 384 | use std::collections::vec_deque::VecDeque; |
ea8adc8c | 385 | |
a1dfa0c6 | 386 | let mut visible_parent_map: DefIdMap<DefId> = Default::default(); |
064997fb FG |
387 | // This is a secondary visible_parent_map, storing the DefId of |
388 | // parents that re-export the child as `_` or module parents | |
389 | // which are `#[doc(hidden)]`. Since we prefer paths that don't | |
390 | // do this, merge this map at the end, only if we're missing | |
391 | // keys from the former. | |
392 | // This is a rudimentary check that does not catch all cases, | |
393 | // just the easiest. | |
f25598a0 | 394 | let mut fallback_map: Vec<(DefId, DefId)> = Default::default(); |
ea8adc8c | 395 | |
ff7c6d11 XL |
396 | // Issue 46112: We want the map to prefer the shortest |
397 | // paths when reporting the path to an item. Therefore we | |
398 | // build up the map via a breadth-first search (BFS), | |
399 | // which naturally yields minimal-length paths. | |
400 | // | |
401 | // Note that it needs to be a BFS over the whole forest of | |
402 | // crates, not just each individual crate; otherwise you | |
403 | // only get paths that are locally minimal with respect to | |
404 | // whatever crate we happened to encounter first in this | |
405 | // traversal, but not globally minimal across all crates. | |
406 | let bfs_queue = &mut VecDeque::new(); | |
407 | ||
c295e0f8 | 408 | for &cnum in tcx.crates(()) { |
ea8adc8c XL |
409 | // Ignore crates without a corresponding local `extern crate` item. |
410 | if tcx.missing_extern_crate_item(cnum) { | |
dfeec247 | 411 | continue; |
ea8adc8c XL |
412 | } |
413 | ||
04454e1e | 414 | bfs_queue.push_back(cnum.as_def_id()); |
ff7c6d11 XL |
415 | } |
416 | ||
5099ac24 FG |
417 | let mut add_child = |bfs_queue: &mut VecDeque<_>, child: &ModChild, parent: DefId| { |
418 | if !child.vis.is_public() { | |
c295e0f8 XL |
419 | return; |
420 | } | |
ea8adc8c | 421 | |
5099ac24 FG |
422 | if let Some(def_id) = child.res.opt_def_id() { |
423 | if child.ident.name == kw::Underscore { | |
f25598a0 | 424 | fallback_map.push((def_id, parent)); |
a2a8927a XL |
425 | return; |
426 | } | |
427 | ||
064997fb | 428 | if ty::util::is_doc_hidden(tcx, parent) { |
f25598a0 | 429 | fallback_map.push((def_id, parent)); |
064997fb FG |
430 | return; |
431 | } | |
432 | ||
5099ac24 | 433 | match visible_parent_map.entry(def_id) { |
c295e0f8 XL |
434 | Entry::Occupied(mut entry) => { |
435 | // If `child` is defined in crate `cnum`, ensure | |
436 | // that it is mapped to a parent in `cnum`. | |
5099ac24 | 437 | if def_id.is_local() && entry.get().is_local() { |
c295e0f8 | 438 | entry.insert(parent); |
dc9dc135 | 439 | } |
ea8adc8c | 440 | } |
c295e0f8 XL |
441 | Entry::Vacant(entry) => { |
442 | entry.insert(parent); | |
5099ac24 FG |
443 | if matches!( |
444 | child.res, | |
445 | Res::Def(DefKind::Mod | DefKind::Enum | DefKind::Trait, _) | |
446 | ) { | |
447 | bfs_queue.push_back(def_id); | |
448 | } | |
c295e0f8 | 449 | } |
ea8adc8c XL |
450 | } |
451 | } | |
c295e0f8 XL |
452 | }; |
453 | ||
454 | while let Some(def) = bfs_queue.pop_front() { | |
5099ac24 | 455 | for child in tcx.module_children(def).iter() { |
c295e0f8 XL |
456 | add_child(bfs_queue, child, def); |
457 | } | |
ea8adc8c XL |
458 | } |
459 | ||
064997fb FG |
460 | // Fill in any missing entries with the less preferable path. |
461 | // If this path re-exports the child as `_`, we still use this | |
462 | // path in a diagnostic that suggests importing `::*`. | |
f25598a0 | 463 | |
a2a8927a XL |
464 | for (child, parent) in fallback_map { |
465 | visible_parent_map.entry(child).or_insert(parent); | |
466 | } | |
467 | ||
f9f354fc | 468 | visible_parent_map |
ea8adc8c XL |
469 | }, |
470 | ||
17df50a5 | 471 | dependency_formats: |tcx, ()| Lrc::new(crate::dependency_format::calculate(tcx)), |
60c5eb7d XL |
472 | has_global_allocator: |tcx, cnum| { |
473 | assert_eq!(cnum, LOCAL_CRATE); | |
474 | CStore::from_tcx(tcx).has_global_allocator() | |
475 | }, | |
487cf647 FG |
476 | has_alloc_error_handler: |tcx, cnum| { |
477 | assert_eq!(cnum, LOCAL_CRATE); | |
478 | CStore::from_tcx(tcx).has_alloc_error_handler() | |
479 | }, | |
17df50a5 XL |
480 | postorder_cnums: |tcx, ()| { |
481 | tcx.arena | |
482 | .alloc_slice(&CStore::from_tcx(tcx).crate_dependencies_in_postorder(LOCAL_CRATE)) | |
60c5eb7d | 483 | }, |
a2a8927a | 484 | crates: |tcx, ()| tcx.arena.alloc_from_iter(CStore::from_tcx(tcx).crates_untracked()), |
041b39d2 XL |
485 | ..*providers |
486 | }; | |
8bb4bdeb | 487 | } |
92a42be0 | 488 | |
60c5eb7d | 489 | impl CStore { |
5099ac24 FG |
490 | pub fn struct_field_names_untracked<'a>( |
491 | &'a self, | |
492 | def: DefId, | |
493 | sess: &'a Session, | |
494 | ) -> impl Iterator<Item = Spanned<Symbol>> + 'a { | |
e74abb32 | 495 | self.get_crate_data(def.krate).get_struct_field_names(def.index, sess) |
532ac7d7 XL |
496 | } |
497 | ||
5099ac24 FG |
498 | pub fn struct_field_visibilities_untracked( |
499 | &self, | |
500 | def: DefId, | |
f2b60f7d | 501 | ) -> impl Iterator<Item = Visibility<DefId>> + '_ { |
17df50a5 XL |
502 | self.get_crate_data(def.krate).get_struct_field_visibilities(def.index) |
503 | } | |
504 | ||
487cf647 FG |
505 | pub fn ctor_untracked(&self, def: DefId) -> Option<(CtorKind, DefId)> { |
506 | self.get_crate_data(def.krate).get_ctor(def.index) | |
17df50a5 XL |
507 | } |
508 | ||
f2b60f7d | 509 | pub fn visibility_untracked(&self, def: DefId) -> Visibility<DefId> { |
17df50a5 XL |
510 | self.get_crate_data(def.krate).get_visibility(def.index) |
511 | } | |
512 | ||
487cf647 FG |
513 | pub fn module_children_untracked<'a>( |
514 | &'a self, | |
515 | def_id: DefId, | |
516 | sess: &'a Session, | |
517 | ) -> impl Iterator<Item = ModChild> + 'a { | |
518 | self.get_crate_data(def_id.krate).get_module_children(def_id.index, sess) | |
92a42be0 SL |
519 | } |
520 | ||
b7449926 | 521 | pub fn load_macro_untracked(&self, id: DefId, sess: &Session) -> LoadedMacro { |
e74abb32 XL |
522 | let _prof_timer = sess.prof.generic_activity("metadata_load_macro"); |
523 | ||
476ff2be | 524 | let data = self.get_crate_data(id.krate); |
60c5eb7d | 525 | if data.root.is_proc_macro_crate() { |
136023e0 | 526 | return LoadedMacro::ProcMacro(data.load_proc_macro(id.index, sess)); |
476ff2be SL |
527 | } |
528 | ||
ba9703b0 | 529 | let span = data.get_span(id.index, sess); |
476ff2be | 530 | |
dfeec247 XL |
531 | LoadedMacro::MacroDef( |
532 | ast::Item { | |
a2a8927a | 533 | ident: data.item_ident(id.index, sess), |
dfeec247 | 534 | id: ast::DUMMY_NODE_ID, |
ba9703b0 | 535 | span, |
a2a8927a | 536 | attrs: data.get_item_attrs(id.index, sess).collect(), |
ba9703b0 | 537 | kind: ast::ItemKind::MacroDef(data.get_macro(id.index, sess)), |
1b1a35ee XL |
538 | vis: ast::Visibility { |
539 | span: span.shrink_to_lo(), | |
540 | kind: ast::VisibilityKind::Inherited, | |
541 | tokens: None, | |
542 | }, | |
dfeec247 XL |
543 | tokens: None, |
544 | }, | |
545 | data.root.edition, | |
546 | ) | |
476ff2be SL |
547 | } |
548 | ||
f2b60f7d FG |
549 | pub fn fn_has_self_parameter_untracked(&self, def: DefId, sess: &Session) -> bool { |
550 | self.get_crate_data(def.krate).get_fn_has_self_parameter(def.index, sess) | |
b7449926 | 551 | } |
e74abb32 | 552 | |
5099ac24 | 553 | pub fn crate_source_untracked(&self, cnum: CrateNum) -> Lrc<CrateSource> { |
e74abb32 XL |
554 | self.get_crate_data(cnum).source.clone() |
555 | } | |
dfeec247 XL |
556 | |
557 | pub fn get_span_untracked(&self, def_id: DefId, sess: &Session) -> Span { | |
558 | self.get_crate_data(def_id.krate).get_span(def_id.index, sess) | |
559 | } | |
560 | ||
136023e0 XL |
561 | pub fn def_kind(&self, def: DefId) -> DefKind { |
562 | self.get_crate_data(def.krate).def_kind(def.index) | |
563 | } | |
564 | ||
a2a8927a XL |
565 | pub fn crates_untracked(&self) -> impl Iterator<Item = CrateNum> + '_ { |
566 | self.iter_crate_data().map(|(cnum, _)| cnum) | |
136023e0 XL |
567 | } |
568 | ||
dfeec247 XL |
569 | pub fn item_generics_num_lifetimes(&self, def_id: DefId, sess: &Session) -> usize { |
570 | self.get_crate_data(def_id.krate).get_generics(def_id.index, sess).own_counts().lifetimes | |
571 | } | |
3dfed10e XL |
572 | |
573 | pub fn module_expansion_untracked(&self, def_id: DefId, sess: &Session) -> ExpnId { | |
574 | self.get_crate_data(def_id.krate).module_expansion(def_id.index, sess) | |
575 | } | |
fc512014 | 576 | |
17df50a5 XL |
577 | /// Only public-facing way to traverse all the definitions in a non-local crate. |
578 | /// Critically useful for this third-party project: <https://github.com/hacspec/hacspec>. | |
579 | /// See <https://github.com/rust-lang/rust/pull/85889> for context. | |
580 | pub fn num_def_ids_untracked(&self, cnum: CrateNum) -> usize { | |
fc512014 XL |
581 | self.get_crate_data(cnum).num_def_ids() |
582 | } | |
6a06907d | 583 | |
5099ac24 FG |
584 | pub fn item_attrs_untracked<'a>( |
585 | &'a self, | |
586 | def_id: DefId, | |
587 | sess: &'a Session, | |
588 | ) -> impl Iterator<Item = ast::Attribute> + 'a { | |
589 | self.get_crate_data(def_id.krate).get_item_attrs(def_id.index, sess) | |
6a06907d | 590 | } |
17df50a5 XL |
591 | |
592 | pub fn get_proc_macro_quoted_span_untracked( | |
593 | &self, | |
594 | cnum: CrateNum, | |
595 | id: usize, | |
596 | sess: &Session, | |
597 | ) -> Span { | |
598 | self.get_crate_data(cnum).get_proc_macro_quoted_span(id, sess) | |
599 | } | |
5099ac24 | 600 | |
5099ac24 FG |
601 | /// Decodes all trait impls in the crate (for rustdoc). |
602 | pub fn trait_impls_in_crate_untracked( | |
603 | &self, | |
604 | cnum: CrateNum, | |
605 | ) -> impl Iterator<Item = (DefId, DefId, Option<SimplifiedType>)> + '_ { | |
606 | self.get_crate_data(cnum).get_trait_impls() | |
607 | } | |
608 | ||
609 | /// Decodes all inherent impls in the crate (for rustdoc). | |
610 | pub fn inherent_impls_in_crate_untracked( | |
611 | &self, | |
612 | cnum: CrateNum, | |
613 | ) -> impl Iterator<Item = (DefId, DefId)> + '_ { | |
614 | self.get_crate_data(cnum).get_inherent_impls() | |
615 | } | |
616 | ||
5e7ed085 FG |
617 | /// Decodes all incoherent inherent impls in the crate (for rustdoc). |
618 | pub fn incoherent_impls_in_crate_untracked( | |
619 | &self, | |
620 | cnum: CrateNum, | |
621 | ) -> impl Iterator<Item = DefId> + '_ { | |
622 | self.get_crate_data(cnum).get_all_incoherent_impls() | |
5099ac24 | 623 | } |
04454e1e FG |
624 | |
625 | pub fn associated_item_def_ids_untracked<'a>( | |
626 | &'a self, | |
627 | def_id: DefId, | |
628 | sess: &'a Session, | |
629 | ) -> impl Iterator<Item = DefId> + 'a { | |
630 | self.get_crate_data(def_id.krate).get_associated_item_def_ids(def_id.index, sess) | |
631 | } | |
632 | ||
633 | pub fn may_have_doc_links_untracked(&self, def_id: DefId) -> bool { | |
634 | self.get_crate_data(def_id.krate).get_may_have_doc_links(def_id.index) | |
635 | } | |
b7449926 XL |
636 | } |
637 | ||
60c5eb7d XL |
638 | impl CrateStore for CStore { |
639 | fn as_any(&self) -> &dyn Any { | |
640 | self | |
b7449926 | 641 | } |
f25598a0 FG |
642 | fn untracked_as_any(&mut self) -> &mut dyn Any { |
643 | self | |
644 | } | |
b7449926 | 645 | |
136023e0 | 646 | fn crate_name(&self, cnum: CrateNum) -> Symbol { |
e74abb32 | 647 | self.get_crate_data(cnum).root.name |
b7449926 XL |
648 | } |
649 | ||
136023e0 XL |
650 | fn stable_crate_id(&self, cnum: CrateNum) -> StableCrateId { |
651 | self.get_crate_data(cnum).root.stable_crate_id | |
b7449926 XL |
652 | } |
653 | ||
c295e0f8 XL |
654 | fn stable_crate_id_to_crate_num(&self, stable_crate_id: StableCrateId) -> CrateNum { |
655 | self.stable_crate_ids[&stable_crate_id] | |
656 | } | |
657 | ||
b7449926 XL |
658 | /// Returns the `DefKey` for a given `DefId`. This indicates the |
659 | /// parent `DefId` as well as some idea of what kind of data the | |
660 | /// `DefId` refers to. | |
661 | fn def_key(&self, def: DefId) -> DefKey { | |
b7449926 XL |
662 | self.get_crate_data(def.krate).def_key(def.index) |
663 | } | |
664 | ||
665 | fn def_path(&self, def: DefId) -> DefPath { | |
b7449926 XL |
666 | self.get_crate_data(def.krate).def_path(def.index) |
667 | } | |
668 | ||
669 | fn def_path_hash(&self, def: DefId) -> DefPathHash { | |
670 | self.get_crate_data(def.krate).def_path_hash(def.index) | |
671 | } | |
672 | ||
c295e0f8 XL |
673 | fn def_path_hash_to_def_id(&self, cnum: CrateNum, hash: DefPathHash) -> DefId { |
674 | let def_index = self.get_crate_data(cnum).def_path_hash_to_def_index(hash); | |
675 | DefId { krate: cnum, index: def_index } | |
676 | } | |
677 | ||
678 | fn expn_hash_to_expn_id( | |
fc512014 | 679 | &self, |
c295e0f8 | 680 | sess: &Session, |
fc512014 XL |
681 | cnum: CrateNum, |
682 | index_guess: u32, | |
c295e0f8 XL |
683 | hash: ExpnHash, |
684 | ) -> ExpnId { | |
685 | self.get_crate_data(cnum).expn_hash_to_expn_id(sess, index_guess, hash) | |
92a42be0 | 686 | } |
a2a8927a XL |
687 | |
688 | fn import_source_files(&self, sess: &Session, cnum: CrateNum) { | |
f2b60f7d FG |
689 | let cdata = self.get_crate_data(cnum); |
690 | for file_index in 0..cdata.root.source_map.size() { | |
691 | cdata.imported_source_file(file_index as u32, sess); | |
692 | } | |
a2a8927a | 693 | } |
92a42be0 | 694 | } |