Skip to content

Commit 2753fab

Browse files
committed
Auto merge of #73879 - ecstatic-morse:discr-switch-uninit, r=oli-obk
Handle inactive enum variants in `MaybeUninitializedPlaces` Resolves the first part of #69715. This is the equivalent of #68528 but for `MaybeUninitializedPlaces`. Because we now notify drop elaboration that inactive enum variants might be uninitialized, some drops get marked as ["open" that were previously "static"](https://github.com/rust-lang/rust/blob/e0e5d82e1677c82d209b214bbfc2cc5705c2336a/src/librustc_mir/transform/elaborate_drops.rs#L191). Unlike in #69715, this isn't strictly better: An "open" drop expands to more MIR than a simple call to the drop shim. However, because drop elaboration considers each field of an "open" drop separately, it can sometimes eliminate unnecessary drops of moved-from or unit-like enum variants. This is the case for `Option::unwrap`, which is reflected in the `mir-opt` test. cc @eddyb r? @oli-obk
2 parents 0cd7ff7 + eb4d28b commit 2753fab

File tree

6 files changed

+133
-39
lines changed

6 files changed

+133
-39
lines changed

src/librustc_mir/dataflow/drop_flag_effects.rs

+40
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
use crate::util::elaborate_drops::DropFlagState;
22
use rustc_middle::mir::{self, Body, Location};
33
use rustc_middle::ty::{self, TyCtxt};
4+
use rustc_target::abi::VariantIdx;
45

56
use super::indexes::MovePathIndex;
67
use super::move_paths::{InitKind, LookupResult, MoveData};
@@ -228,3 +229,42 @@ pub(crate) fn for_location_inits<'tcx, F>(
228229
}
229230
}
230231
}
232+
233+
/// Calls `handle_inactive_variant` for each descendant move path of `enum_place` that contains a
234+
/// `Downcast` to a variant besides the `active_variant`.
235+
///
236+
/// NOTE: If there are no move paths corresponding to an inactive variant,
237+
/// `handle_inactive_variant` will not be called for that variant.
238+
pub(crate) fn on_all_inactive_variants<'tcx>(
239+
tcx: TyCtxt<'tcx>,
240+
body: &mir::Body<'tcx>,
241+
move_data: &MoveData<'tcx>,
242+
enum_place: mir::Place<'tcx>,
243+
active_variant: VariantIdx,
244+
mut handle_inactive_variant: impl FnMut(MovePathIndex),
245+
) {
246+
let enum_mpi = match move_data.rev_lookup.find(enum_place.as_ref()) {
247+
LookupResult::Exact(mpi) => mpi,
248+
LookupResult::Parent(_) => return,
249+
};
250+
251+
let enum_path = &move_data.move_paths[enum_mpi];
252+
for (variant_mpi, variant_path) in enum_path.children(&move_data.move_paths) {
253+
// Because of the way we build the `MoveData` tree, each child should have exactly one more
254+
// projection than `enum_place`. This additional projection must be a downcast since the
255+
// base is an enum.
256+
let (downcast, base_proj) = variant_path.place.projection.split_last().unwrap();
257+
assert_eq!(enum_place.projection.len(), base_proj.len());
258+
259+
let variant_idx = match *downcast {
260+
mir::ProjectionElem::Downcast(_, idx) => idx,
261+
_ => unreachable!(),
262+
};
263+
264+
if variant_idx != active_variant {
265+
on_all_children_bits(tcx, body, move_data, variant_mpi, |mpi| {
266+
handle_inactive_variant(mpi)
267+
});
268+
}
269+
}
270+
}

src/librustc_mir/dataflow/impls/mod.rs

+48-23
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ use super::MoveDataParamEnv;
1212

1313
use crate::util::elaborate_drops::DropFlagState;
1414

15-
use super::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
15+
use super::move_paths::{HasMoveData, InitIndex, InitKind, MoveData, MovePathIndex};
1616
use super::{AnalysisDomain, BottomValue, GenKill, GenKillAnalysis};
1717

1818
use super::drop_flag_effects_for_function_entry;
@@ -124,11 +124,23 @@ pub struct MaybeUninitializedPlaces<'a, 'tcx> {
124124
tcx: TyCtxt<'tcx>,
125125
body: &'a Body<'tcx>,
126126
mdpe: &'a MoveDataParamEnv<'tcx>,
127+
128+
mark_inactive_variants_as_uninit: bool,
127129
}
128130

