Put extract_def_id back on DepNode.

This commit is contained in:
Camille GILLOT 2020-03-21 09:28:37 +01:00
parent 3a8bb20230
commit d08cc0ba67
4 changed files with 28 additions and 40 deletions

View File

@ -220,6 +220,18 @@ macro_rules! define_dep_nodes {
/// single DefId/DefPathHash parameter.
fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> Self;
/// Extracts the DefId corresponding to this DepNode. This will work
/// if two conditions are met:
///
/// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
/// 2. the item that the DefPath refers to exists in the current tcx.
///
/// Condition (1) is determined by the DepKind variant of the
/// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'_>) -> Option<DefId>;
/// Used in testing
fn from_label_string(label: &str, def_path_hash: DefPathHash)
-> Result<Self, ()>;
@ -250,6 +262,15 @@ macro_rules! define_dep_nodes {
/// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id(&self, tcx: TyCtxt<'tcx>) -> Option<DefId> {
if self.kind.can_reconstruct_query_key() {
let def_path_hash = DefPathHash(self.hash);
tcx.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
} else {
None
}
}
/// Used in testing
fn from_label_string(label: &str, def_path_hash: DefPathHash) -> Result<DepNode, ()> {
let kind = match label {
@ -316,7 +337,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for DefId {
}
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
tcx.extract_def_id(dep_node)
dep_node.extract_def_id(tcx)
}
}
@ -332,7 +353,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for LocalDefId {
}
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
tcx.extract_def_id(dep_node).map(|id| id.expect_local())
dep_node.extract_def_id(tcx).map(|id| id.expect_local())
}
}
@ -349,7 +370,7 @@ impl<'tcx> DepNodeParams<TyCtxt<'tcx>> for CrateNum {
}
fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option<Self> {
tcx.extract_def_id(dep_node).map(|id| id.krate)
dep_node.extract_def_id(tcx).map(|id| id.krate)
}
}

View File

@ -1,4 +1,3 @@
use crate::hir::map::definitions::DefPathHash;
use crate::ich::StableHashingContext;
use crate::ty::{self, TyCtxt};
use rustc_data_structures::profiling::SelfProfilerRef;
@ -46,7 +45,7 @@ impl rustc_query_system::dep_graph::DepKind for DepKind {
ty::tls::with_opt(|opt_tcx| {
if let Some(tcx) = opt_tcx {
if let Some(def_id) = tcx.extract_def_id(node) {
if let Some(def_id) = node.extract_def_id(tcx) {
write!(f, "{}", tcx.def_path_debug_str(def_id))?;
} else if let Some(ref s) = tcx.dep_graph.dep_node_debug_str(*node) {
write!(f, "{}", s)?;
@ -92,32 +91,13 @@ impl<'tcx> DepContext for TyCtxt<'tcx> {
TyCtxt::create_stable_hashing_context(*self)
}
/// Extracts the DefId corresponding to this DepNode. This will work
/// if two conditions are met:
///
/// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
/// 2. the item that the DefPath refers to exists in the current tcx.
///
/// Condition (1) is determined by the DepKind variant of the
/// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id(&self, node: &DepNode) -> Option<DefId> {
if node.kind.can_reconstruct_query_key() {
let def_path_hash = DefPathHash(node.hash);
self.def_path_hash_to_def_id.as_ref()?.get(&def_path_hash).cloned()
} else {
None
}
}
fn try_force_previous_green(&self, dep_dep_node: &DepNode) -> bool {
// FIXME: This match is just a workaround for incremental bugs and should
// be removed. https://github.com/rust-lang/rust/issues/62649 is one such
// bug that must be fixed before removing this.
match dep_dep_node.kind {
DepKind::hir_owner | DepKind::hir_owner_nodes | DepKind::CrateMetadata => {
if let Some(def_id) = self.extract_def_id(dep_dep_node) {
if let Some(def_id) = dep_dep_node.extract_def_id(*self) {
if def_id_corresponds_to_hir_dep_node(*self, def_id) {
if dep_dep_node.kind == DepKind::CrateMetadata {
// The `DefPath` has corresponding node,

View File

@ -13,7 +13,7 @@
//! Errors are reported if we are in the suitable configuration but
//! the required condition is not met.
use rustc::dep_graph::{label_strs, DepContext, DepNode, DepNodeExt};
use rustc::dep_graph::{label_strs, DepNode, DepNodeExt};
use rustc::hir::map::Map;
use rustc::ty::TyCtxt;
use rustc_ast::ast::{self, Attribute, NestedMetaItem};
@ -382,7 +382,7 @@ impl DirtyCleanVisitor<'tcx> {
}
fn dep_node_str(&self, dep_node: &DepNode) -> String {
if let Some(def_id) = self.tcx.extract_def_id(dep_node) {
if let Some(def_id) = dep_node.extract_def_id(self.tcx) {
format!("{:?}({})", dep_node.kind, self.tcx.def_path_str(def_id))
} else {
format!("{:?}({:?})", dep_node.kind, dep_node.hash)

View File

@ -19,7 +19,6 @@ use rustc_data_structures::profiling::SelfProfilerRef;
use rustc_data_structures::sync::Lock;
use rustc_data_structures::thin_vec::ThinVec;
use rustc_errors::Diagnostic;
use rustc_hir::def_id::DefId;
use std::fmt;
use std::hash::Hash;
@ -34,18 +33,6 @@ pub trait DepContext: Copy {
/// Try to force a dep node to execute and see if it's green.
fn try_force_previous_green(&self, node: &DepNode<Self::DepKind>) -> bool;
/// Extracts the DefId corresponding to this DepNode. This will work
/// if two conditions are met:
///
/// 1. The Fingerprint of the DepNode actually is a DefPathHash, and
/// 2. the item that the DefPath refers to exists in the current tcx.
///
/// Condition (1) is determined by the DepKind variant of the
/// DepNode. Condition (2) might not be fulfilled if a DepNode
/// refers to something from the previous compilation session that
/// has been removed.
fn extract_def_id(&self, node: &DepNode<Self::DepKind>) -> Option<DefId>;
/// Return whether the current session is tainted by errors.
fn has_errors_or_delayed_span_bugs(&self) -> bool;