From: Camille GILLOT Date: Sat, 21 Mar 2020 08:28:37 +0000 (+0100) Subject: Put extract_def_id back on DepNode. X-Git-Url: https://git.lizzy.rs/?a=commitdiff_plain;h=d08cc0ba67d9989548ae163083b7e3a3dec6b9a2;p=rust.git Put extract_def_id back on DepNode. --- diff --git a/src/librustc/dep_graph/dep_node.rs b/src/librustc/dep_graph/dep_node.rs index ee44c07d3a9..fdcc1a0db05 100644 --- a/src/librustc/dep_graph/dep_node.rs +++ b/src/librustc/dep_graph/dep_node.rs @@ -220,6 +220,18 @@ pub trait DepNodeExt: Sized { /// 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; + /// Used in testing fn from_label_string(label: &str, def_path_hash: DefPathHash) -> Result; @@ -250,6 +262,15 @@ fn from_def_path_hash(def_path_hash: DefPathHash, kind: DepKind) -> DepNode { /// 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 { + 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 { let kind = match label { @@ -316,7 +337,7 @@ fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String { } fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option { - tcx.extract_def_id(dep_node) + dep_node.extract_def_id(tcx) } } @@ -332,7 +353,7 @@ fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String { } fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option { - 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 @@ fn to_debug_str(&self, tcx: TyCtxt<'tcx>) -> String { } fn recover(tcx: TyCtxt<'tcx>, dep_node: &DepNode) -> Option { - tcx.extract_def_id(dep_node).map(|id| id.krate) + dep_node.extract_def_id(tcx).map(|id| id.krate) } } diff --git a/src/librustc/dep_graph/mod.rs b/src/librustc/dep_graph/mod.rs index 5ccc0d281db..47cc5f58559 100644 --- a/src/librustc/dep_graph/mod.rs +++ b/src/librustc/dep_graph/mod.rs @@ -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 @@ fn debug_node(node: &DepNode, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Resu 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 @@ fn create_stable_hashing_context(&self) -> Self::StableHashingContext { 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 { - 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, diff --git a/src/librustc_incremental/persist/dirty_clean.rs b/src/librustc_incremental/persist/dirty_clean.rs index 4fe23a9f7ae..9ddd238afff 100644 --- a/src/librustc_incremental/persist/dirty_clean.rs +++ b/src/librustc_incremental/persist/dirty_clean.rs @@ -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 @@ fn dep_nodes<'l>( } 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) diff --git a/src/librustc_query_system/dep_graph/mod.rs b/src/librustc_query_system/dep_graph/mod.rs index e6a927c11dd..a54b8497fde 100644 --- a/src/librustc_query_system/dep_graph/mod.rs +++ b/src/librustc_query_system/dep_graph/mod.rs @@ -19,7 +19,6 @@ 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) -> 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) -> Option; - /// Return whether the current session is tainted by errors. fn has_errors_or_delayed_span_bugs(&self) -> bool;