Skip to content

Commit 111b0a9

Browse files
committed
Rewrite lint_expectations in a single pass.
1 parent 4928b22 commit 111b0a9

12 files changed

+67
-222
lines changed

compiler/rustc_lint/src/expect.rs

Lines changed: 50 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,67 @@
1+
use rustc_data_structures::fx::FxIndexMap;
2+
use rustc_hir::{HirId, CRATE_OWNER_ID};
3+
use rustc_middle::lint::LintExpectation;
14
use rustc_middle::query::Providers;
25
use rustc_middle::ty::TyCtxt;
36
use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
4-
use rustc_session::lint::LintExpectationId;
7+
use rustc_session::lint::{Level, LintExpectationId};
58
use rustc_span::Symbol;
69

710
use crate::lints::{Expectation, ExpectationNote};
811

912
pub(crate) fn provide(providers: &mut Providers) {
10-
*providers = Providers { check_expectations, ..*providers };
13+
*providers = Providers { lint_expectations, check_expectations, ..*providers };
14+
}
15+
16+
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
17+
let krate = tcx.hir_crate_items(());
18+
19+
let mut expectations = Vec::new();
20+
let mut unstable_to_stable_ids = FxIndexMap::default();
21+
22+
let mut record_stable = |attr_id, hir_id, attr_index| {
23+
let expect_id =
24+
LintExpectationId::Stable { hir_id, attr_index, lint_index: None, attr_id: None };
25+
unstable_to_stable_ids.entry(attr_id).or_insert(expect_id);
26+
};
27+
let mut push_expectations = |owner| {
28+
let lints = tcx.shallow_lint_levels_on(owner);
29+
if lints.expectations.is_empty() {
30+
return;
31+
}
32+
33+
expectations.extend_from_slice(&lints.expectations);
34+
35+
let attrs = tcx.hir_attrs(owner);
36+
for &(local_id, attrs) in attrs.map.iter() {
37+
// Some attributes appear multiple times in HIR, to ensure they are correctly taken
38+
// into account where they matter. This means we cannot just associate the AttrId to
39+
// the first HirId where we see it, but need to check it actually appears in a lint
40+
// level.
41+
// FIXME(cjgillot): Can this cause an attribute to appear in multiple expectation ids?
42+
if !lints.specs.contains_key(&local_id) {
43+
continue;
44+
}
45+
for (attr_index, attr) in attrs.iter().enumerate() {
46+
let Some(Level::Expect(_)) = Level::from_attr(attr) else { continue };
47+
record_stable(attr.id, HirId { owner, local_id }, attr_index.try_into().unwrap());
48+
}
49+
}
50+
};
51+
52+
push_expectations(CRATE_OWNER_ID);
53+
for owner in krate.owners() {
54+
push_expectations(owner);
55+
}
56+
57+
tcx.dcx().update_unstable_expectation_id(unstable_to_stable_ids);
58+
expectations
1159
}
1260

1361
fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
1462
let lint_expectations = tcx.lint_expectations(());
1563
let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
1664

