Skip to content

Commit

Permalink
Put extract_def_id back on DepNode.
Browse files Browse the repository at this point in the history
  • Loading branch information
cjgillot committed Mar 23, 2020
1 parent 3a8bb20 commit d08cc0b
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 40 deletions.
27 changes: 24 additions & 3 deletions src/librustc/dep_graph/dep_node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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, ()>;
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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)
}
}

Expand All @@ -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())
}
}

Expand All @@ -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)
}
}

Expand Down
24 changes: 2 additions & 22 deletions src/librustc/dep_graph/mod.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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)?;
Expand Down Expand Up @@ -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,
Expand Down
4 changes: 2 additions & 2 deletions src/librustc_incremental/persist/dirty_clean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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)
Expand Down
13 changes: 0 additions & 13 deletions src/librustc_query_system/dep_graph/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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;

Expand Down

0 comments on commit d08cc0b

Please sign in to comment.