|
| 1 | +use rustc_data_structures::fx::FxIndexMap; |
| 2 | +use rustc_hir::{HirId, CRATE_OWNER_ID}; |
| 3 | +use rustc_middle::lint::LintExpectation; |
1 | 4 | use rustc_middle::query::Providers;
|
2 | 5 | use rustc_middle::ty::TyCtxt;
|
3 | 6 | use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
|
4 |
| -use rustc_session::lint::LintExpectationId; |
| 7 | +use rustc_session::lint::{Level, LintExpectationId}; |
5 | 8 | use rustc_span::Symbol;
|
6 | 9 |
|
7 | 10 | use crate::lints::{Expectation, ExpectationNote};
|
8 | 11 |
|
9 | 12 | 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 = LintExpectationId::Stable { hir_id, attr_index, lint_index: 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 |
11 | 58 | }
|
12 | 59 |
|
13 | 60 | fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
|
14 | 61 | let lint_expectations = tcx.lint_expectations(());
|
15 | 62 | let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
|
16 | 63 |
|
17 |
| - tracing::debug!(?lint_expectations, ?fulfilled_expectations); |
18 |
| - |
19 | 64 | for (id, expectation) in lint_expectations {
|
20 | 65 | // This check will always be true, since `lint_expectations` only
|
21 | 66 | // holds stable ids
|
|
0 commit comments