From e3d63203a34cfb5d8b309894005648868053b1b7 Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Fri, 29 Apr 2022 19:43:15 +0200 Subject: [PATCH 1/3] Only compute DefKind through the query. --- compiler/rustc_middle/src/hir/map/mod.rs | 3 ++- compiler/rustc_privacy/src/lib.rs | 2 +- compiler/rustc_query_impl/src/plumbing.rs | 2 +- 3 files changed, 4 insertions(+), 3 deletions(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index c3768d5b2d6..85e3b7424ad 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -225,7 +225,8 @@ impl<'hir> Map<'hir> { self.tcx.definitions_untracked().iter_local_def_id() } - pub fn opt_def_kind(self, local_def_id: LocalDefId) -> Option { + /// Do not call this function directly. The query should be called. + pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option { let hir_id = self.local_def_id_to_hir_id(local_def_id); let def_kind = match self.find(hir_id)? { Node::Item(item) => match item.kind { diff --git a/compiler/rustc_privacy/src/lib.rs b/compiler/rustc_privacy/src/lib.rs index f5e323e2bc4..5ac8ac87fbb 100644 --- a/compiler/rustc_privacy/src/lib.rs +++ b/compiler/rustc_privacy/src/lib.rs @@ -467,7 +467,7 @@ impl<'tcx> EmbargoVisitor<'tcx> { } let macro_module_def_id = self.tcx.local_parent(local_def_id); - if self.tcx.hir().opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) { + if self.tcx.opt_def_kind(macro_module_def_id) != Some(DefKind::Mod) { // The macro's parent doesn't correspond to a `mod`, return early (#63164, #65252). return; } diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 66f4508f6b4..7f38f32e25f 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -291,7 +291,7 @@ macro_rules! define_queries { // accidentally triggering an infinite query loop. let def_kind = key.key_as_def_id() .and_then(|def_id| def_id.as_local()) - .and_then(|def_id| tcx.hir().opt_def_kind(def_id)); + .map(|def_id| tcx.def_kind(def_id)); let hash = || { let mut hcx = tcx.create_stable_hashing_context(); let mut hasher = StableHasher::new(); From 974b1e3e5185e9c9205c8fae4eed3f902d4958af Mon Sep 17 00:00:00 2001 From: Camille GILLOT Date: Tue, 5 Jul 2022 22:52:21 +0200 Subject: [PATCH 2/3] Clarify the behaviour from inside the query system. --- compiler/rustc_query_impl/src/plumbing.rs | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/compiler/rustc_query_impl/src/plumbing.rs b/compiler/rustc_query_impl/src/plumbing.rs index 7f38f32e25f..cf1bef206dd 100644 --- a/compiler/rustc_query_impl/src/plumbing.rs +++ b/compiler/rustc_query_impl/src/plumbing.rs @@ -287,11 +287,14 @@ macro_rules! define_queries { } else { Some(key.default_span(*tcx)) }; - // Use `tcx.hir().opt_def_kind()` to reduce the chance of - // accidentally triggering an infinite query loop. - let def_kind = key.key_as_def_id() - .and_then(|def_id| def_id.as_local()) - .map(|def_id| tcx.def_kind(def_id)); + let def_kind = if kind == dep_graph::DepKind::opt_def_kind { + // Try to avoid infinite recursion. + None + } else { + key.key_as_def_id() + .and_then(|def_id| def_id.as_local()) + .and_then(|def_id| tcx.opt_def_kind(def_id)) + }; let hash = || { let mut hcx = tcx.create_stable_hashing_context(); let mut hasher = StableHasher::new(); From 31629860e8bada8156c207b77b91901fc274acf7 Mon Sep 17 00:00:00 2001 From: fee1-dead Date: Wed, 6 Jul 2022 17:02:58 +0800 Subject: [PATCH 3/3] Fix double space --- compiler/rustc_middle/src/hir/map/mod.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/compiler/rustc_middle/src/hir/map/mod.rs b/compiler/rustc_middle/src/hir/map/mod.rs index 85e3b7424ad..fb003010292 100644 --- a/compiler/rustc_middle/src/hir/map/mod.rs +++ b/compiler/rustc_middle/src/hir/map/mod.rs @@ -225,7 +225,7 @@ impl<'hir> Map<'hir> { self.tcx.definitions_untracked().iter_local_def_id() } - /// Do not call this function directly. The query should be called. + /// Do not call this function directly. The query should be called. pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option { let hir_id = self.local_def_id_to_hir_id(local_def_id); let def_kind = match self.find(hir_id)? {