129131
impl<'a, 'tcx> MaybeUninitializedPlaces<'a, 'tcx> {
130132
pub fn new(tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, mdpe: &'a MoveDataParamEnv<'tcx>) -> Self {
131-
MaybeUninitializedPlaces { tcx, body, mdpe }
133+
MaybeUninitializedPlaces { tcx, body, mdpe, mark_inactive_variants_as_uninit: false }
134+
}
135+
136+
/// Causes inactive enum variants to be marked as "maybe uninitialized" after a switch on an
137+
/// enum discriminant.
138+
///
139+
/// This is correct in a vacuum but is not the default because it causes problems in the borrow
140+
/// checker, where this information gets propagated along `FakeEdge`s.
141+
pub fn mark_inactive_variants_as_uninit(mut self) -> Self {
142+
self.mark_inactive_variants_as_uninit = true;
143+
self
132144
}
133145
}
134146

@@ -350,27 +362,16 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeInitializedPlaces<'_, 'tcx> {
350362
_adt: &ty::AdtDef,
351363
variant: VariantIdx,
352364
) {
353-
let enum_mpi = match self.move_data().rev_lookup.find(enum_place.as_ref()) {
354-
LookupResult::Exact(mpi) => mpi,
355-
LookupResult::Parent(_) => return,
356-
};
357-
358-
// Kill all move paths that correspond to variants other than this one
359-
let move_paths = &self.move_data().move_paths;
360-
let enum_path = &move_paths[enum_mpi];
361-
for (mpi, variant_path) in enum_path.children(move_paths) {
362-
trans.kill(mpi);
363-
match variant_path.place.projection.last().unwrap() {
364-
mir::ProjectionElem::Downcast(_, idx) if *idx == variant => continue,
365-
_ => drop_flag_effects::on_all_children_bits(
366-
self.tcx,
367-
self.body,
368-
self.move_data(),
369-
mpi,
370-
|mpi| trans.kill(mpi),
371-
),
372-
}
373-
}
365+
// Kill all move paths that correspond to variants we know to be inactive along this
366+
// particular outgoing edge of a `SwitchInt`.
367+
drop_flag_effects::on_all_inactive_variants(
368+
self.tcx,
369+
self.body,
370+
self.move_data(),
371+
enum_place,
372+
variant,
373+
|mpi| trans.kill(mpi),
374+
);
374375
}
375376
}
376377

@@ -443,6 +444,30 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeUninitializedPlaces<'_, 'tcx> {
443444
},
444445
);
445446
}
447+
448+
fn discriminant_switch_effect(
449+
&self,
450+
trans: &mut impl GenKill<Self::Idx>,
451+
_block: mir::BasicBlock,
452+
enum_place: mir::Place<'tcx>,
453+
_adt: &ty::AdtDef,
454+
variant: VariantIdx,
455+
) {
456+
if !self.mark_inactive_variants_as_uninit {
457+
return;
458+
}
459+
460+
// Mark all move paths that correspond to variants other than this one as maybe
461+
// uninitialized (in reality, they are *definitely* uninitialized).
462+
drop_flag_effects::on_all_inactive_variants(
463+
self.tcx,
464+
self.body,
465+
self.move_data(),
466+
enum_place,
467+
variant,
468+
|mpi| trans.gen(mpi),
469+
);
470+
}
446471
}
447472

