Skip to content

Commit 46c7236

Browse files
committed
Move drop elaboration infrastructure.
`rustc_mir_dataflow/src/elaborate_drops.rs` contains some infrastructure used by a few MIR passes: the `elaborate_drop` function, the `DropElaborator` trait, etc. `rustc_mir_transform/src/elaborate_drops.rs` (same file name, different crate) contains the `ElaborateDrops` pass. It relies on a lot of the infrastructure from `rustc_mir_dataflow/src/elaborate_drops.rs`. It turns out that the drop infrastructure is only used in `rustc_mir_transform`, so this commit moves it there. (The only exception is the small `DropFlagState` type, which is moved to the existing `rustc_mir_dataflow/src/drop_flag_effects.rs`.) The file is renamed from `rustc_mir_dataflow/src/elaborate_drops.rs` to `rustc_mir_transform/src/elaborate_drop.rs` (with no trailing `s`) because (a) the `elaborate_drop` function is the most important export, and (b) `rustc_mir_transform/src/elaborate_drops.rs` already exists. All the infrastructure pieces that used to be `pub` are now `pub(crate)`, because they are now only used within `rustc_mir_transform`.
1 parent a567209 commit 46c7236

File tree

8 files changed

+35
-37
lines changed

8 files changed

+35
-37
lines changed

Diff for: compiler/rustc_mir_dataflow/src/drop_flag_effects.rs

+20-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,26 @@ use rustc_middle::mir::{self, Body, Location, Terminator, TerminatorKind};
33
use tracing::debug;
44

55
use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex};
6-
use crate::elaborate_drops::DropFlagState;
6+
7+
/// The value of an inserted drop flag.
8+
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
9+
pub enum DropFlagState {
10+
/// The tracked value is initialized and needs to be dropped when leaving its scope.
11+
Present,
12+
13+
/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
14+
/// leaving its scope.
15+
Absent,
16+
}
17+
18+
impl DropFlagState {
19+
pub fn value(self) -> bool {
20+
match self {
21+
DropFlagState::Present => true,
22+
DropFlagState::Absent => false,
23+
}
24+
}
25+
}
726

827
pub fn move_path_children_matching<'tcx, F>(
928
move_data: &MoveData<'tcx>,

Diff for: compiler/rustc_mir_dataflow/src/impls/initialized.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use rustc_middle::ty::util::Discr;
99
use rustc_middle::ty::{self, TyCtxt};
1010
use tracing::{debug, instrument};
1111

