Skip to content

Commit 6019cbb

Browse files
committed
Allow query system to recover a HirId.
1 parent 744e397 commit 6019cbb

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, OwnerId};
65+
use rustc_hir::{HirId, ItemLocalId, OwnerId};
6666
use rustc_query_system::dep_graph::FingerprintStyle;
6767
use rustc_span::symbol::Symbol;
6868
use std::hash::Hash;
@@ -194,7 +194,7 @@ impl DepNodeExt for DepNode {
194194
let kind = dep_kind_from_label_string(label)?;
195195

196196
match tcx.fingerprint_style(kind) {
197-
FingerprintStyle::Opaque => Err(()),
197+
FingerprintStyle::Opaque | FingerprintStyle::HirId => Err(()),
198198
FingerprintStyle::Unit => Ok(DepNode::new_no_params(tcx, kind)),
199199
FingerprintStyle::DefPathHash => {
200200
Ok(DepNode::from_def_path_hash(tcx, def_path_hash, kind))
@@ -344,7 +344,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for (DefId, DefId) {
344344
impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
345345
#[inline(always)]
346346
fn fingerprint_style() -> FingerprintStyle {
347-
FingerprintStyle::Opaque
347+
FingerprintStyle::HirId
348348
}
349349

350350
// We actually would not need to specialize the implementation of this
@@ -353,10 +353,36 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for HirId {
353353
#[inline(always)]
354354
fn to_fingerprint(&self, tcx: TyCtxt<'tcx>) -> Fingerprint {
355355
let HirId { owner, local_id } = *self;
356-
357356
let def_path_hash = tcx.def_path_hash(owner.to_def_id());
358-
let local_id = Fingerprint::from_smaller_hash(local_id.as_u32().into());
357+
Fingerprint::new(
358+
// `owner` is local, so is completely defined by the local hash
359+
def_path_hash.local_hash(),
360+
local_id.as_u32().into(),
361+
)
362+
}
359363

360-
def_path_hash.0.combine(local_id)
364+
#[inline(always)]
365+
fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String {
366+
let HirId { owner, local_id } = *self;
367+
format!("{}.{}", tcx.def_path_str(owner.to_def_id()), local_id.as_u32())
368+
}
369+
370+
#[inline(always)]
371+
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
372+
if tcx.fingerprint_style(dep_node.kind) == FingerprintStyle::HirId {
373+
let (local_hash, local_id) = Fingerprint::from(dep_node.hash).as_value();
374+
let def_path_hash = DefPathHash::new(tcx.sess.local_stable_crate_id(), local_hash);
375+
let def_id = tcx
376+
.def_path_hash_to_def_id(def_path_hash, &mut || {
377+
panic!("Failed to extract HirId: {:?} {}", dep_node.kind, dep_node.hash)
378+
})
379+
.expect_local();
380+
let local_id = local_id
381+
.try_into()
382+
.unwrap_or_else(|_| panic!("local id should be u32, found {:?}", local_id));
383+
Some(HirId { owner: OwnerId { def_id }, local_id: ItemLocalId::from_u32(local_id) })
384+
} else {
385+
None
386+
}
361387
}
362388
}

compiler/rustc_query_system/src/dep_graph/mod.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,8 @@ impl<T: DepContext> HasDepContext for T {
9494
pub enum FingerprintStyle {
9595
/// The fingerprint is actually a DefPathHash.
9696
DefPathHash,
97+
/// The fingerprint is actually a HirId.
98+
HirId,
9799
/// Query key was `()` or equivalent, so fingerprint is just zero.
98100
Unit,
99101
/// Some opaque hash.
@@ -104,7 +106,9 @@ impl FingerprintStyle {
104106
#[inline]
105107
pub fn reconstructible(self) -> bool {
106108
match self {
107-
FingerprintStyle::DefPathHash | FingerprintStyle::Unit => true,
109+
FingerprintStyle::DefPathHash | FingerprintStyle::Unit | FingerprintStyle::HirId => {
110+
true
111+
}
108112
FingerprintStyle::Opaque => false,
109113
}
110114
}

0 commit comments

Comments
 (0)