448473
impl<'a, 'tcx> AnalysisDomain<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> {

src/librustc_mir/transform/elaborate_drops.rs

+1
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops {
4848
.into_results_cursor(body);
4949

5050
let uninits = MaybeUninitializedPlaces::new(tcx, body, &env)
51+
.mark_inactive_variants_as_uninit()
5152
.into_engine(tcx, body, def_id)
5253
.dead_unwinds(&dead_unwinds)
5354
.iterate_to_fixpoint()

src/test/mir-opt/no-drop-for-inactive-variant/rustc.unwrap.SimplifyCfg-elaborate-drops.after.mir

+10-12
Original file line numberDiff line numberDiff line change
@@ -8,22 +8,19 @@ fn unwrap(_1: std::option::Option<T>) -> T {
88
let mut _4: !; // in scope 0 at $SRC_DIR/libstd/macros.rs:LL:COL
99
let mut _5: isize; // in scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
1010
let mut _6: isize; // in scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
11+
let mut _7: isize; // in scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
1112
scope 1 {
1213
debug x => _3; // in scope 1 at $DIR/no-drop-for-inactive-variant.rs:9:14: 9:15
1314
}
1415

1516
bb0: {
1617
_2 = discriminant(_1); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:9:9: 9:16
17-
switchInt(move _2) -> [0_isize: bb2, 1_isize: bb4, otherwise: bb3]; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:9:9: 9:16
18+
switchInt(move _2) -> [0_isize: bb1, 1_isize: bb3, otherwise: bb2]; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:9:9: 9:16
1819
}
1920

20-
bb1 (cleanup): {
21-
resume; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:7:1: 12:2
22-
}
23-
24-
bb2: {
21+
bb1: {
2522
StorageLive(_4); // scope 0 at $SRC_DIR/libstd/macros.rs:LL:COL
26-
const std::rt::begin_panic::<&str>(const "explicit panic") -> bb5; // scope 0 at $SRC_DIR/libstd/macros.rs:LL:COL
23+
const std::rt::begin_panic::<&str>(const "explicit panic") -> bb4; // scope 0 at $SRC_DIR/libstd/macros.rs:LL:COL
2724
// ty::Const
2825
// + ty: fn(&str) -> ! {std::rt::begin_panic::<&str>}
2926
// + val: Value(Scalar(<ZST>))
@@ -38,20 +35,21 @@ fn unwrap(_1: std::option::Option<T>) -> T {
3835
// + literal: Const { ty: &str, val: Value(Slice { data: Allocation { bytes: [101, 120, 112, 108, 105, 99, 105, 116, 32, 112, 97, 110, 105, 99], relocations: Relocations(SortedMap { data: [] }), init_mask: InitMask { blocks: [16383], len: Size { raw: 14 } }, size: Size { raw: 14 }, align: Align { pow2: 0 }, mutability: Not, extra: () }, start: 0, end: 14 }) }
3936
}
4037

41-
bb3: {
38+
bb2: {
4239
unreachable; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:8:11: 8:14
4340
}
4441

45-
bb4: {
42+
bb3: {
4643
StorageLive(_3); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:9:14: 9:15
4744
_3 = move ((_1 as Some).0: T); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:9:14: 9:15
4845
_0 = move _3; // scope 1 at $DIR/no-drop-for-inactive-variant.rs:9:20: 9:21
4946
StorageDead(_3); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:9:21: 9:22
50-
_5 = discriminant(_1); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
47+
_6 = discriminant(_1); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
5148
return; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:2: 12:2
5249
}
5350

54-
bb5 (cleanup): {
55-
drop(_1) -> bb1; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
51+
bb4 (cleanup): {
52+
_5 = discriminant(_1); // scope 0 at $DIR/no-drop-for-inactive-variant.rs:12:1: 12:2
53+
resume; // scope 0 at $DIR/no-drop-for-inactive-variant.rs:7:1: 12:2
5654
}
5755
}

src/test/mir-opt/simplify-locals-removes-unused-discriminant-reads/32bit/rustc.map.SimplifyLocals.diff

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@
77
let mut _2: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
88
- let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
99
- let mut _4: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:25: 4:26
10-
- let mut _5: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
10+
- let mut _5: bool; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
1111
- let mut _6: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
12+
- let mut _7: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
1213
scope 1 {
1314
debug x => ((_0 as Some).0: std::boxed::Box<()>); // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
1415
}
1516

1617
bb0: {
18+
- _5 = const false; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
19+
- // ty::Const
20+
- // + ty: bool
21+
- // + val: Value(Scalar(0x00))
22+
- // mir::Constant
23+
- // + span: $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
24+
- // + literal: Const { ty: bool, val: Value(Scalar(0x00)) }
25+
- _5 = const true; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
26+
- // ty::Const
27+
- // + ty: bool
28+
- // + val: Value(Scalar(0x01))
29+
- // mir::Constant
30+
- // + span: $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
31+
- // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
1732
_2 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
1833
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
1934
}
@@ -35,7 +50,7 @@
3550
}
3651

3752
bb3: {
38-
- _5 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
53+
- _6 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
3954
return; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:2: 6:2
4055
}
4156
}

src/test/mir-opt/simplify-locals-removes-unused-discriminant-reads/64bit/rustc.map.SimplifyLocals.diff

+17-2
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,28 @@
77
let mut _2: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
88
- let _3: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
99
- let mut _4: std::boxed::Box<()>; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:25: 4:26
10-
- let mut _5: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
10+
- let mut _5: bool; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
1111
- let mut _6: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
12+
- let mut _7: isize; // in scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
1213
scope 1 {
1314
debug x => ((_0 as Some).0: std::boxed::Box<()>); // in scope 1 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:4:14: 4:15
1415
}
1516

1617
bb0: {
18+
- _5 = const false; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
19+
- // ty::Const
20+
- // + ty: bool
21+
- // + val: Value(Scalar(0x00))
22+
- // mir::Constant
23+
- // + span: $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
24+
- // + literal: Const { ty: bool, val: Value(Scalar(0x00)) }
25+
- _5 = const true; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
26+
- // ty::Const
27+
- // + ty: bool
28+
- // + val: Value(Scalar(0x01))
29+
- // mir::Constant
30+
- // + span: $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
31+
- // + literal: Const { ty: bool, val: Value(Scalar(0x01)) }
1732
_2 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
1833
switchInt(move _2) -> [0_isize: bb2, otherwise: bb1]; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:3:9: 3:13
1934
}
@@ -35,7 +50,7 @@
3550
}
3651

3752
bb3: {
38-
- _5 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
53+
- _6 = discriminant(_1); // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:1: 6:2
3954
return; // scope 0 at $DIR/simplify-locals-removes-unused-discriminant-reads.rs:6:2: 6:2
4055
}
4156
}

0 commit comments

Comments
 (0)