Skip to content

Commit 8a1da1a

Browse files
committed
Rewrite lint_expectations in a single pass.
1 parent 4af4ec8 commit 8a1da1a

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,20 +1,66 @@
11
use crate::lints::{Expectation, ExpectationNote};
2+
use rustc_data_structures::fx::FxIndexMap;
3+
use rustc_hir::{HirId, CRATE_OWNER_ID};
4+
use rustc_middle::lint::LintExpectation;
25
use rustc_middle::query::Providers;
36
use rustc_middle::ty::TyCtxt;
47
use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
5-
use rustc_session::lint::LintExpectationId;
8+
use rustc_session::lint::{Level, LintExpectationId};
69
use rustc_span::Symbol;
710

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

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

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

compiler/rustc_lint/src/levels.rs

Lines changed: 6 additions & 144 deletions
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,7 @@ use rustc_session::lint::{
3939
};
4040
use rustc_session::Session;
4141
use rustc_span::symbol::{sym, Symbol};
42-
use rustc_span::{AttrId, Span, DUMMY_SP};
42+
use rustc_span::{Span, DUMMY_SP};
4343
use tracing::{debug, instrument};
4444

4545
use crate::errors::{
@@ -123,34 +123,6 @@ impl LintLevelSets {
123123
}
124124
}
125125

126-
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
127-
let store = unerased_lint_store(tcx.sess);
128-
129-
let mut builder = LintLevelsBuilder {
130-
sess: tcx.sess,
131-
features: tcx.features(),
132-
provider: QueryMapExpectationsWrapper {
133-
tcx,
134-
cur: hir::CRATE_HIR_ID,
135-
specs: ShallowLintLevelMap::default(),
136-
expectations: Vec::new(),
137-
unstable_to_stable_ids: FxIndexMap::default(),
138-
empty: FxIndexMap::default(),
139-
},
140-
lint_added_lints: false,
141-
store,
142-
registered_tools: tcx.registered_tools(()),
143-
};
144-
145-
builder.add_command_line();
146-
builder.add_id(hir::CRATE_HIR_ID);
147-
tcx.hir().walk_toplevel_module(&mut builder);
148-
149-
tcx.dcx().update_unstable_expectation_id(builder.provider.unstable_to_stable_ids);
150-
151-
builder.provider.expectations
152-
}
153-
154126
#[instrument(level = "trace", skip(tcx), ret)]
155127
fn shallow_lint_levels_on(tcx: TyCtxt<'_>, owner: hir::OwnerId) -> ShallowLintLevelMap {
156128
let store = unerased_lint_store(tcx.sess);
@@ -215,7 +187,7 @@ pub trait LintLevelsProvider {
215187
fn current_specs(&self) -> &FxIndexMap<LintId, LevelAndSource>;
216188
fn insert(&mut self, id: LintId, lvl: LevelAndSource);
217189
fn get_lint_level(&self, lint: &'static Lint, sess: &Session) -> LevelAndSource;
218-
fn push_expectation(&mut self, _id: LintExpectationId, _expectation: LintExpectation) {}
190+
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation);
219191
}
220192

221193
impl LintLevelsProvider for TopDown {
@@ -230,6 +202,8 @@ impl LintLevelsProvider for TopDown {
230202
fn get_lint_level(&self, lint: &'static Lint, sess: &Session) -> LevelAndSource {
231203
self.sets.get_lint_level(lint, self.cur, Some(self.current_specs()), sess)
232204
}
205+
206+
fn push_expectation(&mut self, _: LintExpectationId, _: LintExpectation) {}
233207
}
234208

235209
struct LintLevelQueryMap<'tcx> {
@@ -251,46 +225,8 @@ impl LintLevelsProvider for LintLevelQueryMap<'_> {
251225
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
252226
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
253227
}
254-
}
255-
256-
struct QueryMapExpectationsWrapper<'tcx> {
257-
tcx: TyCtxt<'tcx>,
258-
/// HirId of the currently investigated element.
259-
cur: HirId,
260-
/// Level map for `cur`.
261-
specs: ShallowLintLevelMap,
262-
expectations: Vec<(LintExpectationId, LintExpectation)>,
263-
unstable_to_stable_ids: FxIndexMap<AttrId, LintExpectationId>,
264-
/// Empty hash map to simplify code.
265-
empty: FxIndexMap<LintId, LevelAndSource>,
266-
}
267-
268-
impl LintLevelsProvider for QueryMapExpectationsWrapper<'_> {
269-
fn current_specs(&self) -> &FxIndexMap<LintId, LevelAndSource> {
270-
self.specs.specs.get(&self.cur.local_id).unwrap_or(&self.empty)
271-
}
272-
fn insert(&mut self, id: LintId, lvl: LevelAndSource) {
273-
self.specs.specs.get_mut_or_insert_default(self.cur.local_id).insert(id, lvl);
274-
}
275-
fn get_lint_level(&self, lint: &'static Lint, _: &Session) -> LevelAndSource {
276-
// We cannot use `tcx.lint_level_at_node` because we want to know in which order the
277-
// attributes have been inserted, in particular whether an `expect` follows a `forbid`.
278-
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
279-
}
280228
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation) {
281-
let LintExpectationId::Stable { attr_id: Some(attr_id), hir_id, attr_index, .. } = id
282-
else {
283-
bug!("unstable expectation id should already be mapped")
284-
};
285-
286-
self.unstable_to_stable_ids.entry(attr_id).or_insert(LintExpectationId::Stable {
287-
hir_id,
288-
attr_index,
289-
lint_index: None,
290-
attr_id: None,
291-
});
292-
293-
self.expectations.push((id.normalize(), expectation));
229+
self.specs.expectations.push((id.normalize(), expectation))
294230
}
295231
}
296232

