Move maybe_lint_level_root_bounded.

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`.
This commit is contained in:
Nicholas Nethercote 2023-07-11 22:07:28 +10:00
parent 36458109ae
commit f234dc3e1c
2 changed files with 24 additions and 23 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

View File

@ -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)]
@ -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),
)
};
@ -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,