]>
Commit | Line | Data |
---|---|---|
f2b60f7d | 1 | use crate::errors::{FailCreateFileEncoder, FailSeekFile, FailWriteFile}; |
c295e0f8 | 2 | use crate::rmeta::def_path_hash_map::DefPathHashMapRef; |
923072b8 | 3 | use crate::rmeta::table::TableBuilder; |
dfeec247 | 4 | use crate::rmeta::*; |
92a42be0 | 5 | |
2b03887a | 6 | use rustc_ast::Attribute; |
5e7ed085 | 7 | use rustc_data_structures::fingerprint::Fingerprint; |
5869c6ff | 8 | use rustc_data_structures::fx::{FxHashMap, FxIndexSet}; |
064997fb | 9 | use rustc_data_structures::memmap::{Mmap, MmapMut}; |
49aad941 FG |
10 | use rustc_data_structures::stable_hasher::{Hash128, HashStable, StableHasher}; |
11 | use rustc_data_structures::sync::{join, par_for_each_in, Lrc}; | |
064997fb | 12 | use rustc_data_structures::temp_dir::MaybeTempDir; |
ba9703b0 | 13 | use rustc_hir as hir; |
5e7ed085 | 14 | use rustc_hir::def::DefKind; |
6a06907d XL |
15 | use rustc_hir::def_id::{ |
16 | CrateNum, DefId, DefIndex, LocalDefId, CRATE_DEF_ID, CRATE_DEF_INDEX, LOCAL_CRATE, | |
17 | }; | |
fc512014 | 18 | use rustc_hir::definitions::DefPathData; |
5099ac24 | 19 | use rustc_hir::intravisit::{self, Visitor}; |
487cf647 | 20 | use rustc_hir::lang_items::LangItem; |
5099ac24 | 21 | use rustc_middle::hir::nested_filter; |
49aad941 | 22 | use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile; |
ba9703b0 XL |
23 | use rustc_middle::middle::dependency_format::Linkage; |
24 | use rustc_middle::middle::exported_symbols::{ | |
04454e1e | 25 | metadata_symbol_name, ExportedSymbol, SymbolExportInfo, |
ba9703b0 | 26 | }; |
3dfed10e | 27 | use rustc_middle::mir::interpret; |
353b0b11 | 28 | use rustc_middle::query::LocalCrate; |
49aad941 | 29 | use rustc_middle::query::Providers; |
ba9703b0 | 30 | use rustc_middle::traits::specialization_graph; |
3dfed10e | 31 | use rustc_middle::ty::codec::TyEncoder; |
5e7ed085 | 32 | use rustc_middle::ty::fast_reject::{self, SimplifiedType, TreatParams}; |
ba9703b0 | 33 | use rustc_middle::ty::{self, SymbolName, Ty, TyCtxt}; |
2b03887a | 34 | use rustc_middle::util::common::to_readable_str; |
064997fb | 35 | use rustc_serialize::{opaque, Decodable, Decoder, Encodable, Encoder}; |
2b03887a | 36 | use rustc_session::config::{CrateType, OptLevel}; |
c295e0f8 | 37 | use rustc_session::cstore::{ForeignModule, LinkagePreference, NativeLib}; |
923072b8 FG |
38 | use rustc_span::hygiene::{ExpnIndex, HygieneEncodeContext, MacroKind}; |
39 | use rustc_span::symbol::{sym, Symbol}; | |
49aad941 | 40 | use rustc_span::{self, ExternalSource, FileName, SourceFile, Span, SyntaxContext}; |
923072b8 | 41 | use std::borrow::Borrow; |
f2b60f7d | 42 | use std::collections::hash_map::Entry; |
ff7c6d11 | 43 | use std::hash::Hash; |
064997fb | 44 | use std::io::{Read, Seek, Write}; |
e74abb32 | 45 | use std::num::NonZeroUsize; |
064997fb | 46 | use std::path::{Path, PathBuf}; |
9e0c209e | 47 | |
3dfed10e | 48 | pub(super) struct EncodeContext<'a, 'tcx> { |
064997fb | 49 | opaque: opaque::FileEncoder, |
e74abb32 | 50 | tcx: TyCtxt<'tcx>, |
1b1a35ee | 51 | feat: &'tcx rustc_feature::Features, |
dc9dc135 | 52 | |
923072b8 | 53 | tables: TableBuilders, |
9e0c209e SL |
54 | |
55 | lazy_state: LazyState, | |
476ff2be | 56 | type_shorthands: FxHashMap<Ty<'tcx>, usize>, |
5869c6ff | 57 | predicate_shorthands: FxHashMap<ty::PredicateKind<'tcx>, usize>, |
2c00a5a8 | 58 | |
3dfed10e | 59 | interpret_allocs: FxIndexSet<interpret::AllocId>, |
0531ce1d | 60 | |
2c00a5a8 | 61 | // This is used to speed up Span encoding. |
f035d41b XL |
62 | // The `usize` is an index into the `MonotonicVec` |
63 | // that stores the `SourceFile` | |
64 | source_file_cache: (Lrc<SourceFile>, usize), | |
65 | // The indices (into the `SourceMap`'s `MonotonicVec`) | |
66 | // of all of the `SourceFiles` that we need to serialize. | |
67 | // When we serialize a `Span`, we insert the index of its | |
f2b60f7d FG |
68 | // `SourceFile` into the `FxIndexSet`. |
69 | // The order inside the `FxIndexSet` is used as on-disk | |
70 | // order of `SourceFiles`, and encoded inside `Span`s. | |
71 | required_source_files: Option<FxIndexSet<usize>>, | |
f035d41b | 72 | is_proc_macro: bool, |
3dfed10e | 73 | hygiene_ctxt: &'a HygieneEncodeContext, |
f2b60f7d | 74 | symbol_table: FxHashMap<Symbol, usize>, |
9e0c209e SL |
75 | } |
76 | ||
9ffffee4 | 77 | /// If the current crate is a proc-macro, returns early with `LazyArray::default()`. |
1b1a35ee XL |
78 | /// This is useful for skipping the encoding of things that aren't needed |
79 | /// for proc-macro crates. | |
80 | macro_rules! empty_proc_macro { | |
81 | ($self:ident) => { | |
82 | if $self.is_proc_macro { | |
9ffffee4 | 83 | return LazyArray::default(); |
1b1a35ee XL |
84 | } |
85 | }; | |
86 | } | |
87 | ||
9e0c209e SL |
88 | macro_rules! encoder_methods { |
89 | ($($name:ident($ty:ty);)*) => { | |
923072b8 | 90 | $(fn $name(&mut self, value: $ty) { |
9e0c209e SL |
91 | self.opaque.$name(value) |
92 | })* | |
b039eaaf SL |
93 | } |
94 | } | |
95 | ||
3dfed10e | 96 | impl<'a, 'tcx> Encoder for EncodeContext<'a, 'tcx> { |
9e0c209e SL |
97 | encoder_methods! { |
98 | emit_usize(usize); | |
32a655c1 | 99 | emit_u128(u128); |
9e0c209e SL |
100 | emit_u64(u64); |
101 | emit_u32(u32); | |
102 | emit_u16(u16); | |
103 | emit_u8(u8); | |
104 | ||
105 | emit_isize(isize); | |
32a655c1 | 106 | emit_i128(i128); |
9e0c209e SL |
107 | emit_i64(i64); |
108 | emit_i32(i32); | |
109 | emit_i16(i16); | |
9e0c209e | 110 | |
cdc7bbd5 | 111 | emit_raw_bytes(&[u8]); |
b039eaaf SL |
112 | } |
113 | } | |
114 | ||
923072b8 FG |
115 | impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyValue<T> { |
116 | fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { | |
117 | e.emit_lazy_distance(self.position); | |
9e0c209e | 118 | } |
223e47cc LB |
119 | } |
120 | ||
923072b8 FG |
121 | impl<'a, 'tcx, T> Encodable<EncodeContext<'a, 'tcx>> for LazyArray<T> { |
122 | fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { | |
123 | e.emit_usize(self.num_elems); | |
124 | if self.num_elems > 0 { | |
125 | e.emit_lazy_distance(self.position) | |
9e0c209e | 126 | } |
9e0c209e | 127 | } |
223e47cc LB |
128 | } |
129 | ||
923072b8 FG |
130 | impl<'a, 'tcx, I, T> Encodable<EncodeContext<'a, 'tcx>> for LazyTable<I, T> { |
131 | fn encode(&self, e: &mut EncodeContext<'a, 'tcx>) { | |
132 | e.emit_usize(self.encoded_size); | |
133 | e.emit_lazy_distance(self.position); | |
e74abb32 XL |
134 | } |
135 | } | |
136 | ||
1b1a35ee | 137 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for CrateNum { |
923072b8 | 138 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { |
1b1a35ee | 139 | if *self != LOCAL_CRATE && s.is_proc_macro { |
9c376795 | 140 | panic!("Attempted to encode non-local CrateNum {self:?} for proc-macro crate"); |
1b1a35ee | 141 | } |
923072b8 | 142 | s.emit_u32(self.as_u32()); |
1b1a35ee XL |
143 | } |
144 | } | |
145 | ||
3dfed10e | 146 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for DefIndex { |
923072b8 FG |
147 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { |
148 | s.emit_u32(self.as_u32()); | |
abe05a73 XL |
149 | } |
150 | } | |
151 | ||
136023e0 | 152 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnIndex { |
923072b8 FG |
153 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { |
154 | s.emit_u32(self.as_u32()); | |
136023e0 XL |
155 | } |
156 | } | |
157 | ||
3dfed10e | 158 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for SyntaxContext { |
923072b8 FG |
159 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { |
160 | rustc_span::hygiene::raw_encode_syntax_context(*self, &s.hygiene_ctxt, s); | |
abe05a73 XL |
161 | } |
162 | } | |
163 | ||
3dfed10e | 164 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for ExpnId { |
923072b8 | 165 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { |
136023e0 | 166 | if self.krate == LOCAL_CRATE { |
9c376795 | 167 | // We will only write details for local expansions. Non-local expansions will fetch |
136023e0 XL |
168 | // data from the corresponding crate's metadata. |
169 | // FIXME(#43047) FIXME(#74731) We may eventually want to avoid relying on external | |
170 | // metadata from proc-macro crates. | |
171 | s.hygiene_ctxt.schedule_expn_data_for_encoding(*self); | |
172 | } | |
923072b8 FG |
173 | self.krate.encode(s); |
174 | self.local_id.encode(s); | |
2c00a5a8 XL |
175 | } |
176 | } | |
177 | ||
3dfed10e | 178 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Span { |
923072b8 | 179 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { |
cdc7bbd5 XL |
180 | let span = self.data(); |
181 | ||
182 | // Don't serialize any `SyntaxContext`s from a proc-macro crate, | |
183 | // since we don't load proc-macro dependencies during serialization. | |
184 | // This means that any hygiene information from macros used *within* | |
185 | // a proc-macro crate (e.g. invoking a macro that expands to a proc-macro | |
186 | // definition) will be lost. | |
187 | // | |
188 | // This can show up in two ways: | |
189 | // | |
190 | // 1. Any hygiene information associated with identifier of | |
191 | // a proc macro (e.g. `#[proc_macro] pub fn $name`) will be lost. | |
192 | // Since proc-macros can only be invoked from a different crate, | |
193 | // real code should never need to care about this. | |
194 | // | |
195 | // 2. Using `Span::def_site` or `Span::mixed_site` will not | |
196 | // include any hygiene information associated with the definition | |
197 | // site. This means that a proc-macro cannot emit a `$crate` | |
198 | // identifier which resolves to one of its dependencies, | |
199 | // which also should never come up in practice. | |
200 | // | |
201 | // Additionally, this affects `Span::parent`, and any other | |
202 | // span inspection APIs that would otherwise allow traversing | |
203 | // the `SyntaxContexts` associated with a span. | |
204 | // | |
205 | // None of these user-visible effects should result in any | |
206 | // cross-crate inconsistencies (getting one behavior in the same | |
207 | // crate, and a different behavior in another crate) due to the | |
208 | // limited surface that proc-macros can expose. | |
209 | // | |
210 | // IMPORTANT: If this is ever changed, be sure to update | |
211 | // `rustc_span::hygiene::raw_encode_expn_id` to handle | |
212 | // encoding `ExpnData` for proc-macro crates. | |
213 | if s.is_proc_macro { | |
923072b8 | 214 | SyntaxContext::root().encode(s); |
cdc7bbd5 | 215 | } else { |
923072b8 | 216 | span.ctxt.encode(s); |
2c00a5a8 XL |
217 | } |
218 | ||
cdc7bbd5 XL |
219 | if self.is_dummy() { |
220 | return TAG_PARTIAL_SPAN.encode(s); | |
221 | } | |
2c00a5a8 XL |
222 | |
223 | // The Span infrastructure should make sure that this invariant holds: | |
224 | debug_assert!(span.lo <= span.hi); | |
225 | ||
3dfed10e XL |
226 | if !s.source_file_cache.0.contains(span.lo) { |
227 | let source_map = s.tcx.sess.source_map(); | |
b7449926 | 228 | let source_file_index = source_map.lookup_source_file_idx(span.lo); |
3dfed10e | 229 | s.source_file_cache = |
f035d41b | 230 | (source_map.files()[source_file_index].clone(), source_file_index); |
2c00a5a8 | 231 | } |
f2b60f7d FG |
232 | let (ref source_file, source_file_index) = s.source_file_cache; |
233 | debug_assert!(source_file.contains(span.lo)); | |
2c00a5a8 | 234 | |
f2b60f7d | 235 | if !source_file.contains(span.hi) { |
2c00a5a8 XL |
236 | // Unfortunately, macro expansion still sometimes generates Spans |
237 | // that malformed in this way. | |
cdc7bbd5 | 238 | return TAG_PARTIAL_SPAN.encode(s); |
2c00a5a8 XL |
239 | } |
240 | ||
ba9703b0 XL |
241 | // There are two possible cases here: |
242 | // 1. This span comes from a 'foreign' crate - e.g. some crate upstream of the | |
243 | // crate we are writing metadata for. When the metadata for *this* crate gets | |
244 | // deserialized, the deserializer will need to know which crate it originally came | |
245 | // from. We use `TAG_VALID_SPAN_FOREIGN` to indicate that a `CrateNum` should | |
246 | // be deserialized after the rest of the span data, which tells the deserializer | |
247 | // which crate contains the source map information. | |
5e7ed085 | 248 | // 2. This span comes from our own crate. No special handling is needed - we just |
ba9703b0 XL |
249 | // write `TAG_VALID_SPAN_LOCAL` to let the deserializer know that it should use |
250 | // our own source map information. | |
f035d41b XL |
251 | // |
252 | // If we're a proc-macro crate, we always treat this as a local `Span`. | |
253 | // In `encode_source_map`, we serialize foreign `SourceFile`s into our metadata | |
254 | // if we're a proc-macro crate. | |
255 | // This allows us to avoid loading the dependencies of proc-macro crates: all of | |
256 | // the information we need to decode `Span`s is stored in the proc-macro crate. | |
f2b60f7d | 257 | let (tag, metadata_index) = if source_file.is_imported() && !s.is_proc_macro { |
2b03887a FG |
258 | // To simplify deserialization, we 'rebase' this span onto the crate it originally came |
259 | // from (the crate that 'owns' the file it references. These rebased 'lo' and 'hi' | |
260 | // values are relative to the source map information for the 'foreign' crate whose | |
261 | // CrateNum we write into the metadata. This allows `imported_source_files` to binary | |
ba9703b0 XL |
262 | // search through the 'foreign' crate's source map information, using the |
263 | // deserialized 'lo' and 'hi' values directly. | |
264 | // | |
265 | // All of this logic ensures that the final result of deserialization is a 'normal' | |
266 | // Span that can be used without any additional trouble. | |
f2b60f7d | 267 | let metadata_index = { |
ba9703b0 | 268 | // Introduce a new scope so that we drop the 'lock()' temporary |
f2b60f7d FG |
269 | match &*source_file.external_src.lock() { |
270 | ExternalSource::Foreign { metadata_index, .. } => *metadata_index, | |
9c376795 | 271 | src => panic!("Unexpected external source {src:?}"), |
ba9703b0 XL |
272 | } |
273 | }; | |
e74abb32 | 274 | |
f2b60f7d | 275 | (TAG_VALID_SPAN_FOREIGN, metadata_index) |
ba9703b0 | 276 | } else { |
f2b60f7d FG |
277 | // Record the fact that we need to encode the data for this `SourceFile` |
278 | let source_files = | |
279 | s.required_source_files.as_mut().expect("Already encoded SourceMap!"); | |
280 | let (metadata_index, _) = source_files.insert_full(source_file_index); | |
281 | let metadata_index: u32 = | |
282 | metadata_index.try_into().expect("cannot export more than U32_MAX files"); | |
283 | ||
284 | (TAG_VALID_SPAN_LOCAL, metadata_index) | |
ba9703b0 XL |
285 | }; |
286 | ||
f2b60f7d FG |
287 | // Encode the start position relative to the file start, so we profit more from the |
288 | // variable-length integer encoding. | |
289 | let lo = span.lo - source_file.start_pos; | |
2c00a5a8 XL |
290 | |
291 | // Encode length which is usually less than span.hi and profits more | |
292 | // from the variable-length integer encoding that we use. | |
f2b60f7d FG |
293 | let len = span.hi - span.lo; |
294 | ||
295 | tag.encode(s); | |
296 | lo.encode(s); | |
923072b8 | 297 | len.encode(s); |
3dfed10e | 298 | |
f2b60f7d FG |
299 | // Encode the index of the `SourceFile` for the span, in order to make decoding faster. |
300 | metadata_index.encode(s); | |
301 | ||
ba9703b0 | 302 | if tag == TAG_VALID_SPAN_FOREIGN { |
3dfed10e XL |
303 | // This needs to be two lines to avoid holding the `s.source_file_cache` |
304 | // while calling `cnum.encode(s)` | |
305 | let cnum = s.source_file_cache.0.cnum; | |
923072b8 | 306 | cnum.encode(s); |
ba9703b0 | 307 | } |
abe05a73 XL |
308 | } |
309 | } | |
310 | ||
f2b60f7d FG |
311 | impl<'a, 'tcx> Encodable<EncodeContext<'a, 'tcx>> for Symbol { |
312 | fn encode(&self, s: &mut EncodeContext<'a, 'tcx>) { | |
313 | // if symbol preinterned, emit tag and symbol index | |
314 | if self.is_preinterned() { | |
315 | s.opaque.emit_u8(SYMBOL_PREINTERNED); | |
316 | s.opaque.emit_u32(self.as_u32()); | |
317 | } else { | |
318 | // otherwise write it as string or as offset to it | |
319 | match s.symbol_table.entry(*self) { | |
320 | Entry::Vacant(o) => { | |
321 | s.opaque.emit_u8(SYMBOL_STR); | |
322 | let pos = s.opaque.position(); | |
323 | o.insert(pos); | |
324 | s.emit_str(self.as_str()); | |
325 | } | |
326 | Entry::Occupied(o) => { | |
9c376795 | 327 | let x = *o.get(); |
f2b60f7d FG |
328 | s.emit_u8(SYMBOL_OFFSET); |
329 | s.emit_usize(x); | |
330 | } | |
331 | } | |
332 | } | |
333 | } | |
334 | } | |
335 | ||
923072b8 | 336 | impl<'a, 'tcx> TyEncoder for EncodeContext<'a, 'tcx> { |
3dfed10e | 337 | const CLEAR_CROSS_CRATE: bool = true; |
d9579d0f | 338 | |
923072b8 FG |
339 | type I = TyCtxt<'tcx>; |
340 | ||
3dfed10e XL |
341 | fn position(&self) -> usize { |
342 | self.opaque.position() | |
f035d41b | 343 | } |
f035d41b | 344 | |
3dfed10e XL |
345 | fn type_shorthands(&mut self) -> &mut FxHashMap<Ty<'tcx>, usize> { |
346 | &mut self.type_shorthands | |
0531ce1d | 347 | } |
0531ce1d | 348 | |
5869c6ff | 349 | fn predicate_shorthands(&mut self) -> &mut FxHashMap<ty::PredicateKind<'tcx>, usize> { |
3dfed10e | 350 | &mut self.predicate_shorthands |
2c00a5a8 | 351 | } |
2c00a5a8 | 352 | |
923072b8 | 353 | fn encode_alloc_id(&mut self, alloc_id: &rustc_middle::mir::interpret::AllocId) { |
3dfed10e XL |
354 | let (index, _) = self.interpret_allocs.insert_full(*alloc_id); |
355 | ||
923072b8 | 356 | index.encode(self); |
ff7c6d11 XL |
357 | } |
358 | } | |
359 | ||
9ffffee4 | 360 | // Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy_value($value))`, which would |
e74abb32 XL |
361 | // normally need extra variables to avoid errors about multiple mutable borrows. |
362 | macro_rules! record { | |
60c5eb7d | 363 | ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ |
e74abb32 XL |
364 | { |
365 | let value = $value; | |
366 | let lazy = $self.lazy(value); | |
9ffffee4 | 367 | $self.$tables.$table.set_some($def_id.index, lazy); |
e74abb32 | 368 | } |
dfeec247 | 369 | }}; |
e74abb32 XL |
370 | } |
371 | ||
9ffffee4 | 372 | // Shorthand for `$self.$tables.$table.set_some($def_id.index, $self.lazy_value($value))`, which would |
923072b8 FG |
373 | // normally need extra variables to avoid errors about multiple mutable borrows. |
374 | macro_rules! record_array { | |
9ffffee4 FG |
375 | ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ |
376 | { | |
377 | let value = $value; | |
378 | let lazy = $self.lazy_array(value); | |
379 | $self.$tables.$table.set_some($def_id.index, lazy); | |
380 | } | |
381 | }}; | |
382 | } | |
383 | ||
384 | macro_rules! record_defaulted_array { | |
923072b8 FG |
385 | ($self:ident.$tables:ident.$table:ident[$def_id:expr] <- $value:expr) => {{ |
386 | { | |
387 | let value = $value; | |
388 | let lazy = $self.lazy_array(value); | |
389 | $self.$tables.$table.set($def_id.index, lazy); | |
390 | } | |
391 | }}; | |
392 | } | |
393 | ||
3dfed10e | 394 | impl<'a, 'tcx> EncodeContext<'a, 'tcx> { |
923072b8 FG |
395 | fn emit_lazy_distance(&mut self, position: NonZeroUsize) { |
396 | let pos = position.get(); | |
9e0c209e | 397 | let distance = match self.lazy_state { |
c30ab7b3 | 398 | LazyState::NoNode => bug!("emit_lazy_distance: outside of a metadata node"), |
9e0c209e | 399 | LazyState::NodeStart(start) => { |
e74abb32 | 400 | let start = start.get(); |
5099ac24 FG |
401 | assert!(pos <= start); |
402 | start - pos | |
9e0c209e | 403 | } |
5099ac24 | 404 | LazyState::Previous(last_pos) => { |
0531ce1d | 405 | assert!( |
923072b8 | 406 | last_pos <= position, |
0531ce1d | 407 | "make sure that the calls to `lazy*` \ |
74b04a01 | 408 | are in the same order as the metadata fields", |
0531ce1d | 409 | ); |
923072b8 | 410 | position.get() - last_pos.get() |
9e0c209e SL |
411 | } |
412 | }; | |
5099ac24 | 413 | self.lazy_state = LazyState::Previous(NonZeroUsize::new(pos).unwrap()); |
923072b8 | 414 | self.emit_usize(distance); |
9e0c209e | 415 | } |
1a4d82fc | 416 | |
923072b8 FG |
417 | fn lazy<T: ParameterizedOverTcx, B: Borrow<T::Value<'tcx>>>(&mut self, value: B) -> LazyValue<T> |
418 | where | |
419 | T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>, | |
420 | { | |
421 | let pos = NonZeroUsize::new(self.position()).unwrap(); | |
422 | ||
423 | assert_eq!(self.lazy_state, LazyState::NoNode); | |
424 | self.lazy_state = LazyState::NodeStart(pos); | |
425 | value.borrow().encode(self); | |
426 | self.lazy_state = LazyState::NoNode; | |
427 | ||
428 | assert!(pos.get() <= self.position()); | |
429 | ||
430 | LazyValue::from_position(pos) | |
431 | } | |
432 | ||
433 | fn lazy_array<T: ParameterizedOverTcx, I: IntoIterator<Item = B>, B: Borrow<T::Value<'tcx>>>( | |
3dfed10e | 434 | &mut self, |
923072b8 FG |
435 | values: I, |
436 | ) -> LazyArray<T> | |
437 | where | |
438 | T::Value<'tcx>: Encodable<EncodeContext<'a, 'tcx>>, | |
439 | { | |
e74abb32 | 440 | let pos = NonZeroUsize::new(self.position()).unwrap(); |
223e47cc | 441 | |
e1599b0c XL |
442 | assert_eq!(self.lazy_state, LazyState::NoNode); |
443 | self.lazy_state = LazyState::NodeStart(pos); | |
923072b8 | 444 | let len = values.into_iter().map(|value| value.borrow().encode(self)).count(); |
e1599b0c | 445 | self.lazy_state = LazyState::NoNode; |
1a4d82fc | 446 | |
5099ac24 | 447 | assert!(pos.get() <= self.position()); |
1a4d82fc | 448 | |
923072b8 | 449 | LazyArray::from_position_and_num_elems(pos, len) |
1a4d82fc | 450 | } |
223e47cc | 451 | |
dc9dc135 | 452 | fn encode_info_for_items(&mut self) { |
353b0b11 | 453 | self.encode_info_for_mod(CRATE_DEF_ID); |
1b1a35ee XL |
454 | |
455 | // Proc-macro crates only export proc-macro items, which are looked | |
456 | // up using `proc_macro_data` | |
457 | if self.is_proc_macro { | |
458 | return; | |
459 | } | |
460 | ||
064997fb | 461 | self.tcx.hir().visit_all_item_likes_in_crate(self); |
7cac9316 XL |
462 | } |
463 | ||
3dfed10e | 464 | fn encode_def_path_table(&mut self) { |
064997fb | 465 | let table = self.tcx.def_path_table(); |
1b1a35ee XL |
466 | if self.is_proc_macro { |
467 | for def_index in std::iter::once(CRATE_DEF_INDEX) | |
94222f64 | 468 | .chain(self.tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index)) |
1b1a35ee XL |
469 | { |
470 | let def_key = self.lazy(table.def_key(def_index)); | |
04454e1e | 471 | let def_path_hash = table.def_path_hash(def_index); |
9ffffee4 | 472 | self.tables.def_keys.set_some(def_index, def_key); |
1b1a35ee XL |
473 | self.tables.def_path_hashes.set(def_index, def_path_hash); |
474 | } | |
475 | } else { | |
476 | for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() { | |
477 | let def_key = self.lazy(def_key); | |
9ffffee4 | 478 | self.tables.def_keys.set_some(def_index, def_key); |
04454e1e | 479 | self.tables.def_path_hashes.set(def_index, *def_path_hash); |
1b1a35ee | 480 | } |
3dfed10e | 481 | } |
7cac9316 XL |
482 | } |
483 | ||
923072b8 | 484 | fn encode_def_path_hash_map(&mut self) -> LazyValue<DefPathHashMapRef<'static>> { |
064997fb | 485 | self.lazy(DefPathHashMapRef::BorrowedFromTcx(self.tcx.def_path_hash_to_def_index_map())) |
c295e0f8 XL |
486 | } |
487 | ||
9ffffee4 | 488 | fn encode_source_map(&mut self) -> LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>> { |
b7449926 XL |
489 | let source_map = self.tcx.sess.source_map(); |
490 | let all_source_files = source_map.files(); | |
7cac9316 | 491 | |
f035d41b XL |
492 | // By replacing the `Option` with `None`, we ensure that we can't |
493 | // accidentally serialize any more `Span`s after the source map encoding | |
494 | // is done. | |
495 | let required_source_files = self.required_source_files.take().unwrap(); | |
7cac9316 | 496 | |
923072b8 FG |
497 | let working_directory = &self.tcx.sess.opts.working_dir; |
498 | ||
f2b60f7d FG |
499 | let mut adapted = TableBuilder::default(); |
500 | ||
501 | // Only serialize `SourceFile`s that were used during the encoding of a `Span`. | |
502 | // | |
503 | // The order in which we encode source files is important here: the on-disk format for | |
504 | // `Span` contains the index of the corresponding `SourceFile`. | |
505 | for (on_disk_index, &source_file_index) in required_source_files.iter().enumerate() { | |
506 | let source_file = &all_source_files[source_file_index]; | |
507 | // Don't serialize imported `SourceFile`s, unless we're in a proc-macro crate. | |
508 | assert!(!source_file.is_imported() || self.is_proc_macro); | |
509 | ||
510 | // At export time we expand all source file paths to absolute paths because | |
511 | // downstream compilation sessions can have a different compiler working | |
512 | // directory, so relative paths from this or any other upstream crate | |
513 | // won't be valid anymore. | |
514 | // | |
515 | // At this point we also erase the actual on-disk path and only keep | |
516 | // the remapped version -- as is necessary for reproducible builds. | |
517 | let mut source_file = match source_file.name { | |
518 | FileName::Real(ref original_file_name) => { | |
519 | let adapted_file_name = source_map | |
520 | .path_mapping() | |
521 | .to_embeddable_absolute_path(original_file_name.clone(), working_directory); | |
522 | ||
523 | if adapted_file_name != *original_file_name { | |
524 | let mut adapted: SourceFile = (**source_file).clone(); | |
525 | adapted.name = FileName::Real(adapted_file_name); | |
526 | adapted.name_hash = { | |
527 | let mut hasher: StableHasher = StableHasher::new(); | |
528 | adapted.name.hash(&mut hasher); | |
49aad941 | 529 | hasher.finish::<Hash128>() |
f2b60f7d FG |
530 | }; |
531 | Lrc::new(adapted) | |
532 | } else { | |
533 | // Nothing to adapt | |
534 | source_file.clone() | |
dfeec247 | 535 | } |
7cac9316 | 536 | } |
f2b60f7d FG |
537 | // expanded code, not from a file |
538 | _ => source_file.clone(), | |
539 | }; | |
540 | ||
541 | // We're serializing this `SourceFile` into our crate metadata, | |
542 | // so mark it as coming from this crate. | |
543 | // This also ensures that we don't try to deserialize the | |
544 | // `CrateNum` for a proc-macro dependency - since proc macro | |
545 | // dependencies aren't loaded when we deserialize a proc-macro, | |
546 | // trying to remap the `CrateNum` would fail. | |
547 | if self.is_proc_macro { | |
548 | Lrc::make_mut(&mut source_file).cnum = LOCAL_CRATE; | |
549 | } | |
550 | ||
551 | let on_disk_index: u32 = | |
552 | on_disk_index.try_into().expect("cannot export more than U32_MAX files"); | |
9ffffee4 | 553 | adapted.set_some(on_disk_index, self.lazy(source_file)); |
f2b60f7d | 554 | } |
7cac9316 | 555 | |
f2b60f7d | 556 | adapted.encode(&mut self.opaque) |
7cac9316 XL |
557 | } |
558 | ||
923072b8 | 559 | fn encode_crate_root(&mut self) -> LazyValue<CrateRoot> { |
5e7ed085 | 560 | let tcx = self.tcx; |
2b03887a FG |
561 | let mut stats: Vec<(&'static str, usize)> = Vec::with_capacity(32); |
562 | ||
563 | macro_rules! stat { | |
564 | ($label:literal, $f:expr) => {{ | |
565 | let orig_pos = self.position(); | |
566 | let res = $f(); | |
567 | stats.push(($label, self.position() - orig_pos)); | |
568 | res | |
569 | }}; | |
570 | } | |
571 | ||
572 | // We have already encoded some things. Get their combined size from the current position. | |
573 | stats.push(("preamble", self.position())); | |
574 | ||
575 | let (crate_deps, dylib_dependency_formats) = | |
576 | stat!("dep", || (self.encode_crate_deps(), self.encode_dylib_dependency_formats())); | |
577 | ||
578 | let lib_features = stat!("lib-features", || self.encode_lib_features()); | |
579 | ||
580 | let stability_implications = | |
581 | stat!("stability-implications", || self.encode_stability_implications()); | |
582 | ||
583 | let (lang_items, lang_items_missing) = stat!("lang-items", || { | |
584 | (self.encode_lang_items(), self.encode_lang_items_missing()) | |
585 | }); | |
586 | ||
587 | let diagnostic_items = stat!("diagnostic-items", || self.encode_diagnostic_items()); | |
588 | ||
589 | let native_libraries = stat!("native-libs", || self.encode_native_libraries()); | |
590 | ||
591 | let foreign_modules = stat!("foreign-modules", || self.encode_foreign_modules()); | |
592 | ||
593 | _ = stat!("def-path-table", || self.encode_def_path_table()); | |
7cac9316 | 594 | |
a2a8927a | 595 | // Encode the def IDs of traits, for rustdoc and diagnostics. |
2b03887a | 596 | let traits = stat!("traits", || self.encode_traits()); |
a2a8927a | 597 | |
7cac9316 | 598 | // Encode the def IDs of impls, for coherence checking. |
2b03887a FG |
599 | let impls = stat!("impls", || self.encode_impls()); |
600 | ||
601 | let incoherent_impls = stat!("incoherent-impls", || self.encode_incoherent_impls()); | |
602 | ||
603 | _ = stat!("mir", || self.encode_mir()); | |
604 | ||
353b0b11 FG |
605 | _ = stat!("def-ids", || self.encode_def_ids()); |
606 | ||
607 | _ = stat!("items", || self.encode_info_for_items()); | |
2b03887a FG |
608 | |
609 | let interpret_alloc_index = stat!("interpret-alloc-index", || { | |
0531ce1d XL |
610 | let mut interpret_alloc_index = Vec::new(); |
611 | let mut n = 0; | |
612 | trace!("beginning to encode alloc ids"); | |
613 | loop { | |
3dfed10e | 614 | let new_n = self.interpret_allocs.len(); |
0531ce1d XL |
615 | // if we have found new ids, serialize those, too |
616 | if n == new_n { | |
617 | // otherwise, abort | |
618 | break; | |
619 | } | |
620 | trace!("encoding {} further alloc ids", new_n - n); | |
621 | for idx in n..new_n { | |
3dfed10e | 622 | let id = self.interpret_allocs[idx]; |
0531ce1d XL |
623 | let pos = self.position() as u32; |
624 | interpret_alloc_index.push(pos); | |
923072b8 | 625 | interpret::specialized_encode_alloc_id(self, tcx, id); |
0531ce1d XL |
626 | } |
627 | n = new_n; | |
628 | } | |
923072b8 | 629 | self.lazy_array(interpret_alloc_index) |
2b03887a | 630 | }); |
0531ce1d | 631 | |
2b03887a FG |
632 | // Encode the proc macro data. This affects `tables`, so we need to do this before we |
633 | // encode the tables. This overwrites def_keys, so it must happen after | |
634 | // encode_def_path_table. | |
635 | let proc_macro_data = stat!("proc-macro-data", || self.encode_proc_macros()); | |
e1599b0c | 636 | |
2b03887a | 637 | let tables = stat!("tables", || self.tables.encode(&mut self.opaque)); |
1b1a35ee | 638 | |
2b03887a FG |
639 | let debugger_visualizers = |
640 | stat!("debugger-visualizers", || self.encode_debugger_visualizers()); | |
04454e1e | 641 | |
ba9703b0 | 642 | // Encode exported symbols info. This is prefetched in `encode_metadata` so we encode |
3dfed10e | 643 | // this as late as possible to give the prefetching as much time as possible to complete. |
2b03887a FG |
644 | let exported_symbols = stat!("exported-symbols", || { |
645 | self.encode_exported_symbols(&tcx.exported_symbols(LOCAL_CRATE)) | |
646 | }); | |
647 | ||
648 | // Encode the hygiene data. | |
649 | // IMPORTANT: this *must* be the last thing that we encode (other than `SourceMap`). The | |
650 | // process of encoding other items (e.g. `optimized_mir`) may cause us to load data from | |
651 | // the incremental cache. If this causes us to deserialize a `Span`, then we may load | |
652 | // additional `SyntaxContext`s into the global `HygieneData`. Therefore, we need to encode | |
653 | // the hygiene data last to ensure that we encode any `SyntaxContext`s that might be used. | |
654 | let (syntax_contexts, expn_data, expn_hashes) = stat!("hygiene", || self.encode_hygiene()); | |
655 | ||
656 | let def_path_hash_map = stat!("def-path-hash-map", || self.encode_def_path_hash_map()); | |
657 | ||
658 | // Encode source_map. This needs to be done last, because encoding `Span`s tells us which | |
659 | // `SourceFiles` we actually need to encode. | |
660 | let source_map = stat!("source-map", || self.encode_source_map()); | |
661 | ||
662 | let root = stat!("final", || { | |
663 | let attrs = tcx.hir().krate_attrs(); | |
664 | self.lazy(CrateRoot { | |
665 | name: tcx.crate_name(LOCAL_CRATE), | |
666 | extra_filename: tcx.sess.opts.cg.extra_filename.clone(), | |
667 | triple: tcx.sess.opts.target_triple.clone(), | |
668 | hash: tcx.crate_hash(LOCAL_CRATE), | |
669 | stable_crate_id: tcx.def_path_hash(LOCAL_CRATE.as_def_id()).stable_crate_id(), | |
670 | required_panic_strategy: tcx.required_panic_strategy(LOCAL_CRATE), | |
671 | panic_in_drop_strategy: tcx.sess.opts.unstable_opts.panic_in_drop, | |
672 | edition: tcx.sess.edition(), | |
673 | has_global_allocator: tcx.has_global_allocator(LOCAL_CRATE), | |
487cf647 | 674 | has_alloc_error_handler: tcx.has_alloc_error_handler(LOCAL_CRATE), |
2b03887a | 675 | has_panic_handler: tcx.has_panic_handler(LOCAL_CRATE), |
353b0b11 | 676 | has_default_lib_allocator: attr::contains_name(&attrs, sym::default_lib_allocator), |
2b03887a FG |
677 | proc_macro_data, |
678 | debugger_visualizers, | |
353b0b11 FG |
679 | compiler_builtins: attr::contains_name(&attrs, sym::compiler_builtins), |
680 | needs_allocator: attr::contains_name(&attrs, sym::needs_allocator), | |
681 | needs_panic_runtime: attr::contains_name(&attrs, sym::needs_panic_runtime), | |
682 | no_builtins: attr::contains_name(&attrs, sym::no_builtins), | |
683 | panic_runtime: attr::contains_name(&attrs, sym::panic_runtime), | |
684 | profiler_runtime: attr::contains_name(&attrs, sym::profiler_runtime), | |
2b03887a FG |
685 | symbol_mangling_version: tcx.sess.opts.get_symbol_mangling_version(), |
686 | ||
687 | crate_deps, | |
688 | dylib_dependency_formats, | |
689 | lib_features, | |
690 | stability_implications, | |
691 | lang_items, | |
692 | diagnostic_items, | |
693 | lang_items_missing, | |
694 | native_libraries, | |
695 | foreign_modules, | |
696 | source_map, | |
697 | traits, | |
698 | impls, | |
699 | incoherent_impls, | |
700 | exported_symbols, | |
701 | interpret_alloc_index, | |
702 | tables, | |
703 | syntax_contexts, | |
704 | expn_data, | |
705 | expn_hashes, | |
706 | def_path_hash_map, | |
707 | }) | |
7cac9316 XL |
708 | }); |
709 | ||
710 | let total_bytes = self.position(); | |
711 | ||
2b03887a | 712 | let computed_total_bytes: usize = stats.iter().map(|(_, size)| size).sum(); |
923072b8 FG |
713 | assert_eq!(total_bytes, computed_total_bytes); |
714 | ||
9c376795 | 715 | if tcx.sess.opts.unstable_opts.meta_stats { |
064997fb FG |
716 | self.opaque.flush(); |
717 | ||
718 | // Rewind and re-read all the metadata to count the zero bytes we wrote. | |
719 | let pos_before_rewind = self.opaque.file().stream_position().unwrap(); | |
7cac9316 | 720 | let mut zero_bytes = 0; |
064997fb FG |
721 | self.opaque.file().rewind().unwrap(); |
722 | let file = std::io::BufReader::new(self.opaque.file()); | |
723 | for e in file.bytes() { | |
724 | if e.unwrap() == 0 { | |
7cac9316 XL |
725 | zero_bytes += 1; |
726 | } | |
727 | } | |
064997fb | 728 | assert_eq!(self.opaque.file().stream_position().unwrap(), pos_before_rewind); |
7cac9316 | 729 | |
2b03887a FG |
730 | stats.sort_by_key(|&(_, usize)| usize); |
731 | ||
732 | let prefix = "meta-stats"; | |
923072b8 | 733 | let perc = |bytes| (bytes * 100) as f64 / total_bytes as f64; |
923072b8 | 734 | |
9c376795 | 735 | eprintln!("{prefix} METADATA STATS"); |
2b03887a | 736 | eprintln!("{} {:<23}{:>10}", prefix, "Section", "Size"); |
9c376795 | 737 | eprintln!("{prefix} ----------------------------------------------------------------"); |
2b03887a FG |
738 | for (label, size) in stats { |
739 | eprintln!( | |
740 | "{} {:<23}{:>10} ({:4.1}%)", | |
741 | prefix, | |
742 | label, | |
743 | to_readable_str(size), | |
744 | perc(size) | |
745 | ); | |
746 | } | |
9c376795 | 747 | eprintln!("{prefix} ----------------------------------------------------------------"); |
923072b8 | 748 | eprintln!( |
2b03887a FG |
749 | "{} {:<23}{:>10} (of which {:.1}% are zero bytes)", |
750 | prefix, | |
751 | "Total", | |
752 | to_readable_str(total_bytes), | |
923072b8 FG |
753 | perc(zero_bytes) |
754 | ); | |
9c376795 | 755 | eprintln!("{prefix}"); |
7cac9316 XL |
756 | } |
757 | ||
758 | root | |
759 | } | |
cc61c64b | 760 | } |
1a4d82fc | 761 | |
9ffffee4 FG |
762 | struct AnalyzeAttrState { |
763 | is_exported: bool, | |
764 | is_doc_hidden: bool, | |
765 | } | |
766 | ||
2b03887a FG |
767 | /// Returns whether an attribute needs to be recorded in metadata, that is, if it's usable and |
768 | /// useful in downstream crates. Local-only attributes are an obvious example, but some | |
769 | /// rustdoc-specific attributes can equally be of use while documenting the current crate only. | |
770 | /// | |
771 | /// Removing these superfluous attributes speeds up compilation by making the metadata smaller. | |
772 | /// | |
9ffffee4 | 773 | /// Note: the `is_exported` parameter is used to cache whether the given `DefId` has a public |
2b03887a FG |
774 | /// visibility: this is a piece of data that can be computed once per defid, and not once per |
775 | /// attribute. Some attributes would only be usable downstream if they are public. | |
776 | #[inline] | |
9ffffee4 FG |
777 | fn analyze_attr(attr: &Attribute, state: &mut AnalyzeAttrState) -> bool { |
778 | let mut should_encode = false; | |
2b03887a FG |
779 | if rustc_feature::is_builtin_only_local(attr.name_or_empty()) { |
780 | // Attributes marked local-only don't need to be encoded for downstream crates. | |
2b03887a | 781 | } else if attr.doc_str().is_some() { |
9ffffee4 FG |
782 | // We keep all doc comments reachable to rustdoc because they might be "imported" into |
783 | // downstream crates if they use `#[doc(inline)]` to copy an item's documentation into | |
784 | // their own. | |
785 | if state.is_exported { | |
786 | should_encode = true; | |
787 | } | |
2b03887a | 788 | } else if attr.has_name(sym::doc) { |
9ffffee4 FG |
789 | // If this is a `doc` attribute that doesn't have anything except maybe `inline` (as in |
790 | // `#[doc(inline)]`), then we can remove it. It won't be inlinable in downstream crates. | |
791 | if let Some(item_list) = attr.meta_item_list() { | |
792 | for item in item_list { | |
793 | if !item.has_name(sym::inline) { | |
794 | should_encode = true; | |
795 | if item.has_name(sym::hidden) { | |
796 | state.is_doc_hidden = true; | |
797 | break; | |
798 | } | |
799 | } | |
800 | } | |
801 | } | |
2b03887a | 802 | } else { |
9ffffee4 | 803 | should_encode = true; |
2b03887a | 804 | } |
9ffffee4 | 805 | should_encode |
2b03887a FG |
806 | } |
807 | ||
353b0b11 | 808 | fn should_encode_span(def_kind: DefKind) -> bool { |
5869c6ff XL |
809 | match def_kind { |
810 | DefKind::Mod | |
811 | | DefKind::Struct | |
812 | | DefKind::Union | |
813 | | DefKind::Enum | |
814 | | DefKind::Variant | |
815 | | DefKind::Trait | |
816 | | DefKind::TyAlias | |
817 | | DefKind::ForeignTy | |
818 | | DefKind::TraitAlias | |
819 | | DefKind::AssocTy | |
353b0b11 FG |
820 | | DefKind::TyParam |
821 | | DefKind::ConstParam | |
822 | | DefKind::LifetimeParam | |
5869c6ff XL |
823 | | DefKind::Fn |
824 | | DefKind::Const | |
353b0b11 | 825 | | DefKind::Static(_) |
5869c6ff XL |
826 | | DefKind::Ctor(..) |
827 | | DefKind::AssocFn | |
828 | | DefKind::AssocConst | |
353b0b11 | 829 | | DefKind::Macro(_) |
49aad941 FG |
830 | | DefKind::ExternCrate |
831 | | DefKind::Use | |
353b0b11 FG |
832 | | DefKind::AnonConst |
833 | | DefKind::InlineConst | |
834 | | DefKind::OpaqueTy | |
49aad941 | 835 | | DefKind::ImplTraitPlaceholder |
353b0b11 FG |
836 | | DefKind::Field |
837 | | DefKind::Impl { .. } | |
838 | | DefKind::Closure | |
839 | | DefKind::Generator => true, | |
49aad941 | 840 | DefKind::ForeignMod | DefKind::GlobalAsm => false, |
353b0b11 FG |
841 | } |
842 | } | |
843 | ||
844 | fn should_encode_attrs(def_kind: DefKind) -> bool { | |
845 | match def_kind { | |
846 | DefKind::Mod | |
847 | | DefKind::Struct | |
848 | | DefKind::Union | |
849 | | DefKind::Enum | |
850 | | DefKind::Variant | |
851 | | DefKind::Trait | |
852 | | DefKind::TyAlias | |
853 | | DefKind::ForeignTy | |
854 | | DefKind::TraitAlias | |
855 | | DefKind::AssocTy | |
856 | | DefKind::Fn | |
857 | | DefKind::Const | |
858 | | DefKind::Static(_) | |
859 | | DefKind::AssocFn | |
860 | | DefKind::AssocConst | |
861 | | DefKind::Macro(_) | |
862 | | DefKind::Field | |
863 | | DefKind::Impl { .. } => true, | |
49aad941 FG |
864 | // Tools may want to be able to detect their tool lints on |
865 | // closures from upstream crates, too. This is used by | |
866 | // https://github.com/model-checking/kani and is not a performance | |
867 | // or maintenance issue for us. | |
868 | DefKind::Closure => true, | |
353b0b11 FG |
869 | DefKind::TyParam |
870 | | DefKind::ConstParam | |
871 | | DefKind::Ctor(..) | |
872 | | DefKind::ExternCrate | |
873 | | DefKind::Use | |
874 | | DefKind::ForeignMod | |
875 | | DefKind::AnonConst | |
876 | | DefKind::InlineConst | |
5869c6ff | 877 | | DefKind::OpaqueTy |
f2b60f7d | 878 | | DefKind::ImplTraitPlaceholder |
353b0b11 FG |
879 | | DefKind::LifetimeParam |
880 | | DefKind::GlobalAsm | |
353b0b11 FG |
881 | | DefKind::Generator => false, |
882 | } | |
883 | } | |
884 | ||
885 | fn should_encode_expn_that_defined(def_kind: DefKind) -> bool { | |
886 | match def_kind { | |
887 | DefKind::Mod | |
888 | | DefKind::Struct | |
889 | | DefKind::Union | |
890 | | DefKind::Enum | |
891 | | DefKind::Variant | |
892 | | DefKind::Trait | |
893 | | DefKind::Impl { .. } => true, | |
894 | DefKind::TyAlias | |
895 | | DefKind::ForeignTy | |
896 | | DefKind::TraitAlias | |
897 | | DefKind::AssocTy | |
898 | | DefKind::TyParam | |
899 | | DefKind::Fn | |
900 | | DefKind::Const | |
901 | | DefKind::ConstParam | |
902 | | DefKind::Static(_) | |
903 | | DefKind::Ctor(..) | |
904 | | DefKind::AssocFn | |
905 | | DefKind::AssocConst | |
906 | | DefKind::Macro(_) | |
907 | | DefKind::ExternCrate | |
908 | | DefKind::Use | |
909 | | DefKind::ForeignMod | |
910 | | DefKind::AnonConst | |
911 | | DefKind::InlineConst | |
912 | | DefKind::OpaqueTy | |
913 | | DefKind::ImplTraitPlaceholder | |
914 | | DefKind::Field | |
915 | | DefKind::LifetimeParam | |
916 | | DefKind::GlobalAsm | |
917 | | DefKind::Closure | |
918 | | DefKind::Generator => false, | |
919 | } | |
920 | } | |
921 | ||
922 | fn should_encode_visibility(def_kind: DefKind) -> bool { | |
923 | match def_kind { | |
924 | DefKind::Mod | |
925 | | DefKind::Struct | |
926 | | DefKind::Union | |
927 | | DefKind::Enum | |
928 | | DefKind::Variant | |
929 | | DefKind::Trait | |
930 | | DefKind::TyAlias | |
931 | | DefKind::ForeignTy | |
932 | | DefKind::TraitAlias | |
933 | | DefKind::AssocTy | |
934 | | DefKind::Fn | |
935 | | DefKind::Const | |
936 | | DefKind::Static(..) | |
937 | | DefKind::Ctor(..) | |
938 | | DefKind::AssocFn | |
939 | | DefKind::AssocConst | |
940 | | DefKind::Macro(..) | |
5869c6ff | 941 | | DefKind::Field => true, |
353b0b11 FG |
942 | DefKind::Use |
943 | | DefKind::ForeignMod | |
944 | | DefKind::TyParam | |
5869c6ff XL |
945 | | DefKind::ConstParam |
946 | | DefKind::LifetimeParam | |
947 | | DefKind::AnonConst | |
3c0e092e | 948 | | DefKind::InlineConst |
353b0b11 FG |
949 | | DefKind::OpaqueTy |
950 | | DefKind::ImplTraitPlaceholder | |
5869c6ff | 951 | | DefKind::GlobalAsm |
353b0b11 | 952 | | DefKind::Impl { .. } |
5869c6ff XL |
953 | | DefKind::Closure |
954 | | DefKind::Generator | |
955 | | DefKind::ExternCrate => false, | |
956 | } | |
957 | } | |
958 | ||
959 | fn should_encode_stability(def_kind: DefKind) -> bool { | |
960 | match def_kind { | |
961 | DefKind::Mod | |
962 | | DefKind::Ctor(..) | |
963 | | DefKind::Variant | |
964 | | DefKind::Field | |
965 | | DefKind::Struct | |
966 | | DefKind::AssocTy | |
967 | | DefKind::AssocFn | |
968 | | DefKind::AssocConst | |
969 | | DefKind::TyParam | |
970 | | DefKind::ConstParam | |
5e7ed085 | 971 | | DefKind::Static(..) |
5869c6ff XL |
972 | | DefKind::Const |
973 | | DefKind::Fn | |
974 | | DefKind::ForeignMod | |
975 | | DefKind::TyAlias | |
976 | | DefKind::OpaqueTy | |
f2b60f7d | 977 | | DefKind::ImplTraitPlaceholder |
5869c6ff XL |
978 | | DefKind::Enum |
979 | | DefKind::Union | |
9ffffee4 | 980 | | DefKind::Impl { .. } |
5869c6ff XL |
981 | | DefKind::Trait |
982 | | DefKind::TraitAlias | |
983 | | DefKind::Macro(..) | |
984 | | DefKind::ForeignTy => true, | |
985 | DefKind::Use | |
986 | | DefKind::LifetimeParam | |
987 | | DefKind::AnonConst | |
3c0e092e | 988 | | DefKind::InlineConst |
5869c6ff XL |
989 | | DefKind::GlobalAsm |
990 | | DefKind::Closure | |
991 | | DefKind::Generator | |
992 | | DefKind::ExternCrate => false, | |
993 | } | |
994 | } | |
995 | ||
996 | /// Whether we should encode MIR. | |
997 | /// | |
998 | /// Computing, optimizing and encoding the MIR is a relatively expensive operation. | |
999 | /// We want to avoid this work when not required. Therefore: | |
1000 | /// - we only compute `mir_for_ctfe` on items with const-eval semantics; | |
1001 | /// - we skip `optimized_mir` for check runs. | |
1002 | /// | |
1003 | /// Return a pair, resp. for CTFE and for LLVM. | |
1004 | fn should_encode_mir(tcx: TyCtxt<'_>, def_id: LocalDefId) -> (bool, bool) { | |
1005 | match tcx.def_kind(def_id) { | |
1006 | // Constructors | |
1007 | DefKind::Ctor(_, _) => { | |
1008 | let mir_opt_base = tcx.sess.opts.output_types.should_codegen() | |
064997fb | 1009 | || tcx.sess.opts.unstable_opts.always_encode_mir; |
5869c6ff XL |
1010 | (true, mir_opt_base) |
1011 | } | |
1012 | // Constants | |
3c0e092e XL |
1013 | DefKind::AnonConst |
1014 | | DefKind::InlineConst | |
1015 | | DefKind::AssocConst | |
5e7ed085 | 1016 | | DefKind::Static(..) |
3c0e092e | 1017 | | DefKind::Const => (true, false), |
9c376795 FG |
1018 | // Full-fledged functions + closures |
1019 | DefKind::AssocFn | DefKind::Fn | DefKind::Closure => { | |
5869c6ff XL |
1020 | let generics = tcx.generics_of(def_id); |
1021 | let needs_inline = (generics.requires_monomorphization(tcx) | |
1022 | || tcx.codegen_fn_attrs(def_id).requests_inline()) | |
1023 | && tcx.sess.opts.output_types.should_codegen(); | |
923072b8 | 1024 | // The function has a `const` modifier or is in a `#[const_trait]`. |
a2a8927a | 1025 | let is_const_fn = tcx.is_const_fn_raw(def_id.to_def_id()) |
923072b8 | 1026 | || tcx.is_const_default_method(def_id.to_def_id()); |
064997fb | 1027 | let always_encode_mir = tcx.sess.opts.unstable_opts.always_encode_mir; |
5869c6ff XL |
1028 | (is_const_fn, needs_inline || always_encode_mir) |
1029 | } | |
5869c6ff XL |
1030 | // Generators require optimized MIR to compute layout. |
1031 | DefKind::Generator => (false, true), | |
1032 | // The others don't have MIR. | |
1033 | _ => (false, false), | |
1034 | } | |
1035 | } | |
1036 | ||
6a06907d XL |
1037 | fn should_encode_variances(def_kind: DefKind) -> bool { |
1038 | match def_kind { | |
1039 | DefKind::Struct | |
1040 | | DefKind::Union | |
1041 | | DefKind::Enum | |
1042 | | DefKind::Variant | |
487cf647 FG |
1043 | | DefKind::OpaqueTy |
1044 | | DefKind::ImplTraitPlaceholder | |
6a06907d XL |
1045 | | DefKind::Fn |
1046 | | DefKind::Ctor(..) | |
1047 | | DefKind::AssocFn => true, | |
1048 | DefKind::Mod | |
1049 | | DefKind::Field | |
1050 | | DefKind::AssocTy | |
1051 | | DefKind::AssocConst | |
1052 | | DefKind::TyParam | |
1053 | | DefKind::ConstParam | |
5e7ed085 | 1054 | | DefKind::Static(..) |
6a06907d XL |
1055 | | DefKind::Const |
1056 | | DefKind::ForeignMod | |
1057 | | DefKind::TyAlias | |
9ffffee4 | 1058 | | DefKind::Impl { .. } |
6a06907d XL |
1059 | | DefKind::Trait |
1060 | | DefKind::TraitAlias | |
1061 | | DefKind::Macro(..) | |
1062 | | DefKind::ForeignTy | |
1063 | | DefKind::Use | |
1064 | | DefKind::LifetimeParam | |
1065 | | DefKind::AnonConst | |
3c0e092e | 1066 | | DefKind::InlineConst |
6a06907d XL |
1067 | | DefKind::GlobalAsm |
1068 | | DefKind::Closure | |
1069 | | DefKind::Generator | |
1070 | | DefKind::ExternCrate => false, | |
1071 | } | |
1072 | } | |
1073 | ||
1074 | fn should_encode_generics(def_kind: DefKind) -> bool { | |
1075 | match def_kind { | |
1076 | DefKind::Struct | |
1077 | | DefKind::Union | |
1078 | | DefKind::Enum | |
1079 | | DefKind::Variant | |
1080 | | DefKind::Trait | |
1081 | | DefKind::TyAlias | |
1082 | | DefKind::ForeignTy | |
1083 | | DefKind::TraitAlias | |
1084 | | DefKind::AssocTy | |
1085 | | DefKind::Fn | |
1086 | | DefKind::Const | |
5e7ed085 | 1087 | | DefKind::Static(..) |
6a06907d XL |
1088 | | DefKind::Ctor(..) |
1089 | | DefKind::AssocFn | |
1090 | | DefKind::AssocConst | |
1091 | | DefKind::AnonConst | |
3c0e092e | 1092 | | DefKind::InlineConst |
6a06907d | 1093 | | DefKind::OpaqueTy |
f2b60f7d | 1094 | | DefKind::ImplTraitPlaceholder |
9ffffee4 | 1095 | | DefKind::Impl { .. } |
94222f64 XL |
1096 | | DefKind::Field |
1097 | | DefKind::TyParam | |
6a06907d XL |
1098 | | DefKind::Closure |
1099 | | DefKind::Generator => true, | |
1100 | DefKind::Mod | |
6a06907d | 1101 | | DefKind::ForeignMod |
6a06907d XL |
1102 | | DefKind::ConstParam |
1103 | | DefKind::Macro(..) | |
1104 | | DefKind::Use | |
1105 | | DefKind::LifetimeParam | |
1106 | | DefKind::GlobalAsm | |
1107 | | DefKind::ExternCrate => false, | |
1108 | } | |
1109 | } | |
1110 | ||
f2b60f7d FG |
1111 | fn should_encode_type(tcx: TyCtxt<'_>, def_id: LocalDefId, def_kind: DefKind) -> bool { |
1112 | match def_kind { | |
1113 | DefKind::Struct | |
1114 | | DefKind::Union | |
1115 | | DefKind::Enum | |
1116 | | DefKind::Variant | |
1117 | | DefKind::Ctor(..) | |
1118 | | DefKind::Field | |
1119 | | DefKind::Fn | |
1120 | | DefKind::Const | |
1121 | | DefKind::Static(..) | |
1122 | | DefKind::TyAlias | |
f2b60f7d | 1123 | | DefKind::ForeignTy |
9ffffee4 | 1124 | | DefKind::Impl { .. } |
f2b60f7d FG |
1125 | | DefKind::AssocFn |
1126 | | DefKind::AssocConst | |
1127 | | DefKind::Closure | |
1128 | | DefKind::Generator | |
1129 | | DefKind::ConstParam | |
1130 | | DefKind::AnonConst | |
1131 | | DefKind::InlineConst => true, | |
1132 | ||
353b0b11 FG |
1133 | DefKind::OpaqueTy => { |
1134 | let opaque = tcx.hir().expect_item(def_id).expect_opaque_ty(); | |
1135 | if let hir::OpaqueTyOrigin::FnReturn(fn_def_id) | hir::OpaqueTyOrigin::AsyncFn(fn_def_id) = opaque.origin | |
1136 | && let hir::Node::TraitItem(trait_item) = tcx.hir().get_by_def_id(fn_def_id) | |
1137 | && let (_, hir::TraitFn::Required(..)) = trait_item.expect_fn() | |
1138 | { | |
1139 | false | |
1140 | } else { | |
1141 | true | |
1142 | } | |
1143 | } | |
1144 | ||
f2b60f7d | 1145 | DefKind::ImplTraitPlaceholder => { |
353b0b11 | 1146 | let parent_def_id = tcx.impl_trait_in_trait_parent_fn(def_id.to_def_id()); |
f2b60f7d FG |
1147 | let assoc_item = tcx.associated_item(parent_def_id); |
1148 | match assoc_item.container { | |
1149 | // Always encode an RPIT in an impl fn, since it always has a body | |
1150 | ty::AssocItemContainer::ImplContainer => true, | |
1151 | ty::AssocItemContainer::TraitContainer => { | |
1152 | // Encode an RPIT for a trait only if the trait has a default body | |
1153 | assoc_item.defaultness(tcx).has_value() | |
1154 | } | |
1155 | } | |
1156 | } | |
1157 | ||
1158 | DefKind::AssocTy => { | |
1159 | let assoc_item = tcx.associated_item(def_id); | |
1160 | match assoc_item.container { | |
1161 | ty::AssocItemContainer::ImplContainer => true, | |
353b0b11 FG |
1162 | // FIXME(-Zlower-impl-trait-in-trait-to-assoc-ty) always encode RPITITs, |
1163 | // since we need to be able to "project" from an RPITIT associated item | |
1164 | // to an opaque when installing the default projection predicates in | |
1165 | // default trait methods with RPITITs. | |
1166 | ty::AssocItemContainer::TraitContainer => { | |
1167 | assoc_item.defaultness(tcx).has_value() || assoc_item.opt_rpitit_info.is_some() | |
1168 | } | |
f2b60f7d FG |
1169 | } |
1170 | } | |
1171 | DefKind::TyParam => { | |
1172 | let hir::Node::GenericParam(param) = tcx.hir().get_by_def_id(def_id) else { bug!() }; | |
1173 | let hir::GenericParamKind::Type { default, .. } = param.kind else { bug!() }; | |
1174 | default.is_some() | |
1175 | } | |
1176 | ||
1177 | DefKind::Trait | |
1178 | | DefKind::TraitAlias | |
1179 | | DefKind::Mod | |
1180 | | DefKind::ForeignMod | |
1181 | | DefKind::Macro(..) | |
1182 | | DefKind::Use | |
1183 | | DefKind::LifetimeParam | |
1184 | | DefKind::GlobalAsm | |
1185 | | DefKind::ExternCrate => false, | |
1186 | } | |
1187 | } | |
1188 | ||
1189 | fn should_encode_const(def_kind: DefKind) -> bool { | |
1190 | match def_kind { | |
1191 | DefKind::Const | DefKind::AssocConst | DefKind::AnonConst => true, | |
1192 | ||
1193 | DefKind::Struct | |
1194 | | DefKind::Union | |
1195 | | DefKind::Enum | |
1196 | | DefKind::Variant | |
1197 | | DefKind::Ctor(..) | |
1198 | | DefKind::Field | |
1199 | | DefKind::Fn | |
1200 | | DefKind::Static(..) | |
1201 | | DefKind::TyAlias | |
1202 | | DefKind::OpaqueTy | |
1203 | | DefKind::ImplTraitPlaceholder | |
1204 | | DefKind::ForeignTy | |
9ffffee4 | 1205 | | DefKind::Impl { .. } |
f2b60f7d FG |
1206 | | DefKind::AssocFn |
1207 | | DefKind::Closure | |
1208 | | DefKind::Generator | |
1209 | | DefKind::ConstParam | |
1210 | | DefKind::InlineConst | |
1211 | | DefKind::AssocTy | |
1212 | | DefKind::TyParam | |
1213 | | DefKind::Trait | |
1214 | | DefKind::TraitAlias | |
1215 | | DefKind::Mod | |
1216 | | DefKind::ForeignMod | |
1217 | | DefKind::Macro(..) | |
1218 | | DefKind::Use | |
1219 | | DefKind::LifetimeParam | |
1220 | | DefKind::GlobalAsm | |
1221 | | DefKind::ExternCrate => false, | |
1222 | } | |
1223 | } | |
1224 | ||
9ffffee4 FG |
1225 | // We only encode impl trait in trait when using `lower-impl-trait-in-trait-to-assoc-ty` unstable |
1226 | // option. | |
1227 | fn should_encode_fn_impl_trait_in_trait<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId) -> bool { | |
353b0b11 | 1228 | if tcx.lower_impl_trait_in_trait_to_assoc_ty() |
9ffffee4 FG |
1229 | && let Some(assoc_item) = tcx.opt_associated_item(def_id) |
1230 | && assoc_item.container == ty::AssocItemContainer::TraitContainer | |
1231 | && assoc_item.kind == ty::AssocKind::Fn | |
1232 | { | |
1233 | true | |
1234 | } else { | |
1235 | false | |
2b03887a | 1236 | } |
2b03887a FG |
1237 | } |
1238 | ||
a2a8927a | 1239 | impl<'a, 'tcx> EncodeContext<'a, 'tcx> { |
04454e1e | 1240 | fn encode_attrs(&mut self, def_id: LocalDefId) { |
2b03887a | 1241 | let tcx = self.tcx; |
9ffffee4 FG |
1242 | let mut state = AnalyzeAttrState { |
1243 | is_exported: tcx.effective_visibilities(()).is_exported(def_id), | |
1244 | is_doc_hidden: false, | |
1245 | }; | |
1246 | let attr_iter = tcx | |
1247 | .opt_local_def_id_to_hir_id(def_id) | |
1248 | .map_or(Default::default(), |hir_id| tcx.hir().attrs(hir_id)) | |
04454e1e | 1249 | .iter() |
9ffffee4 FG |
1250 | .filter(|attr| analyze_attr(attr, &mut state)); |
1251 | ||
1252 | record_array!(self.tables.attributes[def_id.to_def_id()] <- attr_iter); | |
04454e1e | 1253 | |
9ffffee4 FG |
1254 | let mut attr_flags = AttrFlags::empty(); |
1255 | if state.is_doc_hidden { | |
1256 | attr_flags |= AttrFlags::IS_DOC_HIDDEN; | |
04454e1e | 1257 | } |
9ffffee4 | 1258 | self.tables.attr_flags.set(def_id.local_def_index, attr_flags); |
04454e1e FG |
1259 | } |
1260 | ||
5869c6ff XL |
1261 | fn encode_def_ids(&mut self) { |
1262 | if self.is_proc_macro { | |
1263 | return; | |
1264 | } | |
1265 | let tcx = self.tcx; | |
064997fb | 1266 | for local_id in tcx.iter_local_def_id() { |
5869c6ff XL |
1267 | let def_id = local_id.to_def_id(); |
1268 | let def_kind = tcx.opt_def_kind(local_id); | |
5099ac24 | 1269 | let Some(def_kind) = def_kind else { continue }; |
9ffffee4 | 1270 | self.tables.opt_def_kind.set_some(def_id.index, def_kind); |
353b0b11 FG |
1271 | if should_encode_span(def_kind) { |
1272 | let def_span = tcx.def_span(local_id); | |
1273 | record!(self.tables.def_span[def_id] <- def_span); | |
1274 | } | |
1275 | if should_encode_attrs(def_kind) { | |
1276 | self.encode_attrs(local_id); | |
1277 | } | |
1278 | if should_encode_expn_that_defined(def_kind) { | |
1279 | record!(self.tables.expn_that_defined[def_id] <- self.tcx.expn_that_defined(def_id)); | |
1280 | } | |
1281 | if should_encode_span(def_kind) && let Some(ident_span) = tcx.def_ident_span(def_id) { | |
923072b8 FG |
1282 | record!(self.tables.def_ident_span[def_id] <- ident_span); |
1283 | } | |
04454e1e FG |
1284 | if def_kind.has_codegen_attrs() { |
1285 | record!(self.tables.codegen_fn_attrs[def_id] <- self.tcx.codegen_fn_attrs(def_id)); | |
1286 | } | |
5869c6ff | 1287 | if should_encode_visibility(def_kind) { |
f2b60f7d FG |
1288 | let vis = |
1289 | self.tcx.local_visibility(local_id).map_id(|def_id| def_id.local_def_index); | |
1290 | record!(self.tables.visibility[def_id] <- vis); | |
5869c6ff XL |
1291 | } |
1292 | if should_encode_stability(def_kind) { | |
1293 | self.encode_stability(def_id); | |
1294 | self.encode_const_stability(def_id); | |
f2b60f7d | 1295 | self.encode_default_body_stability(def_id); |
5869c6ff XL |
1296 | self.encode_deprecation(def_id); |
1297 | } | |
6a06907d XL |
1298 | if should_encode_variances(def_kind) { |
1299 | let v = self.tcx.variances_of(def_id); | |
923072b8 | 1300 | record_array!(self.tables.variances_of[def_id] <- v); |
6a06907d XL |
1301 | } |
1302 | if should_encode_generics(def_kind) { | |
1303 | let g = tcx.generics_of(def_id); | |
5e7ed085 FG |
1304 | record!(self.tables.generics_of[def_id] <- g); |
1305 | record!(self.tables.explicit_predicates_of[def_id] <- self.tcx.explicit_predicates_of(def_id)); | |
6a06907d | 1306 | let inferred_outlives = self.tcx.inferred_outlives_of(def_id); |
9ffffee4 | 1307 | record_defaulted_array!(self.tables.inferred_outlives_of[def_id] <- inferred_outlives); |
6a06907d | 1308 | } |
f2b60f7d FG |
1309 | if should_encode_type(tcx, local_id, def_kind) { |
1310 | record!(self.tables.type_of[def_id] <- self.tcx.type_of(def_id)); | |
1311 | } | |
1312 | if let DefKind::TyParam = def_kind { | |
1313 | let default = self.tcx.object_lifetime_default(def_id); | |
1314 | record!(self.tables.object_lifetime_default[def_id] <- default); | |
1315 | } | |
353b0b11 | 1316 | if let DefKind::Trait = def_kind { |
5e7ed085 | 1317 | record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id)); |
6a06907d | 1318 | } |
353b0b11 FG |
1319 | if let DefKind::TraitAlias = def_kind { |
1320 | record!(self.tables.super_predicates_of[def_id] <- self.tcx.super_predicates_of(def_id)); | |
1321 | record!(self.tables.implied_predicates_of[def_id] <- self.tcx.implied_predicates_of(def_id)); | |
1322 | } | |
2b03887a | 1323 | if let DefKind::Enum | DefKind::Struct | DefKind::Union = def_kind { |
353b0b11 | 1324 | self.encode_info_for_adt(local_id); |
2b03887a | 1325 | } |
9ffffee4 | 1326 | if tcx.impl_method_has_trait_impl_trait_tys(def_id) |
9c376795 | 1327 | && let Ok(table) = self.tcx.collect_return_position_impl_trait_in_trait_tys(def_id) |
2b03887a FG |
1328 | { |
1329 | record!(self.tables.trait_impl_trait_tys[def_id] <- table); | |
1330 | } | |
9ffffee4 | 1331 | if should_encode_fn_impl_trait_in_trait(tcx, def_id) { |
353b0b11 FG |
1332 | let table = tcx.associated_types_for_impl_traits_in_associated_fn(def_id); |
1333 | record_defaulted_array!(self.tables.associated_types_for_impl_traits_in_associated_fn[def_id] <- table); | |
9ffffee4 | 1334 | } |
6a06907d | 1335 | } |
9ffffee4 | 1336 | |
9c376795 FG |
1337 | let inherent_impls = tcx.with_stable_hashing_context(|hcx| { |
1338 | tcx.crate_inherent_impls(()).inherent_impls.to_sorted(&hcx, true) | |
1339 | }); | |
9ffffee4 FG |
1340 | for (def_id, impls) in inherent_impls { |
1341 | record_defaulted_array!(self.tables.inherent_impls[def_id.to_def_id()] <- impls.iter().map(|def_id| { | |
6a06907d XL |
1342 | assert!(def_id.is_local()); |
1343 | def_id.index | |
1344 | })); | |
5869c6ff | 1345 | } |
9ffffee4 FG |
1346 | |
1347 | for (def_id, res_map) in &tcx.resolutions(()).doc_link_resolutions { | |
1348 | record!(self.tables.doc_link_resolutions[def_id.to_def_id()] <- res_map); | |
1349 | } | |
1350 | ||
1351 | for (def_id, traits) in &tcx.resolutions(()).doc_link_traits_in_scope { | |
1352 | record_array!(self.tables.doc_link_traits_in_scope[def_id.to_def_id()] <- traits); | |
1353 | } | |
223e47cc | 1354 | } |
970d7e83 | 1355 | |
9ffffee4 | 1356 | #[instrument(level = "trace", skip(self))] |
353b0b11 FG |
1357 | fn encode_info_for_adt(&mut self, local_def_id: LocalDefId) { |
1358 | let def_id = local_def_id.to_def_id(); | |
9e0c209e | 1359 | let tcx = self.tcx; |
9ffffee4 FG |
1360 | let adt_def = tcx.adt_def(def_id); |
1361 | record!(self.tables.repr_options[def_id] <- adt_def.repr()); | |
1362 | ||
1363 | let params_in_repr = self.tcx.params_in_repr(def_id); | |
1364 | record!(self.tables.params_in_repr[def_id] <- params_in_repr); | |
970d7e83 | 1365 | |
9ffffee4 | 1366 | if adt_def.is_enum() { |
49aad941 FG |
1367 | let module_children = tcx.module_children_local(local_def_id); |
1368 | record_array!(self.tables.module_children_non_reexports[def_id] <- | |
1369 | module_children.iter().map(|child| child.res.def_id().index)); | |
9ffffee4 FG |
1370 | } else { |
1371 | // For non-enum, there is only one variant, and its def_id is the adt's. | |
1372 | debug_assert_eq!(adt_def.variants().len(), 1); | |
1373 | debug_assert_eq!(adt_def.non_enum_variant().def_id, def_id); | |
1374 | // Therefore, the loop over variants will encode its fields as the adt's children. | |
9e0c209e | 1375 | } |
223e47cc | 1376 | |
353b0b11 | 1377 | for (idx, variant) in adt_def.variants().iter_enumerated() { |
9ffffee4 FG |
1378 | let data = VariantData { |
1379 | discr: variant.discr, | |
353b0b11 | 1380 | idx, |
9ffffee4 FG |
1381 | ctor: variant.ctor.map(|(kind, def_id)| (kind, def_id.index)), |
1382 | is_non_exhaustive: variant.is_field_list_non_exhaustive(), | |
1383 | }; | |
1384 | record!(self.tables.variant_data[variant.def_id] <- data); | |
532ac7d7 | 1385 | |
9ffffee4 | 1386 | self.tables.constness.set_some(variant.def_id.index, hir::Constness::Const); |
49aad941 | 1387 | record_array!(self.tables.associated_item_or_field_def_ids[variant.def_id] <- variant.fields.iter().map(|f| { |
9ffffee4 FG |
1388 | assert!(f.did.is_local()); |
1389 | f.did.index | |
1390 | })); | |
532ac7d7 | 1391 | |
9ffffee4 FG |
1392 | if let Some((CtorKind::Fn, ctor_def_id)) = variant.ctor { |
1393 | self.tables.constness.set_some(ctor_def_id.index, hir::Constness::Const); | |
1394 | let fn_sig = tcx.fn_sig(ctor_def_id); | |
1395 | record!(self.tables.fn_sig[ctor_def_id] <- fn_sig); | |
1396 | // FIXME only encode signature for ctor_def_id | |
1397 | record!(self.tables.fn_sig[variant.def_id] <- fn_sig); | |
1398 | } | |
532ac7d7 XL |
1399 | } |
1400 | } | |
1401 | ||
353b0b11 | 1402 | fn encode_info_for_mod(&mut self, local_def_id: LocalDefId) { |
9e0c209e | 1403 | let tcx = self.tcx; |
3dfed10e | 1404 | let def_id = local_def_id.to_def_id(); |
dc9dc135 | 1405 | debug!("EncodeContext::encode_info_for_mod({:?})", def_id); |
9e0c209e | 1406 | |
1b1a35ee XL |
1407 | // If we are encoding a proc-macro crates, `encode_info_for_mod` will |
1408 | // only ever get called for the crate root. We still want to encode | |
1409 | // the crate root for consistency with other crates (some of the resolver | |
1410 | // code uses it). However, we skip encoding anything relating to child | |
1411 | // items - we encode information about proc-macros later on. | |
1b1a35ee | 1412 | if self.is_proc_macro { |
3c0e092e XL |
1413 | // Encode this here because we don't do it in encode_def_ids. |
1414 | record!(self.tables.expn_that_defined[def_id] <- tcx.expn_that_defined(local_def_id)); | |
1b1a35ee | 1415 | } else { |
49aad941 FG |
1416 | let module_children = tcx.module_children_local(local_def_id); |
1417 | ||
1418 | record_array!(self.tables.module_children_non_reexports[def_id] <- | |
1419 | module_children.iter().filter(|child| child.reexport_chain.is_empty()) | |
1420 | .map(|child| child.res.def_id().index)); | |
487cf647 | 1421 | |
353b0b11 | 1422 | record_defaulted_array!(self.tables.module_children_reexports[def_id] <- |
49aad941 | 1423 | module_children.iter().filter(|child| !child.reexport_chain.is_empty())); |
1b1a35ee | 1424 | } |
9e0c209e | 1425 | } |
223e47cc | 1426 | |
29967ef6 XL |
1427 | fn encode_explicit_item_bounds(&mut self, def_id: DefId) { |
1428 | debug!("EncodeContext::encode_explicit_item_bounds({:?})", def_id); | |
49aad941 | 1429 | let bounds = self.tcx.explicit_item_bounds(def_id).skip_binder(); |
9ffffee4 | 1430 | record_defaulted_array!(self.tables.explicit_item_bounds[def_id] <- bounds); |
29967ef6 XL |
1431 | } |
1432 | ||
e74abb32 | 1433 | fn encode_info_for_trait_item(&mut self, def_id: DefId) { |
dc9dc135 | 1434 | debug!("EncodeContext::encode_info_for_trait_item({:?})", def_id); |
9e0c209e | 1435 | let tcx = self.tcx; |
c34b1796 | 1436 | |
9c376795 | 1437 | let impl_defaultness = tcx.impl_defaultness(def_id.expect_local()); |
9ffffee4 | 1438 | self.tables.impl_defaultness.set_some(def_id.index, impl_defaultness); |
476ff2be | 1439 | let trait_item = tcx.associated_item(def_id); |
9ffffee4 | 1440 | self.tables.assoc_container.set_some(def_id.index, trait_item.container); |
476ff2be | 1441 | |
5869c6ff | 1442 | match trait_item.kind { |
f2b60f7d | 1443 | ty::AssocKind::Const => {} |
ba9703b0 | 1444 | ty::AssocKind::Fn => { |
9c376795 | 1445 | record_array!(self.tables.fn_arg_names[def_id] <- tcx.fn_arg_names(def_id)); |
9ffffee4 FG |
1446 | self.tables.asyncness.set_some(def_id.index, tcx.asyncness(def_id)); |
1447 | self.tables.constness.set_some(def_id.index, hir::Constness::NotConst); | |
9e0c209e | 1448 | } |
29967ef6 XL |
1449 | ty::AssocKind::Type => { |
1450 | self.encode_explicit_item_bounds(def_id); | |
e74abb32 | 1451 | } |
e74abb32 | 1452 | } |
ba9703b0 XL |
1453 | if trait_item.kind == ty::AssocKind::Fn { |
1454 | record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); | |
1a4d82fc | 1455 | } |
353b0b11 FG |
1456 | if let Some(rpitit_info) = trait_item.opt_rpitit_info { |
1457 | let rpitit_info = self.lazy(rpitit_info); | |
1458 | self.tables.opt_rpitit_info.set_some(def_id.index, rpitit_info); | |
1459 | } | |
83c7162d XL |
1460 | } |
1461 | ||
e74abb32 | 1462 | fn encode_info_for_impl_item(&mut self, def_id: DefId) { |
dc9dc135 | 1463 | debug!("EncodeContext::encode_info_for_impl_item({:?})", def_id); |
041b39d2 XL |
1464 | let tcx = self.tcx; |
1465 | ||
353b0b11 FG |
1466 | let defaultness = self.tcx.impl_defaultness(def_id.expect_local()); |
1467 | self.tables.impl_defaultness.set_some(def_id.index, defaultness); | |
476ff2be | 1468 | let impl_item = self.tcx.associated_item(def_id); |
9ffffee4 | 1469 | self.tables.assoc_container.set_some(def_id.index, impl_item.container); |
d9579d0f | 1470 | |
5869c6ff | 1471 | match impl_item.kind { |
ba9703b0 | 1472 | ty::AssocKind::Fn => { |
49aad941 FG |
1473 | let (sig, body) = |
1474 | self.tcx.hir().expect_impl_item(def_id.expect_local()).expect_fn(); | |
9ffffee4 | 1475 | self.tables.asyncness.set_some(def_id.index, sig.header.asyncness); |
923072b8 | 1476 | record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)); |
5e7ed085 FG |
1477 | // Can be inside `impl const Trait`, so using sig.header.constness is not reliable |
1478 | let constness = if self.tcx.is_const_fn_raw(def_id) { | |
1479 | hir::Constness::Const | |
9e0c209e | 1480 | } else { |
5e7ed085 | 1481 | hir::Constness::NotConst |
9e0c209e | 1482 | }; |
9ffffee4 | 1483 | self.tables.constness.set_some(def_id.index, constness); |
5869c6ff | 1484 | } |
f2b60f7d | 1485 | ty::AssocKind::Const | ty::AssocKind::Type => {} |
5869c6ff | 1486 | } |
5099ac24 | 1487 | if let Some(trait_item_def_id) = impl_item.trait_item_def_id { |
9ffffee4 | 1488 | self.tables.trait_item_def_id.set_some(def_id.index, trait_item_def_id.into()); |
5099ac24 | 1489 | } |
ba9703b0 XL |
1490 | if impl_item.kind == ty::AssocKind::Fn { |
1491 | record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); | |
9ffffee4 | 1492 | self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id)); |
e74abb32 | 1493 | } |
353b0b11 FG |
1494 | if let Some(rpitit_info) = impl_item.opt_rpitit_info { |
1495 | let rpitit_info = self.lazy(rpitit_info); | |
1496 | self.tables.opt_rpitit_info.set_some(def_id.index, rpitit_info); | |
1497 | } | |
223e47cc | 1498 | } |
1a4d82fc | 1499 | |
5869c6ff XL |
1500 | fn encode_mir(&mut self) { |
1501 | if self.is_proc_macro { | |
1502 | return; | |
1503 | } | |
3dfed10e | 1504 | |
f2b60f7d FG |
1505 | let tcx = self.tcx; |
1506 | ||
1507 | let keys_and_jobs = tcx.mir_keys(()).iter().filter_map(|&def_id| { | |
1508 | let (encode_const, encode_opt) = should_encode_mir(tcx, def_id); | |
1509 | if encode_const || encode_opt { Some((def_id, encode_const, encode_opt)) } else { None } | |
1510 | }); | |
1511 | for (def_id, encode_const, encode_opt) in keys_and_jobs { | |
5869c6ff XL |
1512 | debug_assert!(encode_const || encode_opt); |
1513 | ||
1514 | debug!("EntryBuilder::encode_mir({:?})", def_id); | |
1515 | if encode_opt { | |
f2b60f7d | 1516 | record!(self.tables.optimized_mir[def_id.to_def_id()] <- tcx.optimized_mir(def_id)); |
9ffffee4 | 1517 | |
49aad941 FG |
1518 | if tcx.sess.opts.unstable_opts.drop_tracking_mir |
1519 | && let DefKind::Generator = self.tcx.def_kind(def_id) | |
1520 | && let Some(witnesses) = tcx.mir_generator_witnesses(def_id) | |
1521 | { | |
1522 | record!(self.tables.mir_generator_witnesses[def_id.to_def_id()] <- witnesses); | |
9ffffee4 | 1523 | } |
3dfed10e | 1524 | } |
5869c6ff | 1525 | if encode_const { |
f2b60f7d | 1526 | record!(self.tables.mir_for_ctfe[def_id.to_def_id()] <- tcx.mir_for_ctfe(def_id)); |
1b1a35ee | 1527 | |
c295e0f8 | 1528 | // FIXME(generic_const_exprs): this feels wrong to have in `encode_mir` |
f2b60f7d | 1529 | let abstract_const = tcx.thir_abstract_const(def_id); |
5869c6ff | 1530 | if let Ok(Some(abstract_const)) = abstract_const { |
5e7ed085 | 1531 | record!(self.tables.thir_abstract_const[def_id.to_def_id()] <- abstract_const); |
5869c6ff | 1532 | } |
f2b60f7d FG |
1533 | |
1534 | if should_encode_const(tcx.def_kind(def_id)) { | |
1535 | let qualifs = tcx.mir_const_qualif(def_id); | |
1536 | record!(self.tables.mir_const_qualif[def_id.to_def_id()] <- qualifs); | |
1537 | let body_id = tcx.hir().maybe_body_owned_by(def_id); | |
1538 | if let Some(body_id) = body_id { | |
1539 | let const_data = self.encode_rendered_const_for_body(body_id); | |
1540 | record!(self.tables.rendered_const[def_id.to_def_id()] <- const_data); | |
1541 | } | |
1542 | } | |
1b1a35ee | 1543 | } |
f2b60f7d | 1544 | record!(self.tables.promoted_mir[def_id.to_def_id()] <- tcx.promoted_mir(def_id)); |
5869c6ff | 1545 | |
49aad941 | 1546 | let instance = ty::InstanceDef::Item(def_id.to_def_id()); |
f2b60f7d | 1547 | let unused = tcx.unused_generic_params(instance); |
353b0b11 | 1548 | self.tables.unused_generic_params.set(def_id.local_def_index, unused); |
7cac9316 | 1549 | } |
2b03887a FG |
1550 | |
1551 | // Encode all the deduced parameter attributes for everything that has MIR, even for items | |
1552 | // that can't be inlined. But don't if we aren't optimizing in non-incremental mode, to | |
1553 | // save the query traffic. | |
1554 | if tcx.sess.opts.output_types.should_codegen() | |
1555 | && tcx.sess.opts.optimize != OptLevel::No | |
1556 | && tcx.sess.opts.incremental.is_none() | |
1557 | { | |
1558 | for &local_def_id in tcx.mir_keys(()) { | |
1559 | if let DefKind::AssocFn | DefKind::Fn = tcx.def_kind(local_def_id) { | |
1560 | record_array!(self.tables.deduced_param_attrs[local_def_id.to_def_id()] <- | |
1561 | self.tcx.deduced_param_attrs(local_def_id.to_def_id())); | |
1562 | } | |
1563 | } | |
1564 | } | |
9cc50fc6 | 1565 | } |
223e47cc | 1566 | |
e74abb32 | 1567 | fn encode_stability(&mut self, def_id: DefId) { |
dc9dc135 | 1568 | debug!("EncodeContext::encode_stability({:?})", def_id); |
1b1a35ee XL |
1569 | |
1570 | // The query lookup can take a measurable amount of time in crates with many items. Check if | |
1571 | // the stability attributes are even enabled before using their queries. | |
064997fb | 1572 | if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked { |
1b1a35ee | 1573 | if let Some(stab) = self.tcx.lookup_stability(def_id) { |
5e7ed085 | 1574 | record!(self.tables.lookup_stability[def_id] <- stab) |
1b1a35ee | 1575 | } |
e74abb32 | 1576 | } |
9e0c209e | 1577 | } |
1a4d82fc | 1578 | |
60c5eb7d XL |
1579 | fn encode_const_stability(&mut self, def_id: DefId) { |
1580 | debug!("EncodeContext::encode_const_stability({:?})", def_id); | |
1b1a35ee XL |
1581 | |
1582 | // The query lookup can take a measurable amount of time in crates with many items. Check if | |
1583 | // the stability attributes are even enabled before using their queries. | |
064997fb | 1584 | if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked { |
1b1a35ee | 1585 | if let Some(stab) = self.tcx.lookup_const_stability(def_id) { |
5e7ed085 | 1586 | record!(self.tables.lookup_const_stability[def_id] <- stab) |
1b1a35ee | 1587 | } |
60c5eb7d XL |
1588 | } |
1589 | } | |
1590 | ||
f2b60f7d FG |
1591 | fn encode_default_body_stability(&mut self, def_id: DefId) { |
1592 | debug!("EncodeContext::encode_default_body_stability({:?})", def_id); | |
1593 | ||
1594 | // The query lookup can take a measurable amount of time in crates with many items. Check if | |
1595 | // the stability attributes are even enabled before using their queries. | |
1596 | if self.feat.staged_api || self.tcx.sess.opts.unstable_opts.force_unstable_if_unmarked { | |
1597 | if let Some(stab) = self.tcx.lookup_default_body_stability(def_id) { | |
1598 | record!(self.tables.lookup_default_body_stability[def_id] <- stab) | |
1599 | } | |
1600 | } | |
1601 | } | |
1602 | ||
e74abb32 | 1603 | fn encode_deprecation(&mut self, def_id: DefId) { |
dc9dc135 | 1604 | debug!("EncodeContext::encode_deprecation({:?})", def_id); |
e74abb32 | 1605 | if let Some(depr) = self.tcx.lookup_deprecation(def_id) { |
5e7ed085 | 1606 | record!(self.tables.lookup_deprecation_entry[def_id] <- depr); |
e74abb32 | 1607 | } |
9e0c209e | 1608 | } |
9cc50fc6 | 1609 | |
5e7ed085 | 1610 | fn encode_rendered_const_for_body(&mut self, body_id: hir::BodyId) -> String { |
ba9703b0 XL |
1611 | let hir = self.tcx.hir(); |
1612 | let body = hir.body(body_id); | |
5e7ed085 | 1613 | rustc_hir_pretty::to_string(&(&hir as &dyn intravisit::Map<'_>), |s| { |
ba9703b0 | 1614 | s.print_expr(&body.value) |
5e7ed085 | 1615 | }) |
83c7162d XL |
1616 | } |
1617 | ||
353b0b11 | 1618 | fn encode_info_for_item(&mut self, item: &'tcx hir::Item<'tcx>) { |
9e0c209e | 1619 | let tcx = self.tcx; |
353b0b11 | 1620 | let def_id = item.owner_id.to_def_id(); |
dc9dc135 | 1621 | debug!("EncodeContext::encode_info_for_item({:?})", def_id); |
9e0c209e | 1622 | |
353b0b11 | 1623 | let record_associated_item_def_ids = |this: &mut Self, def_ids: &[DefId]| { |
49aad941 | 1624 | record_array!(this.tables.associated_item_or_field_def_ids[def_id] <- def_ids.iter().map(|&def_id| { |
353b0b11 FG |
1625 | assert!(def_id.is_local()); |
1626 | def_id.index | |
1627 | })) | |
1628 | }; | |
1629 | ||
f2b60f7d | 1630 | match item.kind { |
60c5eb7d | 1631 | hir::ItemKind::Fn(ref sig, .., body) => { |
9ffffee4 | 1632 | self.tables.asyncness.set_some(def_id.index, sig.header.asyncness); |
923072b8 | 1633 | record_array!(self.tables.fn_arg_names[def_id] <- self.tcx.hir().body_param_names(body)); |
9ffffee4 | 1634 | self.tables.constness.set_some(def_id.index, sig.header.constness); |
353b0b11 FG |
1635 | record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); |
1636 | self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id)); | |
b039eaaf | 1637 | } |
5e7ed085 | 1638 | hir::ItemKind::Macro(ref macro_def, _) => { |
9ffffee4 | 1639 | self.tables.is_macro_rules.set(def_id.index, macro_def.macro_rules); |
f2b60f7d | 1640 | record!(self.tables.macro_definition[def_id] <- &*macro_def.body); |
94222f64 | 1641 | } |
353b0b11 FG |
1642 | hir::ItemKind::Mod(..) => { |
1643 | self.encode_info_for_mod(item.owner_id.def_id); | |
9e0c209e | 1644 | } |
487cf647 | 1645 | hir::ItemKind::OpaqueTy(ref opaque) => { |
29967ef6 | 1646 | self.encode_explicit_item_bounds(def_id); |
49aad941 FG |
1647 | self.tables.is_type_alias_impl_trait.set( |
1648 | def_id.index, | |
1649 | matches!(opaque.origin, hir::OpaqueTyOrigin::TyAlias { .. }), | |
1650 | ); | |
9e0c209e | 1651 | } |
136023e0 | 1652 | hir::ItemKind::Impl(hir::Impl { defaultness, constness, .. }) => { |
9ffffee4 FG |
1653 | self.tables.impl_defaultness.set_some(def_id.index, *defaultness); |
1654 | self.tables.constness.set_some(def_id.index, *constness); | |
353b0b11 FG |
1655 | self.tables.impl_polarity.set_some(def_id.index, self.tcx.impl_polarity(def_id)); |
1656 | ||
1657 | if let Some(trait_ref) = self.tcx.impl_trait_ref(def_id) { | |
1658 | record!(self.tables.impl_trait_ref[def_id] <- trait_ref); | |
5e7ed085 | 1659 | |
9ffffee4 | 1660 | let trait_ref = trait_ref.skip_binder(); |
e74abb32 | 1661 | let trait_def = self.tcx.trait_def(trait_ref.def_id); |
9c376795 | 1662 | if let Ok(mut an) = trait_def.ancestors(self.tcx, def_id) { |
5e7ed085 | 1663 | if let Some(specialization_graph::Node::Impl(parent)) = an.nth(1) { |
9ffffee4 | 1664 | self.tables.impl_parent.set_some(def_id.index, parent.into()); |
5e7ed085 FG |
1665 | } |
1666 | } | |
b039eaaf | 1667 | |
5e7ed085 FG |
1668 | // if this is an impl of `CoerceUnsized`, create its |
1669 | // "unsized info", else just store None | |
1670 | if Some(trait_ref.def_id) == self.tcx.lang_items().coerce_unsized_trait() { | |
1671 | let coerce_unsized_info = | |
1672 | self.tcx.at(item.span).coerce_unsized_info(def_id); | |
1673 | record!(self.tables.coerce_unsized_info[def_id] <- coerce_unsized_info); | |
5869c6ff | 1674 | } |
5e7ed085 | 1675 | } |
5869c6ff | 1676 | |
353b0b11 FG |
1677 | let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id); |
1678 | record_associated_item_def_ids(self, associated_item_def_ids); | |
1679 | for &trait_item_def_id in associated_item_def_ids { | |
1680 | self.encode_info_for_impl_item(trait_item_def_id); | |
1681 | } | |
9e0c209e | 1682 | } |
9fa01778 | 1683 | hir::ItemKind::Trait(..) => { |
353b0b11 FG |
1684 | record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id)); |
1685 | ||
49aad941 FG |
1686 | let module_children = tcx.module_children_local(item.owner_id.def_id); |
1687 | record_array!(self.tables.module_children_non_reexports[def_id] <- | |
1688 | module_children.iter().map(|child| child.res.def_id().index)); | |
1689 | ||
353b0b11 FG |
1690 | let associated_item_def_ids = self.tcx.associated_item_def_ids(def_id); |
1691 | record_associated_item_def_ids(self, associated_item_def_ids); | |
1692 | for &item_def_id in associated_item_def_ids { | |
1693 | self.encode_info_for_trait_item(item_def_id); | |
1694 | } | |
5e7ed085 FG |
1695 | } |
1696 | hir::ItemKind::TraitAlias(..) => { | |
353b0b11 | 1697 | record!(self.tables.trait_def[def_id] <- self.tcx.trait_def(def_id)); |
d9579d0f | 1698 | } |
353b0b11 FG |
1699 | hir::ItemKind::ExternCrate(_) |
1700 | | hir::ItemKind::Use(..) | |
1701 | | hir::ItemKind::Static(..) | |
f2b60f7d | 1702 | | hir::ItemKind::Const(..) |
9ffffee4 FG |
1703 | | hir::ItemKind::Enum(..) |
1704 | | hir::ItemKind::Struct(..) | |
1705 | | hir::ItemKind::Union(..) | |
f2b60f7d FG |
1706 | | hir::ItemKind::ForeignMod { .. } |
1707 | | hir::ItemKind::GlobalAsm(..) | |
1708 | | hir::ItemKind::TyAlias(..) => {} | |
223e47cc | 1709 | } |
223e47cc | 1710 | } |
223e47cc | 1711 | |
487cf647 FG |
1712 | #[instrument(level = "debug", skip(self))] |
1713 | fn encode_info_for_closure(&mut self, def_id: LocalDefId) { | |
e74abb32 | 1714 | // NOTE(eddyb) `tcx.type_of(def_id)` isn't used because it's fully generic, |
9ffffee4 | 1715 | // including on the signature, which is inferred in `typeck`. |
04454e1e | 1716 | let typeck_result: &'tcx ty::TypeckResults<'tcx> = self.tcx.typeck(def_id); |
487cf647 | 1717 | let hir_id = self.tcx.hir().local_def_id_to_hir_id(def_id); |
04454e1e | 1718 | let ty = typeck_result.node_type(hir_id); |
5869c6ff | 1719 | match ty.kind() { |
74b04a01 XL |
1720 | ty::Generator(..) => { |
1721 | let data = self.tcx.generator_kind(def_id).unwrap(); | |
04454e1e | 1722 | let generator_diagnostic_data = typeck_result.get_generator_diagnostic_data(); |
5e7ed085 | 1723 | record!(self.tables.generator_kind[def_id.to_def_id()] <- data); |
04454e1e | 1724 | record!(self.tables.generator_diagnostic_data[def_id.to_def_id()] <- generator_diagnostic_data); |
ff7c6d11 XL |
1725 | } |
1726 | ||
f2b60f7d | 1727 | ty::Closure(_, substs) => { |
9c376795 | 1728 | let constness = self.tcx.constness(def_id.to_def_id()); |
9ffffee4 FG |
1729 | self.tables.constness.set_some(def_id.to_def_id().index, constness); |
1730 | record!(self.tables.fn_sig[def_id.to_def_id()] <- ty::EarlyBinder(substs.as_closure().sig())); | |
5869c6ff | 1731 | } |
223e47cc | 1732 | |
e74abb32 | 1733 | _ => bug!("closure that is neither generator nor closure"), |
5869c6ff | 1734 | } |
cc61c64b | 1735 | } |
cc61c64b | 1736 | |
923072b8 | 1737 | fn encode_native_libraries(&mut self) -> LazyArray<NativeLib> { |
1b1a35ee | 1738 | empty_proc_macro!(self); |
ea8adc8c | 1739 | let used_libraries = self.tcx.native_libraries(LOCAL_CRATE); |
923072b8 | 1740 | self.lazy_array(used_libraries.iter()) |
1a4d82fc | 1741 | } |
1a4d82fc | 1742 | |
923072b8 | 1743 | fn encode_foreign_modules(&mut self) -> LazyArray<ForeignModule> { |
1b1a35ee | 1744 | empty_proc_macro!(self); |
0531ce1d | 1745 | let foreign_modules = self.tcx.foreign_modules(LOCAL_CRATE); |
923072b8 | 1746 | self.lazy_array(foreign_modules.iter().map(|(_, m)| m).cloned()) |
e1599b0c XL |
1747 | } |
1748 | ||
136023e0 | 1749 | fn encode_hygiene(&mut self) -> (SyntaxContextTable, ExpnDataTable, ExpnHashTable) { |
3dfed10e XL |
1750 | let mut syntax_contexts: TableBuilder<_, _> = Default::default(); |
1751 | let mut expn_data_table: TableBuilder<_, _> = Default::default(); | |
136023e0 | 1752 | let mut expn_hash_table: TableBuilder<_, _> = Default::default(); |
3dfed10e | 1753 | |
923072b8 | 1754 | self.hygiene_ctxt.encode( |
136023e0 XL |
1755 | &mut (&mut *self, &mut syntax_contexts, &mut expn_data_table, &mut expn_hash_table), |
1756 | |(this, syntax_contexts, _, _), index, ctxt_data| { | |
9ffffee4 | 1757 | syntax_contexts.set_some(index, this.lazy(ctxt_data)); |
3dfed10e | 1758 | }, |
136023e0 XL |
1759 | |(this, _, expn_data_table, expn_hash_table), index, expn_data, hash| { |
1760 | if let Some(index) = index.as_local() { | |
9ffffee4 FG |
1761 | expn_data_table.set_some(index.as_raw(), this.lazy(expn_data)); |
1762 | expn_hash_table.set_some(index.as_raw(), this.lazy(hash)); | |
136023e0 | 1763 | } |
3dfed10e XL |
1764 | }, |
1765 | ); | |
1766 | ||
136023e0 XL |
1767 | ( |
1768 | syntax_contexts.encode(&mut self.opaque), | |
1769 | expn_data_table.encode(&mut self.opaque), | |
1770 | expn_hash_table.encode(&mut self.opaque), | |
1771 | ) | |
3dfed10e XL |
1772 | } |
1773 | ||
1b1a35ee | 1774 | fn encode_proc_macros(&mut self) -> Option<ProcMacroData> { |
f9f354fc | 1775 | let is_proc_macro = self.tcx.sess.crate_types().contains(&CrateType::ProcMacro); |
e1599b0c XL |
1776 | if is_proc_macro { |
1777 | let tcx = self.tcx; | |
1b1a35ee XL |
1778 | let hir = tcx.hir(); |
1779 | ||
17df50a5 | 1780 | let proc_macro_decls_static = tcx.proc_macro_decls_static(()).unwrap().local_def_index; |
04454e1e | 1781 | let stability = tcx.lookup_stability(CRATE_DEF_ID); |
94222f64 | 1782 | let macros = |
923072b8 | 1783 | self.lazy_array(tcx.resolutions(()).proc_macros.iter().map(|p| p.local_def_index)); |
353b0b11 | 1784 | for (i, span) in self.tcx.sess.parse_sess.proc_macro_quoted_spans() { |
17df50a5 | 1785 | let span = self.lazy(span); |
9ffffee4 | 1786 | self.tables.proc_macro_quoted_spans.set_some(i, span); |
17df50a5 | 1787 | } |
1b1a35ee | 1788 | |
9ffffee4 | 1789 | self.tables.opt_def_kind.set_some(LOCAL_CRATE.as_def_id().index, DefKind::Mod); |
5e7ed085 | 1790 | record!(self.tables.def_span[LOCAL_CRATE.as_def_id()] <- tcx.def_span(LOCAL_CRATE.as_def_id())); |
04454e1e | 1791 | self.encode_attrs(LOCAL_CRATE.as_def_id().expect_local()); |
f2b60f7d FG |
1792 | let vis = tcx.local_visibility(CRATE_DEF_ID).map_id(|def_id| def_id.local_def_index); |
1793 | record!(self.tables.visibility[LOCAL_CRATE.as_def_id()] <- vis); | |
5869c6ff | 1794 | if let Some(stability) = stability { |
5e7ed085 | 1795 | record!(self.tables.lookup_stability[LOCAL_CRATE.as_def_id()] <- stability); |
5869c6ff XL |
1796 | } |
1797 | self.encode_deprecation(LOCAL_CRATE.as_def_id()); | |
9ffffee4 FG |
1798 | if let Some(res_map) = tcx.resolutions(()).doc_link_resolutions.get(&CRATE_DEF_ID) { |
1799 | record!(self.tables.doc_link_resolutions[LOCAL_CRATE.as_def_id()] <- res_map); | |
1800 | } | |
1801 | if let Some(traits) = tcx.resolutions(()).doc_link_traits_in_scope.get(&CRATE_DEF_ID) { | |
1802 | record_array!(self.tables.doc_link_traits_in_scope[LOCAL_CRATE.as_def_id()] <- traits); | |
1803 | } | |
5869c6ff | 1804 | |
1b1a35ee XL |
1805 | // Normally, this information is encoded when we walk the items |
1806 | // defined in this crate. However, we skip doing that for proc-macro crates, | |
1807 | // so we manually encode just the information that we need | |
94222f64 XL |
1808 | for &proc_macro in &tcx.resolutions(()).proc_macros { |
1809 | let id = proc_macro; | |
1810 | let proc_macro = hir.local_def_id_to_hir_id(proc_macro); | |
1811 | let mut name = hir.name(proc_macro); | |
1812 | let span = hir.span(proc_macro); | |
1b1a35ee XL |
1813 | // Proc-macros may have attributes like `#[allow_internal_unstable]`, |
1814 | // so downstream crates need access to them. | |
94222f64 | 1815 | let attrs = hir.attrs(proc_macro); |
353b0b11 | 1816 | let macro_kind = if attr::contains_name(attrs, sym::proc_macro) { |
fc512014 | 1817 | MacroKind::Bang |
353b0b11 | 1818 | } else if attr::contains_name(attrs, sym::proc_macro_attribute) { |
fc512014 | 1819 | MacroKind::Attr |
353b0b11 | 1820 | } else if let Some(attr) = attr::find_by_name(attrs, sym::proc_macro_derive) { |
fc512014 XL |
1821 | // This unwrap chain should have been checked by the proc-macro harness. |
1822 | name = attr.meta_item_list().unwrap()[0] | |
1823 | .meta_item() | |
1824 | .unwrap() | |
1825 | .ident() | |
1826 | .unwrap() | |
1827 | .name; | |
1828 | MacroKind::Derive | |
1829 | } else { | |
1830 | bug!("Unknown proc-macro type for item {:?}", id); | |
1831 | }; | |
1832 | ||
94222f64 | 1833 | let mut def_key = self.tcx.hir().def_key(id); |
fc512014 XL |
1834 | def_key.disambiguated_data.data = DefPathData::MacroNs(name); |
1835 | ||
94222f64 | 1836 | let def_id = id.to_def_id(); |
9ffffee4 FG |
1837 | self.tables.opt_def_kind.set_some(def_id.index, DefKind::Macro(macro_kind)); |
1838 | self.tables.proc_macro.set_some(def_id.index, macro_kind); | |
04454e1e | 1839 | self.encode_attrs(id); |
fc512014 | 1840 | record!(self.tables.def_keys[def_id] <- def_key); |
5e7ed085 FG |
1841 | record!(self.tables.def_ident_span[def_id] <- span); |
1842 | record!(self.tables.def_span[def_id] <- span); | |
fc512014 XL |
1843 | record!(self.tables.visibility[def_id] <- ty::Visibility::Public); |
1844 | if let Some(stability) = stability { | |
5e7ed085 | 1845 | record!(self.tables.lookup_stability[def_id] <- stability); |
fc512014 | 1846 | } |
1b1a35ee XL |
1847 | } |
1848 | ||
1849 | Some(ProcMacroData { proc_macro_decls_static, stability, macros }) | |
e1599b0c XL |
1850 | } else { |
1851 | None | |
1852 | } | |
0531ce1d XL |
1853 | } |
1854 | ||
923072b8 | 1855 | fn encode_debugger_visualizers(&mut self) -> LazyArray<DebuggerVisualizerFile> { |
04454e1e | 1856 | empty_proc_macro!(self); |
49aad941 FG |
1857 | self.lazy_array( |
1858 | self.tcx | |
1859 | .debugger_visualizers(LOCAL_CRATE) | |
1860 | .iter() | |
1861 | // Erase the path since it may contain privacy sensitive data | |
1862 | // that we don't want to end up in crate metadata. | |
1863 | // The path is only needed for the local crate because of | |
1864 | // `--emit dep-info`. | |
1865 | .map(DebuggerVisualizerFile::path_erased), | |
1866 | ) | |
04454e1e FG |
1867 | } |
1868 | ||
923072b8 | 1869 | fn encode_crate_deps(&mut self) -> LazyArray<CrateDep> { |
1b1a35ee | 1870 | empty_proc_macro!(self); |
9e0c209e | 1871 | |
c295e0f8 XL |
1872 | let deps = self |
1873 | .tcx | |
1874 | .crates(()) | |
7cac9316 XL |
1875 | .iter() |
1876 | .map(|&cnum| { | |
1877 | let dep = CrateDep { | |
17df50a5 | 1878 | name: self.tcx.crate_name(cnum), |
ea8adc8c | 1879 | hash: self.tcx.crate_hash(cnum), |
e74abb32 | 1880 | host_hash: self.tcx.crate_host_hash(cnum), |
ea8adc8c | 1881 | kind: self.tcx.dep_kind(cnum), |
5099ac24 | 1882 | extra_filename: self.tcx.extra_filename(cnum).clone(), |
7cac9316 XL |
1883 | }; |
1884 | (cnum, dep) | |
1885 | }) | |
1886 | .collect::<Vec<_>>(); | |
1887 | ||
7cac9316 | 1888 | { |
9e0c209e SL |
1889 | // Sanity-check the crate numbers |
1890 | let mut expected_cnum = 1; | |
1891 | for &(n, _) in &deps { | |
1892 | assert_eq!(n, CrateNum::new(expected_cnum)); | |
1893 | expected_cnum += 1; | |
1a4d82fc JJ |
1894 | } |
1895 | } | |
1a4d82fc | 1896 | |
9e0c209e SL |
1897 | // We're just going to write a list of crate 'name-hash-version's, with |
1898 | // the assumption that they are numbered 1 to n. | |
1899 | // FIXME (#2166): This is not nearly enough to support correct versioning | |
1900 | // but is enough to get transitive crate dependencies working. | |
9c376795 | 1901 | self.lazy_array(deps.iter().map(|(_, dep)| dep)) |
223e47cc | 1902 | } |
1a4d82fc | 1903 | |
923072b8 | 1904 | fn encode_lib_features(&mut self) -> LazyArray<(Symbol, Option<Symbol>)> { |
1b1a35ee | 1905 | empty_proc_macro!(self); |
b7449926 | 1906 | let tcx = self.tcx; |
a2a8927a | 1907 | let lib_features = tcx.lib_features(()); |
923072b8 | 1908 | self.lazy_array(lib_features.to_vec()) |
e1599b0c XL |
1909 | } |
1910 | ||
064997fb FG |
1911 | fn encode_stability_implications(&mut self) -> LazyArray<(Symbol, Symbol)> { |
1912 | empty_proc_macro!(self); | |
1913 | let tcx = self.tcx; | |
1914 | let implications = tcx.stability_implications(LOCAL_CRATE); | |
1915 | self.lazy_array(implications.iter().map(|(k, v)| (*k, *v))) | |
1916 | } | |
1917 | ||
923072b8 | 1918 | fn encode_diagnostic_items(&mut self) -> LazyArray<(Symbol, DefIndex)> { |
1b1a35ee | 1919 | empty_proc_macro!(self); |
e1599b0c | 1920 | let tcx = self.tcx; |
c295e0f8 | 1921 | let diagnostic_items = &tcx.diagnostic_items(LOCAL_CRATE).name_to_id; |
923072b8 | 1922 | self.lazy_array(diagnostic_items.iter().map(|(&name, def_id)| (name, def_id.index))) |
b7449926 XL |
1923 | } |
1924 | ||
487cf647 | 1925 | fn encode_lang_items(&mut self) -> LazyArray<(DefIndex, LangItem)> { |
1b1a35ee | 1926 | empty_proc_macro!(self); |
487cf647 FG |
1927 | let lang_items = self.tcx.lang_items().iter(); |
1928 | self.lazy_array(lang_items.filter_map(|(lang_item, def_id)| { | |
1929 | def_id.as_local().map(|id| (id.local_def_index, lang_item)) | |
7cac9316 | 1930 | })) |
1a4d82fc | 1931 | } |
476ff2be | 1932 | |
487cf647 | 1933 | fn encode_lang_items_missing(&mut self) -> LazyArray<LangItem> { |
1b1a35ee | 1934 | empty_proc_macro!(self); |
7cac9316 | 1935 | let tcx = self.tcx; |
923072b8 | 1936 | self.lazy_array(&tcx.lang_items().missing) |
476ff2be | 1937 | } |
1a4d82fc | 1938 | |
923072b8 | 1939 | fn encode_traits(&mut self) -> LazyArray<DefIndex> { |
a2a8927a | 1940 | empty_proc_macro!(self); |
49aad941 | 1941 | self.lazy_array(self.tcx.traits(LOCAL_CRATE).iter().map(|def_id| def_id.index)) |
a2a8927a XL |
1942 | } |
1943 | ||
9e0c209e | 1944 | /// Encodes an index, mapping each trait to its (local) implementations. |
923072b8 | 1945 | fn encode_impls(&mut self) -> LazyArray<TraitImpls> { |
a2a8927a | 1946 | debug!("EncodeContext::encode_traits_and_impls()"); |
1b1a35ee | 1947 | empty_proc_macro!(self); |
7cac9316 | 1948 | let tcx = self.tcx; |
04454e1e FG |
1949 | let mut fx_hash_map: FxHashMap<DefId, Vec<(DefIndex, Option<SimplifiedType>)>> = |
1950 | FxHashMap::default(); | |
1a4d82fc | 1951 | |
04454e1e | 1952 | for id in tcx.hir().items() { |
9ffffee4 | 1953 | if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) { |
2b03887a | 1954 | if let Some(trait_ref) = tcx.impl_trait_ref(id.owner_id) { |
9c376795 FG |
1955 | let trait_ref = trait_ref.subst_identity(); |
1956 | ||
04454e1e FG |
1957 | let simplified_self_ty = fast_reject::simplify_type( |
1958 | self.tcx, | |
1959 | trait_ref.self_ty(), | |
353b0b11 | 1960 | TreatParams::AsCandidateKey, |
04454e1e FG |
1961 | ); |
1962 | ||
1963 | fx_hash_map | |
1964 | .entry(trait_ref.def_id) | |
1965 | .or_default() | |
2b03887a | 1966 | .push((id.owner_id.def_id.local_def_index, simplified_self_ty)); |
04454e1e FG |
1967 | } |
1968 | } | |
1969 | } | |
1970 | ||
1971 | let mut all_impls: Vec<_> = fx_hash_map.into_iter().collect(); | |
7cac9316 XL |
1972 | |
1973 | // Bring everything into deterministic order for hashing | |
dfeec247 | 1974 | all_impls.sort_by_cached_key(|&(trait_def_id, _)| tcx.def_path_hash(trait_def_id)); |
7cac9316 XL |
1975 | |
1976 | let all_impls: Vec<_> = all_impls | |
c30ab7b3 | 1977 | .into_iter() |
7cac9316 XL |
1978 | .map(|(trait_def_id, mut impls)| { |
1979 | // Bring everything into deterministic order for hashing | |
3dfed10e | 1980 | impls.sort_by_cached_key(|&(index, _)| { |
136023e0 | 1981 | tcx.hir().def_path_hash(LocalDefId { local_def_index: index }) |
7cac9316 XL |
1982 | }); |
1983 | ||
c30ab7b3 SL |
1984 | TraitImpls { |
1985 | trait_id: (trait_def_id.krate.as_u32(), trait_def_id.index), | |
923072b8 | 1986 | impls: self.lazy_array(&impls), |
c30ab7b3 SL |
1987 | } |
1988 | }) | |
1989 | .collect(); | |
1a4d82fc | 1990 | |
923072b8 | 1991 | self.lazy_array(&all_impls) |
1a4d82fc | 1992 | } |
970d7e83 | 1993 | |
923072b8 | 1994 | fn encode_incoherent_impls(&mut self) -> LazyArray<IncoherentImpls> { |
5e7ed085 FG |
1995 | debug!("EncodeContext::encode_traits_and_impls()"); |
1996 | empty_proc_macro!(self); | |
1997 | let tcx = self.tcx; | |
5e7ed085 | 1998 | let mut all_impls: Vec<_> = tcx.crate_inherent_impls(()).incoherent_impls.iter().collect(); |
064997fb FG |
1999 | tcx.with_stable_hashing_context(|mut ctx| { |
2000 | all_impls.sort_by_cached_key(|&(&simp, _)| { | |
2001 | let mut hasher = StableHasher::new(); | |
2002 | simp.hash_stable(&mut ctx, &mut hasher); | |
2003 | hasher.finish::<Fingerprint>() | |
2004 | }) | |
5e7ed085 FG |
2005 | }); |
2006 | let all_impls: Vec<_> = all_impls | |
2007 | .into_iter() | |
2008 | .map(|(&simp, impls)| { | |
2009 | let mut impls: Vec<_> = | |
2010 | impls.into_iter().map(|def_id| def_id.local_def_index).collect(); | |
2011 | impls.sort_by_cached_key(|&local_def_index| { | |
2012 | tcx.hir().def_path_hash(LocalDefId { local_def_index }) | |
2013 | }); | |
2014 | ||
923072b8 | 2015 | IncoherentImpls { self_ty: simp, impls: self.lazy_array(impls) } |
5e7ed085 FG |
2016 | }) |
2017 | .collect(); | |
2018 | ||
923072b8 | 2019 | self.lazy_array(&all_impls) |
5e7ed085 FG |
2020 | } |
2021 | ||
476ff2be | 2022 | // Encodes all symbols exported from this crate into the metadata. |
9e0c209e SL |
2023 | // |
2024 | // This pass is seeded off the reachability list calculated in the | |
2025 | // middle::reachable module but filters out items that either don't have a | |
2026 | // symbol associated with them (they weren't translated) or if they're an FFI | |
2027 | // definition (as that's not defined in this crate). | |
dfeec247 XL |
2028 | fn encode_exported_symbols( |
2029 | &mut self, | |
04454e1e | 2030 | exported_symbols: &[(ExportedSymbol<'tcx>, SymbolExportInfo)], |
923072b8 | 2031 | ) -> LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)> { |
1b1a35ee | 2032 | empty_proc_macro!(self); |
0531ce1d XL |
2033 | // The metadata symbol name is special. It should not show up in |
2034 | // downstream crates. | |
3dfed10e | 2035 | let metadata_symbol_name = SymbolName::new(self.tcx, &metadata_symbol_name(self.tcx)); |
0531ce1d | 2036 | |
923072b8 | 2037 | self.lazy_array( |
dfeec247 XL |
2038 | exported_symbols |
2039 | .iter() | |
9c376795 | 2040 | .filter(|&(exported_symbol, _)| match *exported_symbol { |
dfeec247 | 2041 | ExportedSymbol::NoDefId(symbol_name) => symbol_name != metadata_symbol_name, |
0531ce1d | 2042 | _ => true, |
dfeec247 XL |
2043 | }) |
2044 | .cloned(), | |
2045 | ) | |
0531ce1d XL |
2046 | } |
2047 | ||
923072b8 | 2048 | fn encode_dylib_dependency_formats(&mut self) -> LazyArray<Option<LinkagePreference>> { |
1b1a35ee | 2049 | empty_proc_macro!(self); |
17df50a5 | 2050 | let formats = self.tcx.dependency_formats(()); |
e74abb32 | 2051 | for (ty, arr) in formats.iter() { |
f9f354fc | 2052 | if *ty != CrateType::Dylib { |
e74abb32 | 2053 | continue; |
9e0c209e | 2054 | } |
923072b8 | 2055 | return self.lazy_array(arr.iter().map(|slot| match *slot { |
dfeec247 | 2056 | Linkage::NotLinked | Linkage::IncludedFromDylib => None, |
e74abb32 | 2057 | |
dfeec247 XL |
2058 | Linkage::Dynamic => Some(LinkagePreference::RequireDynamic), |
2059 | Linkage::Static => Some(LinkagePreference::RequireStatic), | |
e74abb32 | 2060 | })); |
a7813a04 | 2061 | } |
9ffffee4 | 2062 | LazyArray::default() |
a7813a04 | 2063 | } |
1a4d82fc | 2064 | |
dfeec247 | 2065 | fn encode_info_for_foreign_item(&mut self, def_id: DefId, nitem: &hir::ForeignItem<'_>) { |
7cac9316 | 2066 | let tcx = self.tcx; |
9e0c209e | 2067 | |
dc9dc135 | 2068 | debug!("EncodeContext::encode_info_for_foreign_item({:?})", def_id); |
9e0c209e | 2069 | |
5869c6ff | 2070 | match nitem.kind { |
8faf50e0 | 2071 | hir::ForeignItemKind::Fn(_, ref names, _) => { |
9ffffee4 | 2072 | self.tables.asyncness.set_some(def_id.index, hir::IsAsync::NotAsync); |
923072b8 | 2073 | record_array!(self.tables.fn_arg_names[def_id] <- *names); |
5e7ed085 FG |
2074 | let constness = if self.tcx.is_const_fn_raw(def_id) { |
2075 | hir::Constness::Const | |
2076 | } else { | |
2077 | hir::Constness::NotConst | |
7cac9316 | 2078 | }; |
9ffffee4 | 2079 | self.tables.constness.set_some(def_id.index, constness); |
f2b60f7d | 2080 | record!(self.tables.fn_sig[def_id] <- tcx.fn_sig(def_id)); |
5869c6ff | 2081 | } |
f2b60f7d | 2082 | hir::ForeignItemKind::Static(..) | hir::ForeignItemKind::Type => {} |
5869c6ff | 2083 | } |
e74abb32 | 2084 | if let hir::ForeignItemKind::Fn(..) = nitem.kind { |
9ffffee4 | 2085 | self.tables.is_intrinsic.set(def_id.index, tcx.is_intrinsic(def_id)); |
7cac9316 XL |
2086 | } |
2087 | } | |
2088 | } | |
9e0c209e | 2089 | |
e74abb32 | 2090 | // FIXME(eddyb) make metadata encoding walk over all definitions, instead of HIR. |
a2a8927a | 2091 | impl<'a, 'tcx> Visitor<'tcx> for EncodeContext<'a, 'tcx> { |
5099ac24 | 2092 | type NestedFilter = nested_filter::OnlyBodies; |
dfeec247 | 2093 | |
5099ac24 FG |
2094 | fn nested_visit_map(&mut self) -> Self::Map { |
2095 | self.tcx.hir() | |
7cac9316 | 2096 | } |
dfeec247 | 2097 | fn visit_expr(&mut self, ex: &'tcx hir::Expr<'tcx>) { |
7cac9316 | 2098 | intravisit::walk_expr(self, ex); |
dc9dc135 | 2099 | self.encode_info_for_expr(ex); |
7cac9316 | 2100 | } |
dfeec247 | 2101 | fn visit_item(&mut self, item: &'tcx hir::Item<'tcx>) { |
7cac9316 | 2102 | intravisit::walk_item(self, item); |
353b0b11 | 2103 | self.encode_info_for_item(item); |
7cac9316 | 2104 | } |
dfeec247 | 2105 | fn visit_foreign_item(&mut self, ni: &'tcx hir::ForeignItem<'tcx>) { |
7cac9316 | 2106 | intravisit::walk_foreign_item(self, ni); |
2b03887a | 2107 | self.encode_info_for_foreign_item(ni.owner_id.to_def_id(), ni); |
7cac9316 | 2108 | } |
dfeec247 | 2109 | fn visit_generics(&mut self, generics: &'tcx hir::Generics<'tcx>) { |
7cac9316 | 2110 | intravisit::walk_generics(self, generics); |
dc9dc135 | 2111 | self.encode_info_for_generics(generics); |
7cac9316 | 2112 | } |
7cac9316 | 2113 | } |
9e0c209e | 2114 | |
a2a8927a | 2115 | impl<'a, 'tcx> EncodeContext<'a, 'tcx> { |
dfeec247 XL |
2116 | fn encode_info_for_generics(&mut self, generics: &hir::Generics<'tcx>) { |
2117 | for param in generics.params { | |
b7449926 | 2118 | match param.kind { |
f2b60f7d FG |
2119 | hir::GenericParamKind::Lifetime { .. } | hir::GenericParamKind::Type { .. } => {} |
2120 | hir::GenericParamKind::Const { ref default, .. } => { | |
487cf647 | 2121 | let def_id = param.def_id.to_def_id(); |
cdc7bbd5 | 2122 | if default.is_some() { |
5e7ed085 | 2123 | record!(self.tables.const_param_default[def_id] <- self.tcx.const_param_default(def_id)) |
cdc7bbd5 | 2124 | } |
9fa01778 | 2125 | } |
8faf50e0 | 2126 | } |
b7449926 | 2127 | } |
7cac9316 | 2128 | } |
9cc50fc6 | 2129 | |
dfeec247 | 2130 | fn encode_info_for_expr(&mut self, expr: &hir::Expr<'_>) { |
487cf647 FG |
2131 | if let hir::ExprKind::Closure(closure) = expr.kind { |
2132 | self.encode_info_for_closure(closure.def_id); | |
7cac9316 XL |
2133 | } |
2134 | } | |
7cac9316 | 2135 | } |
9e0c209e | 2136 | |
ba9703b0 XL |
2137 | /// Used to prefetch queries which will be needed later by metadata encoding. |
2138 | /// Only a subset of the queries are actually prefetched to keep this code smaller. | |
5869c6ff XL |
2139 | fn prefetch_mir(tcx: TyCtxt<'_>) { |
2140 | if !tcx.sess.opts.output_types.should_codegen() { | |
2141 | // We won't emit MIR, so don't prefetch it. | |
2142 | return; | |
ba9703b0 | 2143 | } |
ba9703b0 | 2144 | |
49aad941 | 2145 | par_for_each_in(tcx.mir_keys(()), |&def_id| { |
5869c6ff | 2146 | let (encode_const, encode_opt) = should_encode_mir(tcx, def_id); |
ba9703b0 | 2147 | |
5869c6ff | 2148 | if encode_const { |
353b0b11 | 2149 | tcx.ensure_with_value().mir_for_ctfe(def_id); |
ba9703b0 | 2150 | } |
5869c6ff | 2151 | if encode_opt { |
353b0b11 | 2152 | tcx.ensure_with_value().optimized_mir(def_id); |
5869c6ff XL |
2153 | } |
2154 | if encode_opt || encode_const { | |
353b0b11 | 2155 | tcx.ensure_with_value().promoted_mir(def_id); |
5869c6ff XL |
2156 | } |
2157 | }) | |
ba9703b0 XL |
2158 | } |
2159 | ||
9e0c209e SL |
2160 | // NOTE(eddyb) The following comment was preserved for posterity, even |
2161 | // though it's no longer relevant as EBML (which uses nested & tagged | |
2162 | // "documents") was replaced with a scheme that can't go out of bounds. | |
2163 | // | |
2164 | // And here we run into yet another obscure archive bug: in which metadata | |
2165 | // loaded from archives may have trailing garbage bytes. Awhile back one of | |
cc61c64b | 2166 | // our tests was failing sporadically on the macOS 64-bit builders (both nopt |
9e0c209e SL |
2167 | // and opt) by having ebml generate an out-of-bounds panic when looking at |
2168 | // metadata. | |
2169 | // | |
2170 | // Upon investigation it turned out that the metadata file inside of an rlib | |
2171 | // (and ar archive) was being corrupted. Some compilations would generate a | |
2172 | // metadata file which would end in a few extra bytes, while other | |
2173 | // compilations would not have these extra bytes appended to the end. These | |
2174 | // extra bytes were interpreted by ebml as an extra tag, so they ended up | |
2175 | // being interpreted causing the out-of-bounds. | |
2176 | // | |
2177 | // The root cause of why these extra bytes were appearing was never | |
2178 | // discovered, and in the meantime the solution we're employing is to insert | |
2179 | // the length of the metadata to the start of the metadata. Later on this | |
2180 | // will allow us to slice the metadata to the precise length that we just | |
2181 | // generated regardless of trailing bytes that end up in it. | |
2182 | ||
c295e0f8 | 2183 | pub struct EncodedMetadata { |
064997fb FG |
2184 | // The declaration order matters because `mmap` should be dropped before `_temp_dir`. |
2185 | mmap: Option<Mmap>, | |
2186 | // We need to carry MaybeTempDir to avoid deleting the temporary | |
2187 | // directory while accessing the Mmap. | |
2188 | _temp_dir: Option<MaybeTempDir>, | |
c295e0f8 XL |
2189 | } |
2190 | ||
2191 | impl EncodedMetadata { | |
2192 | #[inline] | |
064997fb FG |
2193 | pub fn from_path(path: PathBuf, temp_dir: Option<MaybeTempDir>) -> std::io::Result<Self> { |
2194 | let file = std::fs::File::open(&path)?; | |
2195 | let file_metadata = file.metadata()?; | |
2196 | if file_metadata.len() == 0 { | |
2197 | return Ok(Self { mmap: None, _temp_dir: None }); | |
2198 | } | |
2199 | let mmap = unsafe { Some(Mmap::map(file)?) }; | |
2200 | Ok(Self { mmap, _temp_dir: temp_dir }) | |
c295e0f8 XL |
2201 | } |
2202 | ||
2203 | #[inline] | |
2204 | pub fn raw_data(&self) -> &[u8] { | |
487cf647 | 2205 | self.mmap.as_deref().unwrap_or_default() |
c295e0f8 XL |
2206 | } |
2207 | } | |
2208 | ||
064997fb FG |
2209 | impl<S: Encoder> Encodable<S> for EncodedMetadata { |
2210 | fn encode(&self, s: &mut S) { | |
2211 | let slice = self.raw_data(); | |
2212 | slice.encode(s) | |
2213 | } | |
2214 | } | |
2215 | ||
2216 | impl<D: Decoder> Decodable<D> for EncodedMetadata { | |
2217 | fn decode(d: &mut D) -> Self { | |
2218 | let len = d.read_usize(); | |
2219 | let mmap = if len > 0 { | |
2220 | let mut mmap = MmapMut::map_anon(len).unwrap(); | |
2221 | for _ in 0..len { | |
2222 | (&mut mmap[..]).write(&[d.read_u8()]).unwrap(); | |
2223 | } | |
2224 | mmap.flush().unwrap(); | |
2225 | Some(mmap.make_read_only().unwrap()) | |
2226 | } else { | |
2227 | None | |
2228 | }; | |
2229 | ||
2230 | Self { mmap, _temp_dir: None } | |
2231 | } | |
2232 | } | |
2233 | ||
2234 | pub fn encode_metadata(tcx: TyCtxt<'_>, path: &Path) { | |
c295e0f8 XL |
2235 | let _prof_timer = tcx.prof.verbose_generic_activity("generate_crate_metadata"); |
2236 | ||
ba9703b0 XL |
2237 | // Since encoding metadata is not in a query, and nothing is cached, |
2238 | // there's no need to do dep-graph tracking for any of it. | |
2239 | tcx.dep_graph.assert_ignored(); | |
2240 | ||
2241 | join( | |
064997fb | 2242 | || encode_metadata_impl(tcx, path), |
ba9703b0 XL |
2243 | || { |
2244 | if tcx.sess.threads() == 1 { | |
2245 | return; | |
2246 | } | |
2247 | // Prefetch some queries used by metadata encoding. | |
2248 | // This is not necessary for correctness, but is only done for performance reasons. | |
2249 | // It can be removed if it turns out to cause trouble or be detrimental to performance. | |
5869c6ff | 2250 | join(|| prefetch_mir(tcx), || tcx.exported_symbols(LOCAL_CRATE)); |
ba9703b0 | 2251 | }, |
064997fb | 2252 | ); |
ba9703b0 XL |
2253 | } |
2254 | ||
064997fb FG |
2255 | fn encode_metadata_impl(tcx: TyCtxt<'_>, path: &Path) { |
2256 | let mut encoder = opaque::FileEncoder::new(path) | |
f2b60f7d | 2257 | .unwrap_or_else(|err| tcx.sess.emit_fatal(FailCreateFileEncoder { err })); |
923072b8 | 2258 | encoder.emit_raw_bytes(METADATA_HEADER); |
9e0c209e | 2259 | |
7cac9316 | 2260 | // Will be filled with the root position after encoding everything. |
923072b8 | 2261 | encoder.emit_raw_bytes(&[0, 0, 0, 0]); |
9e0c209e | 2262 | |
f035d41b | 2263 | let source_map_files = tcx.sess.source_map().files(); |
29967ef6 | 2264 | let source_file_cache = (source_map_files[0].clone(), 0); |
f2b60f7d | 2265 | let required_source_files = Some(FxIndexSet::default()); |
29967ef6 XL |
2266 | drop(source_map_files); |
2267 | ||
3dfed10e | 2268 | let hygiene_ctxt = HygieneEncodeContext::default(); |
f035d41b | 2269 | |
ba9703b0 XL |
2270 | let mut ecx = EncodeContext { |
2271 | opaque: encoder, | |
2272 | tcx, | |
1b1a35ee | 2273 | feat: tcx.features(), |
ba9703b0 XL |
2274 | tables: Default::default(), |
2275 | lazy_state: LazyState::NoNode, | |
2276 | type_shorthands: Default::default(), | |
2277 | predicate_shorthands: Default::default(), | |
29967ef6 | 2278 | source_file_cache, |
ba9703b0 | 2279 | interpret_allocs: Default::default(), |
29967ef6 | 2280 | required_source_files, |
f035d41b | 2281 | is_proc_macro: tcx.sess.crate_types().contains(&CrateType::ProcMacro), |
3dfed10e | 2282 | hygiene_ctxt: &hygiene_ctxt, |
f2b60f7d | 2283 | symbol_table: Default::default(), |
ba9703b0 XL |
2284 | }; |
2285 | ||
2286 | // Encode the rustc version string in a predictable location. | |
49aad941 | 2287 | rustc_version(tcx.sess.cfg_version).encode(&mut ecx); |
ba9703b0 XL |
2288 | |
2289 | // Encode all the entries and extra information in the crate, | |
2290 | // culminating in the `CrateRoot` which points to all of it. | |
2291 | let root = ecx.encode_crate_root(); | |
2292 | ||
064997fb FG |
2293 | ecx.opaque.flush(); |
2294 | ||
2295 | let mut file = ecx.opaque.file(); | |
2296 | // We will return to this position after writing the root position. | |
2297 | let pos_before_seek = file.stream_position().unwrap(); | |
9e0c209e SL |
2298 | |
2299 | // Encode the root position. | |
2300 | let header = METADATA_HEADER.len(); | |
064997fb | 2301 | file.seek(std::io::SeekFrom::Start(header as u64)) |
f2b60f7d | 2302 | .unwrap_or_else(|err| tcx.sess.emit_fatal(FailSeekFile { err })); |
e74abb32 | 2303 | let pos = root.position.get(); |
064997fb | 2304 | file.write_all(&[(pos >> 24) as u8, (pos >> 16) as u8, (pos >> 8) as u8, (pos >> 0) as u8]) |
f2b60f7d | 2305 | .unwrap_or_else(|err| tcx.sess.emit_fatal(FailWriteFile { err })); |
9e0c209e | 2306 | |
064997fb FG |
2307 | // Return to the position where we are before writing the root position. |
2308 | file.seek(std::io::SeekFrom::Start(pos_before_seek)).unwrap(); | |
3c0e092e | 2309 | |
064997fb FG |
2310 | // Record metadata size for self-profiling |
2311 | tcx.prof.artifact_size( | |
2312 | "crate_metadata", | |
2313 | "crate_metadata", | |
2314 | file.metadata().unwrap().len() as u64, | |
2315 | ); | |
223e47cc | 2316 | } |
a2a8927a XL |
2317 | |
2318 | pub fn provide(providers: &mut Providers) { | |
2319 | *providers = Providers { | |
9ffffee4 FG |
2320 | doc_link_resolutions: |tcx, def_id| { |
2321 | tcx.resolutions(()) | |
2322 | .doc_link_resolutions | |
353b0b11 | 2323 | .get(&def_id) |
9ffffee4 FG |
2324 | .expect("no resolutions for a doc link") |
2325 | }, | |
2326 | doc_link_traits_in_scope: |tcx, def_id| { | |
2327 | tcx.resolutions(()) | |
2328 | .doc_link_traits_in_scope | |
353b0b11 | 2329 | .get(&def_id) |
9ffffee4 FG |
2330 | .expect("no traits in scope for a doc link") |
2331 | }, | |
49aad941 | 2332 | traits: |tcx, LocalCrate| { |
923072b8 FG |
2333 | let mut traits = Vec::new(); |
2334 | for id in tcx.hir().items() { | |
2b03887a FG |
2335 | if matches!(tcx.def_kind(id.owner_id), DefKind::Trait | DefKind::TraitAlias) { |
2336 | traits.push(id.owner_id.to_def_id()) | |
a2a8927a | 2337 | } |
a2a8927a XL |
2338 | } |
2339 | ||
a2a8927a | 2340 | // Bring everything into deterministic order. |
923072b8 FG |
2341 | traits.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id)); |
2342 | tcx.arena.alloc_slice(&traits) | |
a2a8927a | 2343 | }, |
353b0b11 | 2344 | trait_impls_in_crate: |tcx, LocalCrate| { |
9ffffee4 FG |
2345 | let mut trait_impls = Vec::new(); |
2346 | for id in tcx.hir().items() { | |
2347 | if matches!(tcx.def_kind(id.owner_id), DefKind::Impl { .. }) | |
2348 | && tcx.impl_trait_ref(id.owner_id).is_some() | |
2349 | { | |
2350 | trait_impls.push(id.owner_id.to_def_id()) | |
2351 | } | |
2352 | } | |
2353 | ||
2354 | // Bring everything into deterministic order. | |
2355 | trait_impls.sort_by_cached_key(|&def_id| tcx.def_path_hash(def_id)); | |
2356 | tcx.arena.alloc_slice(&trait_impls) | |
2357 | }, | |
a2a8927a XL |
2358 | |
2359 | ..*providers | |
064997fb | 2360 | } |
a2a8927a | 2361 | } |