@@ -375,80 +311,6 @@ impl<'tcx> Visitor<'tcx> for LintLevelsBuilder<'_, LintLevelQueryMap<'tcx>> {
375311
}
376312
}
377313

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

1111973
pub(crate) fn provide(providers: &mut Providers) {
1112-
*providers = Providers { shallow_lint_levels_on, lint_expectations, ..*providers };
974+
*providers = Providers { shallow_lint_levels_on, ..*providers };
1113975
}
1114976

1115977
pub 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
@@ -7,7 +7,7 @@ use rustc_hir::{HirId, ItemLocalId};
77
use rustc_macros::HashStable;
88
use rustc_session::lint::{
99
builtin::{self, FORBIDDEN_LINT_GROUPS},
10-
FutureIncompatibilityReason, Level, Lint, LintId,
10+
FutureIncompatibilityReason, Level, Lint, LintExpectationId, LintId,
1111
};
1212
use rustc_session::Session;
1313
use rustc_span::hygiene::{ExpnKind, MacroKind};
@@ -63,6 +63,7 @@ pub type LevelAndSource = (Level, LintLevelSource);
6363
/// by the attributes for *a single HirId*.
6464
#[derive(Default, Debug, HashStable)]
6565
pub struct ShallowLintLevelMap {
66+
pub expectations: Vec<(LintExpectationId, LintExpectation)>,
6667
pub specs: SortedMap<ItemLocalId, FxIndexMap<LintId, LevelAndSource>>,
6768
}
6869

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

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

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -30,17 +30,6 @@ error[E0602]: unknown lint: `dead_cod`
3030
= note: requested on the command line with `-D dead_cod`
3131
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
3232

33-
error[E0602]: unknown lint: `bogus`
34-
|
35-
= note: requested on the command line with `-D bogus`
36-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
37-
38-
error[E0602]: unknown lint: `dead_cod`
39-
|
40-
= help: did you mean: `dead_code`
41-
= note: requested on the command line with `-D dead_cod`
42-
= note: duplicate diagnostic emitted due to `-Z deduplicate-diagnostics=no`
43-
44-
error: aborting due to 8 previous errors
33+
error: aborting due to 6 previous errors
4534

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

0 commit comments

Comments
 (0)