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