Skip to content

Do not fetch HIR node when iterating to find lint. #101862

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 1 commit into from
Sep 18, 2022
Merged
Show file tree
Hide file tree
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
20 changes: 12 additions & 8 deletions compiler/rustc_middle/src/hir/map/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ pub struct ParentHirIterator<'hir> {
}

impl<'hir> Iterator for ParentHirIterator<'hir> {
type Item = (HirId, Node<'hir>);
type Item = HirId;

fn next(&mut self) -> Option<Self::Item> {
if self.current_id == CRATE_HIR_ID {
Expand All @@ -77,10 +77,7 @@ impl<'hir> Iterator for ParentHirIterator<'hir> {
}

self.current_id = parent_id;
if let Some(node) = self.map.find(parent_id) {
return Some((parent_id, node));
}
// If this `HirId` doesn't have an entry, skip it and look for its `parent_id`.
return Some(parent_id);
}
}
}
Expand Down Expand Up @@ -393,8 +390,8 @@ impl<'hir> Map<'hir> {
}

pub fn enclosing_body_owner(self, hir_id: HirId) -> LocalDefId {
for (parent, _) in self.parent_iter(hir_id) {
if let Some(body) = self.find(parent).map(associated_body).flatten() {
for (_, node) in self.parent_iter(hir_id) {
if let Some(body) = associated_body(node) {
return self.body_owner_def_id(body);
}
}
Expand Down Expand Up @@ -635,10 +632,17 @@ impl<'hir> Map<'hir> {
/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
#[inline]
pub fn parent_iter(self, current_id: HirId) -> ParentHirIterator<'hir> {
pub fn parent_id_iter(self, current_id: HirId) -> impl Iterator<Item = HirId> + 'hir {
ParentHirIterator { current_id, map: self }
}

/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
#[inline]
pub fn parent_iter(self, current_id: HirId) -> impl Iterator<Item = (HirId, Node<'hir>)> {
self.parent_id_iter(current_id).filter_map(move |id| Some((id, self.find(id)?)))
}

/// Returns an iterator for the nodes in the ancestor tree of the `current_id`
/// until the crate root is reached. Prefer this over your own loop using `get_parent_node`.
#[inline]
Expand Down
2 changes: 1 addition & 1 deletion compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ impl ShallowLintLevelMap {
return (Some(level), src);
}

for (parent, _) in tcx.hir().parent_iter(start) {
for parent in tcx.hir().parent_id_iter(start) {
let specs = tcx.shallow_lint_levels_on(parent);
if let Some(&(level, src)) = specs.specs.get(&id) {
return (Some(level), src);
Expand Down