Skip to content

Commit e35ab7f

Browse files
committed
make ExpectationId compare attrs' target Span instead of AttrId
1 parent fff9e44 commit e35ab7f

6 files changed

Lines changed: 60 additions & 18 deletions

File tree

compiler/rustc_attr_parsing/src/attributes/lint.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,7 @@ fn validate_lint_attr<T: Lint, S: Stage>(
252252
attr_span: cx.attr_span,
253253
attr_style: cx.attr_style,
254254
attr_id: HashIgnoredAttrId { attr_id: cx.attr_id },
255+
target_span: cx.target_span,
255256
kind: T::KIND,
256257
})
257258
}

compiler/rustc_data_structures/src/stable_hasher.rs

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -311,6 +311,38 @@ impl<T1: StableOrd, T2: StableOrd, T3: StableOrd, T4: StableOrd> StableOrd for (
311311
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
312312
}
313313

314+
impl<T1, T2, T3, T4, T5, Hcx> HashStable<Hcx> for (T1, T2, T3, T4, T5)
315+
where
316+
T1: HashStable<Hcx>,
317+
T2: HashStable<Hcx>,
318+
T3: HashStable<Hcx>,
319+
T4: HashStable<Hcx>,
320+
T5: HashStable<Hcx>,
321+
{
322+
fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
323+
let (ref _0, ref _1, ref _2, ref _3, ref _4) = *self;
324+
_0.hash_stable(hcx, hasher);
325+
_1.hash_stable(hcx, hasher);
326+
_2.hash_stable(hcx, hasher);
327+
_3.hash_stable(hcx, hasher);
328+
_4.hash_stable(hcx, hasher);
329+
}
330+
}
331+
332+
impl<T1: StableOrd, T2: StableOrd, T3: StableOrd, T4: StableOrd, T5: StableOrd> StableOrd
333+
for (T1, T2, T3, T4, T5)
334+
{
335+
const CAN_USE_UNSTABLE_SORT: bool = T1::CAN_USE_UNSTABLE_SORT
336+
&& T2::CAN_USE_UNSTABLE_SORT
337+
&& T3::CAN_USE_UNSTABLE_SORT
338+
&& T4::CAN_USE_UNSTABLE_SORT
339+
&& T5::CAN_USE_UNSTABLE_SORT;
340+
341+
// Ordering of tuples is a pure function of their elements' ordering, and since
342+
// the ordering of each element is stable so must be the ordering of the tuple.
343+
const THIS_IMPLEMENTATION_HAS_BEEN_TRIPLE_CHECKED: () = ();
344+
}
345+
314346
impl<T: HashStable<Hcx>, Hcx> HashStable<Hcx> for [T] {
315347
default fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
316348
self.len().hash_stable(hcx, hasher);

compiler/rustc_hir/src/attrs/data_structures.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -903,6 +903,7 @@ pub struct LintAttribute {
903903
pub kind: LintAttributeKind,
904904
pub attr_style: AttrStyle,
905905
pub attr_span: Span,
906+
pub target_span: Span,
906907
/// Needed by `LintExpectationId` to track fulfilled expectations
907908
pub attr_id: HashIgnoredAttrId,
908909
pub lint_instances: ThinVec<LintInstance>,

compiler/rustc_lint/src/expect.rs

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use rustc_middle::query::Providers;
44
use rustc_middle::ty::TyCtxt;
55
use rustc_session::lint::LintExpectationId;
66
use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
7-
use rustc_span::Symbol;
7+
use rustc_span::{Span, Symbol};
88

99
use crate::lints::{Expectation, ExpectationNote};
1010

@@ -33,10 +33,12 @@ fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExp
3333
expectations
3434
}
3535

36-
fn canonicalize_id(expect_id: &LintExpectationId) -> (rustc_span::AttrId, u16) {
36+
fn canonicalize_id(expect_id: &LintExpectationId) -> (Span, u16, u16) {
3737
match *expect_id {
38-
LintExpectationId::Unstable { attr_id, lint_index, .. } => (attr_id, lint_index),
39-
LintExpectationId::Stable { attr_id, lint_index, .. } => (attr_id, lint_index),
38+
LintExpectationId::Unstable { target_span, lint_index, attr_index }
39+
| LintExpectationId::Stable { target_span, lint_index, attr_index, .. } => {
40+
(target_span, lint_index, attr_index)
41+
}
4042
}
4143
}
4244

compiler/rustc_lint/src/levels.rs

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -723,10 +723,9 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
723723
return;
724724
};
725725

726-
for (attr_index, LintAttribute { reason, lint_instances, attr_id, kind, .. }) in
726+
for (attr_index, LintAttribute { reason, lint_instances, kind, target_span, .. }) in
727727
attrs.enumerate()
728728
{
729-
let attr_id = attr_id.attr_id;
730729
let level = match kind {
731730
LintAttributeKind::Allow => Level::Allow,
732731
LintAttributeKind::Deny => Level::Deny,
@@ -737,12 +736,16 @@ impl<'s, P: LintLevelsProvider> LintLevelsBuilder<'s, P> {
737736
let lint_index = lint.lint_index().try_into().unwrap();
738737
let attr_index = attr_index.try_into().unwrap();
739738
let expectation_id = match source_hir_id {
740-
None => LintExpectationId::Unstable { attr_id, lint_index },
739+
None => LintExpectationId::Unstable {
740+
target_span: *target_span,
741+
lint_index,
742+
attr_index,
743+
},
741744
Some(hir_id) => LintExpectationId::Stable {
742745
hir_id,
743-
attr_id,
744746
lint_index,
745747
attr_index,
748+
target_span: *target_span,
746749
},
747750
};
748751

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 13 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
use std::borrow::Cow;
22
use std::fmt::Display;
3+
use std::hash::Hash;
34

45
use rustc_data_structures::fx::FxIndexSet;
56
use rustc_data_structures::stable_hasher::{
@@ -8,9 +9,10 @@ use rustc_data_structures::stable_hasher::{
89
use rustc_error_messages::{DiagArgValue, IntoDiagArg};
910
use rustc_hir_id::{HirId, ItemLocalId};
1011
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
12+
use rustc_serialize::{Decodable, Decoder, Encodable, Encoder};
1113
use rustc_span::def_id::DefPathHash;
1214
pub use rustc_span::edition::Edition;
13-
use rustc_span::{AttrId, HashStableContext, Ident, Span, Symbol, sym};
15+
use rustc_span::{HashStableContext, Ident, Span, SpanDecoder, SpanEncoder, Symbol, sym};
1416
use serde::{Deserialize, Serialize};
1517

1618
pub use self::Level::*;
@@ -91,7 +93,7 @@ pub enum Applicability {
9193
/// instances might be loaded from cache. Lint messages can be emitted during an
9294
/// `EarlyLintPass` operating on the AST and during a `LateLintPass` traversing the
9395
/// HIR tree. The AST doesn't have enough information to create a stable id. The
94-
/// `LintExpectationId` will instead store the [`AttrId`] defining the expectation.
96+
/// `LintExpectationId` will instead store the `target_span` defining the expectation.
9597
/// These `LintExpectationId` will be updated to use the stable [`HirId`] once the
9698
/// AST has been lowered. The transformation is done by the `LintLevelsBuilder`
9799
///
@@ -105,12 +107,12 @@ pub enum Applicability {
105107
pub enum LintExpectationId {
106108
/// Used for lints emitted during the `EarlyLintPass`. This id is not
107109
/// hash stable and should not be cached.
108-
Unstable { attr_id: AttrId, lint_index: u16 },
110+
Unstable { target_span: Span, lint_index: u16, attr_index: u16 },
109111
/// The [`HirId`] that the lint expectation is attached to. This id is
110112
/// stable and can be cached. The additional index ensures that nodes with
111113
/// several expectations can correctly match diagnostics to the individual
112114
/// expectation.
113-
Stable { hir_id: HirId, attr_id: AttrId, attr_index: u16, lint_index: u16 },
115+
Stable { hir_id: HirId, attr_index: u16, lint_index: u16, target_span: Span },
114116
}
115117

116118
impl LintExpectationId {
@@ -140,12 +142,13 @@ impl<Hcx: HashStableContext> HashStable<Hcx> for LintExpectationId {
140142
#[inline]
141143
fn hash_stable(&self, hcx: &mut Hcx, hasher: &mut StableHasher) {
142144
match self {
143-
LintExpectationId::Stable { hir_id, attr_index, lint_index, .. } => {
145+
LintExpectationId::Stable { hir_id, target_span, attr_index, lint_index, .. } => {
144146
hir_id.hash_stable(hcx, hasher);
147+
target_span.hash_stable(hcx, hasher);
145148
attr_index.hash_stable(hcx, hasher);
146149
lint_index.hash_stable(hcx, hasher);
147150
}
148-
_ => {
151+
LintExpectationId::Unstable { .. } => {
149152
unreachable!(
150153
"HashStable should only be called for filled and stable `LintExpectationId`"
151154
)
@@ -155,16 +158,16 @@ impl<Hcx: HashStableContext> HashStable<Hcx> for LintExpectationId {
155158
}
156159

157160
impl<Hcx: HashStableContext> ToStableHashKey<Hcx> for LintExpectationId {
158-
type KeyType = (DefPathHash, ItemLocalId, u16, u16);
161+
type KeyType = (DefPathHash, ItemLocalId, Span, u16, u16);
159162

160163
#[inline]
161164
fn to_stable_hash_key(&self, hcx: &mut Hcx) -> Self::KeyType {
162165
match self {
163-
LintExpectationId::Stable { hir_id, attr_index, lint_index, .. } => {
166+
LintExpectationId::Stable { hir_id, attr_index, lint_index, target_span, .. } => {
164167
let (def_path_hash, lint_idx) = hir_id.to_stable_hash_key(hcx);
165-
(def_path_hash, lint_idx, *attr_index, *lint_index)
168+
(def_path_hash, lint_idx, *target_span, *attr_index, *lint_index)
166169
}
167-
_ => {
170+
LintExpectationId::Unstable { .. } => {
168171
unreachable!("HashStable should only be called for a filled `LintExpectationId`")
169172
}
170173
}

0 commit comments

Comments
 (0)