about summary refs log tree commit diff
path: root/compiler/rustc_lint/src
diff options
context:
space:
mode:
authorCamille GILLOT <gillot.camille@gmail.com>2022-09-10 01:28:08 +0200
committerCamille GILLOT <gillot.camille@gmail.com>2022-09-14 19:06:05 +0200
commitad09abc19415d48e24dbb231c85e521d909e77f7 (patch)
treef72ef57322d484c8b224187bfa6bb749164d4c75 /compiler/rustc_lint/src
parentbd45139cb014d51adaff46b9797504bc6eac2564 (diff)
downloadrust-ad09abc19415d48e24dbb231c85e521d909e77f7.tar.gz
rust-ad09abc19415d48e24dbb231c85e521d909e77f7.zip
Move some code and add comments.
Diffstat (limited to 'compiler/rustc_lint/src')
-rw-r--r--compiler/rustc_lint/src/levels.rs135
-rw-r--r--compiler/rustc_lint/src/lib.rs1
2 files changed, 104 insertions, 32 deletions
diff --git a/compiler/rustc_lint/src/levels.rs b/compiler/rustc_lint/src/levels.rs
index 1626a4a99df..9281d57ac64 100644
--- a/compiler/rustc_lint/src/levels.rs
+++ b/compiler/rustc_lint/src/levels.rs
@@ -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 };
 }
diff --git a/compiler/rustc_lint/src/lib.rs b/compiler/rustc_lint/src/lib.rs
index e0e6f1a8c08..2a22a6e5be1 100644
--- a/compiler/rustc_lint/src/lib.rs
+++ b/compiler/rustc_lint/src/lib.rs
@@ -35,6 +35,7 @@
 #![feature(iter_order_by)]
 #![feature(let_chains)]
 #![feature(let_else)]
+#![feature(min_specialization)]
 #![feature(never_type)]
 #![recursion_limit = "256"]