Skip to content

Only compute DefKind through the query. #98881

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Jul 6, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
@@ -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<DefKind> {
/// Do not call this function directly. The query should be called.
pub(super) fn opt_def_kind(self, local_def_id: LocalDefId) -> Option<DefKind> {
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 {
2 changes: 1 addition & 1 deletion compiler/rustc_privacy/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 8 additions & 5 deletions compiler/rustc_query_impl/src/plumbing.rs
Original file line number Diff line number Diff line change
@@ -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())
.and_then(|def_id| tcx.hir().opt_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();