Skip to content

Commit d96234b

Browse files
committed
Retrieve DefKind from HIR map to reduce chance of cycles
`tcx.def_kind()` could theoretically invoke another query, which could cause an infinite query loop. Accessing the HIR map directly makes that less likely to happen. I also changed it to use `as_local()` (`tcx.def_kind()` seems to implicitly call `expect_local()`) and `opt_def_kind()` to reduce the chance of panicking on valid code.
1 parent c3df832 commit d96234b

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

compiler/rustc_query_impl/src/plumbing.rs

+6-4
Original file line numberDiff line numberDiff line change
@@ -338,10 +338,12 @@ macro_rules! define_queries {
338338
Some(key.default_span(*tcx))
339339
};
340340
let def_id = key.key_as_def_id();
341-
let def_kind = def_id.map(|def_id| {
342-
let def_kind = tcx.def_kind(def_id);
343-
$crate::util::def_kind_to_simple_def_kind(def_kind)
344-
});
341+
let def_kind = def_id
342+
.and_then(|def_id| def_id.as_local())
343+
// Use `tcx.hir().opt_def_kind()` to reduce the chance of
344+
// accidentally triggering an infinite query loop.
345+
.and_then(|def_id| tcx.hir().opt_def_kind(def_id))
346+
.map(|def_kind| $crate::util::def_kind_to_simple_def_kind(def_kind));
345347
let hash = || {
346348
let mut hcx = tcx.create_stable_hashing_context();
347349
let mut hasher = StableHasher::new();

0 commit comments

Comments
 (0)