Skip to content

Commit 68e98f0

Browse files
Rollup merge of rust-lang#156536 - nnethercote:reveal_actual_level-improvements, r=GuillaumeGomez
`reveal_actual_level` improvements Details in individual commits. r? @GuillaumeGomez
2 parents aeae085 + b86ef89 commit 68e98f0

2 files changed

Lines changed: 34 additions & 47 deletions

File tree

compiler/rustc_lint/src/levels.rs

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -83,33 +83,28 @@ impl LintLevelSets {
8383
aux: Option<&FxIndexMap<LintId, LevelAndSource>>,
8484
sess: &Session,
8585
) -> LevelAndSource {
86-
let lint = LintId::of(lint);
87-
let (level, mut src) = self.raw_lint_id_level(lint, idx, aux);
88-
let (level, lint_id) = reveal_actual_level(level, &mut src, sess, lint, |id| {
89-
self.raw_lint_id_level(id, idx, aux)
90-
});
91-
LevelAndSource { level, lint_id, src }
86+
reveal_actual_level(sess, LintId::of(lint), |id| self.raw_lint_id_level(id, idx, aux))
9287
}
9388

9489
fn raw_lint_id_level(
9590
&self,
9691
id: LintId,
9792
mut idx: LintStackIndex,
9893
aux: Option<&FxIndexMap<LintId, LevelAndSource>>,
99-
) -> (Option<(Level, Option<LintExpectationId>)>, LintLevelSource) {
94+
) -> Option<LevelAndSource> {
10095
if let Some(specs) = aux
101-
&& let Some(&LevelAndSource { level, lint_id, src }) = specs.get(&id)
96+
&& let Some(level) = specs.get(&id)
10297
{
103-
return (Some((level, lint_id)), src);
98+
return Some(*level);
10499
}
105100

106101
loop {
107102
let LintSet { ref specs, parent } = self.list[idx];
108-
if let Some(&LevelAndSource { level, lint_id, src }) = specs.get(&id) {
109-
return (Some((level, lint_id)), src);
103+
if let Some(level) = specs.get(&id) {
104+
return Some(*level);
110105
}
111106
if idx == COMMAND_LINE {
112-
return (None, LintLevelSource::Default);
107+
return None;
113108
}
114109
idx = parent;
115110
}

compiler/rustc_middle/src/lint.rs

Lines changed: 27 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -71,31 +71,29 @@ pub struct ShallowLintLevelMap {
7171
pub specs: SortedMap<ItemLocalId, FxIndexMap<LintId, LevelAndSource>>,
7272
}
7373

74-
/// From an initial level and source, verify the effect of special annotations:
75-
/// `warnings` lint level and lint caps.
74+
/// Verify the effect of special annotations: `warnings` lint level and lint caps.
7675
///
7776
/// The return of this function is suitable for diagnostics.
7877
pub fn reveal_actual_level(
79-
level: Option<(Level, Option<LintExpectationId>)>,
80-
src: &mut LintLevelSource,
8178
sess: &Session,
8279
lint: LintId,
83-
probe_for_lint_level: impl FnOnce(
84-
LintId,
85-
)
86-
-> (Option<(Level, Option<LintExpectationId>)>, LintLevelSource),
87-
) -> (Level, Option<LintExpectationId>) {
80+
probe_for_lint_level: impl Fn(LintId) -> Option<LevelAndSource>,
81+
) -> LevelAndSource {
82+
let level = probe_for_lint_level(lint);
83+
8884
// If `level` is none then we actually assume the default level for this lint.
89-
let (mut level, mut lint_id) =
90-
level.unwrap_or_else(|| (lint.lint.default_level(sess.edition()), None));
85+
let mut level = level.unwrap_or_else(|| LevelAndSource {
86+
level: lint.lint.default_level(sess.edition()),
87+
lint_id: None,
88+
src: LintLevelSource::Default,
89+
});
9190

9291
// If we're about to issue a warning, check at the last minute for any
9392
// directives against the `warnings` lint group. If, for example, there's an
9493
// `allow(warnings)` in scope then we want to respect that instead.
95-
if level == Level::Warn {
96-
let (warnings_level, warnings_src) = probe_for_lint_level(LintId::of(builtin::WARNINGS));
97-
if let Some((configured_warning_level, configured_lint_id)) = warnings_level {
98-
let respect_warnings_lint_group = match configured_warning_level {
94+
if level.level == Level::Warn {
95+
if let Some(configured_level) = probe_for_lint_level(LintId::of(builtin::WARNINGS)) {
96+
let respect_warnings_lint_group = match configured_level.level {
9997
// -Wwarnings is a no-op.
10098
Level::Warn => false,
10199
// Some warnings cannot be denied from the `warnings` lint group, only individually.
@@ -107,33 +105,31 @@ pub fn reveal_actual_level(
107105
Level::Expect => true,
108106
Level::ForceWarn => {
109107
sess.dcx().span_delayed_bug(
110-
warnings_src.span(),
108+
configured_level.src.span(),
111109
"cannot --force-warn the `warnings` lint group",
112110
);
113111
false
114112
}
115113
};
116114
if respect_warnings_lint_group {
117-
level = configured_warning_level;
118-
lint_id = configured_lint_id;
119-
*src = warnings_src;
115+
level = configured_level;
120116
}
121117
}
122118
}
123119

124120
// Ensure that we never exceed the `--cap-lints` argument unless the source is a --force-warn
125-
level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = src {
126-
level
121+
level.level = if let LintLevelSource::CommandLine(_, Level::ForceWarn) = level.src {
122+
level.level
127123
} else {
128-
cmp::min(level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
124+
cmp::min(level.level, sess.opts.lint_cap.unwrap_or(Level::Forbid))
129125
};
130126

131127
if let Some(driver_level) = sess.driver_lint_caps.get(&lint) {
132128
// Ensure that we never exceed driver level.
133-
level = cmp::min(*driver_level, level);
129+
level.level = cmp::min(level.level, *driver_level);
134130
}
135131

136-
(level, lint_id)
132+
level
137133
}
138134

139135
impl ShallowLintLevelMap {
@@ -146,11 +142,11 @@ impl ShallowLintLevelMap {
146142
tcx: TyCtxt<'_>,
147143
id: LintId,
148144
start: HirId,
149-
) -> (Option<(Level, Option<LintExpectationId>)>, LintLevelSource) {
145+
) -> Option<LevelAndSource> {
150146
if let Some(map) = self.specs.get(&start.local_id)
151-
&& let Some(&LevelAndSource { level, lint_id, src }) = map.get(&id)
147+
&& let Some(level) = map.get(&id)
152148
{
153-
return (Some((level, lint_id)), src);
149+
return Some(*level);
154150
}
155151

156152
let mut owner = start.owner;
@@ -162,13 +158,13 @@ impl ShallowLintLevelMap {
162158
specs = &tcx.shallow_lint_levels_on(owner).specs;
163159
}
164160
if let Some(map) = specs.get(&parent.local_id)
165-
&& let Some(&LevelAndSource { level, lint_id, src }) = map.get(&id)
161+
&& let Some(level) = map.get(&id)
166162
{
167-
return (Some((level, lint_id)), src);
163+
return Some(*level);
168164
}
169165
}
170166

171-
(None, LintLevelSource::Default)
167+
None
172168
}
173169

174170
/// Fetch and return the user-visible lint level for the given lint at the given HirId.
@@ -179,11 +175,7 @@ impl ShallowLintLevelMap {
179175
lint: LintId,
180176
cur: HirId,
181177
) -> LevelAndSource {
182-
let (level, mut src) = self.probe_for_lint_level(tcx, lint, cur);
183-
let (level, lint_id) = reveal_actual_level(level, &mut src, tcx.sess, lint, |lint| {
184-
self.probe_for_lint_level(tcx, lint, cur)
185-
});
186-
LevelAndSource { level, lint_id, src }
178+
reveal_actual_level(tcx.sess, lint, |lint| self.probe_for_lint_level(tcx, lint, cur))
187179
}
188180
}
189181

0 commit comments

Comments
 (0)