]> git.lizzy.rs Git - rust.git/blob - src/librustc_metadata/rmeta/encoder.rs
Rollup merge of #70762 - RalfJung:miri-leak-check, r=oli-obk
[rust.git] / src / librustc_metadata / rmeta / encoder.rs
1 use crate::rmeta::table::FixedSizeEncoding;
2 use crate::rmeta::*;
3
4 use log::{debug, trace};
5 use rustc_ast::ast::{self, Ident};
6 use rustc_ast::attr;
7 use rustc_data_structures::fingerprint::Fingerprint;
8 use rustc_data_structures::fx::FxHashMap;
9 use rustc_data_structures::stable_hasher::StableHasher;
10 use rustc_data_structures::sync::{join, Lrc};
11 use rustc_hir as hir;
12 use rustc_hir::def::CtorKind;
13 use rustc_hir::def_id::DefIdSet;
14 use rustc_hir::def_id::{CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_INDEX, LOCAL_CRATE};
15 use rustc_hir::definitions::DefPathTable;
16 use rustc_hir::intravisit::{self, NestedVisitorMap, Visitor};
17 use rustc_hir::itemlikevisit::{ItemLikeVisitor, ParItemLikeVisitor};
18 use rustc_hir::lang_items;
19 use rustc_hir::{AnonConst, GenericParamKind};
20 use rustc_index::vec::Idx;
21 use rustc_middle::hir::map::Map;
22 use rustc_middle::middle::cstore::{
23     EncodedMetadata, ForeignModule, LinkagePreference, NativeLibrary,
24 };
25 use rustc_middle::middle::dependency_format::Linkage;
26 use rustc_middle::middle::exported_symbols::{
27     metadata_symbol_name, ExportedSymbol, SymbolExportLevel,
28 };
29 use rustc_middle::mir::{self, interpret};
30 use rustc_middle::traits::specialization_graph;
31 use rustc_middle::ty::codec::{self as ty_codec, TyEncoder};
32 use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt};
33 use rustc_serialize::{opaque, Encodable, Encoder, SpecializedEncoder};
34 use rustc_session::config::{self, CrateType};
35 use rustc_span::source_map::Spanned;
36 use rustc_span::symbol::{kw, sym, Symbol};
37 use rustc_span::{self, ExternalSource, FileName, SourceFile, Span};
38 use rustc_target::abi::VariantIdx;
39 use std::hash::Hash;
40 use std::num::NonZeroUsize;
41 use std::path::Path;
42
43 struct EncodeContext<'tcx> {
44     opaque: opaque::Encoder,
45     tcx: TyCtxt<'tcx>,
46
47     tables: TableBuilders<'tcx>,
48
49     lazy_state: LazyState,
50     type_shorthands: FxHashMap<Ty<'tcx>, usize>,
51     predicate_shorthands: FxHashMap<ty::Predicate<'tcx>, usize>,
52
53     interpret_allocs: FxHashMap<interpret::AllocId, usize>,
54     interpret_allocs_inverse: Vec<interpret::AllocId>,
55
56     // This is used to speed up Span encoding.
57     source_file_cache: Lrc<SourceFile>,
58 }
59
60 macro_rules! encoder_methods {
61     ($($name:ident($ty:ty);)*) => {
62         $(fn $name(&mut self, value: $ty) -> Result<(), Self::Error> {
63             self.opaque.$name(value)
64         })*
65     }
66 }
67
68 impl<'tcx> Encoder for EncodeContext<'tcx> {
69     type Error = <opaque::Encoder as Encoder>::Error;
70
71     fn emit_unit(&mut self) -> Result<(), Self::Error> {
72         Ok(())
73     }
74
75     encoder_methods! {
76         emit_usize(usize);
77         emit_u128(u128);
78         emit_u64(u64);
79         emit_u32(u32);
80         emit_u16(u16);
81         emit_u8(u8);
82
83         emit_isize(isize);
84         emit_i128(i128);
85         emit_i64(i64);
86         emit_i32(i32);
87         emit_i16(i16);
88         emit_i8(i8);
89
90         emit_bool(bool);
91         emit_f64(f64);
92         emit_f32(f32);
93         emit_char(char);
94         emit_str(&str);
95     }
96 }
97
98 impl<'tcx, T> SpecializedEncoder<Lazy<T>> for EncodeContext<'tcx> {
99     fn specialized_encode(&mut self, lazy: &Lazy<T>) -> Result<(), Self::Error> {
100         self.emit_lazy_distance(*lazy)
101     }
102 }
103
104 impl<'tcx, T> SpecializedEncoder<Lazy<[T]>> for EncodeContext<'tcx> {
105     fn specialized_encode(&mut self, lazy: &Lazy<[T]>) -> Result<(), Self::Error> {
106         self.emit_usize(lazy.meta)?;
107         if lazy.meta == 0 {
108             return Ok(());
109         }
110         self.emit_lazy_distance(*lazy)
111     }
112 }
113
114 impl<'tcx, I: Idx, T> SpecializedEncoder<Lazy<Table<I, T>>> for EncodeContext<'tcx>
115 where
116     Option<T>: FixedSizeEncoding,
117 {
118     fn specialized_encode(&mut self, lazy: &Lazy<Table<I, T>>) -> Result<(), Self::Error> {
119         self.emit_usize(lazy.meta)?;
120         self.emit_lazy_distance(*lazy)
121     }
122 }
123
124 impl<'tcx> SpecializedEncoder<CrateNum> for EncodeContext<'tcx> {
125     #[inline]
126     fn specialized_encode(&mut self, cnum: &CrateNum) -> Result<(), Self::Error> {
127         self.emit_u32(cnum.as_u32())
128     }
129 }
130
131 impl<'tcx> SpecializedEncoder<DefId> for EncodeContext<'tcx> {
132     #[inline]
133     fn specialized_encode(&mut self, def_id: &DefId) -> Result<(), Self::Error> {
134         let DefId { krate, index } = *def_id;
135
136         krate.encode(self)?;
137         index.encode(self)
138     }
139 }
140
141 impl<'tcx> SpecializedEncoder<DefIndex> for EncodeContext<'tcx> {
142     #[inline]
143     fn specialized_encode(&mut self, def_index: &DefIndex) -> Result<(), Self::Error> {
144         self.emit_u32(def_index.as_u32())
145     }
146 }
147
148 impl<'tcx> SpecializedEncoder<Span> for EncodeContext<'tcx> {
149     fn specialized_encode(&mut self, span: &Span) -> Result<(), Self::Error> {
150         if span.is_dummy() {
151             return TAG_INVALID_SPAN.encode(self);
152         }
153
154         let span = span.data();
155
156         // The Span infrastructure should make sure that this invariant holds:
157         debug_assert!(span.lo <= span.hi);
158
159         if !self.source_file_cache.contains(span.lo) {
160             let source_map = self.tcx.sess.source_map();
161             let source_file_index = source_map.lookup_source_file_idx(span.lo);
162             self.source_file_cache = source_map.files()[source_file_index].clone();
163         }
164
165         if !self.source_file_cache.contains(span.hi) {
166             // Unfortunately, macro expansion still sometimes generates Spans
167             // that malformed in this way.
168             return TAG_INVALID_SPAN.encode(self);
169         }
170
171         // There are two possible cases here:
172         // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the
173         // crate we are writing metadata for. When the metadata for *this* crate gets
174         // deserialized, the deserializer will need to know which crate it originally came
175         // from. We use `TAG_VALID_SPAN_FOREIGN` to indicate that a `CrateNum` should
176         // be deserialized after the rest of the span data, which tells the deserializer
177         // which crate contains the source map information.
178         // 2. This span comes from our own crate. No special hamdling is needed - we just
179         // write `TAG_VALID_SPAN_LOCAL` to let the deserializer know that it should use
180         // our own source map information.
181         let (tag, lo, hi) = if self.source_file_cache.is_imported() {
182             // To simplify deserialization, we 'rebase' this span onto the crate it originally came from
183             // (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' values
184             // are relative to the source map information for the 'foreign' crate whose CrateNum
185             // we write into the metadata. This allows `imported_source_files` to binary
186             // search through the 'foreign' crate's source map information, using the
187             // deserialized 'lo' and 'hi' values directly.
188             //
189             // All of this logic ensures that the final result of deserialization is a 'normal'
190             // Span that can be used without any additional trouble.
191             let external_start_pos = {
192                 // Introduce a new scope so that we drop the 'lock()' temporary
193                 match &*self.source_file_cache.external_src.lock() {
194                     ExternalSource::Foreign { original_start_pos, .. } => *original_start_pos,
195                     src => panic!("Unexpected external source {:?}", src),
196                 }
197             };
198             let lo = (span.lo - self.source_file_cache.start_pos) + external_start_pos;
199             let hi = (span.hi - self.source_file_cache.start_pos) + external_start_pos;
200
201             (TAG_VALID_SPAN_FOREIGN, lo, hi)
202         } else {
203             (TAG_VALID_SPAN_LOCAL, span.lo, span.hi)
204         };
205
206         tag.encode(self)?;
207         lo.encode(self)?;
208
209         // Encode length which is usually less than span.hi and profits more
210         // from the variable-length integer encoding that we use.
211         let len = hi - lo;
212         len.encode(self)?;
213
214         if tag == TAG_VALID_SPAN_FOREIGN {
215             // This needs to be two lines to avoid holding the `self.source_file_cache`
216             // while calling `cnum.encode(self)`
217             let cnum = self.source_file_cache.cnum;
218             cnum.encode(self)?;
219         }
220         Ok(())
221
222         // Don't encode the expansion context.
223     }
224 }
225
226 impl<'tcx> SpecializedEncoder<LocalDefId> for EncodeContext<'tcx> {
227     #[inline]
228     fn specialized_encode(&mut self, def_id: &LocalDefId) -> Result<(), Self::Error> {
229         self.specialized_encode(&def_id.to_def_id())
230     }
231 }
232
233 impl<'tcx> SpecializedEncoder<Ty<'tcx>> for EncodeContext<'tcx> {
234     fn specialized_encode(&mut self, ty: &Ty<'tcx>) -> Result<(), Self::Error> {
235         ty_codec::encode_with_shorthand(self, ty, |ecx| &mut ecx.type_shorthands)
236     }
237 }
238
239 impl<'tcx> SpecializedEncoder<interpret::AllocId> for EncodeContext<'tcx> {
240     fn specialized_encode(&mut self, alloc_id: &interpret::AllocId) -> Result<(), Self::Error> {
241         use std::collections::hash_map::Entry;
242         let index = match self.interpret_allocs.entry(*alloc_id) {
243             Entry::Occupied(e) => *e.get(),
244             Entry::Vacant(e) => {
245                 let idx = self.interpret_allocs_inverse.len();
246                 self.interpret_allocs_inverse.push(*alloc_id);
247                 e.insert(idx);
248                 idx
249             }
250         };
251
252         index.encode(self)
253     }
254 }
255
256 impl<'tcx> SpecializedEncoder<&'tcx [(ty::Predicate<'tcx>, Span)]> for EncodeContext<'tcx> {
257     fn specialized_encode(
258         &mut self,
259         predicates: &&'tcx [(ty::Predicate<'tcx>, Span)],
260     ) -> Result<(), Self::Error> {
261         ty_codec::encode_spanned_predicates(self, predicates, |ecx| &mut ecx.predicate_shorthands)
262     }
263 }
264
265 impl<'tcx> SpecializedEncoder<Fingerprint> for EncodeContext<'tcx> {
266     fn specialized_encode(&mut self, f: &Fingerprint) -> Result<(), Self::Error> {
267         f.encode_opaque(&mut self.opaque)
268     }
269 }
270
271 impl<'tcx, T: Encodable> SpecializedEncoder<mir::ClearCrossCrate<T>> for EncodeContext<'tcx> {
272     fn specialized_encode(&mut self, _: &mir::ClearCrossCrate<T>) -> Result<(), Self::Error> {
273         Ok(())
274     }
275 }
276
277 impl<'tcx> TyEncoder for EncodeContext<'tcx> {
278     fn position(&self) -> usize {
279         self.opaque.position()
280     }
281 }
282
283 /// Helper trait to allow overloading `EncodeContext::lazy` for iterators.
284 trait EncodeContentsForLazy<T: ?Sized + LazyMeta> {
285     fn encode_contents_for_lazy(self, ecx: &mut EncodeContext<'tcx>) -> T::Meta;
286 }
287
288 impl<T: Encodable> EncodeContentsForLazy<T> for &T {
289     fn encode_contents_for_lazy(self, ecx: &mut EncodeContext<'tcx>) {
290         self.encode(ecx).unwrap()
291     }
292 }
293
294 impl<T: Encodable> EncodeContentsForLazy<T> for T {
295     fn encode_contents_for_lazy(self, ecx: &mut EncodeContext<'tcx>) {
296         self.encode(ecx).unwrap()
297     }
298 }
299
300 impl<I, T: Encodable> EncodeContentsForLazy<[T]> for I
301 where
302     I: IntoIterator,
303     I::Item: EncodeContentsForLazy<T>,
304 {
305     fn encode_contents_for_lazy(self, ecx: &mut EncodeContext<'tcx>) -> usize {
306         self.into_iter().map(|value| value.encode_contents_for_lazy(ecx)).count()
307     }
308 }
309
310 // Shorthand for `$self.$tables.$table.set($def_id.index, $self.lazy($value))`, which would
311 // normally need extra variables to avoid errors about multiple mutable borrows.
312 macro_rules! record {
313     ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{
314         {
315             let value = $value;
316             let lazy = $self.lazy(value);
317             $self.$tables.$table.set($def_id.index, lazy);
318         }
319     }};
320 }
321
322 impl<'tcx> EncodeContext<'tcx> {
323     fn emit_lazy_distance<T: ?Sized + LazyMeta>(
324         &mut self,
325         lazy: Lazy<T>,
326     ) -> Result<(), <Self as Encoder>::Error> {
327         let min_end = lazy.position.get() + T::min_size(lazy.meta);
328         let distance = match self.lazy_state {
329             LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"),
330             LazyState::NodeStart(start) => {
331                 let start = start.get();
332                 assert!(min_end <= start);
333                 start - min_end
334             }
335             LazyState::Previous(last_min_end) => {
336                 assert!(
337                     last_min_end <= lazy.position,
338                     "make sure that the calls to `lazy*` \
339                      are in the same order as the metadata fields",
340                 );
341                 lazy.position.get() - last_min_end.get()
342             }
343         };
344         self.lazy_state = LazyState::Previous(NonZeroUsize::new(min_end).unwrap());
345         self.emit_usize(distance)
346     }
347
348     fn lazy<T: ?Sized + LazyMeta>(&mut self, value: impl EncodeContentsForLazy<T>) -> Lazy<T> {
349         let pos = NonZeroUsize::new(self.position()).unwrap();
350
351         assert_eq!(self.lazy_state, LazyState::NoNode);
352         self.lazy_state = LazyState::NodeStart(pos);
353         let meta = value.encode_contents_for_lazy(self);
354         self.lazy_state = LazyState::NoNode;
355
356         assert!(pos.get() + <T>::min_size(meta) <= self.position());
357
358         Lazy::from_position_and_meta(pos, meta)
359     }
360
361     fn encode_info_for_items(&mut self) {
362         let krate = self.tcx.hir().krate();
363         let vis = Spanned { span: rustc_span::DUMMY_SP, node: hir::VisibilityKind::Public };
364         self.encode_info_for_mod(hir::CRATE_HIR_ID, &krate.item.module, &krate.item.attrs, &vis);
365         krate.visit_all_item_likes(&mut self.as_deep_visitor());
366         for macro_def in krate.exported_macros {
367             self.visit_macro_def(macro_def);
368         }
369     }
370
371     fn encode_def_path_table(&mut self) -> Lazy<DefPathTable> {
372         let definitions = self.tcx.hir().definitions();
373         self.lazy(definitions.def_path_table())
374     }
375
376     fn encode_source_map(&mut self) -> Lazy<[rustc_span::SourceFile]> {
377         let source_map = self.tcx.sess.source_map();
378         let all_source_files = source_map.files();
379
380         let (working_dir, _cwd_remapped) = self.tcx.sess.working_dir.clone();
381
382         let adapted = all_source_files
383             .iter()
384             .filter(|source_file| {
385                 // No need to re-export imported source_files, as any downstream
386                 // crate will import them from their original source.
387                 // FIXME(eddyb) the `Span` encoding should take that into account.
388                 !source_file.is_imported()
389             })
390             .map(|source_file| {
391                 match source_file.name {
392                     // This path of this SourceFile has been modified by
393                     // path-remapping, so we use it verbatim (and avoid
394                     // cloning the whole map in the process).
395                     _ if source_file.name_was_remapped => source_file.clone(),
396
397                     // Otherwise expand all paths to absolute paths because
398                     // any relative paths are potentially relative to a
399                     // wrong directory.
400                     FileName::Real(ref name) => {
401                         let mut adapted = (**source_file).clone();
402                         adapted.name = Path::new(&working_dir).join(name).into();
403                         adapted.name_hash = {
404                             let mut hasher: StableHasher = StableHasher::new();
405                             adapted.name.hash(&mut hasher);
406                             hasher.finish::<u128>()
407                         };
408                         Lrc::new(adapted)
409                     }
410
411                     // expanded code, not from a file
412                     _ => source_file.clone(),
413                 }
414             })
415             .collect::<Vec<_>>();
416
417         self.lazy(adapted.iter().map(|rc| &**rc))
418     }
419
420     fn encode_crate_root(&mut self) -> Lazy<CrateRoot<'tcx>> {
421         let is_proc_macro = self.tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
422
423         let mut i = self.position();
424
425         let crate_deps = self.encode_crate_deps();
426         let dylib_dependency_formats = self.encode_dylib_dependency_formats();
427         let dep_bytes = self.position() - i;
428
429         // Encode the lib features.
430         i = self.position();
431         let lib_features = self.encode_lib_features();
432         let lib_feature_bytes = self.position() - i;
433
434         // Encode the language items.
435         i = self.position();
436         let lang_items = self.encode_lang_items();
437         let lang_items_missing = self.encode_lang_items_missing();
438         let lang_item_bytes = self.position() - i;
439
440         // Encode the diagnostic items.
441         i = self.position();
442         let diagnostic_items = self.encode_diagnostic_items();
443         let diagnostic_item_bytes = self.position() - i;
444
445         // Encode the native libraries used
446         i = self.position();
447         let native_libraries = self.encode_native_libraries();
448         let native_lib_bytes = self.position() - i;
449
450         let foreign_modules = self.encode_foreign_modules();
451
452         // Encode source_map
453         i = self.position();
454         let source_map = self.encode_source_map();
455         let source_map_bytes = self.position() - i;
456
457         // Encode DefPathTable
458         i = self.position();
459         let def_path_table = self.encode_def_path_table();
460         let def_path_table_bytes = self.position() - i;
461
462         // Encode the def IDs of impls, for coherence checking.
463         i = self.position();
464         let impls = self.encode_impls();
465         let impl_bytes = self.position() - i;
466
467         let tcx = self.tcx;
468
469         // Encode the items.
470         i = self.position();
471         self.encode_info_for_items();
472         let item_bytes = self.position() - i;
473
474         // Encode the allocation index
475         let interpret_alloc_index = {
476             let mut interpret_alloc_index = Vec::new();
477             let mut n = 0;
478             trace!("beginning to encode alloc ids");
479             loop {
480                 let new_n = self.interpret_allocs_inverse.len();
481                 // if we have found new ids, serialize those, too
482                 if n == new_n {
483                     // otherwise, abort
484                     break;
485                 }
486                 trace!("encoding {} further alloc ids", new_n - n);
487                 for idx in n..new_n {
488                     let id = self.interpret_allocs_inverse[idx];
489                     let pos = self.position() as u32;
490                     interpret_alloc_index.push(pos);
491                     interpret::specialized_encode_alloc_id(self, tcx, id).unwrap();
492                 }
493                 n = new_n;
494             }
495             self.lazy(interpret_alloc_index)
496         };
497
498         i = self.position();
499         let tables = self.tables.encode(&mut self.opaque);
500         let tables_bytes = self.position() - i;
501
502         // Encode the proc macro data
503         i = self.position();
504         let proc_macro_data = self.encode_proc_macros();
505         let proc_macro_data_bytes = self.position() - i;
506
507         // Encode exported symbols info. This is prefetched in `encode_metadata` so we encode
508         // this last to give the prefetching as much time as possible to complete.
509         i = self.position();
510         let exported_symbols = self.tcx.exported_symbols(LOCAL_CRATE);
511         let exported_symbols = self.encode_exported_symbols(&exported_symbols);
512         let exported_symbols_bytes = self.position() - i;
513
514         let attrs = tcx.hir().krate_attrs();
515         let has_default_lib_allocator = attr::contains_name(&attrs, sym::default_lib_allocator);
516
517         let root = self.lazy(CrateRoot {
518             name: tcx.crate_name(LOCAL_CRATE),
519             extra_filename: tcx.sess.opts.cg.extra_filename.clone(),
520             triple: tcx.sess.opts.target_triple.clone(),
521             hash: tcx.crate_hash(LOCAL_CRATE),
522             disambiguator: tcx.sess.local_crate_disambiguator(),
523             panic_strategy: tcx.sess.panic_strategy(),
524             edition: tcx.sess.edition(),
525             has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE),
526             has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE),
527             has_default_lib_allocator,
528             plugin_registrar_fn: tcx.plugin_registrar_fn(LOCAL_CRATE).map(|id| id.index),
529             proc_macro_decls_static: if is_proc_macro {
530                 let id = tcx.proc_macro_decls_static(LOCAL_CRATE).unwrap();
531                 Some(id.index)
532             } else {
533                 None
534             },
535             proc_macro_data,
536             proc_macro_stability: if is_proc_macro {
537                 tcx.lookup_stability(DefId::local(CRATE_DEF_INDEX)).copied()
538             } else {
539                 None
540             },
541             compiler_builtins: attr::contains_name(&attrs, sym::compiler_builtins),
542             needs_allocator: attr::contains_name(&attrs, sym::needs_allocator),
543             needs_panic_runtime: attr::contains_name(&attrs, sym::needs_panic_runtime),
544             no_builtins: attr::contains_name(&attrs, sym::no_builtins),
545             panic_runtime: attr::contains_name(&attrs, sym::panic_runtime),
546             profiler_runtime: attr::contains_name(&attrs, sym::profiler_runtime),
547             symbol_mangling_version: tcx.sess.opts.debugging_opts.symbol_mangling_version,
548
549             crate_deps,
550             dylib_dependency_formats,
551             lib_features,
552             lang_items,
553             diagnostic_items,
554             lang_items_missing,
555             native_libraries,
556             foreign_modules,
557             source_map,
558             def_path_table,
559             impls,
560             exported_symbols,
561             interpret_alloc_index,
562             tables,
563         });
564
565         let total_bytes = self.position();
566
567         if self.tcx.sess.meta_stats() {
568             let mut zero_bytes = 0;
569             for e in self.opaque.data.iter() {
570                 if *e == 0 {
571                     zero_bytes += 1;
572                 }
573             }
574
575             println!("metadata stats:");
576             println!("             dep bytes: {}", dep_bytes);
577             println!("     lib feature bytes: {}", lib_feature_bytes);
578             println!("       lang item bytes: {}", lang_item_bytes);
579             println!(" diagnostic item bytes: {}", diagnostic_item_bytes);
580             println!("          native bytes: {}", native_lib_bytes);
581             println!("         source_map bytes: {}", source_map_bytes);
582             println!("            impl bytes: {}", impl_bytes);
583             println!("    exp. symbols bytes: {}", exported_symbols_bytes);
584             println!("  def-path table bytes: {}", def_path_table_bytes);
585             println!(" proc-macro-data-bytes: {}", proc_macro_data_bytes);
586             println!("            item bytes: {}", item_bytes);
587             println!("           table bytes: {}", tables_bytes);
588             println!("            zero bytes: {}", zero_bytes);
589             println!("           total bytes: {}", total_bytes);
590         }
591
592         root
593     }
594 }
595
596 impl EncodeContext<'tcx> {
597     fn encode_variances_of(&mut self, def_id: DefId) {
598         debug!("EncodeContext::encode_variances_of({:?})", def_id);
599         record!(self.tables.variances[def_id] <- &self.tcx.variances_of(def_id)[..]);
600     }
601
602     fn encode_item_type(&mut self, def_id: DefId) {
603         debug!("EncodeContext::encode_item_type({:?})", def_id);
604         record!(self.tables.ty[def_id] <- self.tcx.type_of(def_id));
605     }
606
607     fn encode_enum_variant_info(&mut self, enum_did: DefId, index: VariantIdx) {
608         let tcx = self.tcx;
609         let def = tcx.adt_def(enum_did);
610         let variant = &def.variants[index];
611         let def_id = variant.def_id;
612         debug!("EncodeContext::encode_enum_variant_info({:?})", def_id);
613
614         let data = VariantData {
615             ctor_kind: variant.ctor_kind,
616             discr: variant.discr,
617             ctor: variant.ctor_def_id.map(|did| did.index),
618         };
619
620         let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();
621         let enum_vis = &tcx.hir().expect_item(enum_id).vis;
622
623         record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
624         record!(self.tables.visibility[def_id] <-
625             ty::Visibility::from_hir(enum_vis, enum_id, self.tcx));
626         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
627         record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
628         record!(self.tables.children[def_id] <- variant.fields.iter().map(|f| {
629             assert!(f.did.is_local());
630             f.did.index
631         }));
632         self.encode_ident_span(def_id, variant.ident);
633         self.encode_stability(def_id);
634         self.encode_deprecation(def_id);
635         self.encode_item_type(def_id);
636         if variant.ctor_kind == CtorKind::Fn {
637             // FIXME(eddyb) encode signature only in `encode_enum_variant_ctor`.
638             if let Some(ctor_def_id) = variant.ctor_def_id {
639                 record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(ctor_def_id));
640             }
641             // FIXME(eddyb) is this ever used?
642             self.encode_variances_of(def_id);
643         }
644         self.encode_generics(def_id);
645         self.encode_explicit_predicates(def_id);
646         self.encode_inferred_outlives(def_id);
647         self.encode_optimized_mir(def_id);
648         self.encode_promoted_mir(def_id);
649     }
650
651     fn encode_enum_variant_ctor(&mut self, enum_did: DefId, index: VariantIdx) {
652         let tcx = self.tcx;
653         let def = tcx.adt_def(enum_did);
654         let variant = &def.variants[index];
655         let def_id = variant.ctor_def_id.unwrap();
656         debug!("EncodeContext::encode_enum_variant_ctor({:?})", def_id);
657
658         // FIXME(eddyb) encode only the `CtorKind` for constructors.
659         let data = VariantData {
660             ctor_kind: variant.ctor_kind,
661             discr: variant.discr,
662             ctor: Some(def_id.index),
663         };
664
665         // Variant constructors have the same visibility as the parent enums, unless marked as
666         // non-exhaustive, in which case they are lowered to `pub(crate)`.
667         let enum_id = tcx.hir().as_local_hir_id(enum_did).unwrap();
668         let enum_vis = &tcx.hir().expect_item(enum_id).vis;
669         let mut ctor_vis = ty::Visibility::from_hir(enum_vis, enum_id, tcx);
670         if variant.is_field_list_non_exhaustive() && ctor_vis == ty::Visibility::Public {
671             ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
672         }
673
674         record!(self.tables.kind[def_id] <- EntryKind::Variant(self.lazy(data)));
675         record!(self.tables.visibility[def_id] <- ctor_vis);
676         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
677         self.encode_stability(def_id);
678         self.encode_deprecation(def_id);
679         self.encode_item_type(def_id);
680         if variant.ctor_kind == CtorKind::Fn {
681             record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
682             self.encode_variances_of(def_id);
683         }
684         self.encode_generics(def_id);
685         self.encode_explicit_predicates(def_id);
686         self.encode_inferred_outlives(def_id);
687         self.encode_optimized_mir(def_id);
688         self.encode_promoted_mir(def_id);
689     }
690
691     fn encode_info_for_mod(
692         &mut self,
693         id: hir::HirId,
694         md: &hir::Mod<'_>,
695         attrs: &[ast::Attribute],
696         vis: &hir::Visibility<'_>,
697     ) {
698         let tcx = self.tcx;
699         let def_id = tcx.hir().local_def_id(id);
700         debug!("EncodeContext::encode_info_for_mod({:?})", def_id);
701
702         let data = ModData {
703             reexports: match tcx.module_exports(def_id) {
704                 Some(exports) => self.lazy(exports),
705                 _ => Lazy::empty(),
706             },
707         };
708
709         record!(self.tables.kind[def_id] <- EntryKind::Mod(self.lazy(data)));
710         record!(self.tables.visibility[def_id] <- ty::Visibility::from_hir(vis, id, self.tcx));
711         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
712         record!(self.tables.attributes[def_id] <- attrs);
713         record!(self.tables.children[def_id] <- md.item_ids.iter().map(|item_id| {
714             tcx.hir().local_def_id(item_id.id).index
715         }));
716         self.encode_stability(def_id);
717         self.encode_deprecation(def_id);
718     }
719
720     fn encode_field(&mut self, adt_def_id: DefId, variant_index: VariantIdx, field_index: usize) {
721         let tcx = self.tcx;
722         let variant = &tcx.adt_def(adt_def_id).variants[variant_index];
723         let field = &variant.fields[field_index];
724
725         let def_id = field.did;
726         debug!("EncodeContext::encode_field({:?})", def_id);
727
728         let variant_id = tcx.hir().as_local_hir_id(variant.def_id).unwrap();
729         let variant_data = tcx.hir().expect_variant_data(variant_id);
730
731         record!(self.tables.kind[def_id] <- EntryKind::Field);
732         record!(self.tables.visibility[def_id] <- field.vis);
733         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
734         record!(self.tables.attributes[def_id] <- variant_data.fields()[field_index].attrs);
735         self.encode_ident_span(def_id, field.ident);
736         self.encode_stability(def_id);
737         self.encode_deprecation(def_id);
738         self.encode_item_type(def_id);
739         self.encode_generics(def_id);
740         self.encode_explicit_predicates(def_id);
741         self.encode_inferred_outlives(def_id);
742     }
743
744     fn encode_struct_ctor(&mut self, adt_def_id: DefId, def_id: DefId) {
745         debug!("EncodeContext::encode_struct_ctor({:?})", def_id);
746         let tcx = self.tcx;
747         let adt_def = tcx.adt_def(adt_def_id);
748         let variant = adt_def.non_enum_variant();
749
750         let data = VariantData {
751             ctor_kind: variant.ctor_kind,
752             discr: variant.discr,
753             ctor: Some(def_id.index),
754         };
755
756         let struct_id = tcx.hir().as_local_hir_id(adt_def_id).unwrap();
757         let struct_vis = &tcx.hir().expect_item(struct_id).vis;
758         let mut ctor_vis = ty::Visibility::from_hir(struct_vis, struct_id, tcx);
759         for field in &variant.fields {
760             if ctor_vis.is_at_least(field.vis, tcx) {
761                 ctor_vis = field.vis;
762             }
763         }
764
765         // If the structure is marked as non_exhaustive then lower the visibility
766         // to within the crate.
767         if adt_def.non_enum_variant().is_field_list_non_exhaustive()
768             && ctor_vis == ty::Visibility::Public
769         {
770             ctor_vis = ty::Visibility::Restricted(DefId::local(CRATE_DEF_INDEX));
771         }
772
773         record!(self.tables.kind[def_id] <- EntryKind::Struct(self.lazy(data), adt_def.repr));
774         record!(self.tables.visibility[def_id] <- ctor_vis);
775         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
776         self.encode_stability(def_id);
777         self.encode_deprecation(def_id);
778         self.encode_item_type(def_id);
779         if variant.ctor_kind == CtorKind::Fn {
780             record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
781             self.encode_variances_of(def_id);
782         }
783         self.encode_generics(def_id);
784         self.encode_explicit_predicates(def_id);
785         self.encode_inferred_outlives(def_id);
786         self.encode_optimized_mir(def_id);
787         self.encode_promoted_mir(def_id);
788     }
789
790     fn encode_generics(&mut self, def_id: DefId) {
791         debug!("EncodeContext::encode_generics({:?})", def_id);
792         record!(self.tables.generics[def_id] <- self.tcx.generics_of(def_id));
793     }
794
795     fn encode_explicit_predicates(&mut self, def_id: DefId) {
796         debug!("EncodeContext::encode_explicit_predicates({:?})", def_id);
797         record!(self.tables.explicit_predicates[def_id] <-
798             self.tcx.explicit_predicates_of(def_id));
799     }
800
801     fn encode_inferred_outlives(&mut self, def_id: DefId) {
802         debug!("EncodeContext::encode_inferred_outlives({:?})", def_id);
803         let inferred_outlives = self.tcx.inferred_outlives_of(def_id);
804         if !inferred_outlives.is_empty() {
805             record!(self.tables.inferred_outlives[def_id] <- inferred_outlives);
806         }
807     }
808
809     fn encode_super_predicates(&mut self, def_id: DefId) {
810         debug!("EncodeContext::encode_super_predicates({:?})", def_id);
811         record!(self.tables.super_predicates[def_id] <- self.tcx.super_predicates_of(def_id));
812     }
813
814     fn encode_info_for_trait_item(&mut self, def_id: DefId) {
815         debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id);
816         let tcx = self.tcx;
817
818         let hir_id = tcx.hir().as_local_hir_id(def_id).unwrap();
819         let ast_item = tcx.hir().expect_trait_item(hir_id);
820         let trait_item = tcx.associated_item(def_id);
821
822         let container = match trait_item.defaultness {
823             hir::Defaultness::Default { has_value: true } => AssocContainer::TraitWithDefault,
824             hir::Defaultness::Default { has_value: false } => AssocContainer::TraitRequired,
825             hir::Defaultness::Final => span_bug!(ast_item.span, "traits cannot have final items"),
826         };
827
828         record!(self.tables.kind[def_id] <- match trait_item.kind {
829             ty::AssocKind::Const => {
830                 let rendered = rustc_hir_pretty::to_string(
831                     &(&self.tcx.hir() as &dyn intravisit::Map<'_>),
832                     |s| s.print_trait_item(ast_item)
833                 );
834                 let rendered_const = self.lazy(RenderedConst(rendered));
835
836                 EntryKind::AssocConst(
837                     container,
838                     Default::default(),
839                     rendered_const,
840                 )
841             }
842             ty::AssocKind::Method => {
843                 let fn_data = if let hir::TraitItemKind::Fn(m_sig, m) = &ast_item.kind {
844                     let param_names = match *m {
845                         hir::TraitFn::Required(ref names) => {
846                             self.encode_fn_param_names(names)
847                         }
848                         hir::TraitFn::Provided(body) => {
849                             self.encode_fn_param_names_for_body(body)
850                         }
851                     };
852                     FnData {
853                         asyncness: m_sig.header.asyncness,
854                         constness: hir::Constness::NotConst,
855                         param_names,
856                     }
857                 } else {
858                     bug!()
859                 };
860                 EntryKind::AssocFn(self.lazy(AssocFnData {
861                     fn_data,
862                     container,
863                     has_self: trait_item.method_has_self_argument,
864                 }))
865             }
866             ty::AssocKind::Type => EntryKind::AssocType(container),
867             ty::AssocKind::OpaqueTy => span_bug!(ast_item.span, "opaque type in trait"),
868         });
869         record!(self.tables.visibility[def_id] <- trait_item.vis);
870         record!(self.tables.span[def_id] <- ast_item.span);
871         record!(self.tables.attributes[def_id] <- ast_item.attrs);
872         self.encode_ident_span(def_id, ast_item.ident);
873         self.encode_stability(def_id);
874         self.encode_const_stability(def_id);
875         self.encode_deprecation(def_id);
876         match trait_item.kind {
877             ty::AssocKind::Const | ty::AssocKind::Method => {
878                 self.encode_item_type(def_id);
879             }
880             ty::AssocKind::Type => {
881                 if trait_item.defaultness.has_value() {
882                     self.encode_item_type(def_id);
883                 }
884             }
885             ty::AssocKind::OpaqueTy => unreachable!(),
886         }
887         if trait_item.kind == ty::AssocKind::Method {
888             record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
889             self.encode_variances_of(def_id);
890         }
891         self.encode_generics(def_id);
892         self.encode_explicit_predicates(def_id);
893         self.encode_inferred_outlives(def_id);
894
895         // This should be kept in sync with `PrefetchVisitor.visit_trait_item`.
896         self.encode_optimized_mir(def_id);
897         self.encode_promoted_mir(def_id);
898     }
899
900     fn metadata_output_only(&self) -> bool {
901         // MIR optimisation can be skipped when we're just interested in the metadata.
902         !self.tcx.sess.opts.output_types.should_codegen()
903     }
904
905     fn encode_info_for_impl_item(&mut self, def_id: DefId) {
906         debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id);
907         let tcx = self.tcx;
908
909         let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
910         let ast_item = self.tcx.hir().expect_impl_item(hir_id);
911         let impl_item = self.tcx.associated_item(def_id);
912
913         let container = match impl_item.defaultness {
914             hir::Defaultness::Default { has_value: true } => AssocContainer::ImplDefault,
915             hir::Defaultness::Final => AssocContainer::ImplFinal,
916             hir::Defaultness::Default { has_value: false } => {
917                 span_bug!(ast_item.span, "impl items always have values (currently)")
918             }
919         };
920
921         record!(self.tables.kind[def_id] <- match impl_item.kind {
922             ty::AssocKind::Const => {
923                 if let hir::ImplItemKind::Const(_, body_id) = ast_item.kind {
924                     let qualifs = self.tcx.at(ast_item.span).mir_const_qualif(def_id);
925
926                     EntryKind::AssocConst(
927                         container,
928                         qualifs,
929                         self.encode_rendered_const_for_body(body_id))
930                 } else {
931                     bug!()
932                 }
933             }
934             ty::AssocKind::Method => {
935                 let fn_data = if let hir::ImplItemKind::Fn(ref sig, body) = ast_item.kind {
936                     FnData {
937                         asyncness: sig.header.asyncness,
938                         constness: sig.header.constness,
939                         param_names: self.encode_fn_param_names_for_body(body),
940                     }
941                 } else {
942                     bug!()
943                 };
944                 EntryKind::AssocFn(self.lazy(AssocFnData {
945                     fn_data,
946                     container,
947                     has_self: impl_item.method_has_self_argument,
948                 }))
949             }
950             ty::AssocKind::OpaqueTy => EntryKind::AssocOpaqueTy(container),
951             ty::AssocKind::Type => EntryKind::AssocType(container)
952         });
953         record!(self.tables.visibility[def_id] <- impl_item.vis);
954         record!(self.tables.span[def_id] <- ast_item.span);
955         record!(self.tables.attributes[def_id] <- ast_item.attrs);
956         self.encode_ident_span(def_id, impl_item.ident);
957         self.encode_stability(def_id);
958         self.encode_const_stability(def_id);
959         self.encode_deprecation(def_id);
960         self.encode_item_type(def_id);
961         if impl_item.kind == ty::AssocKind::Method {
962             record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
963             self.encode_variances_of(def_id);
964         }
965         self.encode_generics(def_id);
966         self.encode_explicit_predicates(def_id);
967         self.encode_inferred_outlives(def_id);
968
969         // The following part should be kept in sync with `PrefetchVisitor.visit_impl_item`.
970
971         let mir = match ast_item.kind {
972             hir::ImplItemKind::Const(..) => true,
973             hir::ImplItemKind::Fn(ref sig, _) => {
974                 let generics = self.tcx.generics_of(def_id);
975                 let needs_inline = (generics.requires_monomorphization(self.tcx)
976                     || tcx.codegen_fn_attrs(def_id).requests_inline())
977                     && !self.metadata_output_only();
978                 let is_const_fn = sig.header.constness == hir::Constness::Const;
979                 let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
980                 needs_inline || is_const_fn || always_encode_mir
981             }
982             hir::ImplItemKind::OpaqueTy(..) | hir::ImplItemKind::TyAlias(..) => false,
983         };
984         if mir {
985             self.encode_optimized_mir(def_id);
986             self.encode_promoted_mir(def_id);
987         }
988     }
989
990     fn encode_fn_param_names_for_body(&mut self, body_id: hir::BodyId) -> Lazy<[ast::Name]> {
991         self.tcx.dep_graph.with_ignore(|| {
992             let body = self.tcx.hir().body(body_id);
993             self.lazy(body.params.iter().map(|arg| match arg.pat.kind {
994                 hir::PatKind::Binding(_, _, ident, _) => ident.name,
995                 _ => kw::Invalid,
996             }))
997         })
998     }
999
1000     fn encode_fn_param_names(&mut self, param_names: &[ast::Ident]) -> Lazy<[ast::Name]> {
1001         self.lazy(param_names.iter().map(|ident| ident.name))
1002     }
1003
1004     fn encode_optimized_mir(&mut self, def_id: DefId) {
1005         debug!("EntryBuilder::encode_mir({:?})", def_id);
1006         if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
1007             record!(self.tables.mir[def_id] <- self.tcx.optimized_mir(def_id));
1008         }
1009     }
1010
1011     fn encode_promoted_mir(&mut self, def_id: DefId) {
1012         debug!("EncodeContext::encode_promoted_mir({:?})", def_id);
1013         if self.tcx.mir_keys(LOCAL_CRATE).contains(&def_id) {
1014             record!(self.tables.promoted_mir[def_id] <- self.tcx.promoted_mir(def_id));
1015         }
1016     }
1017
1018     // Encodes the inherent implementations of a structure, enumeration, or trait.
1019     fn encode_inherent_implementations(&mut self, def_id: DefId) {
1020         debug!("EncodeContext::encode_inherent_implementations({:?})", def_id);
1021         let implementations = self.tcx.inherent_impls(def_id);
1022         if !implementations.is_empty() {
1023             record!(self.tables.inherent_impls[def_id] <- implementations.iter().map(|&def_id| {
1024                 assert!(def_id.is_local());
1025                 def_id.index
1026             }));
1027         }
1028     }
1029
1030     fn encode_stability(&mut self, def_id: DefId) {
1031         debug!("EncodeContext::encode_stability({:?})", def_id);
1032         if let Some(stab) = self.tcx.lookup_stability(def_id) {
1033             record!(self.tables.stability[def_id] <- stab)
1034         }
1035     }
1036
1037     fn encode_const_stability(&mut self, def_id: DefId) {
1038         debug!("EncodeContext::encode_const_stability({:?})", def_id);
1039         if let Some(stab) = self.tcx.lookup_const_stability(def_id) {
1040             record!(self.tables.const_stability[def_id] <- stab)
1041         }
1042     }
1043
1044     fn encode_deprecation(&mut self, def_id: DefId) {
1045         debug!("EncodeContext::encode_deprecation({:?})", def_id);
1046         if let Some(depr) = self.tcx.lookup_deprecation(def_id) {
1047             record!(self.tables.deprecation[def_id] <- depr);
1048         }
1049     }
1050
1051     fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> Lazy<RenderedConst> {
1052         let hir = self.tcx.hir();
1053         let body = hir.body(body_id);
1054         let rendered = rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| {
1055             s.print_expr(&body.value)
1056         });
1057         let rendered_const = &RenderedConst(rendered);
1058         self.lazy(rendered_const)
1059     }
1060
1061     fn encode_info_for_item(&mut self, def_id: DefId, item: &'tcx hir::Item<'tcx>) {
1062         let tcx = self.tcx;
1063
1064         debug!("EncodeContext::encode_info_for_item({:?})", def_id);
1065
1066         self.encode_ident_span(def_id, item.ident);
1067
1068         record!(self.tables.kind[def_id] <- match item.kind {
1069             hir::ItemKind::Static(_, hir::Mutability::Mut, _) => EntryKind::MutStatic,
1070             hir::ItemKind::Static(_, hir::Mutability::Not, _) => EntryKind::ImmStatic,
1071             hir::ItemKind::Const(_, body_id) => {
1072                 let qualifs = self.tcx.at(item.span).mir_const_qualif(def_id);
1073                 EntryKind::Const(
1074                     qualifs,
1075                     self.encode_rendered_const_for_body(body_id)
1076                 )
1077             }
1078             hir::ItemKind::Fn(ref sig, .., body) => {
1079                 let data = FnData {
1080                     asyncness: sig.header.asyncness,
1081                     constness: sig.header.constness,
1082                     param_names: self.encode_fn_param_names_for_body(body),
1083                 };
1084
1085                 EntryKind::Fn(self.lazy(data))
1086             }
1087             hir::ItemKind::Mod(ref m) => {
1088                 return self.encode_info_for_mod(item.hir_id, m, &item.attrs, &item.vis);
1089             }
1090             hir::ItemKind::ForeignMod(_) => EntryKind::ForeignMod,
1091             hir::ItemKind::GlobalAsm(..) => EntryKind::GlobalAsm,
1092             hir::ItemKind::TyAlias(..) => EntryKind::Type,
1093             hir::ItemKind::OpaqueTy(..) => EntryKind::OpaqueTy,
1094             hir::ItemKind::Enum(..) => EntryKind::Enum(self.tcx.adt_def(def_id).repr),
1095             hir::ItemKind::Struct(ref struct_def, _) => {
1096                 let adt_def = self.tcx.adt_def(def_id);
1097                 let variant = adt_def.non_enum_variant();
1098
1099                 // Encode def_ids for each field and method
1100                 // for methods, write all the stuff get_trait_method
1101                 // needs to know
1102                 let ctor = struct_def.ctor_hir_id().map(|ctor_hir_id| {
1103                     self.tcx.hir().local_def_id(ctor_hir_id).index
1104                 });
1105
1106                 EntryKind::Struct(self.lazy(VariantData {
1107                     ctor_kind: variant.ctor_kind,
1108                     discr: variant.discr,
1109                     ctor,
1110                 }), adt_def.repr)
1111             }
1112             hir::ItemKind::Union(..) => {
1113                 let adt_def = self.tcx.adt_def(def_id);
1114                 let variant = adt_def.non_enum_variant();
1115
1116                 EntryKind::Union(self.lazy(VariantData {
1117                     ctor_kind: variant.ctor_kind,
1118                     discr: variant.discr,
1119                     ctor: None,
1120                 }), adt_def.repr)
1121             }
1122             hir::ItemKind::Impl { defaultness, .. } => {
1123                 let trait_ref = self.tcx.impl_trait_ref(def_id);
1124                 let polarity = self.tcx.impl_polarity(def_id);
1125                 let parent = if let Some(trait_ref) = trait_ref {
1126                     let trait_def = self.tcx.trait_def(trait_ref.def_id);
1127                     trait_def.ancestors(self.tcx, def_id).ok()
1128                         .and_then(|mut an| an.nth(1).and_then(|node| {
1129                             match node {
1130                                 specialization_graph::Node::Impl(parent) => Some(parent),
1131                                 _ => None,
1132                             }
1133                         }))
1134                 } else {
1135                     None
1136                 };
1137
1138                 // if this is an impl of `CoerceUnsized`, create its
1139                 // "unsized info", else just store None
1140                 let coerce_unsized_info =
1141                     trait_ref.and_then(|t| {
1142                         if Some(t.def_id) == self.tcx.lang_items().coerce_unsized_trait() {
1143                             Some(self.tcx.at(item.span).coerce_unsized_info(def_id))
1144                         } else {
1145                             None
1146                         }
1147                     });
1148
1149                 let data = ImplData {
1150                     polarity,
1151                     defaultness,
1152                     parent_impl: parent,
1153                     coerce_unsized_info,
1154                 };
1155
1156                 EntryKind::Impl(self.lazy(data))
1157             }
1158             hir::ItemKind::Trait(..) => {
1159                 let trait_def = self.tcx.trait_def(def_id);
1160                 let data = TraitData {
1161                     unsafety: trait_def.unsafety,
1162                     paren_sugar: trait_def.paren_sugar,
1163                     has_auto_impl: self.tcx.trait_is_auto(def_id),
1164                     is_marker: trait_def.is_marker,
1165                     specialization_kind: trait_def.specialization_kind,
1166                 };
1167
1168                 EntryKind::Trait(self.lazy(data))
1169             }
1170             hir::ItemKind::TraitAlias(..) => EntryKind::TraitAlias,
1171             hir::ItemKind::ExternCrate(_) |
1172             hir::ItemKind::Use(..) => bug!("cannot encode info for item {:?}", item),
1173         });
1174         record!(self.tables.visibility[def_id] <-
1175             ty::Visibility::from_hir(&item.vis, item.hir_id, tcx));
1176         record!(self.tables.span[def_id] <- item.span);
1177         record!(self.tables.attributes[def_id] <- item.attrs);
1178         // FIXME(eddyb) there should be a nicer way to do this.
1179         match item.kind {
1180             hir::ItemKind::ForeignMod(ref fm) => record!(self.tables.children[def_id] <-
1181                 fm.items
1182                     .iter()
1183                     .map(|foreign_item| tcx.hir().local_def_id(
1184                         foreign_item.hir_id).index)
1185             ),
1186             hir::ItemKind::Enum(..) => record!(self.tables.children[def_id] <-
1187                 self.tcx.adt_def(def_id).variants.iter().map(|v| {
1188                     assert!(v.def_id.is_local());
1189                     v.def_id.index
1190                 })
1191             ),
1192             hir::ItemKind::Struct(..) | hir::ItemKind::Union(..) => {
1193                 record!(self.tables.children[def_id] <-
1194                     self.tcx.adt_def(def_id).non_enum_variant().fields.iter().map(|f| {
1195                         assert!(f.did.is_local());
1196                         f.did.index
1197                     })
1198                 )
1199             }
1200             hir::ItemKind::Impl { .. } | hir::ItemKind::Trait(..) => {
1201                 let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id);
1202                 record!(self.tables.children[def_id] <-
1203                     associated_item_def_ids.iter().map(|&def_id| {
1204                         assert!(def_id.is_local());
1205                         def_id.index
1206                     })
1207                 );
1208             }
1209             _ => {}
1210         }
1211         self.encode_stability(def_id);
1212         self.encode_const_stability(def_id);
1213         self.encode_deprecation(def_id);
1214         match item.kind {
1215             hir::ItemKind::Static(..)
1216             | hir::ItemKind::Const(..)
1217             | hir::ItemKind::Fn(..)
1218             | hir::ItemKind::TyAlias(..)
1219             | hir::ItemKind::OpaqueTy(..)
1220             | hir::ItemKind::Enum(..)
1221             | hir::ItemKind::Struct(..)
1222             | hir::ItemKind::Union(..)
1223             | hir::ItemKind::Impl { .. } => self.encode_item_type(def_id),
1224             _ => {}
1225         }
1226         if let hir::ItemKind::Fn(..) = item.kind {
1227             record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1228         }
1229         if let hir::ItemKind::Impl { .. } = item.kind {
1230             if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) {
1231                 record!(self.tables.impl_trait_ref[def_id] <- trait_ref);
1232             }
1233         }
1234         self.encode_inherent_implementations(def_id);
1235         match item.kind {
1236             hir::ItemKind::Enum(..)
1237             | hir::ItemKind::Struct(..)
1238             | hir::ItemKind::Union(..)
1239             | hir::ItemKind::Fn(..) => self.encode_variances_of(def_id),
1240             _ => {}
1241         }
1242         match item.kind {
1243             hir::ItemKind::Static(..)
1244             | hir::ItemKind::Const(..)
1245             | hir::ItemKind::Fn(..)
1246             | hir::ItemKind::TyAlias(..)
1247             | hir::ItemKind::Enum(..)
1248             | hir::ItemKind::Struct(..)
1249             | hir::ItemKind::Union(..)
1250             | hir::ItemKind::Impl { .. }
1251             | hir::ItemKind::OpaqueTy(..)
1252             | hir::ItemKind::Trait(..)
1253             | hir::ItemKind::TraitAlias(..) => {
1254                 self.encode_generics(def_id);
1255                 self.encode_explicit_predicates(def_id);
1256                 self.encode_inferred_outlives(def_id);
1257             }
1258             _ => {}
1259         }
1260         match item.kind {
1261             hir::ItemKind::Trait(..) | hir::ItemKind::TraitAlias(..) => {
1262                 self.encode_super_predicates(def_id);
1263             }
1264             _ => {}
1265         }
1266
1267         // The following part should be kept in sync with `PrefetchVisitor.visit_item`.
1268
1269         let mir = match item.kind {
1270             hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => true,
1271             hir::ItemKind::Fn(ref sig, ..) => {
1272                 let generics = tcx.generics_of(def_id);
1273                 let needs_inline = (generics.requires_monomorphization(tcx)
1274                     || tcx.codegen_fn_attrs(def_id).requests_inline())
1275                     && !self.metadata_output_only();
1276                 let always_encode_mir = self.tcx.sess.opts.debugging_opts.always_encode_mir;
1277                 needs_inline || sig.header.constness == hir::Constness::Const || always_encode_mir
1278             }
1279             _ => false,
1280         };
1281         if mir {
1282             self.encode_optimized_mir(def_id);
1283             self.encode_promoted_mir(def_id);
1284         }
1285     }
1286
1287     /// Serialize the text of exported macros
1288     fn encode_info_for_macro_def(&mut self, macro_def: &hir::MacroDef<'_>) {
1289         let def_id = self.tcx.hir().local_def_id(macro_def.hir_id);
1290         record!(self.tables.kind[def_id] <- EntryKind::MacroDef(self.lazy(macro_def.ast.clone())));
1291         record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1292         record!(self.tables.span[def_id] <- macro_def.span);
1293         record!(self.tables.attributes[def_id] <- macro_def.attrs);
1294         self.encode_ident_span(def_id, macro_def.ident);
1295         self.encode_stability(def_id);
1296         self.encode_deprecation(def_id);
1297     }
1298
1299     fn encode_info_for_generic_param(&mut self, def_id: DefId, kind: EntryKind, encode_type: bool) {
1300         record!(self.tables.kind[def_id] <- kind);
1301         record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1302         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
1303         if encode_type {
1304             self.encode_item_type(def_id);
1305         }
1306     }
1307
1308     fn encode_info_for_closure(&mut self, def_id: DefId) {
1309         debug!("EncodeContext::encode_info_for_closure({:?})", def_id);
1310
1311         // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic,
1312         // including on the signature, which is inferred in `typeck_tables_of.
1313         let hir_id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
1314         let ty = self.tcx.typeck_tables_of(def_id).node_type(hir_id);
1315
1316         record!(self.tables.kind[def_id] <- match ty.kind {
1317             ty::Generator(..) => {
1318                 let data = self.tcx.generator_kind(def_id).unwrap();
1319                 EntryKind::Generator(data)
1320             }
1321
1322             ty::Closure(..) => EntryKind::Closure,
1323
1324             _ => bug!("closure that is neither generator nor closure"),
1325         });
1326         record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1327         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
1328         record!(self.tables.attributes[def_id] <- &self.tcx.get_attrs(def_id)[..]);
1329         self.encode_item_type(def_id);
1330         if let ty::Closure(def_id, substs) = ty.kind {
1331             record!(self.tables.fn_sig[def_id] <- substs.as_closure().sig());
1332         }
1333         self.encode_generics(def_id);
1334         self.encode_optimized_mir(def_id);
1335         self.encode_promoted_mir(def_id);
1336     }
1337
1338     fn encode_info_for_anon_const(&mut self, def_id: DefId) {
1339         debug!("EncodeContext::encode_info_for_anon_const({:?})", def_id);
1340         let id = self.tcx.hir().as_local_hir_id(def_id).unwrap();
1341         let body_id = self.tcx.hir().body_owned_by(id);
1342         let const_data = self.encode_rendered_const_for_body(body_id);
1343         let qualifs = self.tcx.mir_const_qualif(def_id);
1344
1345         record!(self.tables.kind[def_id] <- EntryKind::Const(qualifs, const_data));
1346         record!(self.tables.visibility[def_id] <- ty::Visibility::Public);
1347         record!(self.tables.span[def_id] <- self.tcx.def_span(def_id));
1348         self.encode_item_type(def_id);
1349         self.encode_generics(def_id);
1350         self.encode_explicit_predicates(def_id);
1351         self.encode_inferred_outlives(def_id);
1352         self.encode_optimized_mir(def_id);
1353         self.encode_promoted_mir(def_id);
1354     }
1355
1356     fn encode_native_libraries(&mut self) -> Lazy<[NativeLibrary]> {
1357         let used_libraries = self.tcx.native_libraries(LOCAL_CRATE);
1358         self.lazy(used_libraries.iter().cloned())
1359     }
1360
1361     fn encode_foreign_modules(&mut self) -> Lazy<[ForeignModule]> {
1362         let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE);
1363         self.lazy(foreign_modules.iter().cloned())
1364     }
1365
1366     fn encode_proc_macros(&mut self) -> Option<Lazy<[DefIndex]>> {
1367         let is_proc_macro = self.tcx.sess.crate_types.borrow().contains(&CrateType::ProcMacro);
1368         if is_proc_macro {
1369             let tcx = self.tcx;
1370             Some(self.lazy(tcx.hir().krate().proc_macros.iter().map(|p| p.owner.local_def_index)))
1371         } else {
1372             None
1373         }
1374     }
1375
1376     fn encode_crate_deps(&mut self) -> Lazy<[CrateDep]> {
1377         let crates = self.tcx.crates();
1378
1379         let mut deps = crates
1380             .iter()
1381             .map(|&cnum| {
1382                 let dep = CrateDep {
1383                     name: self.tcx.original_crate_name(cnum),
1384                     hash: self.tcx.crate_hash(cnum),
1385                     host_hash: self.tcx.crate_host_hash(cnum),
1386                     kind: self.tcx.dep_kind(cnum),
1387                     extra_filename: self.tcx.extra_filename(cnum),
1388                 };
1389                 (cnum, dep)
1390             })
1391             .collect::<Vec<_>>();
1392
1393         deps.sort_by_key(|&(cnum, _)| cnum);
1394
1395         {
1396             // Sanity-check the crate numbers
1397             let mut expected_cnum = 1;
1398             for &(n, _) in &deps {
1399                 assert_eq!(n, CrateNum::new(expected_cnum));
1400                 expected_cnum += 1;
1401             }
1402         }
1403
1404         // We're just going to write a list of crate 'name-hash-version's, with
1405         // the assumption that they are numbered 1 to n.
1406         // FIXME (#2166): This is not nearly enough to support correct versioning
1407         // but is enough to get transitive crate dependencies working.
1408         self.lazy(deps.iter().map(|&(_, ref dep)| dep))
1409     }
1410
1411     fn encode_lib_features(&mut self) -> Lazy<[(ast::Name, Option<ast::Name>)]> {
1412         let tcx = self.tcx;
1413         let lib_features = tcx.lib_features();
1414         self.lazy(lib_features.to_vec())
1415     }
1416
1417     fn encode_diagnostic_items(&mut self) -> Lazy<[(Symbol, DefIndex)]> {
1418         let tcx = self.tcx;
1419         let diagnostic_items = tcx.diagnostic_items(LOCAL_CRATE);
1420         self.lazy(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index)))
1421     }
1422
1423     fn encode_lang_items(&mut self) -> Lazy<[(DefIndex, usize)]> {
1424         let tcx = self.tcx;
1425         let lang_items = tcx.lang_items();
1426         let lang_items = lang_items.items().iter();
1427         self.lazy(lang_items.enumerate().filter_map(|(i, &opt_def_id)| {
1428             if let Some(def_id) = opt_def_id {
1429                 if def_id.is_local() {
1430                     return Some((def_id.index, i));
1431                 }
1432             }
1433             None
1434         }))
1435     }
1436
1437     fn encode_lang_items_missing(&mut self) -> Lazy<[lang_items::LangItem]> {
1438         let tcx = self.tcx;
1439         self.lazy(&tcx.lang_items().missing)
1440     }
1441
1442     /// Encodes an index, mapping each trait to its (local) implementations.
1443     fn encode_impls(&mut self) -> Lazy<[TraitImpls]> {
1444         debug!("EncodeContext::encode_impls()");
1445         let tcx = self.tcx;
1446         let mut visitor = ImplVisitor { tcx, impls: FxHashMap::default() };
1447         tcx.hir().krate().visit_all_item_likes(&mut visitor);
1448
1449         let mut all_impls: Vec<_> = visitor.impls.into_iter().collect();
1450
1451         // Bring everything into deterministic order for hashing
1452         all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id));
1453
1454         let all_impls: Vec<_> = all_impls
1455             .into_iter()
1456             .map(|(trait_def_id, mut impls)| {
1457                 // Bring everything into deterministic order for hashing
1458                 impls.sort_by_cached_key(|&index| {
1459                     tcx.hir().definitions().def_path_hash(LocalDefId { local_def_index: index })
1460                 });
1461
1462                 TraitImpls {
1463                     trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index),
1464                     impls: self.lazy(&impls),
1465                 }
1466             })
1467             .collect();
1468
1469         self.lazy(&all_impls)
1470     }
1471
1472     // Encodes all symbols exported from this crate into the metadata.
1473     //
1474     // This pass is seeded off the reachability list calculated in the
1475     // middle::reachable module but filters out items that either don't have a
1476     // symbol associated with them (they weren't translated) or if they're an FFI
1477     // definition (as that's not defined in this crate).
1478     fn encode_exported_symbols(
1479         &mut self,
1480         exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportLevel)],
1481     ) -> Lazy<[(ExportedSymbol<'tcx>, SymbolExportLevel)]> {
1482         // The metadata symbol name is special. It should not show up in
1483         // downstream crates.
1484         let metadata_symbol_name = SymbolName::new(&metadata_symbol_name(self.tcx));
1485
1486         self.lazy(
1487             exported_symbols
1488                 .iter()
1489                 .filter(|&&(ref exported_symbol, _)| match *exported_symbol {
1490                     ExportedSymbol::NoDefId(symbol_name) => symbol_name != metadata_symbol_name,
1491                     _ => true,
1492                 })
1493                 .cloned(),
1494         )
1495     }
1496
1497     fn encode_dylib_dependency_formats(&mut self) -> Lazy<[Option<LinkagePreference>]> {
1498         let formats = self.tcx.dependency_formats(LOCAL_CRATE);
1499         for (ty, arr) in formats.iter() {
1500             if *ty != config::CrateType::Dylib {
1501                 continue;
1502             }
1503             return self.lazy(arr.iter().map(|slot| match *slot {
1504                 Linkage::NotLinked | Linkage::IncludedFromDylib => None,
1505
1506                 Linkage::Dynamic => Some(LinkagePreference::RequireDynamic),
1507                 Linkage::Static => Some(LinkagePreference::RequireStatic),
1508             }));
1509         }
1510         Lazy::empty()
1511     }
1512
1513     fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) {
1514         let tcx = self.tcx;
1515
1516         debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id);
1517
1518         record!(self.tables.kind[def_id] <- match nitem.kind {
1519             hir::ForeignItemKind::Fn(_, ref names, _) => {
1520                 let data = FnData {
1521                     asyncness: hir::IsAsync::NotAsync,
1522                     constness: if self.tcx.is_const_fn_raw(def_id) {
1523                         hir::Constness::Const
1524                     } else {
1525                         hir::Constness::NotConst
1526                     },
1527                     param_names: self.encode_fn_param_names(names),
1528                 };
1529                 EntryKind::ForeignFn(self.lazy(data))
1530             }
1531             hir::ForeignItemKind::Static(_, hir::Mutability::Mut) => EntryKind::ForeignMutStatic,
1532             hir::ForeignItemKind::Static(_, hir::Mutability::Not) => EntryKind::ForeignImmStatic,
1533             hir::ForeignItemKind::Type => EntryKind::ForeignType,
1534         });
1535         record!(self.tables.visibility[def_id] <-
1536             ty::Visibility::from_hir(&nitem.vis, nitem.hir_id, self.tcx));
1537         record!(self.tables.span[def_id] <- nitem.span);
1538         record!(self.tables.attributes[def_id] <- nitem.attrs);
1539         self.encode_ident_span(def_id, nitem.ident);
1540         self.encode_stability(def_id);
1541         self.encode_const_stability(def_id);
1542         self.encode_deprecation(def_id);
1543         self.encode_item_type(def_id);
1544         if let hir::ForeignItemKind::Fn(..) = nitem.kind {
1545             record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id));
1546             self.encode_variances_of(def_id);
1547         }
1548         self.encode_generics(def_id);
1549         self.encode_explicit_predicates(def_id);
1550         self.encode_inferred_outlives(def_id);
1551     }
1552 }
1553
1554 // FIXME(eddyb) make metadata encoding walk over all definitions, instead of HIR.
1555 impl Visitor<'tcx> for EncodeContext<'tcx> {
1556     type Map = Map<'tcx>;
1557
1558     fn nested_visit_map(&mut self) -> NestedVisitorMap<Self::Map> {
1559         NestedVisitorMap::OnlyBodies(self.tcx.hir())
1560     }
1561     fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) {
1562         intravisit::walk_expr(self, ex);
1563         self.encode_info_for_expr(ex);
1564     }
1565     fn visit_anon_const(&mut self, c: &'tcx AnonConst) {
1566         intravisit::walk_anon_const(self, c);
1567         let def_id = self.tcx.hir().local_def_id(c.hir_id);
1568         self.encode_info_for_anon_const(def_id);
1569     }
1570     fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) {
1571         intravisit::walk_item(self, item);
1572         let def_id = self.tcx.hir().local_def_id(item.hir_id);
1573         match item.kind {
1574             hir::ItemKind::ExternCrate(_) | hir::ItemKind::Use(..) => {} // ignore these
1575             _ => self.encode_info_for_item(def_id, item),
1576         }
1577         self.encode_addl_info_for_item(item);
1578     }
1579     fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem<'tcx>) {
1580         intravisit::walk_foreign_item(self, ni);
1581         let def_id = self.tcx.hir().local_def_id(ni.hir_id);
1582         self.encode_info_for_foreign_item(def_id, ni);
1583     }
1584     fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) {
1585         intravisit::walk_generics(self, generics);
1586         self.encode_info_for_generics(generics);
1587     }
1588     fn visit_macro_def(&mut self, macro_def: &'tcx hir::MacroDef<'tcx>) {
1589         self.encode_info_for_macro_def(macro_def);
1590     }
1591 }
1592
1593 impl EncodeContext<'tcx> {
1594     fn encode_fields(&mut self, adt_def_id: DefId) {
1595         let def = self.tcx.adt_def(adt_def_id);
1596         for (variant_index, variant) in def.variants.iter_enumerated() {
1597             for (field_index, _field) in variant.fields.iter().enumerate() {
1598                 // FIXME(eddyb) `adt_def_id` is leftover from incremental isolation,
1599                 // pass `def`, `variant` or `field` instead.
1600                 self.encode_field(adt_def_id, variant_index, field_index);
1601             }
1602         }
1603     }
1604
1605     fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) {
1606         for param in generics.params {
1607             let def_id = self.tcx.hir().local_def_id(param.hir_id);
1608             match param.kind {
1609                 GenericParamKind::Lifetime { .. } => continue,
1610                 GenericParamKind::Type { ref default, .. } => {
1611                     self.encode_info_for_generic_param(
1612                         def_id,
1613                         EntryKind::TypeParam,
1614                         default.is_some(),
1615                     );
1616                 }
1617                 GenericParamKind::Const { .. } => {
1618                     self.encode_info_for_generic_param(def_id, EntryKind::ConstParam, true);
1619                 }
1620             }
1621         }
1622     }
1623
1624     fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) {
1625         if let hir::ExprKind::Closure(..) = expr.kind {
1626             let def_id = self.tcx.hir().local_def_id(expr.hir_id);
1627             self.encode_info_for_closure(def_id);
1628         }
1629     }
1630
1631     fn encode_ident_span(&mut self, def_id: DefId, ident: Ident) {
1632         record!(self.tables.ident_span[def_id] <- ident.span);
1633     }
1634
1635     /// In some cases, along with the item itself, we also
1636     /// encode some sub-items. Usually we want some info from the item
1637     /// so it's easier to do that here then to wait until we would encounter
1638     /// normally in the visitor walk.
1639     fn encode_addl_info_for_item(&mut self, item: &hir::Item<'_>) {
1640         let def_id = self.tcx.hir().local_def_id(item.hir_id);
1641         match item.kind {
1642             hir::ItemKind::Static(..)
1643             | hir::ItemKind::Const(..)
1644             | hir::ItemKind::Fn(..)
1645             | hir::ItemKind::Mod(..)
1646             | hir::ItemKind::ForeignMod(..)
1647             | hir::ItemKind::GlobalAsm(..)
1648             | hir::ItemKind::ExternCrate(..)
1649             | hir::ItemKind::Use(..)
1650             | hir::ItemKind::TyAlias(..)
1651             | hir::ItemKind::OpaqueTy(..)
1652             | hir::ItemKind::TraitAlias(..) => {
1653                 // no sub-item recording needed in these cases
1654             }
1655             hir::ItemKind::Enum(..) => {
1656                 self.encode_fields(def_id);
1657
1658                 let def = self.tcx.adt_def(def_id);
1659                 for (i, variant) in def.variants.iter_enumerated() {
1660                     // FIXME(eddyb) `def_id` is leftover from incremental isolation,
1661                     // pass `def` or `variant` instead.
1662                     self.encode_enum_variant_info(def_id, i);
1663
1664                     // FIXME(eddyb) `def_id` is leftover from incremental isolation,
1665                     // pass `def`, `variant` or `ctor_def_id` instead.
1666                     if let Some(_ctor_def_id) = variant.ctor_def_id {
1667                         self.encode_enum_variant_ctor(def_id, i);
1668                     }
1669                 }
1670             }
1671             hir::ItemKind::Struct(ref struct_def, _) => {
1672                 self.encode_fields(def_id);
1673
1674                 // If the struct has a constructor, encode it.
1675                 if let Some(ctor_hir_id) = struct_def.ctor_hir_id() {
1676                     let ctor_def_id = self.tcx.hir().local_def_id(ctor_hir_id);
1677                     self.encode_struct_ctor(def_id, ctor_def_id);
1678                 }
1679             }
1680             hir::ItemKind::Union(..) => {
1681                 self.encode_fields(def_id);
1682             }
1683             hir::ItemKind::Impl { .. } => {
1684                 for &trait_item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
1685                     self.encode_info_for_impl_item(trait_item_def_id);
1686                 }
1687             }
1688             hir::ItemKind::Trait(..) => {
1689                 for &item_def_id in self.tcx.associated_item_def_ids(def_id).iter() {
1690                     self.encode_info_for_trait_item(item_def_id);
1691                 }
1692             }
1693         }
1694     }
1695 }
1696
1697 struct ImplVisitor<'tcx> {
1698     tcx: TyCtxt<'tcx>,
1699     impls: FxHashMap<DefId, Vec<DefIndex>>,
1700 }
1701
1702 impl<'tcx, 'v> ItemLikeVisitor<'v> for ImplVisitor<'tcx> {
1703     fn visit_item(&mut self, item: &hir::Item<'_>) {
1704         if let hir::ItemKind::Impl { .. } = item.kind {
1705             let impl_id = self.tcx.hir().local_def_id(item.hir_id);
1706             if let Some(trait_ref) = self.tcx.impl_trait_ref(impl_id) {
1707                 self.impls.entry(trait_ref.def_id).or_default().push(impl_id.index);
1708             }
1709         }
1710     }
1711
1712     fn visit_trait_item(&mut self, _trait_item: &'v hir::TraitItem<'v>) {}
1713
1714     fn visit_impl_item(&mut self, _impl_item: &'v hir::ImplItem<'v>) {
1715         // handled in `visit_item` above
1716     }
1717 }
1718
1719 /// Used to prefetch queries which will be needed later by metadata encoding.
1720 /// Only a subset of the queries are actually prefetched to keep this code smaller.
1721 struct PrefetchVisitor<'tcx> {
1722     tcx: TyCtxt<'tcx>,
1723     mir_keys: &'tcx DefIdSet,
1724 }
1725
1726 impl<'tcx> PrefetchVisitor<'tcx> {
1727     fn prefetch_mir(&self, def_id: DefId) {
1728         if self.mir_keys.contains(&def_id) {
1729             self.tcx.optimized_mir(def_id);
1730             self.tcx.promoted_mir(def_id);
1731         }
1732     }
1733 }
1734
1735 impl<'tcx, 'v> ParItemLikeVisitor<'v> for PrefetchVisitor<'tcx> {
1736     fn visit_item(&self, item: &hir::Item<'_>) {
1737         // This should be kept in sync with `encode_info_for_item`.
1738         let tcx = self.tcx;
1739         match item.kind {
1740             hir::ItemKind::Static(..) | hir::ItemKind::Const(..) => {
1741                 self.prefetch_mir(tcx.hir().local_def_id(item.hir_id))
1742             }
1743             hir::ItemKind::Fn(ref sig, ..) => {
1744                 let def_id = tcx.hir().local_def_id(item.hir_id);
1745                 let generics = tcx.generics_of(def_id);
1746                 let needs_inline = generics.requires_monomorphization(tcx)
1747                     || tcx.codegen_fn_attrs(def_id).requests_inline();
1748                 if needs_inline || sig.header.constness == hir::Constness::Const {
1749                     self.prefetch_mir(def_id)
1750                 }
1751             }
1752             _ => (),
1753         }
1754     }
1755
1756     fn visit_trait_item(&self, trait_item: &'v hir::TraitItem<'v>) {
1757         // This should be kept in sync with `encode_info_for_trait_item`.
1758         self.prefetch_mir(self.tcx.hir().local_def_id(trait_item.hir_id));
1759     }
1760
1761     fn visit_impl_item(&self, impl_item: &'v hir::ImplItem<'v>) {
1762         // This should be kept in sync with `encode_info_for_impl_item`.
1763         let tcx = self.tcx;
1764         match impl_item.kind {
1765             hir::ImplItemKind::Const(..) => {
1766                 self.prefetch_mir(tcx.hir().local_def_id(impl_item.hir_id))
1767             }
1768             hir::ImplItemKind::Fn(ref sig, _) => {
1769                 let def_id = tcx.hir().local_def_id(impl_item.hir_id);
1770                 let generics = tcx.generics_of(def_id);
1771                 let needs_inline = generics.requires_monomorphization(tcx)
1772                     || tcx.codegen_fn_attrs(def_id).requests_inline();
1773                 let is_const_fn = sig.header.constness == hir::Constness::Const;
1774                 if needs_inline || is_const_fn {
1775                     self.prefetch_mir(def_id)
1776                 }
1777             }
1778             hir::ImplItemKind::OpaqueTy(..) | hir::ImplItemKind::TyAlias(..) => (),
1779         }
1780     }
1781 }
1782
1783 // NOTE(eddyb) The following comment was preserved for posterity, even
1784 // though it's no longer relevant as EBML (which uses nested & tagged
1785 // "documents") was replaced with a scheme that can't go out of bounds.
1786 //
1787 // And here we run into yet another obscure archive bug: in which metadata
1788 // loaded from archives may have trailing garbage bytes. Awhile back one of
1789 // our tests was failing sporadically on the macOS 64-bit builders (both nopt
1790 // and opt) by having ebml generate an out-of-bounds panic when looking at
1791 // metadata.
1792 //
1793 // Upon investigation it turned out that the metadata file inside of an rlib
1794 // (and ar archive) was being corrupted. Some compilations would generate a
1795 // metadata file which would end in a few extra bytes, while other
1796 // compilations would not have these extra bytes appended to the end. These
1797 // extra bytes were interpreted by ebml as an extra tag, so they ended up
1798 // being interpreted causing the out-of-bounds.
1799 //
1800 // The root cause of why these extra bytes were appearing was never
1801 // discovered, and in the meantime the solution we're employing is to insert
1802 // the length of the metadata to the start of the metadata. Later on this
1803 // will allow us to slice the metadata to the precise length that we just
1804 // generated regardless of trailing bytes that end up in it.
1805
1806 pub(super) fn encode_metadata(tcx: TyCtxt<'_>) -> EncodedMetadata {
1807     // Since encoding metadata is not in a query, and nothing is cached,
1808     // there's no need to do dep-graph tracking for any of it.
1809     tcx.dep_graph.assert_ignored();
1810
1811     join(
1812         || encode_metadata_impl(tcx),
1813         || {
1814             if tcx.sess.threads() == 1 {
1815                 return;
1816             }
1817             // Prefetch some queries used by metadata encoding.
1818             // This is not necessary for correctness, but is only done for performance reasons.
1819             // It can be removed if it turns out to cause trouble or be detrimental to performance.
1820             join(
1821                 || {
1822                     if !tcx.sess.opts.output_types.should_codegen() {
1823                         // We won't emit MIR, so don't prefetch it.
1824                         return;
1825                     }
1826                     tcx.hir().krate().par_visit_all_item_likes(&PrefetchVisitor {
1827                         tcx,
1828                         mir_keys: tcx.mir_keys(LOCAL_CRATE),
1829                     });
1830                 },
1831                 || tcx.exported_symbols(LOCAL_CRATE),
1832             );
1833         },
1834     )
1835     .0
1836 }
1837
1838 fn encode_metadata_impl(tcx: TyCtxt<'_>) -> EncodedMetadata {
1839     let mut encoder = opaque::Encoder::new(vec![]);
1840     encoder.emit_raw_bytes(METADATA_HEADER);
1841
1842     // Will be filled with the root position after encoding everything.
1843     encoder.emit_raw_bytes(&[0, 0, 0, 0]);
1844
1845     let mut ecx = EncodeContext {
1846         opaque: encoder,
1847         tcx,
1848         tables: Default::default(),
1849         lazy_state: LazyState::NoNode,
1850         type_shorthands: Default::default(),
1851         predicate_shorthands: Default::default(),
1852         source_file_cache: tcx.sess.source_map().files()[0].clone(),
1853         interpret_allocs: Default::default(),
1854         interpret_allocs_inverse: Default::default(),
1855     };
1856
1857     // Encode the rustc version string in a predictable location.
1858     rustc_version().encode(&mut ecx).unwrap();
1859
1860     // Encode all the entries and extra information in the crate,
1861     // culminating in the `CrateRoot` which points to all of it.
1862     let root = ecx.encode_crate_root();
1863
1864     let mut result = ecx.opaque.into_inner();
1865
1866     // Encode the root position.
1867     let header = METADATA_HEADER.len();
1868     let pos = root.position.get();
1869     result[header + 0] = (pos >> 24) as u8;
1870     result[header + 1] = (pos >> 16) as u8;
1871     result[header + 2] = (pos >> 8) as u8;
1872     result[header + 3] = (pos >> 0) as u8;
1873
1874     EncodedMetadata { raw_data: result }
1875 }