]> git.lizzy.rs Git - rust.git/commitdiff
Use a newtype_index instead of a u32.
authorCamille GILLOT <gillot.camille@gmail.com>
Tue, 1 Dec 2020 20:47:41 +0000 (21:47 +0100)
committerCamille GILLOT <gillot.camille@gmail.com>
Tue, 29 Jun 2021 17:44:03 +0000 (19:44 +0200)
compiler/rustc_lint/src/levels.rs
compiler/rustc_middle/src/lint.rs

index c0a059b92aa9ff27536a06c0528ab25799c29bca..d190be24b60a5ab8fa25c2898e9614cf72beb0bb 100644 (file)
@@ -11,7 +11,8 @@
 use rustc_middle::lint::LevelAndSource;
 use rustc_middle::lint::LintDiagnosticBuilder;
 use rustc_middle::lint::{
-    struct_lint_level, LintLevelMap, LintLevelSets, LintLevelSource, LintSet,
+    struct_lint_level, LintLevelMap, LintLevelSets, LintLevelSource, LintSet, LintStackIndex,
+    COMMAND_LINE,
 };
 use rustc_middle::ty::query::Providers;
 use rustc_middle::ty::TyCtxt;
@@ -50,15 +51,15 @@ fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
 pub struct LintLevelsBuilder<'s> {
     sess: &'s Session,
     sets: LintLevelSets,
-    id_to_set: FxHashMap<HirId, u32>,
-    cur: u32,
+    id_to_set: FxHashMap<HirId, LintStackIndex>,
+    cur: LintStackIndex,
     warn_about_weird_lints: bool,
     store: &'s LintStore,
     crate_attrs: &'s [ast::Attribute],
 }
 
 pub struct BuilderPush {
-    prev: u32,
+    prev: LintStackIndex,
     pub changed: bool,
 }
 
@@ -72,7 +73,7 @@ pub fn new(
         let mut builder = LintLevelsBuilder {
             sess,
             sets: LintLevelSets::new(),
-            cur: 0,
+            cur: COMMAND_LINE,
             id_to_set: Default::default(),
             warn_about_weird_lints,
             store,
@@ -120,7 +121,7 @@ fn process_command_line(&mut self, sess: &Session, store: &LintStore) {
             }
         }
 
-        self.sets.list.push(LintSet::CommandLine { specs });
+        self.cur = self.sets.list.push(LintSet::CommandLine { specs });
     }
 
     /// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
@@ -523,8 +524,7 @@ pub(crate) fn push(
 
         let prev = self.cur;
         if !specs.is_empty() {
-            self.cur = self.sets.list.len() as u32;
-            self.sets.list.push(LintSet::Node { specs, parent: prev });
+            self.cur = self.sets.list.push(LintSet::Node { specs, parent: prev });
         }
 
         BuilderPush { prev, changed: prev != self.cur }
index 7461979a9fe10c99bae91370706c6325a7aad41b..bd1817684dc840f8f5250b1e6801a35080a8d28f 100644 (file)
@@ -5,6 +5,7 @@
 use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
 use rustc_errors::{DiagnosticBuilder, DiagnosticId};
 use rustc_hir::HirId;
+use rustc_index::vec::IndexVec;
 use rustc_session::lint::{
     builtin::{self, FORBIDDEN_LINT_GROUPS},
     FutureIncompatibilityReason, Level, Lint, LintId,
@@ -53,10 +54,17 @@ pub fn span(&self) -> Span {
 
 #[derive(Debug, HashStable)]
 pub struct LintLevelSets {
-    pub list: Vec<LintSet>,
+    pub list: IndexVec<LintStackIndex, LintSet>,
     pub lint_cap: Level,
 }
 
+rustc_index::newtype_index! {
+    #[derive(HashStable)]
+    pub struct LintStackIndex {
+        const COMMAND_LINE = 0,
+    }
+}
+
 #[derive(Debug, HashStable)]
 pub enum LintSet {
     CommandLine {
@@ -67,19 +75,19 @@ pub enum LintSet {
 
     Node {
         specs: FxHashMap<LintId, LevelAndSource>,
-        parent: u32,
+        parent: LintStackIndex,
     },
 }
 
 impl LintLevelSets {
     pub fn new() -> Self {
-        LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
+        LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid }
     }
 
     pub fn get_lint_level(
         &self,
         lint: &'static Lint,
-        idx: u32,
+        idx: LintStackIndex,
         aux: Option<&FxHashMap<LintId, LevelAndSource>>,
         sess: &Session,
     ) -> LevelAndSource {
@@ -122,7 +130,7 @@ pub fn get_lint_level(
     pub fn get_lint_id_level(
         &self,
         id: LintId,
-        mut idx: u32,
+        mut idx: LintStackIndex,
         aux: Option<&FxHashMap<LintId, LevelAndSource>>,
     ) -> (Option<Level>, LintLevelSource) {
         if let Some(specs) = aux {
@@ -131,7 +139,7 @@ pub fn get_lint_id_level(
             }
         }
         loop {
-            match self.list[idx as usize] {
+            match self.list[idx] {
                 LintSet::CommandLine { ref specs } => {
                     if let Some(&(level, src)) = specs.get(&id) {
                         return (Some(level), src);
@@ -152,7 +160,7 @@ pub fn get_lint_id_level(
 #[derive(Debug)]
 pub struct LintLevelMap {
     pub sets: LintLevelSets,
-    pub id_to_set: FxHashMap<HirId, u32>,
+    pub id_to_set: FxHashMap<HirId, LintStackIndex>,
 }
 
 impl LintLevelMap {