|
1 | 1 | 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; |
2 | 5 | use rustc_middle::query::Providers;
|
3 | 6 | use rustc_middle::ty::TyCtxt;
|
4 | 7 | use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
|
5 |
| -use rustc_session::lint::LintExpectationId; |
| 8 | +use rustc_session::lint::{Level, LintExpectationId}; |
6 | 9 | use rustc_span::Symbol;
|
7 | 10 |
|
8 | 11 | 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 = LintExpectationId::Stable { hir_id, attr_index, lint_index: None }; |
| 23 | + unstable_to_stable_ids.entry(attr_id).or_insert(expect_id); |
| 24 | + }; |
| 25 | + let mut push_expectations = |owner| { |
| 26 | + let lints = tcx.shallow_lint_levels_on(owner); |
| 27 | + if lints.expectations.is_empty() { |
| 28 | + return; |
| 29 | + } |
| 30 | + |
| 31 | + expectations.extend_from_slice(&lints.expectations); |
| 32 | + |
| 33 | + let attrs = tcx.hir_attrs(owner); |
| 34 | + for &(local_id, attrs) in attrs.map.iter() { |
| 35 | + // Some attributes appear multiple times in HIR, to ensure they are correctly taken |
| 36 | + // into account where they matter. This means we cannot just associate the AttrId to |
| 37 | + // the first HirId where we see it, but need to check it actually appears in a lint |
| 38 | + // level. |
| 39 | + // FIXME(cjgillot): Can this cause an attribute to appear in multiple expectation ids? |
| 40 | + if !lints.specs.contains_key(&local_id) { |
| 41 | + continue; |
| 42 | + } |
| 43 | + for (attr_index, attr) in attrs.iter().enumerate() { |
| 44 | + let Some(Level::Expect(_)) = Level::from_attr(attr) else { continue }; |
| 45 | + record_stable(attr.id, HirId { owner, local_id }, attr_index.try_into().unwrap()); |
| 46 | + } |
| 47 | + } |
| 48 | + }; |
| 49 | + |
| 50 | + push_expectations(CRATE_OWNER_ID); |
| 51 | + for owner in krate.owners() { |
| 52 | + push_expectations(owner); |
| 53 | + } |
| 54 | + |
| 55 | + tcx.dcx().update_unstable_expectation_id(unstable_to_stable_ids); |
| 56 | + expectations |
10 | 57 | }
|
11 | 58 |
|
12 | 59 | fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
|
13 | 60 | let lint_expectations = tcx.lint_expectations(());
|
14 | 61 | let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
|
15 | 62 |
|
16 |
| - tracing::debug!(?lint_expectations, ?fulfilled_expectations); |
17 |
| - |
18 | 63 | for (id, expectation) in lint_expectations {
|
19 | 64 | // This check will always be true, since `lint_expectations` only
|
20 | 65 | // holds stable ids
|
|
0 commit comments