Skip to content

Commit d5a406b

Browse files
committed
Auto merge of #82985 - cjgillot:lint, r=jackh726
Cleanup the computation of lint levels This now uses an `IndexVec` and a special root `LintStackIndex = 0` to encode command-line levels.
2 parents 72b0c7d + 5a731ff commit d5a406b

File tree

2 files changed

+37
-62
lines changed

2 files changed

+37
-62
lines changed

compiler/rustc_lint/src/levels.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,8 @@ use rustc_middle::hir::map::Map;
1111
use rustc_middle::lint::LevelAndSource;
1212
use rustc_middle::lint::LintDiagnosticBuilder;
1313
use rustc_middle::lint::{
14-
struct_lint_level, LintLevelMap, LintLevelSets, LintLevelSource, LintSet,
14+
struct_lint_level, LintLevelMap, LintLevelSets, LintLevelSource, LintSet, LintStackIndex,
15+
COMMAND_LINE,
1516
};
1617
use rustc_middle::ty::query::Providers;
1718
use rustc_middle::ty::TyCtxt;
@@ -50,15 +51,15 @@ fn lint_levels(tcx: TyCtxt<'_>, (): ()) -> LintLevelMap {
5051
pub struct LintLevelsBuilder<'s> {
5152
sess: &'s Session,
5253
sets: LintLevelSets,
53-
id_to_set: FxHashMap<HirId, u32>,
54-
cur: u32,
54+
id_to_set: FxHashMap<HirId, LintStackIndex>,
55+
cur: LintStackIndex,
5556
warn_about_weird_lints: bool,
5657
store: &'s LintStore,
5758
crate_attrs: &'s [ast::Attribute],
5859
}
5960

6061
pub struct BuilderPush {
61-
prev: u32,
62+
prev: LintStackIndex,
6263
pub changed: bool,
6364
}
6465

@@ -72,7 +73,7 @@ impl<'s> LintLevelsBuilder<'s> {
7273
let mut builder = LintLevelsBuilder {
7374
sess,
7475
sets: LintLevelSets::new(),
75-
cur: 0,
76+
cur: COMMAND_LINE,
7677
id_to_set: Default::default(),
7778
warn_about_weird_lints,
7879
store,
@@ -120,7 +121,7 @@ impl<'s> LintLevelsBuilder<'s> {
120121
}
121122
}
122123

123-
self.sets.list.push(LintSet::CommandLine { specs });
124+
self.cur = self.sets.list.push(LintSet { specs, parent: COMMAND_LINE });
124125
}
125126

