Skip to content

Commit 05dde13

Browse files
committed
Make PackedFingerprint's Fingerprint private
1 parent f09d474 commit 05dde13

File tree

4 files changed

+25
-8
lines changed

4 files changed

+25
-8
lines changed

compiler/rustc_data_structures/src/fingerprint.rs

+18-1
Original file line numberDiff line numberDiff line change
@@ -170,9 +170,12 @@ impl FingerprintDecoder for opaque::Decoder<'_> {
170170
// `DepNode`s. As of this writing, the size of a `DepNode` decreases by ~30%
171171
// (from 24 bytes to 17) by using the packed representation here, which
172172
// noticeably decreases total memory usage when compiling large crates.
173+
//
174+
// The wrapped `Fingerprint` is private to reduce the chance of a client
175+
// invoking undefined behavior by taking a reference to the packed field.
173176
#[cfg_attr(any(target_arch = "x86", target_arch = "x86_64"), repr(packed))]
174177
#[derive(Eq, PartialEq, Ord, PartialOrd, Debug, Clone, Copy, Hash)]
175-
pub struct PackedFingerprint(pub Fingerprint);
178+
pub struct PackedFingerprint(Fingerprint);
176179

177180
impl std::fmt::Display for PackedFingerprint {
178181
#[inline]
@@ -198,3 +201,17 @@ impl<D: rustc_serialize::Decoder> Decodable<D> for PackedFingerprint {
198201
Fingerprint::decode(d).map(|f| PackedFingerprint(f))
199202
}
200203
}
204+
205+
impl From<Fingerprint> for PackedFingerprint {
206+
#[inline]
207+
fn from(f: Fingerprint) -> PackedFingerprint {
208+
PackedFingerprint(f)
209+
}
210+
}
211+
212+
impl From<PackedFingerprint> for Fingerprint {
213+
#[inline]
214+
fn from(f: PackedFingerprint) -> Fingerprint {
215+
f.0
216+
}
217+
}

compiler/rustc_middle/src/dep_graph/dep_node.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ use crate::traits::query::{
6161
use crate::ty::subst::{GenericArg, SubstsRef};
6262
use crate::ty::{self, ParamEnvAnd, Ty, TyCtxt};
6363

64-
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
64+
use rustc_data_structures::fingerprint::Fingerprint;
6565
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId, CRATE_DEF_INDEX};
6666
use rustc_hir::definitions::DefPathHash;
6767
use rustc_hir::HirId;
@@ -236,7 +236,7 @@ macro_rules! define_dep_nodes {
236236
debug_assert!(kind.can_reconstruct_query_key() && kind.has_params());
237237
DepNode {
238238
kind,
239-
hash: PackedFingerprint(def_path_hash.0),
239+
hash: def_path_hash.0.into(),
240240
}
241241
}
242242

@@ -252,7 +252,7 @@ macro_rules! define_dep_nodes {
252252
/// has been removed.
253253
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
254254
if self.kind.can_reconstruct_query_key() {
255-
let def_path_hash = DefPathHash(self.hash.0);
255+
let def_path_hash = DefPathHash(self.hash.into());
256256
tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
257257
} else {
258258
None

compiler/rustc_query_system/src/dep_graph/dep_node.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ impl<K: DepKind> DepNode<K> {
6262
/// does not require any parameters.
6363
pub fn new_no_params(kind: K) -> DepNode<K> {
6464
debug_assert!(!kind.has_params());
65-
DepNode { kind, hash: PackedFingerprint(Fingerprint::ZERO) }
65+
DepNode { kind, hash: Fingerprint::ZERO.into() }
6666
}
6767

6868
pub fn construct<Ctxt, Key>(tcx: Ctxt, kind: K, arg: &Key) -> DepNode<K>
@@ -71,7 +71,7 @@ impl<K: DepKind> DepNode<K> {
7171
Key: DepNodeParams<Ctxt>,
7272
{
7373
let hash = arg.to_fingerprint(tcx);
74-
let dep_node = DepNode { kind, hash: PackedFingerprint(hash) };
74+
let dep_node = DepNode { kind, hash: hash.into() };
7575

7676
#[cfg(debug_assertions)]
7777
{

compiler/rustc_query_system/src/dep_graph/graph.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
use rustc_data_structures::fingerprint::{Fingerprint, PackedFingerprint};
1+
use rustc_data_structures::fingerprint::Fingerprint;
22
use rustc_data_structures::fx::{FxHashMap, FxHashSet};
33
use rustc_data_structures::profiling::QueryInvocationId;
44
use rustc_data_structures::sharded::{self, Sharded};
@@ -976,7 +976,7 @@ impl<K: DepKind> CurrentDepGraph<K> {
976976
// Fingerprint::combine() is faster than sending Fingerprint
977977
// through the StableHasher (at least as long as StableHasher
978978
// is so slow).
979-
hash: PackedFingerprint(self.anon_id_seed.combine(hasher.finish())),
979+
hash: self.anon_id_seed.combine(hasher.finish()).into(),
980980
};
981981

982982
self.intern_node(target_dep_node, task_deps.reads, Fingerprint::ZERO)

0 commit comments

Comments
 (0)