Move some code and add comments.

This commit is contained in:
Camille GILLOT 2022-09-10 01:28:08 +02:00
parent bd45139cb0
commit ad09abc194
6 changed files with 195 additions and 255 deletions

View File

@ -6,10 +6,11 @@ use rustc_data_structures::fx::FxHashMap;
use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
use rustc_hir as hir;
use rustc_hir::{intravisit, HirId};
use rustc_index::vec::IndexVec;
use rustc_middle::hir::nested_filter;
use rustc_middle::lint::{
struct_lint_level, LevelAndSource, LintExpectation, LintLevelQueryMap, LintLevelSets,
LintLevelSource, LintSet, LintStackIndex, COMMAND_LINE,
reveal_actual_level, struct_lint_level, LevelAndSource, LintExpectation, LintLevelSource,
ShallowLintLevelMap,
};
use rustc_middle::ty::query::Providers;
use rustc_middle::ty::{RegisteredTools, TyCtxt};
@ -27,13 +28,80 @@ use crate::errors::{
UnknownToolInScopedLint,
};
#[derive(Debug)]
struct LintLevelSets {
list: IndexVec<LintStackIndex, LintSet>,
}
rustc_index::newtype_index! {
struct LintStackIndex {
ENCODABLE = custom, // we don't need encoding
const COMMAND_LINE = 0,
}
}
#[derive(Debug)]
struct LintSet {
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
// flag.
specs: FxHashMap<LintId, LevelAndSource>,
parent: LintStackIndex,
}
impl LintLevelSets {
fn new() -> Self {
LintLevelSets { list: IndexVec::new() }
}
fn get_lint_level(
&self,
lint: &'static Lint,
idx: LintStackIndex,
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
sess: &Session,
) -> LevelAndSource {
let lint = LintId::of(lint);
let (level, mut src) = self.raw_lint_id_level(lint, idx, aux);
let level = reveal_actual_level(level, &mut src, sess, lint, |id| {
self.raw_lint_id_level(id, idx, aux)
});
(level, src)
}
fn raw_lint_id_level(
&self,
id: LintId,
mut idx: LintStackIndex,
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
) -> (Option<Level>, LintLevelSource) {
if let Some(specs) = aux {
if let Some(&(level, src)) = specs.get(&id) {
return (Some(level), src);
}
}
loop {
let LintSet { ref specs, parent } = self.list[idx];
if let Some(&(level, src)) = specs.get(&id) {
return (Some(level), src);
}
if idx == COMMAND_LINE {
return (None, LintLevelSource::Default);
}
idx = parent;
}
}
}
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
let store = unerased_lint_store(tcx);
let mut builder = LintLevelsBuilder {
sess: tcx.sess,
provider: QueryMapExpectationsWrapper {
map: LintLevelQueryMap { tcx, cur: hir::CRATE_HIR_ID, specs: FxHashMap::default() },
tcx,
cur: hir::CRATE_HIR_ID,
specs: ShallowLintLevelMap::default(),
expectations: Vec::new(),
unstable_to_stable_ids: FxHashMap::default(),
},
@ -51,12 +119,12 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
builder.provider.expectations
}
fn lint_levels_on(tcx: TyCtxt<'_>, hir_id: HirId) -> FxHashMap<LintId, LevelAndSource> {
fn shallow_lint_levels_on(tcx: TyCtxt<'_>, hir_id: HirId) -> ShallowLintLevelMap {
let store = unerased_lint_store(tcx);
let mut levels = LintLevelsBuilder {
sess: tcx.sess,
provider: LintLevelQueryMap { tcx, cur: hir_id, specs: FxHashMap::default() },
provider: LintLevelQueryMap { tcx, cur: hir_id, specs: ShallowLintLevelMap::default() },
warn_about_weird_lints: false,
store,
registered_tools: &tcx.resolutions(()).registered_tools,
@ -77,12 +145,6 @@ pub struct TopDown {
cur: LintStackIndex,
}
pub struct QueryMapExpectationsWrapper<'tcx> {
map: LintLevelQueryMap<'tcx>,
expectations: Vec<(LintExpectationId, LintExpectation)>,
unstable_to_stable_ids: FxHashMap<LintExpectationId, LintExpectationId>,
}
pub trait LintLevelsProvider {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource>;
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource>;
@ -104,28 +166,42 @@ impl LintLevelsProvider for TopDown {
}
}
struct LintLevelQueryMap<'tcx> {
tcx: TyCtxt<'tcx>,
cur: HirId,
specs: ShallowLintLevelMap,
}
impl LintLevelsProvider for LintLevelQueryMap<'_> {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
&self.specs
&self.specs.specs
}
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
&mut self.specs
&mut self.specs.specs
}
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
self.lint_level(lint)
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
}
}
struct QueryMapExpectationsWrapper<'tcx> {
tcx: TyCtxt<'tcx>,
cur: HirId,
specs: ShallowLintLevelMap,
expectations: Vec<(LintExpectationId, LintExpectation)>,
unstable_to_stable_ids: FxHashMap<LintExpectationId, LintExpectationId>,
}
impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
&self.map.specs
&self.specs.specs
}
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
self.map.specs.clear();
&mut self.map.specs
self.specs.specs.clear();
&mut self.specs.specs
}
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
self.map.lint_level(lint)
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
}
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation) {
let LintExpectationId::Stable { attr_id: Some(attr_id), hir_id, attr_index, .. } = id else { bug!("unstable expectation id should already be mapped") };
@ -144,11 +220,7 @@ impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
impl<'tcx> LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'tcx>> {
fn add_id(&mut self, hir_id: HirId) {
self.add(
self.provider.map.tcx.hir().attrs(hir_id),
hir_id == hir::CRATE_HIR_ID,
Some(hir_id),
);
self.add(self.provider.tcx.hir().attrs(hir_id), hir_id == hir::CRATE_HIR_ID, Some(hir_id));
}
}
@ -156,7 +228,7 @@ impl<'tcx> intravisit::Visitor<'tcx> for LintLevelsBuilder<'_, QueryMapExpectati
type NestedFilter = nested_filter::All;
fn nested_visit_map(&mut self) -> Self::Map {
self.provider.map.tcx.hir()
self.provider.tcx.hir()
}
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
@ -225,13 +297,12 @@ pub struct LintLevelsBuilder<'s, P> {
registered_tools: &'s RegisteredTools,
}
pub struct BuilderPush {
pub(crate) struct BuilderPush {
prev: LintStackIndex,
pub changed: bool,
}
impl<'s> LintLevelsBuilder<'s, TopDown> {
pub fn new(
pub(crate) fn new(
sess: &'s Session,
warn_about_weird_lints: bool,
store: &'s LintStore,
@ -289,11 +360,11 @@ impl<'s> LintLevelsBuilder<'s, TopDown> {
self.provider.cur = prev;
}
BuilderPush { prev, changed: prev != self.provider.cur }
BuilderPush { prev }
}
/// Called after `push` when the scope of a set of attributes are exited.
pub fn pop(&mut self, push: BuilderPush) {
pub(crate) fn pop(&mut self, push: BuilderPush) {
self.provider.cur = push.prev;
}
}
@ -855,7 +926,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
/// Used to emit a lint-related diagnostic based on the current state of
/// this lint context.
pub fn struct_lint(
pub(crate) fn struct_lint(
&self,
lint: &'static Lint,
span: Option<MultiSpan>,
@ -866,6 +937,6 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
}
}
pub fn provide(providers: &mut Providers) {
*providers = Providers { lint_levels_on, lint_expectations, ..*providers };
pub(crate) fn provide(providers: &mut Providers) {
*providers = Providers { shallow_lint_levels_on, lint_expectations, ..*providers };
}

View File

@ -35,6 +35,7 @@
#![feature(iter_order_by)]
#![feature(let_chains)]
#![feature(let_else)]
#![feature(min_specialization)]
#![feature(never_type)]
#![recursion_limit = "256"]

View File

@ -1,14 +1,11 @@
use std::cmp;
use rustc_data_structures::fx::FxHashMap;
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
use rustc_errors::{Diagnostic, DiagnosticId, LintDiagnosticBuilder, MultiSpan};
use rustc_hir::HirId;
use rustc_index::vec::IndexVec;
use rustc_query_system::ich::StableHashingContext;
use rustc_session::lint::{
builtin::{self, FORBIDDEN_LINT_GROUPS},
FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId,
FutureIncompatibilityReason, Level, Lint, LintId,
};
use rustc_session::Session;
use rustc_span::hygiene::MacroKind;
@ -61,42 +58,28 @@ impl LintLevelSource {
/// A tuple of a lint level and its source.
pub type LevelAndSource = (Level, LintLevelSource);
#[derive(Debug, HashStable)]
pub struct LintLevelSets {
pub list: IndexVec<LintStackIndex, LintSet>,
}
rustc_index::newtype_index! {
#[derive(HashStable)]
pub struct LintStackIndex {
const COMMAND_LINE = 0,
}
}
#[derive(Debug, HashStable)]
pub struct LintSet {
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
// flag.
/// Return type for the `shallow_lint_levels_on` query.
///
/// This map represents the set of allowed lints and allowance levels given
/// by the attributes for *a single HirId*.
#[derive(Default, Debug, HashStable)]
pub struct ShallowLintLevelMap {
pub specs: FxHashMap<LintId, LevelAndSource>,
pub parent: LintStackIndex,
}
impl LintLevelSets {
pub fn new() -> Self {
LintLevelSets { list: IndexVec::new() }
}
pub fn actual_level(
/// From an initial level and source, verify the effect of special annotations:
/// `warnings` lint level and lint caps.
///
/// The return of this function is suitable for diagnostics.
pub fn reveal_actual_level(
level: Option<Level>,
src: &mut LintLevelSource,
sess: &Session,
lint: &'static Lint,
get_lint_id_level: impl FnOnce(LintId) -> (Option<Level>, LintLevelSource),
lint: LintId,
probe_for_lint_level: impl FnOnce(LintId) -> (Option<Level>, LintLevelSource),
) -> Level {
// If `level` is none then we actually assume the default level for this
// lint.
let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
// If `level` is none then we actually assume the default level for this lint.
let mut level = level.unwrap_or_else(|| lint.lint.default_level(sess.edition()));
// If we're about to issue a warning, check at the last minute for any
// directives against the warnings "lint". If, for example, there's an
@ -106,8 +89,8 @@ impl LintLevelSets {
// triggers in cases (like #80988) where you have `forbid(warnings)`,
// and so if we turned that into an error, it'd defeat the purpose of the
// future compatibility warning.
if level == Level::Warn && LintId::of(lint) != LintId::of(FORBIDDEN_LINT_GROUPS) {
let (warnings_level, warnings_src) = get_lint_id_level(LintId::of(builtin::WARNINGS));
if level == Level::Warn && lint != LintId::of(FORBIDDEN_LINT_GROUPS) {
let (warnings_level, warnings_src) = probe_for_lint_level(LintId::of(builtin::WARNINGS));
if let Some(configured_warning_level) = warnings_level {
if configured_warning_level != Level::Warn {
level = configured_warning_level;
@ -116,15 +99,14 @@ impl LintLevelSets {
}
}
// Ensure that we never exceed the `--cap-lints` argument
// unless the source is a --force-warn
// Ensure that we never exceed the `--cap-lints` argument unless the source is a --force-warn
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src {
level
} else {
cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
};
if let Some(driver_level) = sess.driver_lint_caps.get(&LintId::of(lint)) {
if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
// Ensure that we never exceed driver level.
level = cmp::min(*driver_level, level);
}
@ -132,141 +114,62 @@ impl LintLevelSets {
level
}
pub fn get_lint_level(
&self,
lint: &'static Lint,
idx: LintStackIndex,
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
sess: &Session,
) -> LevelAndSource {
let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
let level = Self::actual_level(level, &mut src, sess, lint, |id| {
self.get_lint_id_level(id, idx, aux)
});
(level, src)
}
pub fn get_lint_id_level(
impl ShallowLintLevelMap {
/// Perform a deep probe in the HIR tree looking for the actual level for the lint.
/// This lint level is not usable for diagnostics, it needs to be corrected by
/// `reveal_actual_level` beforehand.
fn probe_for_lint_level(
&self,
tcx: TyCtxt<'_>,
id: LintId,
mut idx: LintStackIndex,
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
start: HirId,
) -> (Option<Level>, LintLevelSource) {
if let Some(specs) = aux {
if let Some(&(level, src)) = specs.get(&id) {
if let Some(&(level, src)) = self.specs.get(&id) {
return (Some(level), src);
}
for (parent, _) in tcx.hir().parent_iter(start) {
let specs = tcx.shallow_lint_levels_on(parent);
if let Some(&(level, src)) = specs.specs.get(&id) {
return (Some(level), src);
}
}
loop {
let LintSet { ref specs, parent } = self.list[idx];
if let Some(&(level, src)) = specs.get(&id) {
return (Some(level), src);
}
if idx == COMMAND_LINE {
return (None, LintLevelSource::Default);
}
idx = parent;
}
}
(None, LintLevelSource::Default)
}
#[derive(Debug)]
pub struct LintLevelMap {
/// This is a collection of lint expectations as described in RFC 2383, that
/// can be fulfilled during this compilation session. This means that at least
/// one expected lint is currently registered in the lint store.
///
/// The [`LintExpectationId`] is stored as a part of the [`Expect`](Level::Expect)
/// lint level.
pub lint_expectations: Vec<(LintExpectationId, LintExpectation)>,
pub sets: LintLevelSets,
pub id_to_set: FxHashMap<HirId, LintStackIndex>,
}
impl LintLevelMap {
/// If the `id` was previously registered with `register_id` when building
/// this `LintLevelMap` this returns the corresponding lint level and source
/// of the lint level for the lint provided.
///
/// If the `id` was not previously registered, returns `None`. If `None` is
/// returned then the parent of `id` should be acquired and this function
/// should be called again.
pub fn level_and_source(
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
pub fn lint_level_id_at_node(
&self,
lint: &'static Lint,
tcx: TyCtxt<'_>,
lint: LintId,
id: HirId,
session: &Session,
) -> Option<LevelAndSource> {
self.id_to_set.get(&id).map(|idx| self.sets.get_lint_level(lint, *idx, None, session))
}
}
impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
#[inline]
fn hash_stable(&self, hcx: &mut StableHashingContext<'a>, hasher: &mut StableHasher) {
let LintLevelMap { ref sets, ref id_to_set, ref lint_expectations } = *self;
id_to_set.hash_stable(hcx, hasher);
lint_expectations.hash_stable(hcx, hasher);
hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher))
}
}
pub struct LintLevelQueryMap<'tcx> {
pub tcx: TyCtxt<'tcx>,
pub cur: HirId,
pub specs: FxHashMap<LintId, LevelAndSource>,
}
impl<'tcx> LintLevelQueryMap<'tcx> {
pub fn lint_id_level(&self, id: LintId) -> (Option<Level>, LintLevelSource) {
Self::get_lint_id_level(id, self.cur, self.tcx, &self.specs)
}
pub fn lint_level(&self, lint: &'static Lint) -> LevelAndSource {
Self::get_lint_level(LintId::of(lint), self.cur, self.tcx, &self.specs)
}
pub fn get_lint_id_level(
id: LintId,
cur: HirId,
tcx: TyCtxt<'tcx>,
specs: &FxHashMap<LintId, LevelAndSource>,
) -> (Option<Level>, LintLevelSource) {
if let Some(&(level, src)) = specs.get(&id) {
return (Some(level), src);
}
let mut cur = cur;
loop {
let parent = tcx.hir().get_parent_node(cur);
if cur == parent {
return (None, LintLevelSource::Default);
}
let specs = tcx.lint_levels_on(parent);
if let Some(&(level, src)) = specs.get(&id) {
return (Some(level), src);
}
cur = parent
}
}
pub fn get_lint_level(
id: LintId,
cur: HirId,
tcx: TyCtxt<'tcx>,
specs: &FxHashMap<LintId, LevelAndSource>,
) -> (Level, LintLevelSource) {
let (level, mut src) = Self::get_lint_id_level(id, cur, tcx, specs);
let level = LintLevelSets::actual_level(level, &mut src, tcx.sess, id.lint, |id| {
Self::get_lint_id_level(id, cur, tcx, specs)
let (level, mut src) = self.probe_for_lint_level(tcx, lint, id);
let level = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
self.probe_for_lint_level(tcx, lint, id)
});
debug!(?id, ?level, ?src);
(level, src)
}
}
impl TyCtxt<'_> {
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
pub fn lint_level_at_node(self, lint: &'static Lint, id: HirId) -> (Level, LintLevelSource) {
self.shallow_lint_levels_on(id).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();
while id != bound && self.shallow_lint_levels_on(id).specs.is_empty() {
id = hir.get_parent_node(id)
}
id
}
}
/// This struct represents a lint expectation and holds all required information
/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
/// the `LateLintPass` has completed.

View File

@ -273,7 +273,7 @@ rustc_queries! {
separate_provide_extern
}
query lint_levels_on(key: HirId) -> FxHashMap<LintId, LevelAndSource> {
query shallow_lint_levels_on(key: HirId) -> rustc_middle::lint::ShallowLintLevelMap {
arena_cache
desc { |tcx| "looking up lint levels for `{}`", key }
}

View File

@ -4,7 +4,7 @@ use crate::arena::Arena;
use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
use crate::hir::place::Place as HirPlace;
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
use crate::lint::{struct_lint_level, LintLevelSource};
use crate::lint::struct_lint_level;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::resolve_lifetime;
use crate::middle::stability;
@ -53,7 +53,7 @@ use rustc_query_system::ich::StableHashingContext;
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
use rustc_session::config::{CrateType, OutputFilenames};
use rustc_session::cstore::CrateStoreDyn;
use rustc_session::lint::{Level, Lint, LintId};
use rustc_session::lint::Lint;
use rustc_session::Limit;
use rustc_session::Session;
use rustc_span::def_id::{DefPathHash, StableCrateId};
@ -2809,41 +2809,6 @@ impl<'tcx> TyCtxt<'tcx> {
iter.intern_with(|xs| self.intern_bound_variable_kinds(xs))
}
/// 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.get_parent_node(id);
if next == id {
bug!("lint traversal reached the root of the crate");
}
id = next;
}
}
pub fn lint_level_at_node(
self,
lint: &'static Lint,
id: hir::HirId,
) -> (Level, LintLevelSource) {
let level_and_src = crate::lint::LintLevelQueryMap::get_lint_level(
LintId::of(lint),
id,
self,
self.lint_levels_on(id),
);
debug!(?id, ?level_and_src);
level_and_src
}
/// Emit a lint at `span` from a lint struct (some type that implements `DecorateLint`,
/// typically generated by `#[derive(LintDiagnostic)]`).
pub fn emit_spanned_lint(

View File

@ -1,6 +1,6 @@
use crate::dep_graph;
use crate::infer::canonical::{self, Canonical};
use crate::lint::{LevelAndSource, LintExpectation};
use crate::lint::LintExpectation;
use crate::metadata::ModChild;
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
@ -51,7 +51,7 @@ use rustc_index::{bit_set::FiniteBitSet, vec::IndexVec};
use rustc_session::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
use rustc_session::cstore::{CrateDepKind, CrateSource};
use rustc_session::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLib};
use rustc_session::lint::{LintExpectationId, LintId};
use rustc_session::lint::LintExpectationId;
use rustc_session::utils::NativeLibKind;
use rustc_session::Limits;
use rustc_span::symbol::Symbol;