Skip to content

Commit 2e1bbdb

Browse files
committed
make lint_index not optional in LintExpectationId
also add attr_id to `Stable` variant directly, instead of having to iterate over all the attrs on the hir_id to find it
1 parent 86edddb commit 2e1bbdb

2 files changed

Lines changed: 10 additions & 51 deletions

File tree

compiler/rustc_lint/src/expect.rs

Lines changed: 3 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -30,19 +30,9 @@ fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
3030
let fulfilled_expectations = tcx.dcx().steal_fulfilled_expectation_ids();
3131

3232
// Turn a `LintExpectationId` into a `(AttrId, lint_index)` pair.
33-
let canonicalize_id = |expect_id: &LintExpectationId| {
34-
match *expect_id {
35-
LintExpectationId::Unstable { attr_id, lint_index: Some(lint_index) } => {
36-
(attr_id, lint_index)
37-
}
38-
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
39-
// We are an `eval_always` query, so looking at the attribute's `AttrId` is ok.
40-
let attr_id = tcx.hir_attrs(hir_id)[attr_index as usize].id();
41-
42-
(attr_id, lint_index)
43-
}
44-
_ => panic!("fulfilled expectations must have a lint index"),
45-
}
33+
let canonicalize_id = |expect_id: &LintExpectationId| match *expect_id {
34+
LintExpectationId::Unstable { attr_id, lint_index, .. } => (attr_id, lint_index),
35+
LintExpectationId::Stable { attr_id, lint_index, .. } => (attr_id, lint_index),
4636
};
4737

4838
let fulfilled_expectations: FxHashSet<_> =

compiler/rustc_lint_defs/src/lib.rs

Lines changed: 7 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,6 @@
11
use std::borrow::Cow;
22
use std::fmt::Display;
33

4-
use rustc_ast::AttrId;
5-
use rustc_ast::attr::AttributeExt;
64
use rustc_data_structures::fx::FxIndexSet;
75
use rustc_data_structures::stable_hasher::{
86
HashStable, StableCompare, StableHasher, ToStableHashKey,
@@ -12,7 +10,7 @@ use rustc_hir_id::{HashStableContext, HirId, ItemLocalId};
1210
use rustc_macros::{Decodable, Encodable, HashStable_Generic};
1311
use rustc_span::def_id::DefPathHash;
1412
pub use rustc_span::edition::Edition;
15-
use rustc_span::{Ident, Span, Symbol, sym};
13+
use rustc_span::{AttrId, Ident, Span, Symbol};
1614
use serde::{Deserialize, Serialize};
1715

1816
pub use self::Level::*;
@@ -107,12 +105,12 @@ pub enum Applicability {
107105
pub enum LintExpectationId {
108106
/// Used for lints emitted during the `EarlyLintPass`. This id is not
109107
/// hash stable and should not be cached.
110-
Unstable { attr_id: AttrId, lint_index: Option<u16> },
108+
Unstable { attr_id: AttrId, lint_index: u16 },
111109
/// The [`HirId`] that the lint expectation is attached to. This id is
112110
/// stable and can be cached. The additional index ensures that nodes with
113111
/// several expectations can correctly match diagnostics to the individual
114112
/// expectation.
115-
Stable { hir_id: HirId, attr_index: u16, lint_index: Option<u16> },
113+
Stable { hir_id: HirId, attr_id: AttrId, attr_index: u16, lint_index: u16 },
116114
}
117115

118116
impl LintExpectationId {
@@ -123,14 +121,14 @@ impl LintExpectationId {
123121
}
124122
}
125123

126-
pub fn get_lint_index(&self) -> Option<u16> {
124+
pub fn get_lint_index(&self) -> u16 {
127125
let (LintExpectationId::Unstable { lint_index, .. }
128126
| LintExpectationId::Stable { lint_index, .. }) = self;
129127

130128
*lint_index
131129
}
132130

133-
pub fn set_lint_index(&mut self, new_lint_index: Option<u16>) {
131+
pub fn set_lint_index(&mut self, new_lint_index: u16) {
134132
let (LintExpectationId::Unstable { lint_index, .. }
135133
| LintExpectationId::Stable { lint_index, .. }) = self;
136134

@@ -142,7 +140,7 @@ impl<HCX: HashStableContext> HashStable<HCX> for LintExpectationId {
142140
#[inline]
143141
fn hash_stable(&self, hcx: &mut HCX, hasher: &mut StableHasher) {
144142
match self {
145-
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
143+
LintExpectationId::Stable { hir_id, attr_index, lint_index, .. } => {
146144
hir_id.hash_stable(hcx, hasher);
147145
attr_index.hash_stable(hcx, hasher);
148146
lint_index.hash_stable(hcx, hasher);
@@ -162,7 +160,7 @@ impl<HCX: HashStableContext> ToStableHashKey<HCX> for LintExpectationId {
162160
#[inline]
163161
fn to_stable_hash_key(&self, hcx: &HCX) -> Self::KeyType {
164162
match self {
165-
LintExpectationId::Stable { hir_id, attr_index, lint_index: Some(lint_index) } => {
163+
LintExpectationId::Stable { hir_id, attr_index, lint_index, .. } => {
166164
let (def_path_hash, lint_idx) = hir_id.to_stable_hash_key(hcx);
167165
(def_path_hash, lint_idx, *attr_index, *lint_index)
168166
}
@@ -247,35 +245,6 @@ impl Level {
247245
}
248246
}
249247

250-
/// Converts an `Attribute` to a level.
251-
pub fn from_attr(attr: &impl AttributeExt) -> Option<(Self, Option<LintExpectationId>)> {
252-
attr.name().and_then(|name| Self::from_symbol(name, || Some(attr.id())))
253-
}
254-
255-
/// Converts a `Symbol` to a level.
256-
pub fn from_symbol(
257-
s: Symbol,
258-
id: impl FnOnce() -> Option<AttrId>,
259-
) -> Option<(Self, Option<LintExpectationId>)> {
260-
match s {
261-
sym::allow => Some((Level::Allow, None)),
262-
sym::expect => {
263-
if let Some(attr_id) = id() {
264-
Some((
265-
Level::Expect,
266-
Some(LintExpectationId::Unstable { attr_id, lint_index: None }),
267-
))
268-
} else {
269-
None
270-
}
271-
}
272-
sym::warn => Some((Level::Warn, None)),
273-
sym::deny => Some((Level::Deny, None)),
274-
sym::forbid => Some((Level::Forbid, None)),
275-
_ => None,
276-
}
277-
}
278-
279248
pub fn to_cmd_flag(self) -> &'static str {
280249
match self {
281250
Level::Warn => "-W",

0 commit comments

Comments
 (0)