Remove attr_id from stable lint ids.

This commit is contained in:
Camille GILLOT 2024-07-04 10:12:39 +00:00
parent 111b0a97b4
commit 5f1f45b095
5 changed files with 14 additions and 42 deletions

View File

@ -368,7 +368,7 @@ impl DiagInner {
panic!("{expectation_id:?} must have a matching stable id") panic!("{expectation_id:?} must have a matching stable id")
}; };
let mut stable_id = stable_id.normalize(); let mut stable_id = *stable_id;
stable_id.set_lint_index(lint_index); stable_id.set_lint_index(lint_index);
*expectation_id = stable_id; *expectation_id = stable_id;
} }

View File

@ -1567,7 +1567,7 @@ impl DiagCtxtInner {
if let LintExpectationId::Unstable { .. } = expect_id { if let LintExpectationId::Unstable { .. } = expect_id {
unreachable!(); // this case was handled at the top of this function unreachable!(); // this case was handled at the top of this function
} }
self.fulfilled_expectations.insert(expect_id.normalize()); self.fulfilled_expectations.insert(expect_id);
if let Expect(_) = diagnostic.level { if let Expect(_) = diagnostic.level {
// Nothing emitted here for expected lints. // Nothing emitted here for expected lints.
TRACK_DIAGNOSTIC(diagnostic, &mut |_| None); TRACK_DIAGNOSTIC(diagnostic, &mut |_| None);

View File

@ -20,8 +20,7 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
let mut unstable_to_stable_ids = FxIndexMap::default(); let mut unstable_to_stable_ids = FxIndexMap::default();
let mut record_stable = |attr_id, hir_id, attr_index| { let mut record_stable = |attr_id, hir_id, attr_index| {
let expect_id = let expect_id = LintExpectationId::Stable { hir_id, attr_index, lint_index: None };
LintExpectationId::Stable { hir_id, attr_index, lint_index: None, attr_id: None };
unstable_to_stable_ids.entry(attr_id).or_insert(expect_id); unstable_to_stable_ids.entry(attr_id).or_insert(expect_id);
}; };
let mut push_expectations = |owner| { let mut push_expectations = |owner| {

View File

@ -218,7 +218,7 @@ impl LintLevelsProvider for LintLevelQueryMap<'_> {
self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur) self.specs.lint_level_id_at_node(self.tcx, LintId::of(lint), self.cur)
} }
fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation) { fn push_expectation(&mut self, id: LintExpectationId, expectation: LintExpectation) {
self.specs.expectations.push((id.normalize(), expectation)) self.specs.expectations.push((id, expectation))
} }
} }
@ -486,13 +486,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
/// Attempts to insert the `id` to `level_src` map entry. If unsuccessful /// Attempts to insert the `id` to `level_src` map entry. If unsuccessful
/// (e.g. if a forbid was already inserted on the same scope), then emits a /// (e.g. if a forbid was already inserted on the same scope), then emits a
/// diagnostic with no change to `specs`. /// diagnostic with no change to `specs`.
fn insert_spec(&mut self, id: LintId, (mut level, src): LevelAndSource) { fn insert_spec(&mut self, id: LintId, (level, src): LevelAndSource) {
let (old_level, old_src) = self.provider.get_lint_level(id.lint, self.sess); let (old_level, old_src) = self.provider.get_lint_level(id.lint, self.sess);
if let Level::Expect(id) = &mut level
&& let LintExpectationId::Stable { .. } = id
{
*id = id.normalize();
}
// Setting to a non-forbid level is an error if the lint previously had // Setting to a non-forbid level is an error if the lint previously had
// a forbid level. Note that this is not necessarily true even with a // a forbid level. Note that this is not necessarily true even with a
// `#[forbid(..)]` attribute present, as that is overridden by `--cap-lints`. // `#[forbid(..)]` attribute present, as that is overridden by `--cap-lints`.
@ -604,17 +600,15 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
// This is the only lint level with a `LintExpectationId` that can be created from // This is the only lint level with a `LintExpectationId` that can be created from
// an attribute. // an attribute.
Some(Level::Expect(unstable_id)) if let Some(hir_id) = source_hir_id => { Some(Level::Expect(unstable_id)) if let Some(hir_id) = source_hir_id => {
let LintExpectationId::Unstable { attr_id, lint_index } = unstable_id else { let LintExpectationId::Unstable { lint_index: None, attr_id: _ } = unstable_id
else {
bug!("stable id Level::from_attr") bug!("stable id Level::from_attr")
}; };
let stable_id = LintExpectationId::Stable { let stable_id = LintExpectationId::Stable {
hir_id, hir_id,
attr_index: attr_index.try_into().unwrap(), attr_index: attr_index.try_into().unwrap(),
lint_index, lint_index: None,
// We pass the previous unstable attr_id such that we can trace the ast id
// when building a map to go from unstable to stable id.
attr_id: Some(attr_id),
}; };
Level::Expect(stable_id) Level::Expect(stable_id)

View File

@ -98,7 +98,7 @@ pub enum LintExpectationId {
/// stable and can be cached. The additional index ensures that nodes with /// stable and can be cached. The additional index ensures that nodes with
/// several expectations can correctly match diagnostics to the individual /// several expectations can correctly match diagnostics to the individual
/// expectation. /// expectation.
Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16>, attr_id: Option<AttrId> }, Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16> },
} }
impl LintExpectationId { impl LintExpectationId {
@ -122,31 +122,13 @@ impl LintExpectationId {
*lint_index = new_lint_index *lint_index = new_lint_index
} }
/// Prepares the id for hashing. Removes references to the ast.
/// Should only be called when the id is stable.
pub fn normalize(self) -> Self {
match self {
Self::Stable { hir_id, attr_index, lint_index, .. } => {
Self::Stable { hir_id, attr_index, lint_index, attr_id: None }
}
Self::Unstable { .. } => {
unreachable!("`normalize` called when `ExpectationId` is unstable")
}
}
}
} }
impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId { impl<HCX: rustc_hir::HashStableContext> HashStable<HCX> for LintExpectationId {
#[inline] #[inline]
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) { fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
match self { match self {
LintExpectationId::Stable { LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
hir_id,
attr_index,
lint_index: Some(lint_index),
attr_id: _,
} => {
hir_id.hash_stable(hcx, hasher); hir_id.hash_stable(hcx, hasher);
attr_index.hash_stable(hcx, hasher); attr_index.hash_stable(hcx, hasher);
lint_index.hash_stable(hcx, hasher); lint_index.hash_stable(hcx, hasher);
@ -166,12 +148,9 @@ impl<HCX: rustc_hir::HashStableContext> ToStableHashKey<HCX> for LintExpectation
#[inline] #[inline]
fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType { fn to_stable_hash_key(&self, _: &HCX) -> Self::KeyType {
match self { match self {
LintExpectationId::Stable { LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
hir_id, (*hir_id, *attr_index, *lint_index)
attr_index, }
lint_index: Some(lint_index),
attr_id: _,
} => (*hir_id, *attr_index, *lint_index),
_ => { _ => {
unreachable!("HashStable should only be called for a filled `LintExpectationId`") unreachable!("HashStable should only be called for a filled `LintExpectationId`")
} }