Skip to content

Commit b0c869a

Browse files
Rollup merge of rust-lang#156596 - nnethercote:split-LintExpectation, r=GuillaumeGomez
Split `LintExpectationId`s This PR makes clearer where stable and unstable `LintExpectationIds` can occur, plus a few other small cleanups. Details in individual commits. r? @GuillaumeGomez
2 parents ff42fc0 + 6c9d519 commit b0c869a

11 files changed

Lines changed: 224 additions & 188 deletions

File tree

compiler/rustc_codegen_ssa/src/lib.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ use rustc_lint_defs::builtin::LINKER_INFO;
2727
use rustc_macros::{Decodable, Encodable};
2828
use rustc_metadata::EncodedMetadata;
2929
use rustc_middle::dep_graph::WorkProduct;
30-
use rustc_middle::lint::LevelSpec;
30+
use rustc_middle::lint::StableLevelSpec;
3131
use rustc_middle::middle::debugger_visualizer::DebuggerVisualizerFile;
3232
use rustc_middle::middle::dependency_format::Dependencies;
3333
use rustc_middle::middle::exported_symbols::SymbolExportKind;
@@ -374,8 +374,8 @@ impl CompiledModules {
374374
/// Instead, encode exactly the information we need.
375375
#[derive(Copy, Clone, Debug, Encodable, Decodable)]
376376
pub struct CodegenLintLevelSpecs {
377-
linker_messages: LevelSpec,
378-
linker_info: LevelSpec,
377+
linker_messages: StableLevelSpec,
378+
linker_info: StableLevelSpec,
379379
}
380380

381381
impl CodegenLintLevelSpecs {

compiler/rustc_lint/src/context.rs

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,17 @@ use rustc_hir::def_id::{CrateNum, DefId};
2020
use rustc_hir::definitions::{DefPathData, DisambiguatedDefPathData};
2121
use rustc_hir::{Pat, PatKind};
2222
use rustc_middle::bug;
23-
use rustc_middle::lint::LevelSpec;
23+
use rustc_middle::lint::{LevelSpec, StableLevelSpec, UnstableLevelSpec};
2424
use rustc_middle::middle::privacy::EffectiveVisibilities;
2525
use rustc_middle::ty::layout::{LayoutError, LayoutOfHelpers, TyAndLayout};
2626
use rustc_middle::ty::print::{PrintError, PrintTraitRefExt as _, Printer, with_no_trimmed_paths};
2727
use rustc_middle::ty::{
2828
self, GenericArg, RegisteredTools, Ty, TyCtxt, TypingEnv, TypingMode, Unnormalized,
2929
};
30-
use rustc_session::lint::{FutureIncompatibleInfo, Lint, LintExpectationId, LintId};
30+
use rustc_session::lint::{
31+
FutureIncompatibleInfo, Lint, LintExpectationId, LintId, StableLintExpectationId,
32+
UnstableLintExpectationId,
33+
};
3134
use rustc_session::{DynLintStore, Session};
3235
use rustc_span::edit_distance::find_best_match_for_names;
3336
use rustc_span::{Ident, Span, Symbol, sym};
@@ -510,6 +513,8 @@ pub struct EarlyContext<'a> {
510513
}
511514

512515
pub trait LintContext {
516+
type LintExpectationId: Copy + Into<LintExpectationId>;
517+
513518
fn sess(&self) -> &Session;
514519

515520
// FIXME: These methods should not take an Into<MultiSpan> -- instead, callers should need to
@@ -538,7 +543,7 @@ pub trait LintContext {
538543
}
539544

540545
/// This returns the lint level spec for the given lint at the current location.
541-
fn get_lint_level_spec(&self, lint: &'static Lint) -> LevelSpec;
546+
fn get_lint_level_spec(&self, lint: &'static Lint) -> LevelSpec<Self::LintExpectationId>;
542547

543548
/// This function can be used to manually fulfill an expectation. This can
544549
/// be used for lints which contain several spans, and should be suppressed,
@@ -547,7 +552,7 @@ pub trait LintContext {
547552
/// Note that this function should only be called for [`LintExpectationId`]s
548553
/// retrieved from the current lint pass. Buffered or manually created ids can
549554
/// cause ICEs.
550-
fn fulfill_expectation(&self, expectation: LintExpectationId) {
555+
fn fulfill_expectation(&self, expectation: Self::LintExpectationId) {
551556
// We need to make sure that submitted expectation ids are correctly fulfilled suppressed
552557
// and stored between compilation sessions. To not manually do these steps, we simply create
553558
// a dummy diagnostic and emit it as usual, which will be suppressed and stored like a
@@ -556,7 +561,7 @@ pub trait LintContext {
556561
.dcx()
557562
.struct_expect(
558563
"this is a dummy diagnostic, to submit and store an expectation",
559-
expectation,
564+
expectation.into(),
560565
)
561566
.emit();
562567
}
@@ -585,6 +590,8 @@ impl<'a> EarlyContext<'a> {
585590
}
586591

587592
impl<'tcx> LintContext for LateContext<'tcx> {
593+
type LintExpectationId = StableLintExpectationId;
594+
588595
/// Gets the overall compiler `Session` object.
589596
fn sess(&self) -> &Session {
590597
self.tcx.sess
@@ -604,12 +611,14 @@ impl<'tcx> LintContext for LateContext<'tcx> {
604611
}
605612
}
606613

607-
fn get_lint_level_spec(&self, lint: &'static Lint) -> LevelSpec {
614+
fn get_lint_level_spec(&self, lint: &'static Lint) -> StableLevelSpec {
608615
self.tcx.lint_level_spec_at_node(lint, self.last_node_with_lint_attrs)
609616
}
610617
}
611618

612619
impl LintContext for EarlyContext<'_> {
620+
type LintExpectationId = UnstableLintExpectationId;
621+
613622
/// Gets the overall compiler `Session` object.
614623
fn sess(&self) -> &Session {
615624
self.builder.sess()
@@ -624,7 +633,7 @@ impl LintContext for EarlyContext<'_> {
624633
self.builder.opt_span_lint(lint, span.map(|s| s.into()), decorator)
625634
}
626635

627-
fn get_lint_level_spec(&self, lint: &'static Lint) -> LevelSpec {
636+
fn get_lint_level_spec(&self, lint: &'static Lint) -> UnstableLevelSpec {
628637
self.builder.lint_level_spec(lint)
629638
}
630639
}

compiler/rustc_lint/src/early.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ impl<'ecx, T: EarlyLintPass> EarlyContextAndPass<'ecx, T> {
5353
{
5454
let is_crate_node = id == ast::CRATE_NODE_ID;
5555
debug!(?id);
56-
let push = self.context.builder.push(attrs, is_crate_node, None);
56+
let push = self.context.builder.push(attrs, is_crate_node);
5757

5858
debug!("early context: enter_attrs({:?})", attrs);
5959
lint_callback!(self, check_attributes, attrs);

compiler/rustc_lint/src/expect.rs

Lines changed: 11 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,8 @@ use rustc_data_structures::fx::FxHashSet;
22
use rustc_middle::lint::LintExpectation;
33
use rustc_middle::query::Providers;
44
use rustc_middle::ty::TyCtxt;
5-
use rustc_session::lint::LintExpectationId;
65
use rustc_session::lint::builtin::UNFULFILLED_LINT_EXPECTATIONS;
6+
use rustc_session::lint::{LintExpectationId, StableLintExpectationId};
77
use rustc_span::Symbol;
88

99
use crate::lints::{Expectation, ExpectationNote};
@@ -12,7 +12,7 @@ pub(crate) fn provide(providers: &mut Providers) {
1212
*providers = Providers { lint_expectations, check_expectations, ..*providers };
1313
}
1414

15-
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(LintExpectationId, LintExpectation)> {
15+
fn lint_expectations(tcx: TyCtxt<'_>, (): ()) -> Vec<(StableLintExpectationId, LintExpectation)> {
1616
let krate = tcx.hir_crate_items(());
1717

1818
let mut expectations = Vec::new();
@@ -31,30 +31,22 @@ fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
3131

3232
// Turn a `LintExpectationId` into a `(AttrId, lint_index)` pair.
3333
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) } => {
34+
let (attr_id, lint_index) = match *expect_id {
35+
LintExpectationId::Unstable(id) => (id.attr_id, id.lint_index),
36+
LintExpectationId::Stable(id) => {
3937
// 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)
38+
(tcx.hir_attrs(id.hir_id)[id.attr_index as usize].id(), id.lint_index)
4339
}
44-
_ => panic!("fulfilled expectations must have a lint index"),
45-
}
40+
};
41+
(attr_id, lint_index.expect("fulfilled expectations must have a lint index"))
4642
};
4743

4844
let fulfilled_expectations: FxHashSet<_> =
4945
fulfilled_expectations.iter().map(canonicalize_id).collect();
5046

5147
for (expect_id, expectation) in lint_expectations {
52-
// This check will always be true, since `lint_expectations` only holds stable ids
53-
let LintExpectationId::Stable { hir_id, .. } = expect_id else {
54-
unreachable!("at this stage all `LintExpectationId`s are stable");
55-
};
56-
57-
let expect_id = canonicalize_id(expect_id);
48+
let hir_id = expect_id.hir_id;
49+
let expect_id = canonicalize_id(&LintExpectationId::Stable(*expect_id));
5850

5951
if !fulfilled_expectations.contains(&expect_id)
6052
&& tool_filter.is_none_or(|filter| expectation.lint_tool == Some(filter))
@@ -63,7 +55,7 @@ fn check_expectations(tcx: TyCtxt<'_>, tool_filter: Option<Symbol>) {
6355
let note = expectation.is_unfulfilled_lint_expectations;
6456
tcx.emit_node_span_lint(
6557
UNFULFILLED_LINT_EXPECTATIONS,
66-
*hir_id,
58+
hir_id,
6759
expectation.emission_span,
6860
Expectation { rationale, note },
6961
);

compiler/rustc_lint/src/late.rs

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,14 +439,6 @@ fn late_lint_crate<'tcx>(tcx: TyCtxt<'tcx>) {
439439

440440
filtered_passes.push(Box::new(HardwiredLints));
441441
let pass = RuntimeCombinedLateLintPass { passes: &mut filtered_passes[..] };
442-
late_lint_crate_inner(tcx, context, pass);
443-
}
444-
445-
fn late_lint_crate_inner<'tcx, T: LateLintPass<'tcx>>(
446-
tcx: TyCtxt<'tcx>,
447-
context: LateContext<'tcx>,
448-
pass: T,
449-
) {
450442
let mut cx = LateContextAndPass { context, pass };
451443

452444
// Visit the whole crate.

0 commit comments

Comments
 (0)