17-
tracing::debug!(?lint_expectations, ?fulfilled_expectations);
18-
1965
for (id, expectation) in lint_expectations {
2066
// This check will always be true, since `lint_expectations` only
2167
// holds stable ids

compiler/rustc_lint/src/levels.rs

Lines changed: 6 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ use rustc_session::lint::builtin::{
2020
use rustc_session::lint::{Level, Lint, LintExpectationId, LintId};
2121
use rustc_session::Session;
2222
use rustc_span::symbol::{sym, Symbol};
23-
use rustc_span::{AttrId, Span, DUMMY_SP};
23+
use rustc_span::{Span, DUMMY_SP};
2424
use tracing::{debug, instrument};
2525
use {rustc_ast as ast, rustc_hir as hir};
2626

@@ -115,34 +115,6 @@ impl LintLevelSets {
115115
}
116116
}
117117

118-
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
119-
let store = unerased_lint_store(tcx.sess);
120-
121-
let mut builder = LintLevelsBuilder {
122-
sess: tcx.sess,
123-
features: tcx.features(),
124-
provider: QueryMapExpectationsWrapper {
125-
tcx,
126-
cur: hir::CRATE_HIR_ID,
127-
specs: ShallowLintLevelMap::default(),
128-
expectations: Vec::new(),
129-
unstable_to_stable_ids: FxIndexMap::default(),
130-
empty: FxIndexMap::default(),
131-
},
132-
lint_added_lints: false,
133-
store,
134-
registered_tools: tcx.registered_tools(()),
135-
};
136-
137-
builder.add_command_line();
138-
builder.add_id(hir::CRATE_HIR_ID);
139-
tcx.hir().walk_toplevel_module(&mut builder);
140-
141-
tcx.dcx().update_unstable_expectation_id(builder.provider.unstable_to_stable_ids);
142-
143-
builder.provider.expectations
144-
}
145-
146118
#[instrument(level = "trace", skip(tcx), ret)]
147119
fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLevelMap {
148120
let store = unerased_lint_store(tcx.sess);
@@ -207,7 +179,7 @@ pub trait LintLevelsProvider {
207179
fn current_specs(&self) -> &FxIndexMap<LintId, LevelAndSource>;
208180
fn insert(&mut self, id: LintId, lvl: LevelAndSource);
209181
fn get_lint_level(&self, lint: &'static Lint, sess: &Session) -> LevelAndSource;
210-
fn push_expectation(&mut self, _id: LintExpectationId, _expectation: LintExpectation) {}
182+
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation);
211183
}
212184

213185
impl LintLevelsProvider for TopDown {
@@ -222,6 +194,8 @@ impl LintLevelsProvider for TopDown {
222194
fn get_lint_level(&self, lint: &'static Lint, sess: &Session) -> LevelAndSource {
223195
self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), sess)
224196
}
197+
198+
fn push_expectation(&mut self, _: LintExpectationId, _: LintExpectation) {}
225199
}
226200

227201
struct LintLevelQueryMap<'tcx> {
@@ -243,46 +217,8 @@ impl LintLevelsProvider for LintLevelQueryMap<'_> {
243217
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
244218
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
245219
}
246-
}
247-
248-
struct QueryMapExpectationsWrapper<'tcx> {
249-
tcx: TyCtxt<'tcx>,
250-
/// HirId of the currently investigated element.
251-
cur: HirId,
252-
/// Level map for `cur`.
253-
specs: ShallowLintLevelMap,
254-
expectations: Vec<(LintExpectationId, LintExpectation)>,
255-
unstable_to_stable_ids: FxIndexMap<AttrId, LintExpectationId>,
256-
/// Empty hash map to simplify code.
257-
empty: FxIndexMap<LintId, LevelAndSource>,
258-
}
259-
260-
impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
261-
fn current_specs(&self) -> &FxIndexMap<LintId, LevelAndSource> {
262-
self.specs.specs.get(&self.cur.local_id).unwrap_or(&self.empty)
263-
}
264-
fn insert(&mut self, id: LintId, lvl: LevelAndSource) {
265-
self.specs.specs.get_mut_or_insert_default(self.cur.local_id).insert(id, lvl);
266-
}
267-
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
268-
// We cannot use `tcx.lint_level_at_node` because we want to know in which order the
269-
// attributes have been inserted, in particular whether an `expect` follows a `forbid`.
270-
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
271-
}
272220
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation) {
273-
let LintExpectationId::Stable { attr_id: Some(attr_id), hir_id, attr_index, .. } = id
274-
else {
275-
bug!("unstable expectation id should already be mapped")
276-
};
277-
278-
self.unstable_to_stable_ids.entry(attr_id).or_insert(LintExpectationId::Stable {
279-
hir_id,
280-
attr_index,
281-
lint_index: None,
282-
attr_id: None,
283-
});
284-
285-
self.expectations.push((id.normalize(), expectation));
221+
self.specs.expectations.push((id.normalize(), expectation))
286222
}
287223
}
288224

@@ -367,80 +303,6 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
367303
}
368304
}
369305

