Skip to content

Commit 8b90e70

Browse files
committed
mir validator: don't store mir phase
1 parent bd36e69 commit 8b90e70

File tree

3 files changed

+34
-57
lines changed

3 files changed

+34
-57
lines changed

compiler/rustc_mir_transform/src/inline.rs

+1-9
Original file line numberDiff line numberDiff line change
@@ -220,15 +220,7 @@ impl<'tcx> Inliner<'tcx> {
220220

221221
// Normally, this shouldn't be required, but trait normalization failure can create a
222222
// validation ICE.
223-
if !validate_types(
224-
self.tcx,
225-
MirPhase::Runtime(RuntimePhase::Optimized),
226-
self.typing_env,
227-
&callee_body,
228-
&caller_body,
229-
)
230-
.is_empty()
231-
{
223+
if !validate_types(self.tcx, self.typing_env, &callee_body, &caller_body).is_empty() {
232224
return Err("failed to validate callee body");
233225
}
234226

compiler/rustc_mir_transform/src/pass_manager.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -292,7 +292,7 @@ fn run_passes_inner<'tcx>(
292292
}
293293

294294
pub(super) fn validate_body<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, when: String) {
295-
validate::Validator { when, mir_phase: body.phase }.run_pass(tcx, body);
295+
validate::Validator { when }.run_pass(tcx, body);
296296
}
297297

