Skip to content

Commit bd45139

Browse files
committed
Allow query system to recover a HirId.
1 parent eb19a8a commit bd45139

File tree

2 files changed

+37
-7
lines changed

2 files changed

+37
-7
lines changed

compiler/rustc_middle/src/dep_graph/dep_node.rs

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ use crate::ty::TyCtxt;
6262
use rustc_data_structures::fingerprint::Fingerprint;
6363
use rustc_hir::def_id::{CrateNum, DefId, LocalDefId};
6464
use rustc_hir::definitions::DefPathHash;
65-
use rustc_hir::HirId;
65+
use rustc_hir::{HirId, ItemLocalId};
6666
use rustc_query_system::dep_graph::FingerprintStyle;
6767
use rustc_span::symbol::Symbol;
6868
use std::hash::Hash;
@@ -289,7 +289,7 @@ impl DepNodeExt for DepNode {
289289
let kind = dep_kind_from_label_string(label)?;
290290

291291
match kind.fingerprint_style(tcx) {
292-
FingerprintStyle::Opaque => Err(()),
292+
FingerprintStyle::Opaque | FingerprintStyle::HirId => Err(()),
293293
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
294294
FingerprintStyle::DefPathHash => {
295295
Ok(DepNode::from_def_path_hash(tcx, def_path_hash, kind))
@@ -417,7 +417,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
417417
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
418418
#[inline(always)]
419419
fn fingerprint_style() -> FingerprintStyle {
420-
FingerprintStyle::Opaque
420+
FingerprintStyle::HirId
421421
}
422422

423423
// We actually would not need to specialize the implementation of this
@@ -426,10 +426,36 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
426426
#[inline(always)]
427427
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
428428
let HirId { owner, local_id } = *self;
429-
430429
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
431-
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
430+
Fingerprint::new(
431+
// `owner` is local, so is completely defined by the local hash
432+
def_path_hash.local_hash(),
433+
local_id.as_u32().into(),
434+
)
435+
}
432436

433-
def_path_hash.0.combine(local_id)
437+
#[inline(always)]
438+
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
439+
let HirId { owner, local_id } = *self;
440+
format!("{}.{}", tcx.def_path_str(owner.to_def_id()), local_id.as_u32())
441+
}
442+
443+
#[inline(always)]
444+
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
445+
if dep_node.kind.fingerprint_style(tcx) == FingerprintStyle::HirId {
446+
let (local_hash, local_id) = Fingerprint::from(dep_node.hash).as_value();
447+
let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash);
448+
let owner = tcx
449+
.def_path_hash_to_def_id(def_path_hash, &mut || {
450+
panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)
451+
})
452+
.expect_local();
453+
let local_id = local_id
454+
.try_into()
455+
.unwrap_or_else(|_| panic!("local id should be u32, found {:?}", local_id));
456+
Some(HirId { owner, local_id: ItemLocalId::from_u32(local_id) })
457+
} else {
458+
None
459+
}
434460
}
435461
}

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ impl<T: DepContext> HasDepContext for T {
6767
pub enum FingerprintStyle {
6868
/// The fingerprint is actually a DefPathHash.
6969
DefPathHash,
70+
/// The fingerprint is actually a HirId.
71+
HirId,
7072
/// Query key was `()` or equivalent, so fingerprint is just zero.
7173
Unit,
7274
/// Some opaque hash.
@@ -77,7 +79,9 @@ impl FingerprintStyle {
7779
#[inline]
7880
pub fn reconstructible(self) -> bool {
7981
match self {
80-
FingerprintStyle::DefPathHash | FingerprintStyle::Unit => true,
82+
FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
83+
true
84+
}
8185
FingerprintStyle::Opaque => false,
8286
}
8387
}

0 commit comments

Comments
 (0)