rustc_metadata/rmeta/
mod.rs

1use std::marker::PhantomData;
2use std::num::NonZero;
3
4use decoder::LazyDecoder;
5pub(crate) use decoder::{CrateMetadata, CrateNumMap, MetadataBlob, TargetModifiers};
6use def_path_hash_map::DefPathHashMapRef;
7use encoder::EncodeContext;
8pub use encoder::{EncodedMetadata, encode_metadata, rendered_const};
9pub(crate) use parameterized::ParameterizedOverTcx;
10use rustc_abi::{FieldIdx, ReprOptions, VariantIdx};
11use rustc_data_structures::fx::FxHashMap;
12use rustc_data_structures::svh::Svh;
13use rustc_hir::attrs::StrippedCfgItem;
14use rustc_hir::def::{CtorKind, DefKind, DocLinkResMap, MacroKinds};
15use rustc_hir::def_id::{CrateNum, DefId, DefIdMap, DefIndex, DefPathHash, StableCrateId};
16use rustc_hir::definitions::DefKey;
17use rustc_hir::lang_items::LangItem;
18use rustc_hir::{PreciseCapturingArgKind, attrs};
19use rustc_index::IndexVec;
20use rustc_index::bit_set::DenseBitSet;
21use rustc_macros::{
22    BlobDecodable, Decodable, Encodable, LazyDecodable, MetadataEncodable, TyDecodable, TyEncodable,
23};
24use rustc_middle::metadata::{AmbigModChild, ModChild};
25use rustc_middle::middle::codegen_fn_attrs::CodegenFnAttrs;
26use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
27use rustc_middle::middle::deduced_param_attrs::DeducedParamAttrs;
28use rustc_middle::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
29use rustc_middle::middle::lib_features::FeatureStability;
30use rustc_middle::middle::resolve_bound_vars::ObjectLifetimeDefault;
31use rustc_middle::mir;
32use rustc_middle::mir::ConstValue;
33use rustc_middle::ty::fast_reject::SimplifiedType;
34use rustc_middle::ty::{self, Ty, TyCtxt, UnusedGenericParams};
35use rustc_middle::util::Providers;
36use rustc_serialize::opaque::FileEncoder;
37use rustc_session::config::{SymbolManglingVersion, TargetModifier};
38use rustc_session::cstore::{CrateDepKind, ForeignModule, LinkagePreference, NativeLib};
39use rustc_span::edition::Edition;
40use rustc_span::hygiene::{ExpnIndex, MacroKind, SyntaxContextKey};
41use rustc_span::{self, ExpnData, ExpnHash, ExpnId, Ident, Span, Symbol};
42use rustc_target::spec::{PanicStrategy, TargetTuple};
43use table::TableBuilder;
44use {rustc_ast as ast, rustc_hir as hir};
45
46use crate::creader::CrateMetadataRef;
47use crate::eii::EiiMapEncodedKeyValue;
48
49mod decoder;
50mod def_path_hash_map;
51mod encoder;
52mod parameterized;
53mod table;
54
55pub(crate) fn rustc_version(cfg_version: &'static str) -> String {
56    format!("rustc {cfg_version}")
57}
58
59/// Metadata encoding version.
60/// N.B., increment this if you change the format of metadata such that
61/// the rustc version can't be found to compare with `rustc_version()`.
62const METADATA_VERSION: u8 = 10;
63
64/// Metadata header which includes `METADATA_VERSION`.
65///
66/// This header is followed by the length of the compressed data, then
67/// the position of the `CrateRoot`, which is encoded as a 64-bit little-endian
68/// unsigned integer, and further followed by the rustc version string.
69pub const METADATA_HEADER: &[u8] = &[b'r', b'u', b's', b't', 0, 0, 0, METADATA_VERSION];
70
71/// A value of type T referred to by its absolute position
72/// in the metadata, and which can be decoded lazily.
73///
74/// Metadata is effective a tree, encoded in post-order,
75/// and with the root's position written next to the header.
76/// That means every single `LazyValue` points to some previous
77/// location in the metadata and is part of a larger node.
78///
79/// The first `LazyValue` in a node is encoded as the backwards
80/// distance from the position where the containing node
81/// starts and where the `LazyValue` points to, while the rest
82/// use the forward distance from the previous `LazyValue`.
83/// Distances start at 1, as 0-byte nodes are invalid.
84/// Also invalid are nodes being referred in a different
85/// order than they were encoded in.
86#[must_use]
87struct LazyValue<T> {
88    position: NonZero<usize>,
89    _marker: PhantomData<fn() -> T>,
90}
91
92impl<T> LazyValue<T> {
93    fn from_position(position: NonZero<usize>) -> LazyValue<T> {
94        LazyValue { position, _marker: PhantomData }
95    }
96}
97
98/// A list of lazily-decoded values.
99///
100/// Unlike `LazyValue<Vec<T>>`, the length is encoded next to the
101/// position, not at the position, which means that the length
102/// doesn't need to be known before encoding all the elements.
103///
104/// If the length is 0, no position is encoded, but otherwise,
105/// the encoding is that of `LazyArray`, with the distinction that
106/// the minimal distance the length of the sequence, i.e.
107/// it's assumed there's no 0-byte element in the sequence.
108struct LazyArray<T> {
109    position: NonZero<usize>,
110    num_elems: usize,
111    _marker: PhantomData<fn() -> T>,
112}
113
114impl<T> Default for LazyArray<T> {
115    fn default() -> LazyArray<T> {
116        LazyArray::from_position_and_num_elems(NonZero::new(1).unwrap(), 0)
117    }
118}
119
120impl<T> LazyArray<T> {
121    fn from_position_and_num_elems(position: NonZero<usize>, num_elems: usize) -> LazyArray<T> {
122        LazyArray { position, num_elems, _marker: PhantomData }
123    }
124}
125
126/// A list of lazily-decoded values, with the added capability of random access.
127///
128/// Random-access table (i.e. offering constant-time `get`/`set`), similar to
129/// `LazyArray<T>`, but without requiring encoding or decoding all the values
130/// eagerly and in-order.
131struct LazyTable<I, T> {
132    position: NonZero<usize>,
133    /// The encoded size of the elements of a table is selected at runtime to drop
134    /// trailing zeroes. This is the number of bytes used for each table element.
135    width: usize,
136    /// How many elements are in the table.
137    len: usize,
138    _marker: PhantomData<fn(I) -> T>,
139}
140
141impl<I, T> LazyTable<I, T> {
142    fn from_position_and_encoded_size(
143        position: NonZero<usize>,
144        width: usize,
145        len: usize,
146    ) -> LazyTable<I, T> {
147        LazyTable { position, width, len, _marker: PhantomData }
148    }
149}
150
151impl<T> Copy for LazyValue<T> {}
152impl<T> Clone for LazyValue<T> {
153    fn clone(&self) -> Self {
154        *self
155    }
156}
157
158impl<T> Copy for LazyArray<T> {}
159impl<T> Clone for LazyArray<T> {
160    fn clone(&self) -> Self {
161        *self
162    }
163}
164
165impl<I, T> Copy for LazyTable<I, T> {}
166impl<I, T> Clone for LazyTable<I, T> {
167    fn clone(&self) -> Self {
168        *self
169    }
170}
171
172/// Encoding / decoding state for `Lazy`s (`LazyValue`, `LazyArray`, and `LazyTable`).
173#[derive(Copy, Clone, PartialEq, Eq, Debug)]
174enum LazyState {
175    /// Outside of a metadata node.
176    NoNode,
177
178    /// Inside a metadata node, and before any `Lazy`s.
179    /// The position is that of the node itself.
180    NodeStart(NonZero<usize>),
181
182    /// Inside a metadata node, with a previous `Lazy`s.
183    /// The position is where that previous `Lazy` would start.
184    Previous(NonZero<usize>),
185}
186
187type SyntaxContextTable = LazyTable<u32, Option<LazyValue<SyntaxContextKey>>>;
188type ExpnDataTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnData>>>;
189type ExpnHashTable = LazyTable<ExpnIndex, Option<LazyValue<ExpnHash>>>;
190
191#[derive(MetadataEncodable, LazyDecodable)]
192pub(crate) struct ProcMacroData {
193    proc_macro_decls_static: DefIndex,
194    stability: Option<hir::Stability>,
195    macros: LazyArray<DefIndex>,
196}
197
198/// Serialized crate metadata.
199///
200/// This contains just enough information to determine if we should load the `CrateRoot` or not.
201/// Prefer [`CrateRoot`] whenever possible to avoid ICEs when using `omit-git-hash` locally.
202/// See #76720 for more details.
203///
204/// If you do modify this struct, also bump the [`METADATA_VERSION`] constant.
205#[derive(MetadataEncodable, BlobDecodable)]
206pub(crate) struct CrateHeader {
207    pub(crate) triple: TargetTuple,
208    pub(crate) hash: Svh,
209    pub(crate) name: Symbol,
210    /// Whether this is the header for a proc-macro crate.
211    ///
212    /// This is separate from [`ProcMacroData`] to avoid having to update [`METADATA_VERSION`] every
213    /// time ProcMacroData changes.
214    pub(crate) is_proc_macro_crate: bool,
215    /// Whether this crate metadata section is just a stub.
216    /// Stubs do not contain the full metadata (it will be typically stored
217    /// in a separate rmeta file).
218    ///
219    /// This is used inside rlibs and dylibs when using `-Zembed-metadata=no`.
220    pub(crate) is_stub: bool,
221}
222
223/// Serialized `.rmeta` data for a crate.
224///
225/// When compiling a proc-macro crate, we encode many of
226/// the `LazyArray<T>` fields as `Lazy::empty()`. This serves two purposes:
227///
228/// 1. We avoid performing unnecessary work. Proc-macro crates can only
229/// export proc-macros functions, which are compiled into a shared library.
230/// As a result, a large amount of the information we normally store
231/// (e.g. optimized MIR) is unneeded by downstream crates.
232/// 2. We avoid serializing invalid `CrateNum`s. When we deserialize
233/// a proc-macro crate, we don't load any of its dependencies (since we
234/// just need to invoke a native function from the shared library).
235/// This means that any foreign `CrateNum`s that we serialize cannot be
236/// deserialized, since we will not know how to map them into the current
237/// compilation session. If we were to serialize a proc-macro crate like
238/// a normal crate, much of what we serialized would be unusable in addition
239/// to being unused.
240#[derive(MetadataEncodable, LazyDecodable)]
241pub(crate) struct CrateRoot {
242    /// A header used to detect if this is the right crate to load.
243    header: CrateHeader,
244
245    extra_filename: String,
246    stable_crate_id: StableCrateId,
247    required_panic_strategy: Option<PanicStrategy>,
248    panic_in_drop_strategy: PanicStrategy,
249    edition: Edition,
250    has_global_allocator: bool,
251    has_alloc_error_handler: bool,
252    has_panic_handler: bool,
253    has_default_lib_allocator: bool,
254    externally_implementable_items: LazyArray<EiiMapEncodedKeyValue>,
255
256    crate_deps: LazyArray<CrateDep>,
257    dylib_dependency_formats: LazyArray<Option<LinkagePreference>>,
258    lib_features: LazyArray<(Symbol, FeatureStability)>,
259    stability_implications: LazyArray<(Symbol, Symbol)>,
260    lang_items: LazyArray<(DefIndex, LangItem)>,
261    lang_items_missing: LazyArray<LangItem>,
262    stripped_cfg_items: LazyArray<StrippedCfgItem<DefIndex>>,
263    diagnostic_items: LazyArray<(Symbol, DefIndex)>,
264    native_libraries: LazyArray<NativeLib>,
265    foreign_modules: LazyArray<ForeignModule>,
266    traits: LazyArray<DefIndex>,
267    impls: LazyArray<TraitImpls>,
268    incoherent_impls: LazyArray<IncoherentImpls>,
269    interpret_alloc_index: LazyArray<u64>,
270    proc_macro_data: Option<ProcMacroData>,
271
272    tables: LazyTables,
273    debugger_visualizers: LazyArray<DebuggerVisualizerFile>,
274
275    exportable_items: LazyArray<DefIndex>,
276    stable_order_of_exportable_impls: LazyArray<(DefIndex, usize)>,
277    exported_non_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
278    exported_generic_symbols: LazyArray<(ExportedSymbol<'static>, SymbolExportInfo)>,
279
280    syntax_contexts: SyntaxContextTable,
281    expn_data: ExpnDataTable,
282    expn_hashes: ExpnHashTable,
283
284    def_path_hash_map: LazyValue<DefPathHashMapRef<'static>>,
285
286    source_map: LazyTable<u32, Option<LazyValue<rustc_span::SourceFile>>>,
287    target_modifiers: LazyArray<TargetModifier>,
288
289    compiler_builtins: bool,
290    needs_allocator: bool,
291    needs_panic_runtime: bool,
292    no_builtins: bool,
293    panic_runtime: bool,
294    profiler_runtime: bool,
295    symbol_mangling_version: SymbolManglingVersion,
296
297    specialization_enabled_in: bool,
298}
299
300/// On-disk representation of `DefId`.
301/// This creates a type-safe way to enforce that we remap the CrateNum between the on-disk
302/// representation and the compilation session.
303#[derive(Copy, Clone)]
304pub(crate) struct RawDefId {
305    krate: u32,
306    index: u32,
307}
308
309impl From<DefId> for RawDefId {
310    fn from(val: DefId) -> Self {
311        RawDefId { krate: val.krate.as_u32(), index: val.index.as_u32() }
312    }
313}
314
315impl RawDefId {
316    /// This exists so that `provide_one!` is happy
317    fn decode(self, meta: (CrateMetadataRef<'_>, TyCtxt<'_>)) -> DefId {
318        self.decode_from_cdata(meta.0)
319    }
320
321    fn decode_from_cdata(self, cdata: CrateMetadataRef<'_>) -> DefId {
322        let krate = CrateNum::from_u32(self.krate);
323        let krate = cdata.map_encoded_cnum_to_current(krate);
324        DefId { krate, index: DefIndex::from_u32(self.index) }
325    }
326}
327
328#[derive(Encodable, BlobDecodable)]
329pub(crate) struct CrateDep {
330    pub name: Symbol,
331    pub hash: Svh,
332    pub host_hash: Option<Svh>,
333    pub kind: CrateDepKind,
334    pub extra_filename: String,
335    pub is_private: bool,
336}
337
338#[derive(MetadataEncodable, LazyDecodable)]
339pub(crate) struct TraitImpls {
340    trait_id: (u32, DefIndex),
341    impls: LazyArray<(DefIndex, Option<SimplifiedType>)>,
342}
343
344#[derive(MetadataEncodable, LazyDecodable)]
345pub(crate) struct IncoherentImpls {
346    self_ty: LazyValue<SimplifiedType>,
347    impls: LazyArray<DefIndex>,
348}
349
350/// Define `LazyTables` and `TableBuilders` at the same time.
351macro_rules! define_tables {
352    (
353        - defaulted: $($name1:ident: Table<$IDX1:ty, $T1:ty>,)+
354        - optional: $($name2:ident: Table<$IDX2:ty, $T2:ty>,)+
355    ) => {
356        #[derive(MetadataEncodable, LazyDecodable)]
357        pub(crate) struct LazyTables {
358            $($name1: LazyTable<$IDX1, $T1>,)+
359            $($name2: LazyTable<$IDX2, Option<$T2>>,)+
360        }
361
362        #[derive(Default)]
363        struct TableBuilders {
364            $($name1: TableBuilder<$IDX1, $T1>,)+
365            $($name2: TableBuilder<$IDX2, Option<$T2>>,)+
366        }
367
368        impl TableBuilders {
369            fn encode(&self, buf: &mut FileEncoder) -> LazyTables {
370                LazyTables {
371                    $($name1: self.$name1.encode(buf),)+
372                    $($name2: self.$name2.encode(buf),)+
373                }
374            }
375        }
376    }
377}
378
379define_tables! {
380- defaulted:
381    intrinsic: Table<DefIndex, Option<LazyValue<ty::IntrinsicDef>>>,
382    is_macro_rules: Table<DefIndex, bool>,
383    type_alias_is_lazy: Table<DefIndex, bool>,
384    attr_flags: Table<DefIndex, AttrFlags>,
385    // The u64 is the crate-local part of the DefPathHash. All hashes in this crate have the same
386    // StableCrateId, so we omit encoding those into the table.
387    //
388    // Note also that this table is fully populated (no gaps) as every DefIndex should have a
389    // corresponding DefPathHash.
390    def_path_hashes: Table<DefIndex, u64>,
391    explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
392    explicit_item_self_bounds: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
393    inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
394    explicit_super_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
395    explicit_implied_predicates_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
396    explicit_implied_const_bounds: Table<DefIndex, LazyArray<(ty::PolyTraitRef<'static>, Span)>>,
397    inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
398    opt_rpitit_info: Table<DefIndex, Option<LazyValue<ty::ImplTraitInTraitData>>>,
399    // Reexported names are not associated with individual `DefId`s,
400    // e.g. a glob import can introduce a lot of names, all with the same `DefId`.
401    // That's why the encoded list needs to contain `ModChild` structures describing all the names
402    // individually instead of `DefId`s.
403    module_children_reexports: Table<DefIndex, LazyArray<ModChild>>,
404    ambig_module_children: Table<DefIndex, LazyArray<AmbigModChild>>,
405    cross_crate_inlinable: Table<DefIndex, bool>,
406    asyncness: Table<DefIndex, ty::Asyncness>,
407    constness: Table<DefIndex, hir::Constness>,
408    safety: Table<DefIndex, hir::Safety>,
409    defaultness: Table<DefIndex, hir::Defaultness>,
410
411- optional:
412    attributes: Table<DefIndex, LazyArray<hir::Attribute>>,
413    // For non-reexported names in a module every name is associated with a separate `DefId`,
414    // so we can take their names, visibilities etc from other encoded tables.
415    module_children_non_reexports: Table<DefIndex, LazyArray<DefIndex>>,
416    associated_item_or_field_def_ids: Table<DefIndex, LazyArray<DefIndex>>,
417    def_kind: Table<DefIndex, DefKind>,
418    visibility: Table<DefIndex, LazyValue<ty::Visibility<DefIndex>>>,
419    def_span: Table<DefIndex, LazyValue<Span>>,
420    def_ident_span: Table<DefIndex, LazyValue<Span>>,
421    lookup_stability: Table<DefIndex, LazyValue<hir::Stability>>,
422    lookup_const_stability: Table<DefIndex, LazyValue<hir::ConstStability>>,
423    lookup_default_body_stability: Table<DefIndex, LazyValue<hir::DefaultBodyStability>>,
424    lookup_deprecation_entry: Table<DefIndex, LazyValue<attrs::Deprecation>>,
425    explicit_predicates_of: Table<DefIndex, LazyValue<ty::GenericPredicates<'static>>>,
426    generics_of: Table<DefIndex, LazyValue<ty::Generics>>,
427    type_of: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, Ty<'static>>>>,
428    variances_of: Table<DefIndex, LazyArray<ty::Variance>>,
429    fn_sig: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::PolyFnSig<'static>>>>,
430    codegen_fn_attrs: Table<DefIndex, LazyValue<CodegenFnAttrs>>,
431    impl_trait_header: Table<DefIndex, LazyValue<ty::ImplTraitHeader<'static>>>,
432    const_param_default: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, rustc_middle::ty::Const<'static>>>>,
433    object_lifetime_default: Table<DefIndex, LazyValue<ObjectLifetimeDefault>>,
434    optimized_mir: Table<DefIndex, LazyValue<mir::Body<'static>>>,
435    mir_for_ctfe: Table<DefIndex, LazyValue<mir::Body<'static>>>,
436    trivial_const: Table<DefIndex, LazyValue<(ConstValue, Ty<'static>)>>,
437    closure_saved_names_of_captured_variables: Table<DefIndex, LazyValue<IndexVec<FieldIdx, Symbol>>>,
438    mir_coroutine_witnesses: Table<DefIndex, LazyValue<mir::CoroutineLayout<'static>>>,
439    promoted_mir: Table<DefIndex, LazyValue<IndexVec<mir::Promoted, mir::Body<'static>>>>,
440    thir_abstract_const: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
441    impl_parent: Table<DefIndex, RawDefId>,
442    const_conditions: Table<DefIndex, LazyValue<ty::ConstConditions<'static>>>,
443    // FIXME(eddyb) perhaps compute this on the fly if cheap enough?
444    coerce_unsized_info: Table<DefIndex, LazyValue<ty::adjustment::CoerceUnsizedInfo>>,
445    mir_const_qualif: Table<DefIndex, LazyValue<mir::ConstQualifs>>,
446    rendered_const: Table<DefIndex, LazyValue<String>>,
447    rendered_precise_capturing_args: Table<DefIndex, LazyArray<PreciseCapturingArgKind<Symbol, Symbol>>>,
448    fn_arg_idents: Table<DefIndex, LazyArray<Option<Ident>>>,
449    coroutine_kind: Table<DefIndex, hir::CoroutineKind>,
450    coroutine_for_closure: Table<DefIndex, RawDefId>,
451    adt_destructor: Table<DefIndex, LazyValue<ty::Destructor>>,
452    adt_async_destructor: Table<DefIndex, LazyValue<ty::AsyncDestructor>>,
453    coroutine_by_move_body_def_id: Table<DefIndex, RawDefId>,
454    eval_static_initializer: Table<DefIndex, LazyValue<mir::interpret::ConstAllocation<'static>>>,
455    trait_def: Table<DefIndex, LazyValue<ty::TraitDef>>,
456    expn_that_defined: Table<DefIndex, LazyValue<ExpnId>>,
457    default_fields: Table<DefIndex, LazyValue<DefId>>,
458    params_in_repr: Table<DefIndex, LazyValue<DenseBitSet<u32>>>,
459    repr_options: Table<DefIndex, LazyValue<ReprOptions>>,
460    // `def_keys` and `def_path_hashes` represent a lazy version of a
461    // `DefPathTable`. This allows us to avoid deserializing an entire
462    // `DefPathTable` up front, since we may only ever use a few
463    // definitions from any given crate.
464    def_keys: Table<DefIndex, LazyValue<DefKey>>,
465    proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
466    variant_data: Table<DefIndex, LazyValue<VariantData>>,
467    assoc_container: Table<DefIndex, LazyValue<ty::AssocContainer>>,
468    macro_definition: Table<DefIndex, LazyValue<ast::DelimArgs>>,
469    proc_macro: Table<DefIndex, MacroKind>,
470    deduced_param_attrs: Table<DefIndex, LazyArray<DeducedParamAttrs>>,
471    trait_impl_trait_tys: Table<DefIndex, LazyValue<DefIdMap<ty::EarlyBinder<'static, Ty<'static>>>>>,
472    doc_link_resolutions: Table<DefIndex, LazyValue<DocLinkResMap>>,
473    doc_link_traits_in_scope: Table<DefIndex, LazyArray<DefId>>,
474    assumed_wf_types_for_rpitit: Table<DefIndex, LazyArray<(Ty<'static>, Span)>>,
475    opaque_ty_origin: Table<DefIndex, LazyValue<hir::OpaqueTyOrigin<DefId>>>,
476    anon_const_kind: Table<DefIndex, LazyValue<ty::AnonConstKind>>,
477    const_of_item: Table<DefIndex, LazyValue<ty::EarlyBinder<'static, ty::Const<'static>>>>,
478    associated_types_for_impl_traits_in_trait_or_impl: Table<DefIndex, LazyValue<DefIdMap<Vec<DefId>>>>,
479}
480
481#[derive(TyEncodable, TyDecodable)]
482struct VariantData {
483    idx: VariantIdx,
484    discr: ty::VariantDiscr,
485    /// If this is unit or tuple-variant/struct, then this is the index of the ctor id.
486    ctor: Option<(CtorKind, DefIndex)>,
487    is_non_exhaustive: bool,
488}
489
490bitflags::bitflags! {
491    #[derive(Default)]
492    pub struct AttrFlags: u8 {
493        const IS_DOC_HIDDEN = 1 << 0;
494    }
495}
496
497/// A span tag byte encodes a bunch of data, so that we can cut out a few extra bytes from span
498/// encodings (which are very common, for example, libcore has ~650,000 unique spans and over 1.1
499/// million references to prior-written spans).
500///
501/// The byte format is split into several parts:
502///
503/// [ a a a a a c d d ]
504///
505/// `a` bits represent the span length. We have 5 bits, so we can store lengths up to 30 inline, with
506/// an all-1s pattern representing that the length is stored separately.
507///
508/// `c` represents whether the span context is zero (and then it is not stored as a separate varint)
509/// for direct span encodings, and whether the offset is absolute or relative otherwise (zero for
510/// absolute).
511///
512/// d bits represent the kind of span we are storing (local, foreign, partial, indirect).
513#[derive(Encodable, Decodable, Copy, Clone)]
514struct SpanTag(u8);
515
516#[derive(Debug, Copy, Clone, PartialEq, Eq)]
517enum SpanKind {
518    Local = 0b00,
519    Foreign = 0b01,
520    Partial = 0b10,
521    // Indicates the actual span contents are elsewhere.
522    // If this is the kind, then the span context bit represents whether it is a relative or
523    // absolute offset.
524    Indirect = 0b11,
525}
526
527impl SpanTag {
528    fn new(kind: SpanKind, context: rustc_span::SyntaxContext, length: usize) -> SpanTag {
529        let mut data = 0u8;
530        data |= kind as u8;
531        if context.is_root() {
532            data |= 0b100;
533        }
534        let all_1s_len = (0xffu8 << 3) >> 3;
535        // strictly less than - all 1s pattern is a sentinel for storage being out of band.
536        if length < all_1s_len as usize {
537            data |= (length as u8) << 3;
538        } else {
539            data |= all_1s_len << 3;
540        }
541
542        SpanTag(data)
543    }
544
545    fn indirect(relative: bool, length_bytes: u8) -> SpanTag {
546        let mut tag = SpanTag(SpanKind::Indirect as u8);
547        if relative {
548            tag.0 |= 0b100;
549        }
550        assert!(length_bytes <= 8);
551        tag.0 |= length_bytes << 3;
552        tag
553    }
554
555    fn kind(self) -> SpanKind {
556        let masked = self.0 & 0b11;
557        match masked {
558            0b00 => SpanKind::Local,
559            0b01 => SpanKind::Foreign,
560            0b10 => SpanKind::Partial,
561            0b11 => SpanKind::Indirect,
562            _ => unreachable!(),
563        }
564    }
565
566    fn is_relative_offset(self) -> bool {
567        debug_assert_eq!(self.kind(), SpanKind::Indirect);
568        self.0 & 0b100 != 0
569    }
570
571    fn context(self) -> Option<rustc_span::SyntaxContext> {
572        if self.0 & 0b100 != 0 { Some(rustc_span::SyntaxContext::root()) } else { None }
573    }
574
575    fn length(self) -> Option<rustc_span::BytePos> {
576        let all_1s_len = (0xffu8 << 3) >> 3;
577        let len = self.0 >> 3;
578        if len != all_1s_len { Some(rustc_span::BytePos(u32::from(len))) } else { None }
579    }
580}
581
582// Tags for encoding Symbol's
583const SYMBOL_STR: u8 = 0;
584const SYMBOL_OFFSET: u8 = 1;
585const SYMBOL_PREDEFINED: u8 = 2;
586
587pub fn provide(providers: &mut Providers) {
588    encoder::provide(providers);
589    decoder::provide(providers);
590}