Skip to content

Commit 51cf1b6

Browse files
authored
Rollup merge of #111714 - cjgillot:lint-expect-confusion, r=wesleywiser
Stop confusing specification levels when computing expectations. Fixes #111682
2 parents 1a5f8bc + f52db3e commit 51cf1b6

File tree

2 files changed

+14
-3
lines changed

2 files changed

+14
-3
lines changed

compiler/rustc_lint/src/levels.rs

+7-3
Original file line numberDiff line numberDiff line change
@@ -242,7 +242,9 @@ impl LintLevelsProvider for LintLevelQueryMap<'_> {
242242

243243
struct QueryMapExpectationsWrapper<'tcx> {
244244
tcx: TyCtxt<'tcx>,
245+
/// HirId of the currently investigated element.
245246
cur: HirId,
247+
/// Level map for `cur`.
246248
specs: ShallowLintLevelMap,
247249
expectations: Vec<(LintExpectationId, LintExpectation)>,
248250
unstable_to_stable_ids: FxHashMap<LintExpectationId, LintExpectationId>,
@@ -255,11 +257,11 @@ impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
255257
self.specs.specs.get(&self.cur.local_id).unwrap_or(&self.empty)
256258
}
257259
fn insert(&mut self, id: LintId, lvl: LevelAndSource) {
258-
let specs = self.specs.specs.get_mut_or_insert_default(self.cur.local_id);
259-
specs.clear();
260-
specs.insert(id, lvl);
260+
self.specs.specs.get_mut_or_insert_default(self.cur.local_id).insert(id, lvl);
261261
}
262262
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
263+
// We cannot use `tcx.lint_level_at_node` because we want to know in which order the
264+
// attributes have been inserted, in particular whether an `expect` follows a `forbid`.
263265
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
264266
}
265267
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation) {
@@ -355,7 +357,9 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
355357

356358
impl<'tcx> LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'tcx>> {
357359
fn add_id(&mut self, hir_id: HirId) {
360+
// Change both the `HirId` and the associated specs.
358361
self.provider.cur = hir_id;
362+
self.provider.specs.specs.clear();
359363
self.add(self.provider.tcx.hir().attrs(hir_id), hir_id == hir::CRATE_HIR_ID, Some(hir_id));
360364
}
361365
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
// check-pass
2+
// compile-flags: -Dunused_attributes
3+
4+
#![deny(unused_crate_dependencies)]
5+
#![feature(lint_reasons)]
6+
7+
fn main() {}

0 commit comments

Comments
 (0)