1
Fork 0

Auto merge of #113609 - nnethercote:maybe_lint_level_root_bounded-cache, r=cjgillot

Add a cache for `maybe_lint_level_root_bounded`

`maybe_lint_level_root_bounded` is called many times and traces node sub-paths many times. This PR adds a cache that lets many of these tracings be skipped, avoiding lots of calls to functions like `Map::attrs` and `Map::parent_id`.

r? `@cjgillot`
This commit is contained in:
bors 2023-07-14 05:30:53 +00:00
commit fe03b46ee4
3 changed files with 71 additions and 32 deletions

View file

@ -169,26 +169,6 @@ impl TyCtxt<'_> {
pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> (Level, LintLevelSource) {
self.shallow_lint_levels_on(id.owner).lint_level_id_at_node(self, LintId::of(lint), id)
}
/// Walks upwards from `id` to find a node which might change lint levels with attributes.
/// It stops at `bound` and just returns it if reached.
pub fn maybe_lint_level_root_bounded(self, mut id: HirId, bound: HirId) -> HirId {
let hir = self.hir();
loop {
if id == bound {
return bound;
}
if hir.attrs(id).iter().any(|attr| Level::from_attr(attr).is_some()) {
return id;
}
let next = hir.parent_id(id);
if next == id {
bug!("lint traversal reached the root of the crate");
}
id = next;
}
}
}
/// This struct represents a lint expectation and holds all required information