[go: nahoru, domu]

Skip to content

Commit

Permalink
Move maybe_lint_level_root_bounded.
Browse files Browse the repository at this point in the history
From `TyCtxt` to the MIR `Builder`. This will allow us to add a cache to
`Builder` and use it from `maybe_lint_level_root_bounded`.
  • Loading branch information
nnethercote committed Jul 12, 2023
1 parent 3645810 commit f234dc3
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 23 deletions.
20 changes: 0 additions & 20 deletions compiler/rustc_middle/src/lint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
27 changes: 24 additions & 3 deletions compiler/rustc_mir_build/src/build/scope.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,8 +90,8 @@ use rustc_index::{IndexSlice, IndexVec};
use rustc_middle::middle::region;
use rustc_middle::mir::*;
use rustc_middle::thir::{Expr, LintLevel};

use rustc_middle::ty::Ty;
use rustc_session::lint::Level;
use rustc_span::{Span, DUMMY_SP};

#[derive(Debug)]
Expand Down Expand Up @@ -773,8 +773,8 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
// to avoid adding Hir dependencies on our parents.
// We estimate the true lint roots here to avoid creating a lot of source scopes.
(
self.tcx.maybe_lint_level_root_bounded(current_id, self.hir_id),
self.tcx.maybe_lint_level_root_bounded(parent_id, self.hir_id),
self.maybe_lint_level_root_bounded(current_id, self.hir_id),
self.maybe_lint_level_root_bounded(parent_id, self.hir_id),
)
};

Expand All @@ -784,6 +784,27 @@ impl<'a, 'tcx> Builder<'a, 'tcx> {
}
}

/// 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.
fn maybe_lint_level_root_bounded(&self, mut id: HirId, bound: HirId) -> HirId {
let hir = self.tcx.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;
}
}

/// Creates a new source scope, nested in the current one.
pub(crate) fn new_source_scope(
&mut self,
Expand Down

0 comments on commit f234dc3

Please sign in to comment.