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`.
This commit is contained in:
Nicholas Nethercote 2025-02-14 15:30:51 +11:00
parent a567209daa
commit 46c72362bc
8 changed files with 35 additions and 37 deletions

View file

@ -3,7 +3,26 @@ use rustc_middle::mir::{self, Body, Location, Terminator, TerminatorKind};
use tracing::debug; use tracing::debug;
use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex}; use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex};
use crate::elaborate_drops::DropFlagState;
/// The value of an inserted drop flag.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum DropFlagState {
/// The tracked value is initialized and needs to be dropped when leaving its scope.
Present,
/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
/// leaving its scope.
Absent,
}
impl DropFlagState {
pub fn value(self) -> bool {
match self {
DropFlagState::Present => true,
DropFlagState::Absent => false,
}
}
}
pub fn move_path_children_matching<'tcx, F>( pub fn move_path_children_matching<'tcx, F>(
move_data: &MoveData<'tcx>, move_data: &MoveData<'tcx>,

View file

@ -9,7 +9,7 @@ use rustc_middle::ty::util::Discr;
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use tracing::{debug, instrument}; use tracing::{debug, instrument};
use crate::elaborate_drops::DropFlagState; use crate::drop_flag_effects::DropFlagState;
use crate::framework::SwitchIntTarget; use crate::framework::SwitchIntTarget;
use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex}; use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex};
use crate::{ use crate::{

View file

@ -15,7 +15,7 @@ use rustc_middle::ty;
// Please change the public `use` directives cautiously, as they might be used by external tools. // Please change the public `use` directives cautiously, as they might be used by external tools.
// See issue #120130. // See issue #120130.
pub use self::drop_flag_effects::{ pub use self::drop_flag_effects::{
drop_flag_effects_for_function_entry, drop_flag_effects_for_location, DropFlagState, drop_flag_effects_for_function_entry, drop_flag_effects_for_location,
move_path_children_matching, on_all_children_bits, on_lookup_result_bits, move_path_children_matching, on_all_children_bits, on_lookup_result_bits,
}; };
pub use self::framework::{ pub use self::framework::{
@ -26,7 +26,6 @@ use self::move_paths::MoveData;
pub mod debuginfo; pub mod debuginfo;
mod drop_flag_effects; mod drop_flag_effects;
pub mod elaborate_drops;
mod errors; mod errors;
mod framework; mod framework;
pub mod impls; pub mod impls;

View file

@ -1062,8 +1062,8 @@ fn insert_switch<'tcx>(
fn elaborate_coroutine_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { fn elaborate_coroutine_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) {
use rustc_middle::mir::patch::MirPatch; use rustc_middle::mir::patch::MirPatch;
use rustc_mir_dataflow::elaborate_drops::{Unwind, elaborate_drop};
use crate::elaborate_drop::{Unwind, elaborate_drop};
use crate::shim::DropShimElaborator; use crate::shim::DropShimElaborator;
// Note that `elaborate_drops` only drops the upvars of a coroutine, and // Note that `elaborate_drops` only drops the upvars of a coroutine, and

View file

@ -13,29 +13,9 @@ use rustc_span::DUMMY_SP;
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use tracing::{debug, instrument}; use tracing::{debug, instrument};
/// The value of an inserted drop flag.
#[derive(Debug, PartialEq, Eq, Copy, Clone)]
pub enum DropFlagState {
/// The tracked value is initialized and needs to be dropped when leaving its scope.
Present,
/// The tracked value is uninitialized or was moved out of and does not need to be dropped when
/// leaving its scope.
Absent,
}
impl DropFlagState {
pub fn value(self) -> bool {
match self {
DropFlagState::Present => true,
DropFlagState::Absent => false,
}
}
}
/// Describes how/if a value should be dropped. /// Describes how/if a value should be dropped.
#[derive(Debug)] #[derive(Debug)]
pub enum DropStyle { pub(crate) enum DropStyle {
/// The value is already dead at the drop location, no drop will be executed. /// The value is already dead at the drop location, no drop will be executed.
Dead, Dead,
@ -56,7 +36,7 @@ pub enum DropStyle {
/// Which drop flags to affect/check with an operation. /// Which drop flags to affect/check with an operation.
#[derive(Debug)] #[derive(Debug)]
pub enum DropFlagMode { pub(crate) enum DropFlagMode {
/// Only affect the top-level drop flag, not that of any contained fields. /// Only affect the top-level drop flag, not that of any contained fields.
Shallow, Shallow,
/// Affect all nested drop flags in addition to the top-level one. /// Affect all nested drop flags in addition to the top-level one.
@ -65,7 +45,7 @@ pub enum DropFlagMode {
/// Describes if unwinding is necessary and where to unwind to if a panic occurs. /// Describes if unwinding is necessary and where to unwind to if a panic occurs.
#[derive(Copy, Clone, Debug)] #[derive(Copy, Clone, Debug)]
pub enum Unwind { pub(crate) enum Unwind {
/// Unwind to this block. /// Unwind to this block.
To(BasicBlock), To(BasicBlock),
/// Already in an unwind path, any panic will cause an abort. /// Already in an unwind path, any panic will cause an abort.
@ -98,7 +78,7 @@ impl Unwind {
} }
} }
pub trait DropElaborator<'a, 'tcx>: fmt::Debug { pub(crate) trait DropElaborator<'a, 'tcx>: fmt::Debug {
/// The type representing paths that can be moved out of. /// The type representing paths that can be moved out of.
/// ///
/// Users can move out of individual fields of a struct, such as `a.b.c`. This type is used to /// 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
/// value. /// value.
/// ///
/// When this returns, the MIR patch in the `elaborator` contains the necessary changes. /// When this returns, the MIR patch in the `elaborator` contains the necessary changes.
pub fn elaborate_drop<'b, 'tcx, D>( pub(crate) fn elaborate_drop<'b, 'tcx, D>(
elaborator: &mut D, elaborator: &mut D,
source_info: SourceInfo, source_info: SourceInfo,
place: Place<'tcx>, place: Place<'tcx>,

View file

@ -6,18 +6,17 @@ use rustc_index::bit_set::DenseBitSet;
use rustc_middle::mir::patch::MirPatch; use rustc_middle::mir::patch::MirPatch;
use rustc_middle::mir::*; use rustc_middle::mir::*;
use rustc_middle::ty::{self, TyCtxt}; use rustc_middle::ty::{self, TyCtxt};
use rustc_mir_dataflow::elaborate_drops::{
DropElaborator, DropFlagMode, DropFlagState, DropStyle, Unwind, elaborate_drop,
};
use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces}; use rustc_mir_dataflow::impls::{MaybeInitializedPlaces, MaybeUninitializedPlaces};
use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex}; use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex};
use rustc_mir_dataflow::{ use rustc_mir_dataflow::{
Analysis, MoveDataTypingEnv, ResultsCursor, on_all_children_bits, on_lookup_result_bits, Analysis, DropFlagState, MoveDataTypingEnv, ResultsCursor, on_all_children_bits,
on_lookup_result_bits,
}; };
use rustc_span::Span; use rustc_span::Span;
use tracing::{debug, instrument}; use tracing::{debug, instrument};
use crate::deref_separator::deref_finder; use crate::deref_separator::deref_finder;
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
/// During MIR building, Drop terminators are inserted in every place where a drop may occur. /// During MIR building, Drop terminators are inserted in every place where a drop may occur.
/// However, in this phase, the presence of these terminators does not guarantee that a destructor /// However, in this phase, the presence of these terminators does not guarantee that a destructor

View file

@ -49,6 +49,7 @@ mod check_pointers;
mod cost_checker; mod cost_checker;
mod cross_crate_inline; mod cross_crate_inline;
mod deduce_param_attrs; mod deduce_param_attrs;
mod elaborate_drop;
mod errors; mod errors;
mod ffi_unwind_calls; mod ffi_unwind_calls;
mod lint; mod lint;

View file

@ -14,11 +14,11 @@ use rustc_middle::ty::{
self, CoroutineArgs, CoroutineArgsExt, EarlyBinder, GenericArgs, Ty, TyCtxt, self, CoroutineArgs, CoroutineArgsExt, EarlyBinder, GenericArgs, Ty, TyCtxt,
}; };
use rustc_middle::{bug, span_bug}; use rustc_middle::{bug, span_bug};
use rustc_mir_dataflow::elaborate_drops::{self, DropElaborator, DropFlagMode, DropStyle};
use rustc_span::source_map::Spanned; use rustc_span::source_map::Spanned;
use rustc_span::{DUMMY_SP, Span}; use rustc_span::{DUMMY_SP, Span};
use tracing::{debug, instrument}; use tracing::{debug, instrument};
use crate::elaborate_drop::{DropElaborator, DropFlagMode, DropStyle, Unwind, elaborate_drop};
use crate::{ use crate::{
abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator, inline, abort_unwinding_calls, add_call_guards, add_moves_for_packed_drops, deref_separator, inline,
instsimplify, mentioned_items, pass_manager as pm, remove_noop_landing_pads, simplify, 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>>)
DropShimElaborator { body: &body, patch: MirPatch::new(&body), tcx, typing_env }; DropShimElaborator { body: &body, patch: MirPatch::new(&body), tcx, typing_env };
let dropee = tcx.mk_place_deref(dropee_ptr); let dropee = tcx.mk_place_deref(dropee_ptr);
let resume_block = elaborator.patch.resume_block(); let resume_block = elaborator.patch.resume_block();
elaborate_drops::elaborate_drop( elaborate_drop(
&mut elaborator, &mut elaborator,
source_info, source_info,
dropee, dropee,
(), (),
return_block, return_block,
elaborate_drops::Unwind::To(resume_block), Unwind::To(resume_block),
START_BLOCK, START_BLOCK,
); );
elaborator.patch elaborator.patch