12-
use crate::elaborate_drops::DropFlagState;
12+
use crate::drop_flag_effects::DropFlagState;
1313
use crate::framework::SwitchIntTarget;
1414
use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
1515
use crate::{

Diff for: compiler/rustc_mir_dataflow/src/lib.rs

+1-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty;
1515
// Please change the public `use` directives cautiously, as they might be used by external tools.
1616
// See issue #120130.
1717
pub use self::drop_flag_effects::{
18-
drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
18+
DropFlagState, drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
1919
move_path_children_matching, on_all_children_bits, on_lookup_result_bits,
2020
};
2121
pub use self::framework::{
@@ -26,7 +26,6 @@ use self::move_paths::MoveData;
2626

2727
pub mod debuginfo;
2828
mod drop_flag_effects;
29-
pub mod elaborate_drops;
3029
mod errors;
3130
mod framework;
3231
pub mod impls;

Diff for: compiler/rustc_mir_transform/src/coroutine.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -1062,8 +1062,8 @@ fn insert_switch<'tcx>(
10621062

10631063
fn elaborate_coroutine_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
10641064
use rustc_middle::mir::patch::MirPatch;
1065-
use rustc_mir_dataflow::elaborate_drops::{Unwind, elaborate_drop};
10661065

1066+
use crate::elaborate_drop::{Unwind, elaborate_drop};
10671067
use crate::shim::DropShimElaborator;
10681068

10691069
// Note that `elaborate_drops` only drops the upvars of a coroutine, and

Diff for: compiler/rustc_mir_dataflow/src/elaborate_drops.rs renamed to compiler/rustc_mir_transform/src/elaborate_drop.rs

+5-25
Original file line numberDiff line numberDiff line change
@@ -13,29 +13,9 @@ use rustc_span::DUMMY_SP;
1313
use rustc_span::source_map::Spanned;
1414
use tracing::{debug, instrument};
1515

16-
/// The value of an inserted drop flag.
17-
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
18-
pub enum DropFlagState {
19-
/// The tracked value is initialized and needs to be dropped when leaving its scope.
20-
Present,
21-
22-
/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
23-
/// leaving its scope.
24-
Absent,
25-
}
26-
27-
impl DropFlagState {
28-
pub fn value(self) -> bool {
29-
match self {
30-
DropFlagState::Present => true,
31-
DropFlagState::Absent => false,
32-
}
33-
}
34-
}
35-
3616
/// Describes how/if a value should be dropped.
3717
#[derive(Debug)]
38-
pub enum DropStyle {
18+
pub(crate) enum DropStyle {
3919
/// The value is already dead at the drop location, no drop will be executed.
4020
Dead,
4121

@@ -56,7 +36,7 @@ pub enum DropStyle {
5636

5737
/// Which drop flags to affect/check with an operation.
5838
#[derive(Debug)]
59-
pub enum DropFlagMode {
39+
pub(crate) enum DropFlagMode {
6040
/// Only affect the top-level drop flag, not that of any contained fields.
6141
Shallow,
6242
/// Affect all nested drop flags in addition to the top-level one.
@@ -65,7 +45,7 @@ pub enum DropFlagMode {
6545

6646
/// Describes if unwinding is necessary and where to unwind to if a panic occurs.
6747
#[derive(Copy, Clone, Debug)]
68-
pub enum Unwind {
48+
pub(crate) enum Unwind {
6949
/// Unwind to this block.
7050
To(BasicBlock),
7151
/// Already in an unwind path, any panic will cause an abort.
@@ -98,7 +78,7 @@ impl Unwind {
9878
}
9979
}
10080

101-
pub trait DropElaborator<'a, 'tcx>: fmt::Debug {
81+
pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
10282
/// The type representing paths that can be moved out of.
10383
///
10484
/// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to
@@ -177,7 +157,7 @@ where
177157
/// value.
178158
///
179159
/// When this returns, the MIR patch in the `elaborator` contains the necessary changes.
180-
pub fn elaborate_drop<'b, 'tcx, D>(
160+
pub(crate) fn elaborate_drop<'b, 'tcx, D>(
181161
elaborator: &mut D,
182162
source_info: SourceInfo,
183163
place: Place<'tcx>,

Diff for: compiler/rustc_mir_transform/src/elaborate_drops.rs

+3-4
Original file line numberDiff line numberDiff line change
@@ -6,18 +6,17 @@ use rustc_index::bit_set::DenseBitSet;
66
use rustc_middle::mir::patch::MirPatch;
77
use rustc_middle::mir::*;
88
use rustc_middle::ty::{self, TyCtxt};
9-
use rustc_mir_dataflow::elaborate_drops::{
10-
DropElaborator, DropFlagMode, DropFlagState, DropStyle, Unwind, elaborate_drop,
11-
};
129
use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
1310
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
1411
use rustc_mir_dataflow::{
15-
Analysis, MoveDataTypingEnv, ResultsCursor, on_all_children_bits, on_lookup_result_bits,
12+
Analysis, DropFlagState, MoveDataTypingEnv, ResultsCursor, on_all_children_bits,
13+
on_lookup_result_bits,
1614
};
1715
use rustc_span::Span;
1816
use tracing::{debug, instrument};
1917

2018
use crate::deref_separator::deref_finder;
19+
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
2120

2221
/// During MIR building, Drop terminators are inserted in every place where a drop may occur.
2322
/// However, in this phase, the presence of these terminators does not guarantee that a destructor

Diff for: compiler/rustc_mir_transform/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ mod check_pointers;
4949
mod cost_checker;
5050
mod cross_crate_inline;
5151
mod deduce_param_attrs;
52+
mod elaborate_drop;
5253
mod errors;
5354
mod ffi_unwind_calls;
5455
mod lint;

Diff for: compiler/rustc_mir_transform/src/shim.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,11 @@ use rustc_middle::ty::{
1414
self, CoroutineArgs, CoroutineArgsExt, EarlyBinder, GenericArgs, Ty, TyCtxt,
1515
};
1616
use rustc_middle::{bug, span_bug};
17-
use rustc_mir_dataflow::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
1817
use rustc_span::source_map::Spanned;
1918
use rustc_span::{DUMMY_SP, Span};
2019
use tracing::{debug, instrument};
2120

21+
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
2222
use crate::{
2323
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator, inline,
2424
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify,
@@ -283,13 +283,13 @@ fn build_drop_shim<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, ty: Option<Ty<'tcx>>)
283283
DropShimElaborator { body: &body, patch: MirPatch::new(&body), tcx, typing_env };
284284
let dropee = tcx.mk_place_deref(dropee_ptr);
285285
let resume_block = elaborator.patch.resume_block();
286-
elaborate_drops::elaborate_drop(
286+
elaborate_drop(
287287
&mut elaborator,
288288
source_info,
289289
dropee,
290290
(),
291291
return_block,
292-
elaborate_drops::Unwind::To(resume_block),
292+
Unwind::To(resume_block),
293293
START_BLOCK,
294294
);
295295
elaborator.patch

0 commit comments

Comments
 (0)