370-
impl<'tcx> LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'tcx>> {
371-
fn add_id(&mut self, hir_id: HirId) {
372-
// Change both the `HirId` and the associated specs.
373-
self.provider.cur = hir_id;
374-
self.provider.specs.specs.clear();
375-
self.add(self.provider.tcx.hir().attrs(hir_id), hir_id == hir::CRATE_HIR_ID, Some(hir_id));
376-
}
377-
}
378-
379-
impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, QueryMapExpectationsWrapper<'tcx>> {
380-
type NestedFilter = nested_filter::All;
381-
382-
fn nested_visit_map(&mut self) -> Self::Map {
383-
self.provider.tcx.hir()
384-
}
385-
386-
fn visit_param(&mut self, param: &'tcx hir::Param<'tcx>) {
387-
self.add_id(param.hir_id);
388-
intravisit::walk_param(self, param);
389-
}
390-
391-
fn visit_item(&mut self, it: &'tcx hir::Item<'tcx>) {
392-
self.add_id(it.hir_id());
393-
intravisit::walk_item(self, it);
394-
}
395-
396-
fn visit_foreign_item(&mut self, it: &'tcx hir::ForeignItem<'tcx>) {
397-
self.add_id(it.hir_id());
398-
intravisit::walk_foreign_item(self, it);
399-
}
400-
401-
fn visit_stmt(&mut self, e: &'tcx hir::Stmt<'tcx>) {
402-
// We will call `add_id` when we walk
403-
// the `StmtKind`. The outer statement itself doesn't
404-
// define the lint levels.
405-
intravisit::walk_stmt(self, e);
406-
}
407-
408-
fn visit_expr(&mut self, e: &'tcx hir::Expr<'tcx>) {
409-
self.add_id(e.hir_id);
410-
intravisit::walk_expr(self, e);
411-
}
412-
413-
fn visit_field_def(&mut self, s: &'tcx hir::FieldDef<'tcx>) {
414-
self.add_id(s.hir_id);
415-
intravisit::walk_field_def(self, s);
416-
}
417-
418-
fn visit_variant(&mut self, v: &'tcx hir::Variant<'tcx>) {
419-
self.add_id(v.hir_id);
420-
intravisit::walk_variant(self, v);
421-
}
422-
423-
fn visit_local(&mut self, l: &'tcx hir::LetStmt<'tcx>) {
424-
self.add_id(l.hir_id);
425-
intravisit::walk_local(self, l);
426-
}
427-
428-
fn visit_arm(&mut self, a: &'tcx hir::Arm<'tcx>) {
429-
self.add_id(a.hir_id);
430-
intravisit::walk_arm(self, a);
431-
}
432-
433-
fn visit_trait_item(&mut self, trait_item: &'tcx hir::TraitItem<'tcx>) {
434-
self.add_id(trait_item.hir_id());
435-
intravisit::walk_trait_item(self, trait_item);
436-
}
437-
438-
fn visit_impl_item(&mut self, impl_item: &'tcx hir::ImplItem<'tcx>) {
439-
self.add_id(impl_item.hir_id());
440-
intravisit::walk_impl_item(self, impl_item);
441-
}
442-
}
443-
444306
pub struct LintLevelsBuilder<'s, P> {
445307
sess: &'s Session,
446308
features: &'s Features,
@@ -1099,7 +961,7 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
1099961
}
1100962

1101963
pub(crate) fn provide(providers: &mut Providers) {
1102-
*providers = Providers { shallow_lint_levels_on, lint_expectations, ..*providers };
964+
*providers = Providers { shallow_lint_levels_on, ..*providers };
1103965
}
1104966

