Skip to content

Commit f4e2b95

Browse files
committed
rustc_metadata: Encode/decode DefPathHashes without an Option
1 parent c60cc43 commit f4e2b95

File tree

5 files changed

+20
-11
lines changed

5 files changed

+20
-11
lines changed

compiler/rustc_metadata/src/rmeta/decoder.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1319,7 +1319,7 @@ impl<'a, 'tcx> CrateMetadataRef<'a> {
13191319
) -> DefPathHash {
13201320
*def_path_hashes
13211321
.entry(index)
1322-
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index).unwrap())
1322+
.or_insert_with(|| self.root.tables.def_path_hashes.get(self, index))
13231323
}
13241324

13251325
#[inline]

compiler/rustc_metadata/src/rmeta/encoder.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -478,13 +478,13 @@ impl<'a, 'tcx> EncodeContext<'a, 'tcx> {
478478
let def_key = self.lazy(table.def_key(def_index));
479479
let def_path_hash = table.def_path_hash(def_index);
480480
self.tables.def_keys.set_some(def_index, def_key);
481-
self.tables.def_path_hashes.set_some(def_index, def_path_hash);
481+
self.tables.def_path_hashes.set(def_index, def_path_hash);
482482
}
483483
} else {
484484
for (def_index, def_key, def_path_hash) in table.enumerated_keys_and_path_hashes() {
485485
let def_key = self.lazy(def_key);
486486
self.tables.def_keys.set_some(def_index, def_key);
487-
self.tables.def_path_hashes.set_some(def_index, *def_path_hash);
487+
self.tables.def_path_hashes.set(def_index, *def_path_hash);
488488
}
489489
}
490490
}

compiler/rustc_metadata/src/rmeta/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -350,6 +350,7 @@ define_tables! {
350350
is_macro_rules: Table<DefIndex, bool>,
351351
is_type_alias_impl_trait: Table<DefIndex, bool>,
352352
attr_flags: Table<DefIndex, AttrFlags>,
353+
def_path_hashes: Table<DefIndex, DefPathHash>,
353354
explicit_item_bounds: Table<DefIndex, LazyArray<(ty::Predicate<'static>, Span)>>,
354355
inferred_outlives_of: Table<DefIndex, LazyArray<(ty::Clause<'static>, Span)>>,
355356
inherent_impls: Table<DefIndex, LazyArray<DefIndex>>,
@@ -403,7 +404,6 @@ define_tables! {
403404
// `DefPathTable` up front, since we may only ever use a few
404405
// definitions from any given crate.
405406
def_keys: Table<DefIndex, LazyValue<DefKey>>,
406-
def_path_hashes: Table<DefIndex, DefPathHash>,
407407
proc_macro_quoted_spans: Table<usize, LazyValue<Span>>,
408408
generator_diagnostic_data: Table<DefIndex, LazyValue<GeneratorDiagnosticData<'static>>>,
409409
variant_data: Table<DefIndex, LazyValue<VariantData>>,

compiler/rustc_metadata/src/rmeta/table.rs

+10-7
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,12 @@ impl<T> IsDefault for LazyArray<T> {
4444
}
4545
}
4646

47+
impl IsDefault for DefPathHash {
48+
fn is_default(&self) -> bool {
49+
self.0 == Fingerprint::ZERO
50+
}
51+
}
52+
4753
/// Helper trait, for encoding to, and decoding from, a fixed number of bytes.
4854
/// Used mainly for Lazy positions and lengths.
4955
/// Unchecked invariant: `Self::default()` should encode as `[0; BYTE_LEN]`,
@@ -191,21 +197,18 @@ fixed_size_enum! {
191197
}
192198

193199
// We directly encode `DefPathHash` because a `LazyValue` would incur a 25% cost.
194-
impl FixedSizeEncoding for Option<DefPathHash> {
200+
impl FixedSizeEncoding for DefPathHash {
195201
type ByteArray = [u8; 16];
196202

197203
#[inline]
198204
fn from_bytes(b: &[u8; 16]) -> Self {
199-
// NOTE: There's a collision between `None` and `Some(0)`.
200-
Some(DefPathHash(Fingerprint::from_le_bytes(*b)))
205+
DefPathHash(Fingerprint::from_le_bytes(*b))
201206
}
202207

203208
#[inline]
204209
fn write_to_bytes(self, b: &mut [u8; 16]) {
205-
match self {
206-
None => unreachable!(),
207-
Some(DefPathHash(fingerprint)) => *b = fingerprint.to_le_bytes(),
208-
}
210+
debug_assert!(!self.is_default());
211+
*b = self.0.to_le_bytes();
209212
}
210213
}
211214

compiler/rustc_span/src/def_id.rs

+6
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,12 @@ impl DefPathHash {
119119
}
120120
}
121121

122+
impl Default for DefPathHash {
123+
fn default() -> Self {
124+
DefPathHash(Fingerprint::ZERO)
125+
}
126+
}
127+
122128
impl Borrow<Fingerprint> for DefPathHash {
123129
#[inline]
124130
fn borrow(&self) -> &Fingerprint {

0 commit comments

Comments
 (0)