Skip to content

Commit 5410900

Browse files
committed
Adjust SanityCheck.
The actual implementation remains in `rustc_mir_dataflow`, but this commit moves the `MirPass` impl to `rustc_mir_transform` and changes it to a `MirLint` (fixing a `FIXME` comment). (I originally tried moving the full implementation from `rustc_mir_dataflow` but I had some trait problems with `HasMoveData` and `RustcPeekAt` and `MaybeLiveLocals`. This commit was much smaller and simpler, but still will allow some follow-up cleanups.)
1 parent bd53aa3 commit 5410900

File tree

3 files changed

+50
-42
lines changed

3 files changed

+50
-42
lines changed

compiler/rustc_mir_dataflow/src/rustc_peek.rs

Lines changed: 35 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
use rustc_ast::MetaItem;
22
use rustc_hir::def_id::DefId;
33
use rustc_index::bit_set::BitSet;
4-
use rustc_middle::mir::{self, Body, Local, Location, MirPass};
4+
use rustc_middle::mir::{self, Body, Local, Location};
55
use rustc_middle::ty::{self, Ty, TyCtxt};
66
use rustc_span::symbol::{sym, Symbol};
77
use rustc_span::Span;
@@ -18,8 +18,6 @@ use crate::impls::{
1818
use crate::move_paths::{HasMoveData, LookupResult, MoveData, MovePathIndex};
1919
use crate::{Analysis, JoinSemiLattice, ResultsCursor};
2020

21-
pub struct SanityCheck;
22-
2321
fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<MetaItem> {
2422
for attr in tcx.get_attrs(def_id, sym::rustc_mir) {
2523
let items = attr.meta_item_list();
@@ -33,53 +31,50 @@ fn has_rustc_mir_with(tcx: TyCtxt<'_>, def_id: DefId, name: Symbol) -> Option<Me
3331
None
3432
}
3533

36-
// FIXME: This should be a `MirLint`, but it needs to be moved back to `rustc_mir_transform` first.
37-
impl<'tcx> MirPass<'tcx> for SanityCheck {
38-
fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
39-
let def_id = body.source.def_id();
40-
if !tcx.has_attr(def_id, sym::rustc_mir) {
41-
debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
42-
return;
43-
} else {
44-
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
45-
}
34+
pub fn sanity_check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
35+
let def_id = body.source.def_id();
36+
if !tcx.has_attr(def_id, sym::rustc_mir) {
37+
debug!("skipping rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
38+
return;
39+
} else {
40+
debug!("running rustc_peek::SanityCheck on {}", tcx.def_path_str(def_id));
41+
}
4642

47-
let param_env = tcx.param_env(def_id);
48-
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
43+
let param_env = tcx.param_env(def_id);
44+
let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true);
4945

50-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
51-
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
52-
.into_engine(tcx, body)
53-
.iterate_to_fixpoint();
46+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() {
47+
let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data)
48+
.into_engine(tcx, body)
49+
.iterate_to_fixpoint();
5450

55-
sanity_check_via_rustc_peek(tcx, flow_inits.into_results_cursor(body));
56-
}
51+
sanity_check_via_rustc_peek(tcx, flow_inits.into_results_cursor(body));
52+
}
5753

58-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() {
59-
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
60-
.into_engine(tcx, body)
61-
.iterate_to_fixpoint();
54+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() {
55+
let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data)
56+
.into_engine(tcx, body)
57+
.iterate_to_fixpoint();
6258

63-
sanity_check_via_rustc_peek(tcx, flow_uninits.into_results_cursor(body));
64-
}
59+
sanity_check_via_rustc_peek(tcx, flow_uninits.into_results_cursor(body));
60+
}
6561

66-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() {
67-
let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data)
68-
.into_engine(tcx, body)
69-
.iterate_to_fixpoint();
62+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() {
63+
let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data)
64+
.into_engine(tcx, body)
65+
.iterate_to_fixpoint();
7066

71-
sanity_check_via_rustc_peek(tcx, flow_def_inits.into_results_cursor(body));
72-
}
67+
sanity_check_via_rustc_peek(tcx, flow_def_inits.into_results_cursor(body));
68+
}
7369

74-
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_liveness).is_some() {
75-
let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint();
70+
if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_liveness).is_some() {
71+
let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint();
7672

77-
sanity_check_via_rustc_peek(tcx, flow_liveness.into_results_cursor(body));
78-
}
73+
sanity_check_via_rustc_peek(tcx, flow_liveness.into_results_cursor(body));
74+
}
7975

80-
if has_rustc_mir_with(tcx, def_id, sym::stop_after_dataflow).is_some() {
81-
tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation);
82-
}
76+
if has_rustc_mir_with(tcx, def_id, sym::stop_after_dataflow).is_some() {
77+
tcx.dcx().emit_fatal(StopAfterDataFlowEndedCompilation);
8378
}
8479
}
8580

compiler/rustc_mir_transform/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ use rustc_middle::mir::{
3232
use rustc_middle::ty::{self, TyCtxt, TypeVisitableExt};
3333
use rustc_middle::util::Providers;
3434
use rustc_middle::{bug, query, span_bug};
35-
use rustc_mir_dataflow::rustc_peek;
3635
use rustc_span::source_map::Spanned;
3736
use rustc_span::{sym, DUMMY_SP};
3837
use rustc_trait_selection::traits;
@@ -96,6 +95,7 @@ mod remove_unneeded_drops;
9695
mod remove_zsts;
9796
mod required_consts;
9897
mod reveal_all;
98+
mod sanity_check;
9999
mod shim;
100100
mod ssa;
101101
// This pass is public to allow external drivers to perform MIR cleanup
@@ -288,7 +288,7 @@ fn mir_built(tcx: TyCtxt<'_>, def: LocalDefId) -> &Steal<Body<'_>> {
288288
&Lint(function_item_references::FunctionItemReferences),
289289
// What we need to do constant evaluation.
290290
&simplify::SimplifyCfg::Initial,
291-
&rustc_peek::SanityCheck, // Just a lint
291+
&Lint(sanity_check::SanityCheck),
292292
],
293293
None,
294294
);
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
use rustc_middle::mir::Body;
2+
use rustc_middle::ty::TyCtxt;
3+
use rustc_mir_dataflow::rustc_peek::sanity_check;
4+
5+
use crate::MirLint;
6+
7+
pub(super) struct SanityCheck;
8+
9+
impl<'tcx> MirLint<'tcx> for SanityCheck {
10+
fn run_lint(&self, tcx: TyCtxt<'tcx>, body: &Body<'tcx>) {
11+
sanity_check(tcx, body);
12+
}
13+
}

0 commit comments

Comments
 (0)