126127
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
@@ -523,8 +524,7 @@ impl<'s> LintLevelsBuilder<'s> {
523524

524525
let prev = self.cur;
525526
if !specs.is_empty() {
526-
self.cur = self.sets.list.len() as u32;
527-
self.sets.list.push(LintSet::Node { specs, parent: prev });
527+
self.cur = self.sets.list.push(LintSet { specs, parent: prev });
528528
}
529529

530530
BuilderPush { prev, changed: prev != self.cur }

compiler/rustc_middle/src/lint.rs

+29-54
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use rustc_data_structures::fx::FxHashMap;
55
use rustc_data_structures::stable_hasher::{HashStable, StableHasher};
66
use rustc_errors::{DiagnosticBuilder, DiagnosticId};
77
use rustc_hir::HirId;
8+
use rustc_index::vec::IndexVec;
89
use rustc_session::lint::{
910
builtin::{self, FORBIDDEN_LINT_GROUPS},
1011
FutureIncompatibilityReason, Level, Lint, LintId,
@@ -51,35 +52,37 @@ impl LintLevelSource {
5152
/// A tuple of a lint level and its source.
5253
pub type LevelAndSource = (Level, LintLevelSource);
5354

54-
#[derive(Debug)]
55+
#[derive(Debug, HashStable)]
5556
pub struct LintLevelSets {
56-
pub list: Vec<LintSet>,
57+
pub list: IndexVec<LintStackIndex, LintSet>,
5758
pub lint_cap: Level,
5859
}
5960

60-
#[derive(Debug)]
61-
pub enum LintSet {
62-
CommandLine {
63-
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
64-
// flag.
65-
specs: FxHashMap<LintId, LevelAndSource>,
66-
},
67-
68-
Node {
69-
specs: FxHashMap<LintId, LevelAndSource>,
70-
parent: u32,
71-
},
61+
rustc_index::newtype_index! {
62+
#[derive(HashStable)]
63+
pub struct LintStackIndex {
64+
const COMMAND_LINE = 0,
65+
}
66+
}
67+
68+
#[derive(Debug, HashStable)]
69+
pub struct LintSet {
70+
// -A,-W,-D flags, a `Symbol` for the flag itself and `Level` for which
71+
// flag.
72+
pub specs: FxHashMap<LintId, LevelAndSource>,
73+
74+
pub parent: LintStackIndex,
7275
}
7376

7477
impl LintLevelSets {
7578
pub fn new() -> Self {
76-
LintLevelSets { list: Vec::new(), lint_cap: Level::Forbid }
79+
LintLevelSets { list: IndexVec::new(), lint_cap: Level::Forbid }
7780
}
7881

7982
pub fn get_lint_level(
8083
&self,
8184
lint: &'static Lint,
82-
idx: u32,
85+
idx: LintStackIndex,
8386
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
8487
sess: &Session,
8588
) -> LevelAndSource {
@@ -122,7 +125,7 @@ impl LintLevelSets {
122125
pub fn get_lint_id_level(
123126
&self,
124127
id: LintId,
125-
mut idx: u32,
128+
mut idx: LintStackIndex,
126129
aux: Option<&FxHashMap<LintId, LevelAndSource>>,
127130
) -> (Option<Level>, LintLevelSource) {
128131
if let Some(specs) = aux {
@@ -131,28 +134,22 @@ impl LintLevelSets {
131134
}
132135
}
133136
loop {
134-
match self.list[idx as usize] {
135-
LintSet::CommandLine { ref specs } => {
136-
if let Some(&(level, src)) = specs.get(&id) {
137-
return (Some(level), src);
138-
}
139-
return (None, LintLevelSource::Default);
140-
}
141-
LintSet::Node { ref specs, parent } => {
142-
if let Some(&(level, src)) = specs.get(&id) {
143-
return (Some(level), src);
144-
}
145-
idx = parent;
146-
}
137+
let LintSet { ref specs, parent } = self.list[idx];
138+
if let Some(&(level, src)) = specs.get(&id) {
139+
return (Some(level), src);
140+
}
141+
if idx == COMMAND_LINE {
142+
return (None, LintLevelSource::Default);
147143
}
144+
idx = parent;
148145
}
149146
}
150147
}
151148

152149
#[derive(Debug)]
153150
pub struct LintLevelMap {
154151
pub sets: LintLevelSets,
155-
pub id_to_set: FxHashMap<HirId, u32>,
152+
pub id_to_set: FxHashMap<HirId, LintStackIndex>,
156153
}
157154

158155
impl LintLevelMap {
@@ -180,29 +177,7 @@ impl<'a> HashStable<StableHashingContext<'a>> for LintLevelMap {
180177

181178
id_to_set.hash_stable(hcx, hasher);
182179

183-
let LintLevelSets { ref list, lint_cap } = *sets;
184-
185-
lint_cap.hash_stable(hcx, hasher);
186-
187-
hcx.while_hashing_spans(true, |hcx| {
188-
list.len().hash_stable(hcx, hasher);
189-
190-
// We are working under the assumption here that the list of
191-
// lint-sets is built in a deterministic order.
192-
for lint_set in list {
193-
::std::mem::discriminant(lint_set).hash_stable(hcx, hasher);
194-
195-
match *lint_set {
196-
LintSet::CommandLine { ref specs } => {
197-
specs.hash_stable(hcx, hasher);
198-
}
199-
LintSet::Node { ref specs, parent } => {
200-
specs.hash_stable(hcx, hasher);
201-
parent.hash_stable(hcx, hasher);
202-
}
203-
}
204-
}
205-
})
180+
hcx.while_hashing_spans(true, |hcx| sets.hash_stable(hcx, hasher))
206181
}
207182
}
208183

0 commit comments

Comments
 (0)