From a17e4264f9ee0dd3df546819e14707b6cfc9c9e6 Mon Sep 17 00:00:00 2001 From: Michael Goulet Date: Wed, 24 Jul 2024 15:58:34 -0400 Subject: [PATCH] Stop using MoveDataParamEnv for places that don't need a param-env --- compiler/rustc_borrowck/src/lib.rs | 15 ++--- .../src/drop_flag_effects.rs | 7 +-- .../src/impls/initialized.rs | 59 ++++++++----------- compiler/rustc_mir_dataflow/src/rustc_peek.rs | 8 +-- .../src/elaborate_drops.rs | 16 +++-- .../src/remove_uninit_drops.rs | 9 ++- 6 files changed, 50 insertions(+), 64 deletions(-) diff --git a/compiler/rustc_borrowck/src/lib.rs b/compiler/rustc_borrowck/src/lib.rs index 9ad941dabbe65..7c51be57b35bf 100644 --- a/compiler/rustc_borrowck/src/lib.rs +++ b/compiler/rustc_borrowck/src/lib.rs @@ -48,7 +48,6 @@ use rustc_mir_dataflow::impls::{ use rustc_mir_dataflow::move_paths::{InitIndex, MoveOutIndex, MovePathIndex}; use rustc_mir_dataflow::move_paths::{InitLocation, LookupResult, MoveData}; use rustc_mir_dataflow::Analysis; -use rustc_mir_dataflow::MoveDataParamEnv; use crate::session_diagnostics::VarNeedNotMut; @@ -196,9 +195,7 @@ fn do_mir_borrowck<'tcx>( .iter_enumerated() .map(|(idx, body)| (idx, MoveData::gather_moves(body, tcx, param_env, |_| true))); - let mdpe = MoveDataParamEnv { move_data, param_env }; - - let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) + let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data) .into_engine(tcx, body) .pass_name("borrowck") .iterate_to_fixpoint() @@ -206,7 +203,7 @@ fn do_mir_borrowck<'tcx>( let locals_are_invalidated_at_exit = tcx.hir().body_owner_kind(def).is_fn_or_closure(); let borrow_set = - Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &mdpe.move_data)); + Rc::new(BorrowSet::build(tcx, body, locals_are_invalidated_at_exit, &move_data)); // Compute non-lexical lifetimes. let nll::NllOutput { @@ -224,7 +221,7 @@ fn do_mir_borrowck<'tcx>( &location_table, param_env, &mut flow_inits, - &mdpe.move_data, + &move_data, &borrow_set, tcx.closure_captures(def), consumer_options, @@ -256,11 +253,11 @@ fn do_mir_borrowck<'tcx>( .into_engine(tcx, body) .pass_name("borrowck") .iterate_to_fixpoint(); - let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &mdpe) + let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data) .into_engine(tcx, body) .pass_name("borrowck") .iterate_to_fixpoint(); - let flow_ever_inits = EverInitializedPlaces::new(body, &mdpe) + let flow_ever_inits = EverInitializedPlaces::new(body, &move_data) .into_engine(tcx, body) .pass_name("borrowck") .iterate_to_fixpoint(); @@ -326,7 +323,7 @@ fn do_mir_borrowck<'tcx>( infcx: &infcx, param_env, body, - move_data: &mdpe.move_data, + move_data: &move_data, location_table: &location_table, movable_coroutine, locals_are_invalidated_at_exit, diff --git a/compiler/rustc_mir_dataflow/src/drop_flag_effects.rs b/compiler/rustc_mir_dataflow/src/drop_flag_effects.rs index 82c59d7d9595c..41ca7c7499293 100644 --- a/compiler/rustc_mir_dataflow/src/drop_flag_effects.rs +++ b/compiler/rustc_mir_dataflow/src/drop_flag_effects.rs @@ -4,7 +4,6 @@ use rustc_target::abi::VariantIdx; use tracing::debug; use super::move_paths::{InitKind, LookupResult, MoveData, MovePathIndex}; -use super::MoveDataParamEnv; pub fn move_path_children_matching<'tcx, F>( move_data: &MoveData<'tcx>, @@ -70,12 +69,11 @@ pub fn on_all_children_bits<'tcx, F>( pub fn drop_flag_effects_for_function_entry<'tcx, F>( body: &Body<'tcx>, - ctxt: &MoveDataParamEnv<'tcx>, + move_data: &MoveData<'tcx>, mut callback: F, ) where F: FnMut(MovePathIndex, DropFlagState), { - let move_data = &ctxt.move_data; for arg in body.args_iter() { let place = mir::Place::from(arg); let lookup_result = move_data.rev_lookup.find(place.as_ref()); @@ -87,13 +85,12 @@ pub fn drop_flag_effects_for_function_entry<'tcx, F>( pub fn drop_flag_effects_for_location<'tcx, F>( body: &Body<'tcx>, - ctxt: &MoveDataParamEnv<'tcx>, + move_data: &MoveData<'tcx>, loc: Location, mut callback: F, ) where F: FnMut(MovePathIndex, DropFlagState), { - let move_data = &ctxt.move_data; debug!("drop_flag_effects_for_location({:?})", loc); // first, move out of the RHS diff --git a/compiler/rustc_mir_dataflow/src/impls/initialized.rs b/compiler/rustc_mir_dataflow/src/impls/initialized.rs index d44da42416dbb..cca1fc945a94d 100644 --- a/compiler/rustc_mir_dataflow/src/impls/initialized.rs +++ b/compiler/rustc_mir_dataflow/src/impls/initialized.rs @@ -11,7 +11,6 @@ use crate::elaborate_drops::DropFlagState; use crate::framework::SwitchIntEdgeEffects; use crate::move_paths::{HasMoveData, InitIndex, InitKind, LookupResult, MoveData, MovePathIndex}; use crate::on_lookup_result_bits; -use crate::MoveDataParamEnv; use crate::{drop_flag_effects, on_all_children_bits}; use crate::{lattice, AnalysisDomain, GenKill, GenKillAnalysis, MaybeReachable}; @@ -53,17 +52,13 @@ use crate::{lattice, AnalysisDomain, GenKill, GenKillAnalysis, MaybeReachable}; pub struct MaybeInitializedPlaces<'a, 'mir, 'tcx> { tcx: TyCtxt<'tcx>, body: &'mir Body<'tcx>, - mdpe: &'a MoveDataParamEnv<'tcx>, + move_data: &'a MoveData<'tcx>, skip_unreachable_unwind: bool, } impl<'a, 'mir, 'tcx> MaybeInitializedPlaces<'a, 'mir, 'tcx> { - pub fn new( - tcx: TyCtxt<'tcx>, - body: &'mir Body<'tcx>, - mdpe: &'a MoveDataParamEnv<'tcx>, - ) -> Self { - MaybeInitializedPlaces { tcx, body, mdpe, skip_unreachable_unwind: false } + pub fn new(tcx: TyCtxt<'tcx>, body: &'mir Body<'tcx>, move_data: &'a MoveData<'tcx>) -> Self { + MaybeInitializedPlaces { tcx, body, move_data, skip_unreachable_unwind: false } } pub fn skipping_unreachable_unwind(mut self) -> Self { @@ -90,7 +85,7 @@ impl<'a, 'mir, 'tcx> MaybeInitializedPlaces<'a, 'mir, 'tcx> { impl<'a, 'mir, 'tcx> HasMoveData<'tcx> for MaybeInitializedPlaces<'a, 'mir, 'tcx> { fn move_data(&self) -> &MoveData<'tcx> { - &self.mdpe.move_data + self.move_data } } @@ -132,22 +127,18 @@ impl<'a, 'mir, 'tcx> HasMoveData<'tcx> for MaybeInitializedPlaces<'a, 'mir, 'tcx pub struct MaybeUninitializedPlaces<'a, 'mir, 'tcx> { tcx: TyCtxt<'tcx>, body: &'mir Body<'tcx>, - mdpe: &'a MoveDataParamEnv<'tcx>, + move_data: &'a MoveData<'tcx>, mark_inactive_variants_as_uninit: bool, skip_unreachable_unwind: BitSet, } impl<'a, 'mir, 'tcx> MaybeUninitializedPlaces<'a, 'mir, 'tcx> { - pub fn new( - tcx: TyCtxt<'tcx>, - body: &'mir Body<'tcx>, - mdpe: &'a MoveDataParamEnv<'tcx>, - ) -> Self { + pub fn new(tcx: TyCtxt<'tcx>, body: &'mir Body<'tcx>, move_data: &'a MoveData<'tcx>) -> Self { MaybeUninitializedPlaces { tcx, body, - mdpe, + move_data, mark_inactive_variants_as_uninit: false, skip_unreachable_unwind: BitSet::new_empty(body.basic_blocks.len()), } @@ -174,7 +165,7 @@ impl<'a, 'mir, 'tcx> MaybeUninitializedPlaces<'a, 'mir, 'tcx> { impl<'a, 'tcx> HasMoveData<'tcx> for MaybeUninitializedPlaces<'a, '_, 'tcx> { fn move_data(&self) -> &MoveData<'tcx> { - &self.mdpe.move_data + self.move_data } } @@ -214,18 +205,18 @@ impl<'a, 'tcx> HasMoveData<'tcx> for MaybeUninitializedPlaces<'a, '_, 'tcx> { /// that would require a dynamic drop-flag at that statement. pub struct DefinitelyInitializedPlaces<'a, 'tcx> { body: &'a Body<'tcx>, - mdpe: &'a MoveDataParamEnv<'tcx>, + move_data: &'a MoveData<'tcx>, } impl<'a, 'tcx> DefinitelyInitializedPlaces<'a, 'tcx> { - pub fn new(body: &'a Body<'tcx>, mdpe: &'a MoveDataParamEnv<'tcx>) -> Self { - DefinitelyInitializedPlaces { body, mdpe } + pub fn new(body: &'a Body<'tcx>, move_data: &'a MoveData<'tcx>) -> Self { + DefinitelyInitializedPlaces { body, move_data } } } impl<'a, 'tcx> HasMoveData<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> { fn move_data(&self) -> &MoveData<'tcx> { - &self.mdpe.move_data + self.move_data } } @@ -260,18 +251,18 @@ impl<'a, 'tcx> HasMoveData<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> { /// ``` pub struct EverInitializedPlaces<'a, 'mir, 'tcx> { body: &'mir Body<'tcx>, - mdpe: &'a MoveDataParamEnv<'tcx>, + move_data: &'a MoveData<'tcx>, } impl<'a, 'mir, 'tcx> EverInitializedPlaces<'a, 'mir, 'tcx> { - pub fn new(body: &'mir Body<'tcx>, mdpe: &'a MoveDataParamEnv<'tcx>) -> Self { - EverInitializedPlaces { body, mdpe } + pub fn new(body: &'mir Body<'tcx>, move_data: &'a MoveData<'tcx>) -> Self { + EverInitializedPlaces { body, move_data } } } impl<'a, 'tcx> HasMoveData<'tcx> for EverInitializedPlaces<'a, '_, 'tcx> { fn move_data(&self) -> &MoveData<'tcx> { - &self.mdpe.move_data + self.move_data } } @@ -329,7 +320,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeInitializedPlaces<'_, '_, 'tcx> { fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) { *state = MaybeReachable::Reachable(ChunkedBitSet::new_empty(self.move_data().move_paths.len())); - drop_flag_effects_for_function_entry(self.body, self.mdpe, |path, s| { + drop_flag_effects_for_function_entry(self.body, self.move_data, |path, s| { assert!(s == DropFlagState::Present); state.gen_(path); }); @@ -349,7 +340,7 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeInitializedPlaces<'_, '_, 'tcx> { statement: &mir::Statement<'tcx>, location: Location, ) { - drop_flag_effects_for_location(self.body, self.mdpe, location, |path, s| { + drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| { Self::update_bits(trans, path, s) }); @@ -381,7 +372,7 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeInitializedPlaces<'_, '_, 'tcx> { { edges = TerminatorEdges::Single(target); } - drop_flag_effects_for_location(self.body, self.mdpe, location, |path, s| { + drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| { Self::update_bits(state, path, s) }); edges @@ -466,7 +457,7 @@ impl<'tcx> AnalysisDomain<'tcx> for MaybeUninitializedPlaces<'_, '_, 'tcx> { // set all bits to 1 (uninit) before gathering counter-evidence state.insert_all(); - drop_flag_effects_for_function_entry(self.body, self.mdpe, |path, s| { + drop_flag_effects_for_function_entry(self.body, self.move_data, |path, s| { assert!(s == DropFlagState::Present); state.remove(path); }); @@ -486,7 +477,7 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeUninitializedPlaces<'_, '_, 'tcx> { _statement: &mir::Statement<'tcx>, location: Location, ) { - drop_flag_effects_for_location(self.body, self.mdpe, location, |path, s| { + drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| { Self::update_bits(trans, path, s) }); @@ -500,7 +491,7 @@ impl<'tcx> GenKillAnalysis<'tcx> for MaybeUninitializedPlaces<'_, '_, 'tcx> { terminator: &'mir mir::Terminator<'tcx>, location: Location, ) -> TerminatorEdges<'mir, 'tcx> { - drop_flag_effects_for_location(self.body, self.mdpe, location, |path, s| { + drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| { Self::update_bits(trans, path, s) }); if self.skip_unreachable_unwind.contains(location.block) { @@ -593,7 +584,7 @@ impl<'a, 'tcx> AnalysisDomain<'tcx> for DefinitelyInitializedPlaces<'a, 'tcx> { fn initialize_start_block(&self, _: &mir::Body<'tcx>, state: &mut Self::Domain) { state.0.clear(); - drop_flag_effects_for_function_entry(self.body, self.mdpe, |path, s| { + drop_flag_effects_for_function_entry(self.body, self.move_data, |path, s| { assert!(s == DropFlagState::Present); state.0.insert(path); }); @@ -613,7 +604,7 @@ impl<'tcx> GenKillAnalysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> { _statement: &mir::Statement<'tcx>, location: Location, ) { - drop_flag_effects_for_location(self.body, self.mdpe, location, |path, s| { + drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| { Self::update_bits(trans, path, s) }) } @@ -624,7 +615,7 @@ impl<'tcx> GenKillAnalysis<'tcx> for DefinitelyInitializedPlaces<'_, 'tcx> { terminator: &'mir mir::Terminator<'tcx>, location: Location, ) -> TerminatorEdges<'mir, 'tcx> { - drop_flag_effects_for_location(self.body, self.mdpe, location, |path, s| { + drop_flag_effects_for_location(self.body, self.move_data, location, |path, s| { Self::update_bits(trans, path, s) }); terminator.edges() diff --git a/compiler/rustc_mir_dataflow/src/rustc_peek.rs b/compiler/rustc_mir_dataflow/src/rustc_peek.rs index 1de9055273b73..c254dcfe1e1b8 100644 --- a/compiler/rustc_mir_dataflow/src/rustc_peek.rs +++ b/compiler/rustc_mir_dataflow/src/rustc_peek.rs @@ -8,7 +8,6 @@ use crate::impls::{ }; use crate::move_paths::{HasMoveData, MoveData}; use crate::move_paths::{LookupResult, MovePathIndex}; -use crate::MoveDataParamEnv; use crate::{Analysis, JoinSemiLattice, ResultsCursor}; use rustc_ast::MetaItem; use rustc_hir::def_id::DefId; @@ -48,10 +47,9 @@ impl<'tcx> MirPass<'tcx> for SanityCheck { let param_env = tcx.param_env(def_id); let move_data = MoveData::gather_moves(body, tcx, param_env, |_| true); - let mdpe = MoveDataParamEnv { move_data, param_env }; if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_init).is_some() { - let flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) + let flow_inits = MaybeInitializedPlaces::new(tcx, body, &move_data) .into_engine(tcx, body) .iterate_to_fixpoint(); @@ -59,7 +57,7 @@ impl<'tcx> MirPass<'tcx> for SanityCheck { } if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_maybe_uninit).is_some() { - let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &mdpe) + let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &move_data) .into_engine(tcx, body) .iterate_to_fixpoint(); @@ -67,7 +65,7 @@ impl<'tcx> MirPass<'tcx> for SanityCheck { } if has_rustc_mir_with(tcx, def_id, sym::rustc_peek_definite_init).is_some() { - let flow_def_inits = DefinitelyInitializedPlaces::new(body, &mdpe) + let flow_def_inits = DefinitelyInitializedPlaces::new(body, &move_data) .into_engine(tcx, body) .iterate_to_fixpoint(); diff --git a/compiler/rustc_mir_transform/src/elaborate_drops.rs b/compiler/rustc_mir_transform/src/elaborate_drops.rs index 25bebb0539a4f..07fd0822a0738 100644 --- a/compiler/rustc_mir_transform/src/elaborate_drops.rs +++ b/compiler/rustc_mir_transform/src/elaborate_drops.rs @@ -60,7 +60,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { let elaborate_patch = { let env = MoveDataParamEnv { move_data, param_env }; - let mut inits = MaybeInitializedPlaces::new(tcx, body, &env) + let mut inits = MaybeInitializedPlaces::new(tcx, body, &env.move_data) .skipping_unreachable_unwind() .into_engine(tcx, body) .pass_name("elaborate_drops") @@ -68,7 +68,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { .into_results_cursor(body); let dead_unwinds = compute_dead_unwinds(body, &mut inits); - let uninits = MaybeUninitializedPlaces::new(tcx, body, &env) + let uninits = MaybeUninitializedPlaces::new(tcx, body, &env.move_data) .mark_inactive_variants_as_uninit() .skipping_unreachable_unwind(dead_unwinds) .into_engine(tcx, body) @@ -441,9 +441,13 @@ impl<'b, 'mir, 'tcx> ElaborateDropsCtxt<'b, 'mir, 'tcx> { fn drop_flags_for_args(&mut self) { let loc = Location::START; - rustc_mir_dataflow::drop_flag_effects_for_function_entry(self.body, self.env, |path, ds| { - self.set_drop_flag(loc, path, ds); - }) + rustc_mir_dataflow::drop_flag_effects_for_function_entry( + self.body, + &self.env.move_data, + |path, ds| { + self.set_drop_flag(loc, path, ds); + }, + ) } fn drop_flags_for_locs(&mut self) { @@ -476,7 +480,7 @@ impl<'b, 'mir, 'tcx> ElaborateDropsCtxt<'b, 'mir, 'tcx> { let loc = Location { block: bb, statement_index: i }; rustc_mir_dataflow::drop_flag_effects_for_location( self.body, - self.env, + &self.env.move_data, loc, |path, ds| self.set_drop_flag(loc, path, ds), ) diff --git a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs index 7d12bcf2fa153..4fac53d7cc987 100644 --- a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs +++ b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs @@ -4,7 +4,7 @@ use rustc_middle::ty::GenericArgsRef; use rustc_middle::ty::{self, ParamEnv, Ty, TyCtxt, VariantDef}; use rustc_mir_dataflow::impls::MaybeInitializedPlaces; use rustc_mir_dataflow::move_paths::{LookupResult, MoveData, MovePathIndex}; -use rustc_mir_dataflow::{move_path_children_matching, Analysis, MaybeReachable, MoveDataParamEnv}; +use rustc_mir_dataflow::{move_path_children_matching, Analysis, MaybeReachable}; use rustc_target::abi::FieldIdx; use crate::MirPass; @@ -25,8 +25,7 @@ impl<'tcx> MirPass<'tcx> for RemoveUninitDrops { let move_data = MoveData::gather_moves(body, tcx, param_env, |ty| ty.needs_drop(tcx, param_env)); - let mdpe = MoveDataParamEnv { move_data, param_env }; - let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) + let mut maybe_inits = MaybeInitializedPlaces::new(tcx, body, &move_data) .into_engine(tcx, body) .pass_name("remove_uninit_drops") .iterate_to_fixpoint() @@ -41,7 +40,7 @@ impl<'tcx> MirPass<'tcx> for RemoveUninitDrops { let MaybeReachable::Reachable(maybe_inits) = maybe_inits.get() else { continue }; // If there's no move path for the dropped place, it's probably a `Deref`. Let it alone. - let LookupResult::Exact(mpi) = mdpe.move_data.rev_lookup.find(place.as_ref()) else { + let LookupResult::Exact(mpi) = move_data.rev_lookup.find(place.as_ref()) else { continue; }; @@ -49,7 +48,7 @@ impl<'tcx> MirPass<'tcx> for RemoveUninitDrops { tcx, param_env, maybe_inits, - &mdpe.move_data, + &move_data, place.ty(body, tcx).ty, mpi, );