298298
fn dump_mir_for_pass<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, pass_name: &str, is_after: bool) {

compiler/rustc_mir_transform/src/validate.rs

+32-47
Original file line numberDiff line numberDiff line change
@@ -29,12 +29,6 @@ enum EdgeKind {
2929
pub(super) struct Validator {
3030
/// Describes at which point in the pipeline this validation is happening.
3131
pub when: String,
32-
/// The phase for which we are upholding the dialect. If the given phase forbids a specific
33-
/// element, this validator will now emit errors if that specific element is encountered.
34-
/// Note that phases that change the dialect cause all *following* phases to check the
35-
/// invariants of the new dialect. A phase that changes dialects never checks the new invariants
36-
/// itself.
37-
pub mir_phase: MirPhase,
3832
}
3933

4034
impl<'tcx> crate::MirPass<'tcx> for Validator {
@@ -46,11 +40,9 @@ impl<'tcx> crate::MirPass<'tcx> for Validator {
4640
if matches!(body.source.instance, InstanceKind::Intrinsic(..) | InstanceKind::Virtual(..)) {
4741
return;
4842
}
49-
debug_assert_eq!(self.mir_phase, body.phase);
5043
let def_id = body.source.def_id();
51-
let mir_phase = body.phase;
5244
let typing_env = body.typing_env(tcx);
53-
let can_unwind = if mir_phase <= MirPhase::Runtime(RuntimePhase::Initial) {
45+
let can_unwind = if body.phase <= MirPhase::Runtime(RuntimePhase::Initial) {
5446
// In this case `AbortUnwindingCalls` haven't yet been executed.
5547
true
5648
} else if !tcx.def_kind(def_id).is_fn_like() {
@@ -64,9 +56,7 @@ impl<'tcx> crate::MirPass<'tcx> for Validator {
6456
ty::Coroutine(..) => ExternAbi::Rust,
6557
// No need to do MIR validation on error bodies
6658
ty::Error(_) => return,
67-
_ => {
68-
span_bug!(body.span, "unexpected body ty: {:?} phase {:?}", body_ty, mir_phase)
69-
}
59+
_ => span_bug!(body.span, "unexpected body ty: {body_ty:?}"),
7060
};
7161

7262
ty::layout::fn_can_unwind(tcx, Some(def_id), body_abi)
@@ -76,7 +66,6 @@ impl<'tcx> crate::MirPass<'tcx> for Validator {
7666
when: &self.when,
7767
body,
7868
tcx,
79-
mir_phase,
8069
unwind_edge_count: 0,
8170
reachable_blocks: traversal::reachable_as_bitset(body),
8271
value_cache: FxHashSet::default(),
@@ -86,7 +75,7 @@ impl<'tcx> crate::MirPass<'tcx> for Validator {
8675
cfg_checker.check_cleanup_control_flow();
8776

8877
// Also run the TypeChecker.
89-
for (location, msg) in validate_types(tcx, self.mir_phase, typing_env, body, body) {
78+
for (location, msg) in validate_types(tcx, typing_env, body, body) {
9079
cfg_checker.fail(location, msg);
9180
}
9281

@@ -107,7 +96,6 @@ struct CfgChecker<'a, 'tcx> {
10796
when: &'a str,
10897
body: &'a Body<'tcx>,
10998
tcx: TyCtxt<'tcx>,
110-
mir_phase: MirPhase,
11199
unwind_edge_count: usize,
112100
reachable_blocks: BitSet<BasicBlock>,
113101
value_cache: FxHashSet<u128>,
@@ -294,41 +282,41 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
294282
fn visit_statement(&mut self, statement: &Statement<'tcx>, location: Location) {
295283
match &statement.kind {
296284
StatementKind::AscribeUserType(..) => {
297-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
285+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
298286
self.fail(
299287
location,
300288
"`AscribeUserType` should have been removed after drop lowering phase",
301289
);
302290
}
303291
}
304292
StatementKind::FakeRead(..) => {
305-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
293+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
306294
self.fail(
307295
location,
308296
"`FakeRead` should have been removed after drop lowering phase",
309297
);
310298
}
311299
}
312300
StatementKind::SetDiscriminant { .. } => {
313-
if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
301+
if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
314302
self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
315303
}
316304
}
317305
StatementKind::Deinit(..) => {
318-
if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
306+
if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
319307
self.fail(location, "`Deinit`is not allowed until deaggregation");
320308
}
321309
}
322310
StatementKind::Retag(kind, _) => {
323-
// FIXME(JakobDegen) The validator should check that `self.mir_phase <
311+
// FIXME(JakobDegen) The validator should check that `self.body.phase <
324312
// DropsLowered`. However, this causes ICEs with generation of drop shims, which
325313
// seem to fail to set their `MirPhase` correctly.
326314
if matches!(kind, RetagKind::TwoPhase) {
327315
self.fail(location, format!("explicit `{kind:?}` is forbidden"));
328316
}
329317
}
330318
StatementKind::Coverage(kind) => {
331-
if self.mir_phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup)
319+
if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup)
332320
&& let CoverageKind::BlockMarker { .. } | CoverageKind::SpanMarker { .. } = kind
333321
{
334322
self.fail(
@@ -391,7 +379,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
391379
// the return edge from the call. FIXME(tmiasko): Since this is a strictly code
392380
// generation concern, the code generation should be responsible for handling
393381
// it.
394-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Optimized)
382+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Optimized)
395383
&& self.is_critical_call_edge(target, unwind)
396384
{
397385
self.fail(
@@ -440,7 +428,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
440428
if self.body.coroutine.is_none() {
441429
self.fail(location, "`Yield` cannot appear outside coroutine bodies");
442430
}
443-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
431+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
444432
self.fail(location, "`Yield` should have been replaced by coroutine lowering");
445433
}
446434
self.check_edge(location, *resume, EdgeKind::Normal);
@@ -449,7 +437,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
449437
}
450438
}
451439
TerminatorKind::FalseEdge { real_target, imaginary_target } => {
452-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
440+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
453441
self.fail(
454442
location,
455443
"`FalseEdge` should have been removed after drop elaboration",
@@ -459,7 +447,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
459447
self.check_edge(location, *imaginary_target, EdgeKind::Normal);
460448
}
461449
TerminatorKind::FalseUnwind { real_target, unwind } => {
462-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
450+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
463451
self.fail(
464452
location,
465453
"`FalseUnwind` should have been removed after drop elaboration",
@@ -478,7 +466,7 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
478466
if self.body.coroutine.is_none() {
479467
self.fail(location, "`CoroutineDrop` cannot appear outside coroutine bodies");
480468
}
481-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
469+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
482470
self.fail(
483471
location,
484472
"`CoroutineDrop` should have been replaced by coroutine lowering",
@@ -532,13 +520,11 @@ impl<'a, 'tcx> Visitor<'tcx> for CfgChecker<'a, 'tcx> {
532520
/// `optimized_mir` is available.
533521
pub(super) fn validate_types<'tcx>(
534522
tcx: TyCtxt<'tcx>,
535-
mir_phase: MirPhase,
536523
typing_env: ty::TypingEnv<'tcx>,
537524
body: &Body<'tcx>,
538525
caller_body: &Body<'tcx>,
539526
) -> Vec<(Location, String)> {
540-
let mut type_checker =
541-
TypeChecker { body, caller_body, tcx, typing_env, mir_phase, failures: Vec::new() };
527+
let mut type_checker = TypeChecker { body, caller_body, tcx, typing_env, failures: Vec::new() };
542528
type_checker.visit_body(body);
543529
type_checker.failures
544530
}
@@ -548,7 +534,6 @@ struct TypeChecker<'a, 'tcx> {
548534
caller_body: &'a Body<'tcx>,
549535
tcx: TyCtxt<'tcx>,
550536
typing_env: ty::TypingEnv<'tcx>,
551-
mir_phase: MirPhase,
552537
failures: Vec<(Location, String)>,
553538
}
554539

@@ -577,7 +562,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> {
577562

578563
// After borrowck subtyping should be fully explicit via
579564
// `Subtype` projections.
580-
let variance = if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
565+
let variance = if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
581566
Variance::Invariant
582567
} else {
583568
Variance::Covariant
@@ -618,7 +603,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
618603
fn visit_operand(&mut self, operand: &Operand<'tcx>, location: Location) {
619604
// This check is somewhat expensive, so only run it when -Zvalidate-mir is passed.
620605
if self.tcx.sess.opts.unstable_opts.validate_mir
621-
&& self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial)
606+
&& self.body.phase < MirPhase::Runtime(RuntimePhase::Initial)
622607
{
623608
// `Operand::Copy` is only supposed to be used with `Copy` types.
624609
if let Operand::Copy(place) = operand {
@@ -642,7 +627,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
642627
) {
643628
match elem {
644629
ProjectionElem::OpaqueCast(ty)
645-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) =>
630+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) =>
646631
{
647632
self.fail(
648633
location,
@@ -656,7 +641,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
656641
}
657642
}
658643
ProjectionElem::Deref
659-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::PostCleanup) =>
644+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::PostCleanup) =>
660645
{
661646
let base_ty = place_ref.ty(&self.body.local_decls, self.tcx).ty;
662647

@@ -856,7 +841,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
856841
// Set off any `bug!`s in the type computation code
857842
let _ = place.ty(&self.body.local_decls, self.tcx);
858843

859-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial)
844+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial)
860845
&& place.projection.len() > 1
861846
&& cntxt != PlaceContext::NonUse(NonUseContext::VarDebugInfo)
862847
&& place.projection[1..].contains(&ProjectionElem::Deref)
@@ -974,7 +959,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
974959
}
975960
}
976961
AggregateKind::RawPtr(pointee_ty, mutability) => {
977-
if !matches!(self.mir_phase, MirPhase::Runtime(_)) {
962+
if !matches!(self.body.phase, MirPhase::Runtime(_)) {
978963
// It would probably be fine to support this in earlier phases, but at the
979964
// time of writing it's only ever introduced from intrinsic lowering, so
980965
// earlier things just `bug!` on it.
@@ -1016,7 +1001,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
10161001
}
10171002
},
10181003
Rvalue::Ref(_, BorrowKind::Fake(_), _) => {
1019-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1004+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
10201005
self.fail(
10211006
location,
10221007
"`Assign` statement with a `Fake` borrow should have been removed in runtime MIR",
@@ -1130,7 +1115,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
11301115
);
11311116
}
11321117
UnOp::PtrMetadata => {
1133-
if !matches!(self.mir_phase, MirPhase::Runtime(_)) {
1118+
if !matches!(self.body.phase, MirPhase::Runtime(_)) {
11341119
// It would probably be fine to support this in earlier phases, but at
11351120
// the time of writing it's only ever introduced from intrinsic
11361121
// lowering or other runtime-phase optimization passes, so earlier
@@ -1206,7 +1191,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12061191
"CastKind::{kind:?} output must be a raw const pointer, not {:?}",
12071192
ty::RawPtr(_, Mutability::Not)
12081193
);
1209-
if self.mir_phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
1194+
if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
12101195
self.fail(location, format!("After borrowck, MIR disallows {kind:?}"));
12111196
}
12121197
}
@@ -1222,7 +1207,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12221207
"CastKind::{kind:?} output must be a raw pointer, not {:?}",
12231208
ty::RawPtr(..)
12241209
);
1225-
if self.mir_phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
1210+
if self.body.phase >= MirPhase::Analysis(AnalysisPhase::PostCleanup) {
12261211
self.fail(location, format!("After borrowck, MIR disallows {kind:?}"));
12271212
}
12281213
}
@@ -1288,7 +1273,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
12881273
}
12891274
}
12901275
CastKind::Transmute => {
1291-
if let MirPhase::Runtime(..) = self.mir_phase {
1276+
if let MirPhase::Runtime(..) = self.body.phase {
12921277
// Unlike `mem::transmute`, a MIR `Transmute` is well-formed
12931278
// for any two `Sized` types, just potentially UB to run.
12941279

@@ -1317,7 +1302,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
13171302
location,
13181303
format!(
13191304
"Transmute is not supported in non-runtime phase {:?}.",
1320-
self.mir_phase
1305+
self.body.phase
13211306
),
13221307
);
13231308
}
@@ -1404,15 +1389,15 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14041389
}
14051390
}
14061391
StatementKind::AscribeUserType(..) => {
1407-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1392+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
14081393
self.fail(
14091394
location,
14101395
"`AscribeUserType` should have been removed after drop lowering phase",
14111396
);
14121397
}
14131398
}
14141399
StatementKind::FakeRead(..) => {
1415-
if self.mir_phase >= MirPhase::Runtime(RuntimePhase::Initial) {
1400+
if self.body.phase >= MirPhase::Runtime(RuntimePhase::Initial) {
14161401
self.fail(
14171402
location,
14181403
"`FakeRead` should have been removed after drop lowering phase",
@@ -1463,7 +1448,7 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14631448
}
14641449
}
14651450
StatementKind::SetDiscriminant { place, .. } => {
1466-
if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
1451+
if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
14671452
self.fail(location, "`SetDiscriminant`is not allowed until deaggregation");
14681453
}
14691454
let pty = place.ty(&self.body.local_decls, self.tcx).ty.kind();
@@ -1477,12 +1462,12 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
14771462
}
14781463
}
14791464
StatementKind::Deinit(..) => {
1480-
if self.mir_phase < MirPhase::Runtime(RuntimePhase::Initial) {
1465+
if self.body.phase < MirPhase::Runtime(RuntimePhase::Initial) {
14811466
self.fail(location, "`Deinit`is not allowed until deaggregation");
14821467
}
14831468
}
14841469
StatementKind::Retag(kind, _) => {
1485-
// FIXME(JakobDegen) The validator should check that `self.mir_phase <
1470+
// FIXME(JakobDegen) The validator should check that `self.body.phase <
14861471
// DropsLowered`. However, this causes ICEs with generation of drop shims, which
14871472
// seem to fail to set their `MirPhase` correctly.
14881473
if matches!(kind, RetagKind::TwoPhase) {

0 commit comments

Comments
 (0)