1105967
pub(crate) fn parse_lint_and_tool_name(lint_name: &str) -> (Option<Symbol>, &str) {

compiler/rustc_middle/src/lint.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use rustc_errors::{Diag, MultiSpan};
66
use rustc_hir::{HirId, ItemLocalId};
77
use rustc_macros::HashStable;
88
use rustc_session::lint::builtin::{self, FORBIDDEN_LINT_GROUPS};
9-
use rustc_session::lint::{FutureIncompatibilityReason, Level, Lint, LintId};
9+
use rustc_session::lint::{FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId};
1010
use rustc_session::Session;
1111
use rustc_span::hygiene::{ExpnKind, MacroKind};
1212
use rustc_span::{symbol, DesugaringKind, Span, Symbol, DUMMY_SP};
@@ -61,6 +61,7 @@ pub type LevelAndSource = (Level, LintLevelSource);
6161
/// by the attributes for *a single HirId*.
6262
#[derive(Default, Debug, HashStable)]
6363
pub struct ShallowLintLevelMap {
64+
pub expectations: Vec<(LintExpectationId, LintExpectation)>,
6465
pub specs: SortedMap<ItemLocalId, FxIndexMap<LintId, LevelAndSource>>,
6566
}
6667

tests/ui/error-codes/E0602.stderr

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ warning[E0602]: unknown lint: `bogus`
1313
= note: requested on the command line with `-D bogus`
1414
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1515

16-
warning[E0602]: unknown lint: `bogus`
17-
|
18-
= note: requested on the command line with `-D bogus`
19-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20-
21-
warning: 4 warnings emitted
16+
warning: 3 warnings emitted
2217

2318
For more information about this error, try `rustc --explain E0602`.

tests/ui/lint/cli-unknown-force-warn.stderr

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,6 @@ warning[E0602]: unknown lint: `foo_qux`
1313
= note: requested on the command line with `--force-warn foo_qux`
1414
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
1515

16-
warning[E0602]: unknown lint: `foo_qux`
17-
|
18-
= note: requested on the command line with `--force-warn foo_qux`
19-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
20-
21-
warning: 4 warnings emitted
16+
warning: 3 warnings emitted
2217

2318
For more information about this error, try `rustc --explain E0602`.

tests/ui/lint/lint-removed-cmdline-deny.stderr

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,5 @@ LL | #[deny(warnings)]
2626
| ^^^^^^^^
2727
= note: `#[deny(unused_variables)]` implied by `#[deny(warnings)]`
2828

29-
error: lint `raw_pointer_derive` has been removed: using derive with raw pointers is ok
30-
|
31-
= note: requested on the command line with `-D raw_pointer_derive`
32-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
33-
34-
error: aborting due to 5 previous errors
29+
error: aborting due to 4 previous errors
3530

tests/ui/lint/lint-removed-cmdline.stderr

Lines changed: 1 addition & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,5 @@ LL | #[deny(warnings)]
2626
| ^^^^^^^^
2727
= note: `#[deny(unused_variables)]` implied by `#[deny(warnings)]`
2828

29-
warning: lint `raw_pointer_derive` has been removed: using derive with raw pointers is ok
30-
|
31-
= note: requested on the command line with `-D raw_pointer_derive`
32-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
33-
34-
error: aborting due to 1 previous error; 4 warnings emitted
29+
error: aborting due to 1 previous error; 3 warnings emitted
3530

tests/ui/lint/lint-renamed-cmdline-deny.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,5 @@ LL | #[deny(unused)]
2929
| ^^^^^^
3030
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`
3131

32-
error: lint `bare_trait_object` has been renamed to `bare_trait_objects`
33-
|
34-
= help: use the new name `bare_trait_objects`
35-
= note: requested on the command line with `-D bare_trait_object`
36-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
37-
38-
error: aborting due to 5 previous errors
32+
error: aborting due to 4 previous errors
3933

tests/ui/lint/lint-renamed-cmdline.stderr

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -29,11 +29,5 @@ LL | #[deny(unused)]
2929
| ^^^^^^
3030
= note: `#[deny(unused_variables)]` implied by `#[deny(unused)]`
3131

32-
warning: lint `bare_trait_object` has been renamed to `bare_trait_objects`
33-
|
34-
= help: use the new name `bare_trait_objects`
35-
= note: requested on the command line with `-D bare_trait_object`
36-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
37-
38-
error: aborting due to 1 previous error; 4 warnings emitted
32+
error: aborting due to 1 previous error; 3 warnings emitted
3933

tests/ui/lint/lint-unexported-no-mangle.stderr

Lines changed: 1 addition & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -45,15 +45,5 @@ LL | pub const PUB_FOO: u64 = 1;
4545
| |
4646
| help: try a static value: `pub static`
4747

48-
warning: lint `private_no_mangle_fns` has been removed: no longer a warning, `#[no_mangle]` functions always exported
49-
|
50-
= note: requested on the command line with `-F private_no_mangle_fns`
51-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
52-
53-
warning: lint `private_no_mangle_statics` has been removed: no longer a warning, `#[no_mangle]` statics always exported
54-
|
55-
= note: requested on the command line with `-F private_no_mangle_statics`
56-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
57-
58-
error: aborting due to 2 previous errors; 8 warnings emitted
48+
error: aborting due to 2 previous errors; 6 warnings emitted
5949

0 commit comments

Comments
 (0)