Skip to content

Commit 3414ad9

Browse files
committed
Emit unfullfilled_lint_expectation using a HirId for performance (RFC-2383)
1 parent a14456f commit 3414ad9

File tree

3 files changed

+29
-69
lines changed

3 files changed

+29
-69
lines changed

compiler/rustc_lint/src/expect.rs

+16-13
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
11
use crate::builtin;
22
use rustc_data_structures::fx::FxHashMap;
3-
use rustc_middle::lint::struct_lint_level;
3+
use rustc_hir::HirId;
44
use rustc_middle::{lint::LintExpectation, ty::TyCtxt};
55
use rustc_session::lint::LintExpectationId;
66
use rustc_span::symbol::sym;
7-
use rustc_span::MultiSpan;
87

98
pub fn check_expectations(tcx: TyCtxt<'_>) {
109
if !tcx.sess.features_untracked().enabled(sym::lint_reasons) {
@@ -17,24 +16,28 @@ pub fn check_expectations(tcx: TyCtxt<'_>) {
1716

1817
for (id, expectation) in lint_expectations {
1918
if !fulfilled_expectations.contains(id) {
20-
emit_unfulfilled_expectation_lint(tcx, expectation);
19+
// This check will always be true, since `lint_expectations` only
20+
// holds stable ids
21+
if let LintExpectationId::Stable { hir_id, .. } = id {
22+
emit_unfulfilled_expectation_lint(tcx, *hir_id, expectation);
23+
}
2124
}
2225
}
2326
}
2427

25-
fn emit_unfulfilled_expectation_lint(tcx: TyCtxt<'_>, expectation: &LintExpectation) {
28+
fn emit_unfulfilled_expectation_lint(
29+
tcx: TyCtxt<'_>,
30+
hir_id: HirId,
31+
expectation: &LintExpectation,
32+
) {
2633
// FIXME: The current implementation doesn't cover cases where the
2734
// `unfulfilled_lint_expectations` is actually expected by another lint
28-
// expectation. This can be added here as we have the lint level of this
29-
// expectation, and we can also mark the lint expectation it would fulfill
30-
// as such. This is currently not implemented to get some early feedback
31-
// before diving deeper into this.
32-
struct_lint_level(
33-
tcx.sess,
35+
// expectation. This can be added here by checking the lint level and
36+
// retrieving the `LintExpectationId` if it was expected.
37+
tcx.struct_span_lint_hir(
3438
builtin::UNFULFILLED_LINT_EXPECTATIONS,
35-
expectation.emission_level,
36-
expectation.emission_level_source,
37-
Some(MultiSpan::from_span(expectation.emission_span)),
39+
hir_id,
40+
expectation.emission_span,
3841
|diag| {
3942
let mut diag = diag.build("this lint expectation is unfulfilled");
4043
if let Some(rationale) = expectation.reason {

compiler/rustc_lint/src/levels.rs

+11-45
Original file line numberDiff line numberDiff line change
@@ -245,9 +245,9 @@ impl<'s> LintLevelsBuilder<'s> {
245245
for (attr_index, attr) in attrs.iter().enumerate() {
246246
let level = match Level::from_attr(attr) {
247247
None => continue,
248-
Some(Level::Expect(unstable_id)) if source_hir_id.is_some() => {
249-
let stable_id =
250-
self.create_stable_id(unstable_id, source_hir_id.unwrap(), attr_index);
248+
Some(Level::Expect(unstable_id)) if let Some(hir_id) = source_hir_id => {
249+
let stable_id = self.create_stable_id(unstable_id, hir_id, attr_index);
250+
251251
Level::Expect(stable_id)
252252
}
253253
Some(lvl) => lvl,
@@ -303,12 +303,6 @@ impl<'s> LintLevelsBuilder<'s> {
303303
}
304304
}
305305

306-
let (unfulfilled_lint_lvl, unfulfilled_lint_src) = self.sets.get_lint_level(
307-
builtin::UNFULFILLED_LINT_EXPECTATIONS,
308-
self.cur,
309-
Some(&specs),
310-
&sess,
311-
);
312306
for (lint_index, li) in metas.iter_mut().enumerate() {
313307
let level = match level {
314308
Level::Expect(mut id) => {
@@ -360,15 +354,8 @@ impl<'s> LintLevelsBuilder<'s> {
360354
self.insert_spec(&mut specs, id, (level, src));
361355
}
362356
if let Level::Expect(expect_id) = level {
363-
self.lint_expectations.insert(
364-
expect_id,
365-
LintExpectation::new(
366-
reason,
367-
sp,
368-
unfulfilled_lint_lvl,
369-
unfulfilled_lint_src,
370-
),
371-
);
357+
self.lint_expectations
358+
.insert(expect_id, LintExpectation::new(reason, sp));
372359
}
373360
}
374361

@@ -386,15 +373,8 @@ impl<'s> LintLevelsBuilder<'s> {
386373
self.insert_spec(&mut specs, *id, (level, src));
387374
}
388375
if let Level::Expect(expect_id) = level {
389-
self.lint_expectations.insert(
390-
expect_id,
391-
LintExpectation::new(
392-
reason,
393-
sp,
394-
unfulfilled_lint_lvl,
395-
unfulfilled_lint_src,
396-
),
397-
);
376+
self.lint_expectations
377+
.insert(expect_id, LintExpectation::new(reason, sp));
398378
}
399379
}
400380
Err((Some(ids), ref new_lint_name)) => {
@@ -433,15 +413,8 @@ impl<'s> LintLevelsBuilder<'s> {
433413
self.insert_spec(&mut specs, *id, (level, src));
434414
}
435415
if let Level::Expect(expect_id) = level {
436-
self.lint_expectations.insert(
437-
expect_id,
438-
LintExpectation::new(
439-
reason,
440-
sp,
441-
unfulfilled_lint_lvl,
442-
unfulfilled_lint_src,
443-
),
444-
);
416+
self.lint_expectations
417+
.insert(expect_id, LintExpectation::new(reason, sp));
445418
}
446419
}
447420
Err((None, _)) => {
@@ -537,15 +510,8 @@ impl<'s> LintLevelsBuilder<'s> {
537510
self.insert_spec(&mut specs, id, (level, src));
538511
}
539512
if let Level::Expect(expect_id) = level {
540-
self.lint_expectations.insert(
541-
expect_id,
542-
LintExpectation::new(
543-
reason,
544-
sp,
545-
unfulfilled_lint_lvl,
546-
unfulfilled_lint_src,
547-
),
548-
);
513+
self.lint_expectations
514+
.insert(expect_id, LintExpectation::new(reason, sp));
549515
}
550516
} else {
551517
panic!("renamed lint does not exist: {}", new_name);

compiler/rustc_middle/src/lint.rs

+2-11
Original file line numberDiff line numberDiff line change
@@ -204,20 +204,11 @@ pub struct LintExpectation {
204204
pub reason: Option<Symbol>,
205205
/// The [`Span`] of the attribute that this expectation originated from.
206206
pub emission_span: Span,
207-
/// The [`Level`] that this lint diagnostic should be emitted if unfulfilled.
208-
pub emission_level: Level,
209-
/// The [`LintLevelSource`] information needed for [`struct_lint_level`].
210-
pub emission_level_source: LintLevelSource,
211207
}
212208

213209
impl LintExpectation {
214-
pub fn new(
215-
reason: Option<Symbol>,
216-
attr_span: Span,
217-
emission_level: Level,
218-
emission_level_source: LintLevelSource,
219-
) -> Self {
220-
Self { reason, emission_span: attr_span, emission_level, emission_level_source }
210+
pub fn new(reason: Option<Symbol>, attr_span: Span) -> Self {
211+
Self { reason, emission_span: attr_span }
221212
}
222213
}
223214

0 commit comments

Comments
 (0)