Move some code and add comments.
This commit is contained in:
parent
bd45139cb0
commit
ad09abc194
6 changed files with 195 additions and 255 deletions
|
@ -6,10 +6,11 @@ use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
|
use rustc_errors::{Applicability, Diagnostic, LintDiagnosticBuilder, MultiSpan};
|
||||||
use rustc_hir as hir;
|
use rustc_hir as hir;
|
||||||
use rustc_hir::{intravisit, HirId};
|
use rustc_hir::{intravisit, HirId};
|
||||||
|
use rustc_index::vec::IndexVec;
|
||||||
use rustc_middle::hir::nested_filter;
|
use rustc_middle::hir::nested_filter;
|
||||||
use rustc_middle::lint::{
|
use rustc_middle::lint::{
|
||||||
struct_lint_level, LevelAndSource, LintExpectation, LintLevelQueryMap, LintLevelSets,
|
reveal_actual_level, struct_lint_level, LevelAndSource, LintExpectation, LintLevelSource,
|
||||||
LintLevelSource, LintSet, LintStackIndex, COMMAND_LINE,
|
ShallowLintLevelMap,
|
||||||
};
|
};
|
||||||
use rustc_middle::ty::query::Providers;
|
use rustc_middle::ty::query::Providers;
|
||||||
use rustc_middle::ty::{RegisteredTools, TyCtxt};
|
use rustc_middle::ty::{RegisteredTools, TyCtxt};
|
||||||
|
@ -27,13 +28,80 @@ use crate::errors::{
|
||||||
UnknownToolInScopedLint,
|
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)> {
|
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
|
||||||
let store = unerased_lint_store(tcx);
|
let store = unerased_lint_store(tcx);
|
||||||
|
|
||||||
let mut builder = LintLevelsBuilder {
|
let mut builder = LintLevelsBuilder {
|
||||||
sess: tcx.sess,
|
sess: tcx.sess,
|
||||||
provider: QueryMapExpectationsWrapper {
|
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(),
|
expectations: Vec::new(),
|
||||||
unstable_to_stable_ids: FxHashMap::default(),
|
unstable_to_stable_ids: FxHashMap::default(),
|
||||||
},
|
},
|
||||||
|
@ -51,12 +119,12 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
|
||||||
builder.provider.expectations
|
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 store = unerased_lint_store(tcx);
|
||||||
|
|
||||||
let mut levels = LintLevelsBuilder {
|
let mut levels = LintLevelsBuilder {
|
||||||
sess: tcx.sess,
|
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,
|
warn_about_weird_lints: false,
|
||||||
store,
|
store,
|
||||||
registered_tools: &tcx.resolutions(()).registered_tools,
|
registered_tools: &tcx.resolutions(()).registered_tools,
|
||||||
|
@ -77,12 +145,6 @@ pub struct TopDown {
|
||||||
cur: LintStackIndex,
|
cur: LintStackIndex,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct QueryMapExpectationsWrapper<'tcx> {
|
|
||||||
map: LintLevelQueryMap<'tcx>,
|
|
||||||
expectations: Vec<(LintExpectationId, LintExpectation)>,
|
|
||||||
unstable_to_stable_ids: FxHashMap<LintExpectationId, LintExpectationId>,
|
|
||||||
}
|
|
||||||
|
|
||||||
pub trait LintLevelsProvider {
|
pub trait LintLevelsProvider {
|
||||||
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource>;
|
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource>;
|
||||||
fn current_specs_mut(&mut self) -> &mut 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<'_> {
|
impl LintLevelsProvider for LintLevelQueryMap<'_> {
|
||||||
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
|
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
|
||||||
&self.specs
|
&self.specs.specs
|
||||||
}
|
}
|
||||||
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
|
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 {
|
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<'_> {
|
impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
|
||||||
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
|
fn current_specs(&self) -> &FxHashMap<LintId, LevelAndSource> {
|
||||||
&self.map.specs
|
&self.specs.specs
|
||||||
}
|
}
|
||||||
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
|
fn current_specs_mut(&mut self) -> &mut FxHashMap<LintId, LevelAndSource> {
|
||||||
self.map.specs.clear();
|
self.specs.specs.clear();
|
||||||
&mut self.map.specs
|
&mut self.specs.specs
|
||||||
}
|
}
|
||||||
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
|
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) {
|
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") };
|
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>> {
|
impl<'tcx> LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'tcx>> {
|
||||||
fn add_id(&mut self, hir_id: HirId) {
|
fn add_id(&mut self, hir_id: HirId) {
|
||||||
self.add(
|
self.add(self.provider.tcx.hir().attrs(hir_id), hir_id == hir::CRATE_HIR_ID, Some(hir_id));
|
||||||
self.provider.map.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;
|
type NestedFilter = nested_filter::All;
|
||||||
|
|
||||||
fn nested_visit_map(&mut self) -> Self::Map {
|
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>) {
|
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
|
||||||
|
@ -225,13 +297,12 @@ pub struct LintLevelsBuilder<'s, P> {
|
||||||
registered_tools: &'s RegisteredTools,
|
registered_tools: &'s RegisteredTools,
|
||||||
}
|
}
|
||||||
|
|
||||||
pub struct BuilderPush {
|
pub(crate) struct BuilderPush {
|
||||||
prev: LintStackIndex,
|
prev: LintStackIndex,
|
||||||
pub changed: bool,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<'s> LintLevelsBuilder<'s, TopDown> {
|
impl<'s> LintLevelsBuilder<'s, TopDown> {
|
||||||
pub fn new(
|
pub(crate) fn new(
|
||||||
sess: &'s Session,
|
sess: &'s Session,
|
||||||
warn_about_weird_lints: bool,
|
warn_about_weird_lints: bool,
|
||||||
store: &'s LintStore,
|
store: &'s LintStore,
|
||||||
|
@ -289,11 +360,11 @@ impl<'s> LintLevelsBuilder<'s, TopDown> {
|
||||||
self.provider.cur = prev;
|
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.
|
/// 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;
|
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
|
/// Used to emit a lint-related diagnostic based on the current state of
|
||||||
/// this lint context.
|
/// this lint context.
|
||||||
pub fn struct_lint(
|
pub(crate) fn struct_lint(
|
||||||
&self,
|
&self,
|
||||||
lint: &'static Lint,
|
lint: &'static Lint,
|
||||||
span: Option<MultiSpan>,
|
span: Option<MultiSpan>,
|
||||||
|
@ -866,6 +937,6 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn provide(providers: &mut Providers) {
|
pub(crate) fn provide(providers: &mut Providers) {
|
||||||
*providers = Providers { lint_levels_on, lint_expectations, ..*providers };
|
*providers = Providers { shallow_lint_levels_on, lint_expectations, ..*providers };
|
||||||
}
|
}
|
||||||
|
|
|
@ -35,6 +35,7 @@
|
||||||
#![feature(iter_order_by)]
|
#![feature(iter_order_by)]
|
||||||
#![feature(let_chains)]
|
#![feature(let_chains)]
|
||||||
#![feature(let_else)]
|
#![feature(let_else)]
|
||||||
|
#![feature(min_specialization)]
|
||||||
#![feature(never_type)]
|
#![feature(never_type)]
|
||||||
#![recursion_limit = "256"]
|
#![recursion_limit = "256"]
|
||||||
|
|
||||||
|
|
|
@ -1,14 +1,11 @@
|
||||||
use std::cmp;
|
use std::cmp;
|
||||||
|
|
||||||
use rustc_data_structures::fx::FxHashMap;
|
use rustc_data_structures::fx::FxHashMap;
|
||||||
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
|
|
||||||
use rustc_errors::{Diagnostic, DiagnosticId, LintDiagnosticBuilder, MultiSpan};
|
use rustc_errors::{Diagnostic, DiagnosticId, LintDiagnosticBuilder, MultiSpan};
|
||||||
use rustc_hir::HirId;
|
use rustc_hir::HirId;
|
||||||
use rustc_index::vec::IndexVec;
|
|
||||||
use rustc_query_system::ich::StableHashingContext;
|
|
||||||
use rustc_session::lint::{
|
use rustc_session::lint::{
|
||||||
builtin::{self, FORBIDDEN_LINT_GROUPS},
|
builtin::{self, FORBIDDEN_LINT_GROUPS},
|
||||||
FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId,
|
FutureIncompatibilityReason, Level, Lint, LintId,
|
||||||
};
|
};
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
use rustc_span::hygiene::MacroKind;
|
use rustc_span::hygiene::MacroKind;
|
||||||
|
@ -61,212 +58,118 @@ impl LintLevelSource {
|
||||||
/// A tuple of a lint level and its source.
|
/// A tuple of a lint level and its source.
|
||||||
pub type LevelAndSource = (Level, LintLevelSource);
|
pub type LevelAndSource = (Level, LintLevelSource);
|
||||||
|
|
||||||
#[derive(Debug, HashStable)]
|
/// Return type for the `shallow_lint_levels_on` query.
|
||||||
pub struct LintLevelSets {
|
///
|
||||||
pub list: IndexVec<LintStackIndex, LintSet>,
|
/// This map represents the set of allowed lints and allowance levels given
|
||||||
}
|
/// by the attributes for *a single HirId*.
|
||||||
|
#[derive(Default, Debug, HashStable)]
|
||||||
rustc_index::newtype_index! {
|
pub struct ShallowLintLevelMap {
|
||||||
#[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.
|
|
||||||
pub specs: FxHashMap<LintId, LevelAndSource>,
|
pub specs: FxHashMap<LintId, LevelAndSource>,
|
||||||
|
|
||||||
pub parent: LintStackIndex,
|
|
||||||
}
|
}
|
||||||
|
|
||||||
impl LintLevelSets {
|
/// From an initial level and source, verify the effect of special annotations:
|
||||||
pub fn new() -> Self {
|
/// `warnings` lint level and lint caps.
|
||||||
LintLevelSets { list: IndexVec::new() }
|
///
|
||||||
}
|
/// The return of this function is suitable for diagnostics.
|
||||||
|
pub fn reveal_actual_level(
|
||||||
|
level: Option<Level>,
|
||||||
|
src: &mut LintLevelSource,
|
||||||
|
sess: &Session,
|
||||||
|
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.lint.default_level(sess.edition()));
|
||||||
|
|
||||||
pub fn actual_level(
|
// If we're about to issue a warning, check at the last minute for any
|
||||||
level: Option<Level>,
|
// directives against the warnings "lint". If, for example, there's an
|
||||||
src: &mut LintLevelSource,
|
// `allow(warnings)` in scope then we want to respect that instead.
|
||||||
sess: &Session,
|
//
|
||||||
lint: &'static Lint,
|
// We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
|
||||||
get_lint_id_level: impl FnOnce(LintId) -> (Option<Level>, LintLevelSource),
|
// triggers in cases (like #80988) where you have `forbid(warnings)`,
|
||||||
) -> Level {
|
// and so if we turned that into an error, it'd defeat the purpose of the
|
||||||
// If `level` is none then we actually assume the default level for this
|
// future compatibility warning.
|
||||||
// lint.
|
if level == Level::Warn && lint != LintId::of(FORBIDDEN_LINT_GROUPS) {
|
||||||
let mut level = level.unwrap_or_else(|| lint.default_level(sess.edition()));
|
let (warnings_level, warnings_src) = probe_for_lint_level(LintId::of(builtin::WARNINGS));
|
||||||
|
if let Some(configured_warning_level) = warnings_level {
|
||||||
// If we're about to issue a warning, check at the last minute for any
|
if configured_warning_level != Level::Warn {
|
||||||
// directives against the warnings "lint". If, for example, there's an
|
level = configured_warning_level;
|
||||||
// `allow(warnings)` in scope then we want to respect that instead.
|
*src = warnings_src;
|
||||||
//
|
|
||||||
// We exempt `FORBIDDEN_LINT_GROUPS` from this because it specifically
|
|
||||||
// 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 let Some(configured_warning_level) = warnings_level {
|
|
||||||
if configured_warning_level != Level::Warn {
|
|
||||||
level = configured_warning_level;
|
|
||||||
*src = warnings_src;
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Ensure that we never exceed the `--cap-lints` argument
|
// Ensure that we never exceed the `--cap-lints` argument unless the source is a --force-warn
|
||||||
// unless the source is a --force-warn
|
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn(_)) = src {
|
||||||
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)) {
|
|
||||||
// Ensure that we never exceed driver level.
|
|
||||||
level = cmp::min(*driver_level, level);
|
|
||||||
}
|
|
||||||
|
|
||||||
level
|
level
|
||||||
|
} else {
|
||||||
|
cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
|
||||||
|
};
|
||||||
|
|
||||||
|
if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
|
||||||
|
// Ensure that we never exceed driver level.
|
||||||
|
level = cmp::min(*driver_level, level);
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_lint_level(
|
level
|
||||||
&self,
|
}
|
||||||
lint: &'static Lint,
|
|
||||||
idx: LintStackIndex,
|
impl ShallowLintLevelMap {
|
||||||
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
|
/// Perform a deep probe in the HIR tree looking for the actual level for the lint.
|
||||||
sess: &Session,
|
/// This lint level is not usable for diagnostics, it needs to be corrected by
|
||||||
) -> LevelAndSource {
|
/// `reveal_actual_level` beforehand.
|
||||||
let (level, mut src) = self.get_lint_id_level(LintId::of(lint), idx, aux);
|
fn probe_for_lint_level(
|
||||||
|
|
||||||
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(
|
|
||||||
&self,
|
&self,
|
||||||
|
tcx: TyCtxt<'_>,
|
||||||
id: LintId,
|
id: LintId,
|
||||||
mut idx: LintStackIndex,
|
start: HirId,
|
||||||
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
|
|
||||||
) -> (Option<Level>, LintLevelSource) {
|
) -> (Option<Level>, LintLevelSource) {
|
||||||
if let Some(specs) = aux {
|
if let Some(&(level, src)) = self.specs.get(&id) {
|
||||||
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;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[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(
|
|
||||||
&self,
|
|
||||||
lint: &'static Lint,
|
|
||||||
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);
|
return (Some(level), src);
|
||||||
}
|
}
|
||||||
let mut cur = cur;
|
|
||||||
|
|
||||||
loop {
|
for (parent, _) in tcx.hir().parent_iter(start) {
|
||||||
let parent = tcx.hir().get_parent_node(cur);
|
let specs = tcx.shallow_lint_levels_on(parent);
|
||||||
if cur == parent {
|
if let Some(&(level, src)) = specs.specs.get(&id) {
|
||||||
return (None, LintLevelSource::Default);
|
|
||||||
}
|
|
||||||
let specs = tcx.lint_levels_on(parent);
|
|
||||||
if let Some(&(level, src)) = specs.get(&id) {
|
|
||||||
return (Some(level), src);
|
return (Some(level), src);
|
||||||
}
|
}
|
||||||
cur = parent
|
|
||||||
}
|
}
|
||||||
|
(None, LintLevelSource::Default)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn get_lint_level(
|
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
|
||||||
id: LintId,
|
pub fn lint_level_id_at_node(
|
||||||
cur: HirId,
|
&self,
|
||||||
tcx: TyCtxt<'tcx>,
|
tcx: TyCtxt<'_>,
|
||||||
specs: &FxHashMap<LintId, LevelAndSource>,
|
lint: LintId,
|
||||||
|
id: HirId,
|
||||||
) -> (Level, LintLevelSource) {
|
) -> (Level, LintLevelSource) {
|
||||||
let (level, mut src) = Self::get_lint_id_level(id, cur, tcx, specs);
|
let (level, mut src) = self.probe_for_lint_level(tcx, lint, id);
|
||||||
let level = LintLevelSets::actual_level(level, &mut src, tcx.sess, id.lint, |id| {
|
let level = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
|
||||||
Self::get_lint_id_level(id, cur, tcx, specs)
|
self.probe_for_lint_level(tcx, lint, id)
|
||||||
});
|
});
|
||||||
|
debug!(?id, ?level, ?src);
|
||||||
(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
|
/// This struct represents a lint expectation and holds all required information
|
||||||
/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
|
/// to emit the `unfulfilled_lint_expectations` lint if it is unfulfilled after
|
||||||
/// the `LateLintPass` has completed.
|
/// the `LateLintPass` has completed.
|
||||||
|
|
|
@ -273,7 +273,7 @@ rustc_queries! {
|
||||||
separate_provide_extern
|
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
|
arena_cache
|
||||||
desc { |tcx| "looking up lint levels for `{}`", key }
|
desc { |tcx| "looking up lint levels for `{}`", key }
|
||||||
}
|
}
|
||||||
|
|
|
@ -4,7 +4,7 @@ use crate::arena::Arena;
|
||||||
use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
|
use crate::dep_graph::{DepGraph, DepKind, DepKindStruct};
|
||||||
use crate::hir::place::Place as HirPlace;
|
use crate::hir::place::Place as HirPlace;
|
||||||
use crate::infer::canonical::{Canonical, CanonicalVarInfo, CanonicalVarInfos};
|
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::codegen_fn_attrs::CodegenFnAttrs;
|
||||||
use crate::middle::resolve_lifetime;
|
use crate::middle::resolve_lifetime;
|
||||||
use crate::middle::stability;
|
use crate::middle::stability;
|
||||||
|
@ -53,7 +53,7 @@ use rustc_query_system::ich::StableHashingContext;
|
||||||
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
|
use rustc_serialize::opaque::{FileEncodeResult, FileEncoder};
|
||||||
use rustc_session::config::{CrateType, OutputFilenames};
|
use rustc_session::config::{CrateType, OutputFilenames};
|
||||||
use rustc_session::cstore::CrateStoreDyn;
|
use rustc_session::cstore::CrateStoreDyn;
|
||||||
use rustc_session::lint::{Level, Lint, LintId};
|
use rustc_session::lint::Lint;
|
||||||
use rustc_session::Limit;
|
use rustc_session::Limit;
|
||||||
use rustc_session::Session;
|
use rustc_session::Session;
|
||||||
use rustc_span::def_id::{DefPathHash, StableCrateId};
|
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))
|
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`,
|
/// Emit a lint at `span` from a lint struct (some type that implements `DecorateLint`,
|
||||||
/// typically generated by `#[derive(LintDiagnostic)]`).
|
/// typically generated by `#[derive(LintDiagnostic)]`).
|
||||||
pub fn emit_spanned_lint(
|
pub fn emit_spanned_lint(
|
||||||
|
|
|
@ -1,6 +1,6 @@
|
||||||
use crate::dep_graph;
|
use crate::dep_graph;
|
||||||
use crate::infer::canonical::{self, Canonical};
|
use crate::infer::canonical::{self, Canonical};
|
||||||
use crate::lint::{LevelAndSource, LintExpectation};
|
use crate::lint::LintExpectation;
|
||||||
use crate::metadata::ModChild;
|
use crate::metadata::ModChild;
|
||||||
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
|
use crate::middle::codegen_fn_attrs::CodegenFnAttrs;
|
||||||
use crate::middle::exported_symbols::{ExportedSymbol, SymbolExportInfo};
|
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::config::{EntryFnType, OptLevel, OutputFilenames, SymbolManglingVersion};
|
||||||
use rustc_session::cstore::{CrateDepKind, CrateSource};
|
use rustc_session::cstore::{CrateDepKind, CrateSource};
|
||||||
use rustc_session::cstore::{ExternCrate, ForeignModule, LinkagePreference, NativeLib};
|
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::utils::NativeLibKind;
|
||||||
use rustc_session::Limits;
|
use rustc_session::Limits;
|
||||||
use rustc_span::symbol::Symbol;
|
use rustc_span::symbol::Symbol;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue