diff --git a/compiler/rustc_mir/src/borrow_check/mod.rs b/compiler/rustc_mir/src/borrow_check/mod.rs index 37e2730025d..ad874be6ee9 100644 --- a/compiler/rustc_mir/src/borrow_check/mod.rs +++ b/compiler/rustc_mir/src/borrow_check/mod.rs @@ -203,7 +203,7 @@ fn do_mir_borrowck<'a, 'tcx>( let mdpe = MoveDataParamEnv { move_data, param_env }; let mut flow_inits = MaybeInitializedPlaces::new(tcx, &body, &mdpe) - .into_engine(tcx, &body, def.did.to_def_id()) + .into_engine(tcx, &body) .pass_name("borrowck") .iterate_to_fixpoint() .into_results_cursor(&body); @@ -221,7 +221,6 @@ fn do_mir_borrowck<'a, 'tcx>( nll_errors, } = nll::compute_regions( infcx, - def.did, free_regions, body, &promoted, @@ -242,7 +241,6 @@ fn do_mir_borrowck<'a, 'tcx>( nll::dump_annotation( infcx, &body, - def.did.to_def_id(), ®ioncx, &opt_closure_req, &opaque_type_values, @@ -257,15 +255,15 @@ fn do_mir_borrowck<'a, 'tcx>( let regioncx = Rc::new(regioncx); let flow_borrows = Borrows::new(tcx, &body, regioncx.clone(), &borrow_set) - .into_engine(tcx, &body, def.did.to_def_id()) + .into_engine(tcx, &body) .pass_name("borrowck") .iterate_to_fixpoint(); let flow_uninits = MaybeUninitializedPlaces::new(tcx, &body, &mdpe) - .into_engine(tcx, &body, def.did.to_def_id()) + .into_engine(tcx, &body) .pass_name("borrowck") .iterate_to_fixpoint(); let flow_ever_inits = EverInitializedPlaces::new(tcx, &body, &mdpe) - .into_engine(tcx, &body, def.did.to_def_id()) + .into_engine(tcx, &body) .pass_name("borrowck") .iterate_to_fixpoint(); diff --git a/compiler/rustc_mir/src/borrow_check/nll.rs b/compiler/rustc_mir/src/borrow_check/nll.rs index 44125885f2a..70f068997b6 100644 --- a/compiler/rustc_mir/src/borrow_check/nll.rs +++ b/compiler/rustc_mir/src/borrow_check/nll.rs @@ -156,7 +156,6 @@ fn populate_polonius_move_facts( /// This may result in errors being reported. pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( infcx: &InferCtxt<'cx, 'tcx>, - def_id: LocalDefId, universal_regions: UniversalRegions<'tcx>, body: &Body<'tcx>, promoted: &IndexVec>, @@ -180,7 +179,6 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( param_env, body, promoted, - def_id, &universal_regions, location_table, borrow_set, @@ -270,10 +268,12 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( // Generate various additional constraints. invalidation::generate_invalidates(infcx.tcx, &mut all_facts, location_table, body, borrow_set); + let def_id = body.source.def_id(); + // Dump facts if requested. let polonius_output = all_facts.and_then(|all_facts| { if infcx.tcx.sess.opts.debugging_opts.nll_facts { - let def_path = infcx.tcx.def_path(def_id.to_def_id()); + let def_path = infcx.tcx.def_path(def_id); let dir_path = PathBuf::from("nll-facts").join(def_path.to_filename_friendly_no_crate()); all_facts.write_to_dir(dir_path, location_table).unwrap(); @@ -293,7 +293,7 @@ pub(in crate::borrow_check) fn compute_regions<'cx, 'tcx>( // Solve the region constraints. let (closure_region_requirements, nll_errors) = - regioncx.solve(infcx, &body, def_id.to_def_id(), polonius_output.clone()); + regioncx.solve(infcx, &body, def_id, polonius_output.clone()); if !nll_errors.is_empty() { // Suppress unhelpful extra errors in `infer_opaque_types`. @@ -364,14 +364,13 @@ pub(super) fn dump_mir_results<'a, 'tcx>( pub(super) fn dump_annotation<'a, 'tcx>( infcx: &InferCtxt<'a, 'tcx>, body: &Body<'tcx>, - mir_def_id: DefId, regioncx: &RegionInferenceContext<'tcx>, closure_region_requirements: &Option>, opaque_type_values: &FxHashMap>, errors_buffer: &mut Vec, ) { let tcx = infcx.tcx; - let base_def_id = tcx.closure_base_def_id(mir_def_id); + let base_def_id = tcx.closure_base_def_id(body.source.def_id()); if !tcx.has_attr(base_def_id, sym::rustc_regions) { return; } diff --git a/compiler/rustc_mir/src/borrow_check/type_check/input_output.rs b/compiler/rustc_mir/src/borrow_check/type_check/input_output.rs index 4846ef06a8b..3c8cbeeca38 100644 --- a/compiler/rustc_mir/src/borrow_check/type_check/input_output.rs +++ b/compiler/rustc_mir/src/borrow_check/type_check/input_output.rs @@ -28,42 +28,43 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let (&normalized_output_ty, normalized_input_tys) = normalized_inputs_and_output.split_last().unwrap(); + let mir_def_id = body.source.def_id().expect_local(); + // If the user explicitly annotated the input types, extract // those. // // e.g., `|x: FxHashMap<_, &'static u32>| ...` let user_provided_sig; - if !self.tcx().is_closure(self.mir_def_id.to_def_id()) { + if !self.tcx().is_closure(mir_def_id.to_def_id()) { user_provided_sig = None; } else { - let typeck_results = self.tcx().typeck(self.mir_def_id); - user_provided_sig = - match typeck_results.user_provided_sigs.get(&self.mir_def_id.to_def_id()) { - None => None, - Some(user_provided_poly_sig) => { - // Instantiate the canonicalized variables from - // user-provided signature (e.g., the `_` in the code - // above) with fresh variables. - let (poly_sig, _) = - self.infcx.instantiate_canonical_with_fresh_inference_vars( - body.span, - &user_provided_poly_sig, - ); + let typeck_results = self.tcx().typeck(mir_def_id); + user_provided_sig = match typeck_results.user_provided_sigs.get(&mir_def_id.to_def_id()) + { + None => None, + Some(user_provided_poly_sig) => { + // Instantiate the canonicalized variables from + // user-provided signature (e.g., the `_` in the code + // above) with fresh variables. + let (poly_sig, _) = self.infcx.instantiate_canonical_with_fresh_inference_vars( + body.span, + &user_provided_poly_sig, + ); - // Replace the bound items in the fn sig with fresh - // variables, so that they represent the view from - // "inside" the closure. - Some( - self.infcx - .replace_bound_vars_with_fresh_vars( - body.span, - LateBoundRegionConversionTime::FnCall, - &poly_sig, - ) - .0, - ) - } + // Replace the bound items in the fn sig with fresh + // variables, so that they represent the view from + // "inside" the closure. + Some( + self.infcx + .replace_bound_vars_with_fresh_vars( + body.span, + LateBoundRegionConversionTime::FnCall, + &poly_sig, + ) + .0, + ) } + } }; debug!( @@ -122,7 +123,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { if let Err(terr) = self.eq_opaque_type_and_type( mir_output_ty, normalized_output_ty, - self.mir_def_id, + mir_def_id, Locations::All(output_span), ConstraintCategory::BoringNoLocation, ) { @@ -145,7 +146,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { if let Err(err) = self.eq_opaque_type_and_type( mir_output_ty, user_provided_output_ty, - self.mir_def_id, + mir_def_id, Locations::All(output_span), ConstraintCategory::BoringNoLocation, ) { diff --git a/compiler/rustc_mir/src/borrow_check/type_check/mod.rs b/compiler/rustc_mir/src/borrow_check/type_check/mod.rs index 3ace14610e2..f8a8801595a 100644 --- a/compiler/rustc_mir/src/borrow_check/type_check/mod.rs +++ b/compiler/rustc_mir/src/borrow_check/type_check/mod.rs @@ -73,7 +73,7 @@ macro_rules! span_mirbug { $context.last_span, &format!( "broken MIR in {:?} ({:?}): {}", - $context.mir_def_id, + $context.body.source.def_id(), $elem, format_args!($($message)*), ), @@ -113,7 +113,6 @@ mod relate_tys; /// - `param_env` -- parameter environment to use for trait solving /// - `body` -- MIR body to type-check /// - `promoted` -- map of promoted constants within `body` -/// - `mir_def_id` -- `LocalDefId` from which the MIR is derived /// - `universal_regions` -- the universal regions from `body`s function signature /// - `location_table` -- MIR location map of `body` /// - `borrow_set` -- information about borrows occurring in `body` @@ -126,7 +125,6 @@ pub(crate) fn type_check<'mir, 'tcx>( param_env: ty::ParamEnv<'tcx>, body: &Body<'tcx>, promoted: &IndexVec>, - mir_def_id: LocalDefId, universal_regions: &Rc>, location_table: &LocationTable, borrow_set: &BorrowSet<'tcx>, @@ -170,7 +168,6 @@ pub(crate) fn type_check<'mir, 'tcx>( let opaque_type_values = type_check_internal( infcx, - mir_def_id, param_env, body, promoted, @@ -192,7 +189,6 @@ pub(crate) fn type_check<'mir, 'tcx>( fn type_check_internal<'a, 'tcx, R>( infcx: &'a InferCtxt<'a, 'tcx>, - mir_def_id: LocalDefId, param_env: ty::ParamEnv<'tcx>, body: &'a Body<'tcx>, promoted: &'a IndexVec>, @@ -205,7 +201,6 @@ fn type_check_internal<'a, 'tcx, R>( let mut checker = TypeChecker::new( infcx, body, - mir_def_id, param_env, region_bound_pairs, implicit_region_bound, @@ -272,7 +267,6 @@ struct TypeVerifier<'a, 'b, 'tcx> { body: &'b Body<'tcx>, promoted: &'b IndexVec>, last_span: Span, - mir_def_id: LocalDefId, errors_reported: bool, } @@ -460,14 +454,7 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> { body: &'b Body<'tcx>, promoted: &'b IndexVec>, ) -> Self { - TypeVerifier { - body, - promoted, - mir_def_id: cx.mir_def_id, - cx, - last_span: body.span, - errors_reported: false, - } + TypeVerifier { body, promoted, cx, last_span: body.span, errors_reported: false } } fn tcx(&self) -> TyCtxt<'tcx> { @@ -816,7 +803,6 @@ struct TypeChecker<'a, 'tcx> { /// User type annotations are shared between the main MIR and the MIR of /// all of the promoted items. user_type_annotations: &'a CanonicalUserTypeAnnotations<'tcx>, - mir_def_id: LocalDefId, region_bound_pairs: &'a RegionBoundPairs<'tcx>, implicit_region_bound: ty::Region<'tcx>, reported_errors: FxHashSet<(Ty<'tcx>, Span)>, @@ -965,7 +951,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { fn new( infcx: &'a InferCtxt<'a, 'tcx>, body: &'a Body<'tcx>, - mir_def_id: LocalDefId, param_env: ty::ParamEnv<'tcx>, region_bound_pairs: &'a RegionBoundPairs<'tcx>, implicit_region_bound: ty::Region<'tcx>, @@ -975,7 +960,6 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let mut checker = Self { infcx, last_span: DUMMY_SP, - mir_def_id, body, user_type_annotations: &body.user_type_annotations, param_env, @@ -1145,7 +1129,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { // the resulting inferend values are stored with the // def-id of the base function. let parent_def_id = - self.tcx().closure_base_def_id(self.mir_def_id.to_def_id()).expect_local(); + self.tcx().closure_base_def_id(self.body.source.def_id()).expect_local(); return self.eq_opaque_type_and_type(sub, sup, parent_def_id, locations, category); } else { return Err(terr); @@ -1242,7 +1226,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let concrete_opaque_types = &tcx.typeck(anon_owner_def_id).concrete_opaque_types; let mut opaque_type_values = Vec::new(); - debug!("eq_opaque_type_and_type: mir_def_id={:?}", self.mir_def_id); + debug!("eq_opaque_type_and_type: mir_def_id={:?}", body.source.def_id()); let opaque_type_map = self.fully_perform_op( locations, category, @@ -2001,12 +1985,7 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { let span = body.source_info(location).span; let ty = operand.ty(body, tcx); if !self.infcx.type_is_copy_modulo_regions(self.param_env, ty, span) { - let ccx = ConstCx::new_with_param_env( - tcx, - self.mir_def_id, - body, - self.param_env, - ); + let ccx = ConstCx::new_with_param_env(tcx, body, self.param_env); // To determine if `const_in_array_repeat_expressions` feature gate should // be mentioned, need to check if the rvalue is promotable. let should_suggest = @@ -2015,11 +1994,12 @@ impl<'a, 'tcx> TypeChecker<'a, 'tcx> { ); debug!("check_rvalue: should_suggest={:?}", should_suggest); + let def_id = body.source.def_id().expect_local(); self.infcx.report_selection_error( &traits::Obligation::new( ObligationCause::new( span, - self.tcx().hir().local_def_id_to_hir_id(self.mir_def_id), + self.tcx().hir().local_def_id_to_hir_id(def_id), traits::ObligationCauseCode::RepeatVec(should_suggest), ), self.param_env, diff --git a/compiler/rustc_mir/src/dataflow/framework/engine.rs b/compiler/rustc_mir/src/dataflow/framework/engine.rs index f39c78f503d..b836e85c3a7 100644 --- a/compiler/rustc_mir/src/dataflow/framework/engine.rs +++ b/compiler/rustc_mir/src/dataflow/framework/engine.rs @@ -81,7 +81,6 @@ where { tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, - def_id: DefId, dead_unwinds: Option<&'a BitSet>, entry_sets: IndexVec, pass_name: Option<&'static str>, @@ -103,18 +102,13 @@ where T: Idx, { /// Creates a new `Engine` to solve a gen-kill dataflow problem. - pub fn new_gen_kill( - tcx: TyCtxt<'tcx>, - body: &'a mir::Body<'tcx>, - def_id: DefId, - analysis: A, - ) -> Self { + pub fn new_gen_kill(tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, analysis: A) -> Self { // If there are no back-edges in the control-flow graph, we only ever need to apply the // transfer function for each block exactly once (assuming that we process blocks in RPO). // // In this case, there's no need to compute the block transfer functions ahead of time. if !body.is_cfg_cyclic() { - return Self::new(tcx, body, def_id, analysis, None); + return Self::new(tcx, body, analysis, None); } // Otherwise, compute and store the cumulative transfer function for each block. @@ -131,7 +125,7 @@ where trans_for_block[bb].apply(state.borrow_mut()); }); - Self::new(tcx, body, def_id, analysis, Some(apply_trans as Box<_>)) + Self::new(tcx, body, analysis, Some(apply_trans as Box<_>)) } } @@ -145,19 +139,13 @@ where /// /// Gen-kill problems should use `new_gen_kill`, which will coalesce transfer functions for /// better performance. - pub fn new_generic( - tcx: TyCtxt<'tcx>, - body: &'a mir::Body<'tcx>, - def_id: DefId, - analysis: A, - ) -> Self { - Self::new(tcx, body, def_id, analysis, None) + pub fn new_generic(tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, analysis: A) -> Self { + Self::new(tcx, body, analysis, None) } fn new( tcx: TyCtxt<'tcx>, body: &'a mir::Body<'tcx>, - def_id: DefId, analysis: A, apply_trans_for_block: Option>, ) -> Self { @@ -173,7 +161,6 @@ where analysis, tcx, body, - def_id, dead_unwinds: None, pass_name: None, entry_sets, @@ -209,7 +196,6 @@ where analysis, body, dead_unwinds, - def_id, mut entry_sets, tcx, apply_trans_for_block, @@ -261,7 +247,7 @@ where let results = Results { analysis, entry_sets }; - let res = write_graphviz_results(tcx, def_id, &body, &results, pass_name); + let res = write_graphviz_results(tcx, &body, &results, pass_name); if let Err(e) = res { warn!("Failed to write graphviz dataflow results: {}", e); } @@ -276,7 +262,6 @@ where /// `rustc_mir` attributes. fn write_graphviz_results( tcx: TyCtxt<'tcx>, - def_id: DefId, body: &mir::Body<'tcx>, results: &Results<'tcx, A>, pass_name: Option<&'static str>, @@ -285,6 +270,7 @@ where A: Analysis<'tcx>, A::Domain: DebugWithContext, { + let def_id = body.source.def_id(); let attrs = match RustcMirAttrs::parse(tcx, def_id) { Ok(attrs) => attrs, @@ -323,7 +309,7 @@ where debug!("printing dataflow results for {:?} to {}", def_id, path.display()); let mut buf = Vec::new(); - let graphviz = graphviz::Formatter::new(body, def_id, results, style); + let graphviz = graphviz::Formatter::new(body, results, style); let mut render_opts = vec![dot::RenderOption::Fontname(tcx.sess.opts.debugging_opts.graphviz_font.clone())]; if tcx.sess.opts.debugging_opts.graphviz_dark_mode { diff --git a/compiler/rustc_mir/src/dataflow/framework/graphviz.rs b/compiler/rustc_mir/src/dataflow/framework/graphviz.rs index 5d4c4251961..4e54257a1cb 100644 --- a/compiler/rustc_mir/src/dataflow/framework/graphviz.rs +++ b/compiler/rustc_mir/src/dataflow/framework/graphviz.rs @@ -6,7 +6,6 @@ use std::{io, ops, str}; use regex::Regex; use rustc_graphviz as dot; -use rustc_hir::def_id::DefId; use rustc_middle::mir::{self, BasicBlock, Body, Location}; use super::fmt::{DebugDiffWithAdapter, DebugWithAdapter, DebugWithContext}; @@ -33,7 +32,6 @@ where A: Analysis<'tcx>, { body: &'a Body<'tcx>, - def_id: DefId, results: &'a Results<'tcx, A>, style: OutputStyle, } @@ -42,13 +40,8 @@ impl Formatter<'a, 'tcx, A> where A: Analysis<'tcx>, { - pub fn new( - body: &'a Body<'tcx>, - def_id: DefId, - results: &'a Results<'tcx, A>, - style: OutputStyle, - ) -> Self { - Formatter { body, def_id, results, style } + pub fn new(body: &'a Body<'tcx>, results: &'a Results<'tcx, A>, style: OutputStyle) -> Self { + Formatter { body, results, style } } } @@ -77,7 +70,7 @@ where type Edge = CfgEdge; fn graph_id(&self) -> dot::Id<'_> { - let name = graphviz_safe_def_name(self.def_id); + let name = graphviz_safe_def_name(self.body.source.def_id()); dot::Id::new(format!("graph_for_def_id_{}", name)).unwrap() } diff --git a/compiler/rustc_mir/src/dataflow/framework/mod.rs b/compiler/rustc_mir/src/dataflow/framework/mod.rs index 65c159e6a72..524ad0af1a7 100644 --- a/compiler/rustc_mir/src/dataflow/framework/mod.rs +++ b/compiler/rustc_mir/src/dataflow/framework/mod.rs @@ -13,9 +13,9 @@ //! ```ignore(cross-crate-imports) //! use rustc_mir::dataflow::Analysis; // Makes `into_engine` available. //! -//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>, did: DefId) { +//! fn do_my_analysis(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) { //! let analysis = MyAnalysis::new() -//! .into_engine(tcx, body, did) +//! .into_engine(tcx, body) //! .iterate_to_fixpoint() //! .into_results_cursor(body); //! @@ -33,7 +33,6 @@ use std::borrow::BorrowMut; use std::cmp::Ordering; -use rustc_hir::def_id::DefId; use rustc_index::bit_set::{BitSet, HybridBitSet}; use rustc_index::vec::Idx; use rustc_middle::mir::{self, BasicBlock, Location}; @@ -218,16 +217,11 @@ pub trait Analysis<'tcx>: AnalysisDomain<'tcx> { /// .iterate_to_fixpoint() /// .into_results_cursor(body); /// ``` - fn into_engine( - self, - tcx: TyCtxt<'tcx>, - body: &'mir mir::Body<'tcx>, - def_id: DefId, - ) -> Engine<'mir, 'tcx, Self> + fn into_engine(self, tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Engine<'mir, 'tcx, Self> where Self: Sized, { - Engine::new_generic(tcx, body, def_id, self) + Engine::new_generic(tcx, body, self) } } @@ -381,16 +375,11 @@ where /* Extension methods */ - fn into_engine( - self, - tcx: TyCtxt<'tcx>, - body: &'mir mir::Body<'tcx>, - def_id: DefId, - ) -> Engine<'mir, 'tcx, Self> + fn into_engine(self, tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Engine<'mir, 'tcx, Self> where Self: Sized, { - Engine::new_gen_kill(tcx, body, def_id, self) + Engine::new_gen_kill(tcx, body, self) } } diff --git a/compiler/rustc_mir/src/transform/add_moves_for_packed_drops.rs b/compiler/rustc_mir/src/transform/add_moves_for_packed_drops.rs index 977bc853100..417e0a51aec 100644 --- a/compiler/rustc_mir/src/transform/add_moves_for_packed_drops.rs +++ b/compiler/rustc_mir/src/transform/add_moves_for_packed_drops.rs @@ -1,4 +1,3 @@ -use rustc_hir::def_id::DefId; use rustc_middle::mir::*; use rustc_middle::ty::TyCtxt; @@ -42,20 +41,17 @@ pub struct AddMovesForPackedDrops; impl<'tcx> MirPass<'tcx> for AddMovesForPackedDrops { fn run_pass(&self, tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { debug!("add_moves_for_packed_drops({:?} @ {:?})", body.source, body.span); - add_moves_for_packed_drops(tcx, body, body.source.def_id()); + add_moves_for_packed_drops(tcx, body); } } -pub fn add_moves_for_packed_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>, def_id: DefId) { - let patch = add_moves_for_packed_drops_patch(tcx, body, def_id); +pub fn add_moves_for_packed_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { + let patch = add_moves_for_packed_drops_patch(tcx, body); patch.apply(body); } -fn add_moves_for_packed_drops_patch<'tcx>( - tcx: TyCtxt<'tcx>, - body: &Body<'tcx>, - def_id: DefId, -) -> MirPatch<'tcx> { +fn add_moves_for_packed_drops_patch<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) -> MirPatch<'tcx> { + let def_id = body.source.def_id(); let mut patch = MirPatch::new(body); let param_env = tcx.param_env(def_id); diff --git a/compiler/rustc_mir/src/transform/check_consts/mod.rs b/compiler/rustc_mir/src/transform/check_consts/mod.rs index 8df134860a5..33815ceba62 100644 --- a/compiler/rustc_mir/src/transform/check_consts/mod.rs +++ b/compiler/rustc_mir/src/transform/check_consts/mod.rs @@ -24,25 +24,28 @@ pub mod validation; pub struct ConstCx<'mir, 'tcx> { pub body: &'mir mir::Body<'tcx>, pub tcx: TyCtxt<'tcx>, - pub def_id: LocalDefId, pub param_env: ty::ParamEnv<'tcx>, pub const_kind: Option, } impl ConstCx<'mir, 'tcx> { - pub fn new(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &'mir mir::Body<'tcx>) -> Self { + pub fn new(tcx: TyCtxt<'tcx>, body: &'mir mir::Body<'tcx>) -> Self { + let def_id = body.source.def_id().expect_local(); let param_env = tcx.param_env(def_id); - Self::new_with_param_env(tcx, def_id, body, param_env) + Self::new_with_param_env(tcx, body, param_env) } pub fn new_with_param_env( tcx: TyCtxt<'tcx>, - def_id: LocalDefId, body: &'mir mir::Body<'tcx>, param_env: ty::ParamEnv<'tcx>, ) -> Self { - let const_kind = tcx.hir().body_const_context(def_id); - ConstCx { body, tcx, def_id: def_id, param_env, const_kind } + let const_kind = tcx.hir().body_const_context(body.source.def_id().expect_local()); + ConstCx { body, tcx, param_env, const_kind } + } + + pub fn def_id(&self) -> LocalDefId { + self.body.source.def_id().expect_local() } /// Returns the kind of const context this `Item` represents (`const`, `static`, etc.). @@ -55,7 +58,7 @@ impl ConstCx<'mir, 'tcx> { pub fn is_const_stable_const_fn(&self) -> bool { self.const_kind == Some(hir::ConstContext::ConstFn) && self.tcx.features().staged_api - && is_const_stable_const_fn(self.tcx, self.def_id.to_def_id()) + && is_const_stable_const_fn(self.tcx, self.def_id().to_def_id()) } /// Returns the function signature of the item being const-checked if it is a `fn` or `const fn`. @@ -64,7 +67,7 @@ impl ConstCx<'mir, 'tcx> { // // FIXME: Is this still an issue? let hir_map = self.tcx.hir(); - let hir_id = hir_map.local_def_id_to_hir_id(self.def_id); + let hir_id = hir_map.local_def_id_to_hir_id(self.def_id()); hir_map.fn_sig_by_hir_id(hir_id) } } diff --git a/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs b/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs index 9b2568d5abb..1a2d932ba19 100644 --- a/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs +++ b/compiler/rustc_mir/src/transform/check_consts/post_drop_elaboration.rs @@ -1,4 +1,3 @@ -use rustc_hir::def_id::LocalDefId; use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::{self, BasicBlock, Location}; use rustc_middle::ty::TyCtxt; @@ -24,13 +23,14 @@ pub fn checking_enabled(ccx: &ConstCx<'_, '_>) -> bool { /// /// This is separate from the rest of the const checking logic because it must run after drop /// elaboration. -pub fn check_live_drops(tcx: TyCtxt<'tcx>, def_id: LocalDefId, body: &mir::Body<'tcx>) { +pub fn check_live_drops(tcx: TyCtxt<'tcx>, body: &mir::Body<'tcx>) { + let def_id = body.source.def_id().expect_local(); let const_kind = tcx.hir().body_const_context(def_id); if const_kind.is_none() { return; } - let ccx = ConstCx { body, tcx, def_id, const_kind, param_env: tcx.param_env(def_id) }; + let ccx = ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def_id) }; if !checking_enabled(&ccx) { return; } diff --git a/compiler/rustc_mir/src/transform/check_consts/qualifs.rs b/compiler/rustc_mir/src/transform/check_consts/qualifs.rs index 3f4b3ca2eed..b3d9beb3742 100644 --- a/compiler/rustc_mir/src/transform/check_consts/qualifs.rs +++ b/compiler/rustc_mir/src/transform/check_consts/qualifs.rs @@ -126,7 +126,7 @@ impl Qualif for CustomEq { // because that component may be part of an enum variant (e.g., // `Option::::Some`), in which case some values of this type may be // structural-match (`Option::None`). - let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id); + let id = cx.tcx.hir().local_def_id_to_hir_id(cx.def_id()); traits::search_for_structural_match_violation(id, cx.body.span, cx.tcx, ty).is_some() } diff --git a/compiler/rustc_mir/src/transform/check_consts/validation.rs b/compiler/rustc_mir/src/transform/check_consts/validation.rs index 4e714bfeed3..b3e94ccde52 100644 --- a/compiler/rustc_mir/src/transform/check_consts/validation.rs +++ b/compiler/rustc_mir/src/transform/check_consts/validation.rs @@ -50,7 +50,7 @@ impl Qualifs<'mir, 'tcx> { location: Location, ) -> bool { let indirectly_mutable = self.indirectly_mutable.get_or_insert_with(|| { - let ConstCx { tcx, body, def_id, param_env, .. } = *ccx; + let ConstCx { tcx, body, param_env, .. } = *ccx; // We can use `unsound_ignore_borrow_on_drop` here because custom drop impls are not // allowed in a const. @@ -59,7 +59,7 @@ impl Qualifs<'mir, 'tcx> { // without breaking stable code? MaybeMutBorrowedLocals::mut_borrows_only(tcx, &body, param_env) .unsound_ignore_borrow_on_drop() - .into_engine(tcx, &body, def_id.to_def_id()) + .into_engine(tcx, &body) .pass_name("const_qualification") .iterate_to_fixpoint() .into_results_cursor(&body) @@ -84,10 +84,10 @@ impl Qualifs<'mir, 'tcx> { } let needs_drop = self.needs_drop.get_or_insert_with(|| { - let ConstCx { tcx, body, def_id, .. } = *ccx; + let ConstCx { tcx, body, .. } = *ccx; FlowSensitiveAnalysis::new(NeedsDrop, ccx) - .into_engine(tcx, &body, def_id.to_def_id()) + .into_engine(tcx, &body) .iterate_to_fixpoint() .into_results_cursor(&body) }); @@ -111,10 +111,10 @@ impl Qualifs<'mir, 'tcx> { } let has_mut_interior = self.has_mut_interior.get_or_insert_with(|| { - let ConstCx { tcx, body, def_id, .. } = *ccx; + let ConstCx { tcx, body, .. } = *ccx; FlowSensitiveAnalysis::new(HasMutInterior, ccx) - .into_engine(tcx, &body, def_id.to_def_id()) + .into_engine(tcx, &body) .iterate_to_fixpoint() .into_results_cursor(&body) }); @@ -157,7 +157,7 @@ impl Qualifs<'mir, 'tcx> { hir::ConstContext::Const | hir::ConstContext::Static(_) => { let mut cursor = FlowSensitiveAnalysis::new(CustomEq, ccx) - .into_engine(ccx.tcx, &ccx.body, ccx.def_id.to_def_id()) + .into_engine(ccx.tcx, &ccx.body) .iterate_to_fixpoint() .into_results_cursor(&ccx.body); @@ -205,7 +205,8 @@ impl Validator<'mir, 'tcx> { } pub fn check_body(&mut self) { - let ConstCx { tcx, body, def_id, .. } = *self.ccx; + let ConstCx { tcx, body, .. } = *self.ccx; + let def_id = self.ccx.def_id(); // `async` functions cannot be `const fn`. This is checked during AST lowering, so there's // no need to emit duplicate errors here. @@ -219,7 +220,7 @@ impl Validator<'mir, 'tcx> { // Prevent const trait methods from being annotated as `stable`. // FIXME: Do this as part of stability checking. if self.is_const_stable_const_fn() { - let hir_id = tcx.hir().local_def_id_to_hir_id(self.def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if crate::const_eval::is_parent_const_impl_raw(tcx, hir_id) { struct_span_err!( self.ccx.tcx.sess, @@ -291,7 +292,7 @@ impl Validator<'mir, 'tcx> { Status::Unstable(gate) if self.tcx.features().enabled(gate) => { let unstable_in_stable = self.ccx.is_const_stable_const_fn() - && !super::allow_internal_unstable(self.tcx, self.def_id.to_def_id(), gate); + && !super::allow_internal_unstable(self.tcx, self.def_id().to_def_id(), gate); if unstable_in_stable { emit_unstable_in_stable_error(self.ccx, span, gate); } @@ -367,9 +368,9 @@ impl Validator<'mir, 'tcx> { } fn check_item_predicates(&mut self) { - let ConstCx { tcx, def_id, .. } = *self.ccx; + let ConstCx { tcx, .. } = *self.ccx; - let mut current = def_id.to_def_id(); + let mut current = self.def_id().to_def_id(); loop { let predicates = tcx.predicates_of(current); for (predicate, _) in predicates.predicates { @@ -734,8 +735,8 @@ impl Visitor<'tcx> for Validator<'mir, 'tcx> { match &terminator.kind { TerminatorKind::Call { func, .. } => { - let ConstCx { tcx, body, def_id: caller, param_env, .. } = *self.ccx; - let caller = caller.to_def_id(); + let ConstCx { tcx, body, param_env, .. } = *self.ccx; + let caller = self.def_id().to_def_id(); let fn_ty = func.ty(body, tcx); diff --git a/compiler/rustc_mir/src/transform/dest_prop.rs b/compiler/rustc_mir/src/transform/dest_prop.rs index 0982bcfb0ca..410f462ed46 100644 --- a/compiler/rustc_mir/src/transform/dest_prop.rs +++ b/compiler/rustc_mir/src/transform/dest_prop.rs @@ -408,15 +408,12 @@ impl Conflicts<'a> { body.local_decls.len(), ); - let def_id = body.source.def_id(); let mut init = MaybeInitializedLocals - .into_engine(tcx, body, def_id) - .iterate_to_fixpoint() - .into_results_cursor(body); - let mut live = MaybeLiveLocals - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .iterate_to_fixpoint() .into_results_cursor(body); + let mut live = + MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint().into_results_cursor(body); let mut reachable = None; dump_mir(tcx, None, "DestinationPropagation-dataflow", &"", body, |pass_where, w| { diff --git a/compiler/rustc_mir/src/transform/elaborate_drops.rs b/compiler/rustc_mir/src/transform/elaborate_drops.rs index f59050f5838..3d435f6d0e7 100644 --- a/compiler/rustc_mir/src/transform/elaborate_drops.rs +++ b/compiler/rustc_mir/src/transform/elaborate_drops.rs @@ -10,7 +10,6 @@ use crate::util::elaborate_drops::{elaborate_drop, DropFlagState, Unwind}; use crate::util::elaborate_drops::{DropElaborator, DropFlagMode, DropStyle}; use crate::util::patch::MirPatch; use rustc_data_structures::fx::FxHashMap; -use rustc_hir as hir; use rustc_index::bit_set::BitSet; use rustc_middle::mir::*; use rustc_middle::ty::{self, TyCtxt}; @@ -39,10 +38,10 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { let elaborate_patch = { let body = &*body; let env = MoveDataParamEnv { move_data, param_env }; - let dead_unwinds = find_dead_unwinds(tcx, body, def_id, &env); + let dead_unwinds = find_dead_unwinds(tcx, body, &env); let inits = MaybeInitializedPlaces::new(tcx, body, &env) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .dead_unwinds(&dead_unwinds) .pass_name("elaborate_drops") .iterate_to_fixpoint() @@ -50,7 +49,7 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { let uninits = MaybeUninitializedPlaces::new(tcx, body, &env) .mark_inactive_variants_as_uninit() - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .dead_unwinds(&dead_unwinds) .pass_name("elaborate_drops") .iterate_to_fixpoint() @@ -76,7 +75,6 @@ impl<'tcx> MirPass<'tcx> for ElaborateDrops { fn find_dead_unwinds<'tcx>( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, - def_id: hir::def_id::DefId, env: &MoveDataParamEnv<'tcx>, ) -> BitSet { debug!("find_dead_unwinds({:?})", body.span); @@ -84,7 +82,7 @@ fn find_dead_unwinds<'tcx>( // reach cleanup blocks, which can't have unwind edges themselves. let mut dead_unwinds = BitSet::new_empty(body.basic_blocks().len()); let mut flow_inits = MaybeInitializedPlaces::new(tcx, body, &env) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .pass_name("find_dead_unwinds") .iterate_to_fixpoint() .into_results_cursor(body); diff --git a/compiler/rustc_mir/src/transform/generator.rs b/compiler/rustc_mir/src/transform/generator.rs index 7c3a0502dec..924bb4996fc 100644 --- a/compiler/rustc_mir/src/transform/generator.rs +++ b/compiler/rustc_mir/src/transform/generator.rs @@ -61,7 +61,6 @@ use crate::util::expand_aggregate; use crate::util::storage; use rustc_data_structures::fx::FxHashMap; use rustc_hir as hir; -use rustc_hir::def_id::DefId; use rustc_hir::lang_items::LangItem; use rustc_index::bit_set::{BitMatrix, BitSet}; use rustc_index::vec::{Idx, IndexVec}; @@ -454,20 +453,19 @@ fn locals_live_across_suspend_points( always_live_locals: &storage::AlwaysLiveLocals, movable: bool, ) -> LivenessInfo { - let def_id = body.source.def_id(); let body_ref: &Body<'_> = &body; // Calculate when MIR locals have live storage. This gives us an upper bound of their // lifetimes. let mut storage_live = MaybeStorageLive::new(always_live_locals.clone()) - .into_engine(tcx, body_ref, def_id) + .into_engine(tcx, body_ref) .iterate_to_fixpoint() .into_results_cursor(body_ref); // Calculate the MIR locals which have been previously // borrowed (even if they are still active). let borrowed_locals_results = MaybeBorrowedLocals::all_borrows() - .into_engine(tcx, body_ref, def_id) + .into_engine(tcx, body_ref) .pass_name("generator") .iterate_to_fixpoint(); @@ -477,14 +475,14 @@ fn locals_live_across_suspend_points( // Calculate the MIR locals that we actually need to keep storage around // for. let requires_storage_results = MaybeRequiresStorage::new(body, &borrowed_locals_results) - .into_engine(tcx, body_ref, def_id) + .into_engine(tcx, body_ref) .iterate_to_fixpoint(); let mut requires_storage_cursor = dataflow::ResultsCursor::new(body_ref, &requires_storage_results); // Calculate the liveness of MIR locals ignoring borrows. let mut liveness = MaybeLiveLocals - .into_engine(tcx, body_ref, def_id) + .into_engine(tcx, body_ref) .pass_name("generator") .iterate_to_fixpoint() .into_results_cursor(body_ref); @@ -722,11 +720,11 @@ impl<'body, 'tcx, 's> StorageConflictVisitor<'body, 'tcx, 's> { fn sanitize_witness<'tcx>( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, - did: DefId, witness: Ty<'tcx>, upvars: &Vec>, saved_locals: &GeneratorSavedLocals, ) { + let did = body.source.def_id(); let allowed_upvars = tcx.erase_regions(upvars); let allowed = match witness.kind() { ty::GeneratorWitness(s) => tcx.erase_late_bound_regions(&s), @@ -865,7 +863,7 @@ fn insert_switch<'tcx>( } } -fn elaborate_generator_drops<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, body: &mut Body<'tcx>) { +fn elaborate_generator_drops<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) { use crate::shim::DropShimElaborator; use crate::util::elaborate_drops::{elaborate_drop, Unwind}; use crate::util::patch::MirPatch; @@ -874,6 +872,7 @@ fn elaborate_generator_drops<'tcx>(tcx: TyCtxt<'tcx>, def_id: DefId, body: &mut // this is ok because `open_drop` can only be reached within that own // generator's resume function. + let def_id = body.source.def_id(); let param_env = tcx.param_env(def_id); let mut elaborator = DropShimElaborator { body, patch: MirPatch::new(body), tcx, param_env }; @@ -1246,8 +1245,6 @@ impl<'tcx> MirPass<'tcx> for StateTransform { assert!(body.generator_drop.is_none()); - let def_id = body.source.def_id(); - // The first argument is the generator type passed by value let gen_ty = body.local_decls.raw[1].ty; @@ -1306,7 +1303,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform { let liveness_info = locals_live_across_suspend_points(tcx, body, &always_live_locals, movable); - sanitize_witness(tcx, body, def_id, interior, &upvars, &liveness_info.saved_locals); + sanitize_witness(tcx, body, interior, &upvars, &liveness_info.saved_locals); if tcx.sess.opts.debugging_opts.validate_mir { let mut vis = EnsureGeneratorFieldAssignmentsNeverAlias { @@ -1358,7 +1355,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform { // Expand `drop(generator_struct)` to a drop ladder which destroys upvars. // If any upvars are moved out of, drop elaboration will handle upvar destruction. // However we need to also elaborate the code generated by `insert_clean_drop`. - elaborate_generator_drops(tcx, def_id, body); + elaborate_generator_drops(tcx, body); dump_mir(tcx, None, "generator_post-transform", &0, body, |_, _| Ok(())); diff --git a/compiler/rustc_mir/src/transform/mod.rs b/compiler/rustc_mir/src/transform/mod.rs index d8bc00c5f3f..b4f5947f5a3 100644 --- a/compiler/rustc_mir/src/transform/mod.rs +++ b/compiler/rustc_mir/src/transform/mod.rs @@ -229,13 +229,7 @@ fn mir_const_qualif(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) -> return Default::default(); } - let ccx = check_consts::ConstCx { - body, - tcx, - def_id: def.did, - const_kind, - param_env: tcx.param_env(def.did), - }; + let ccx = check_consts::ConstCx { body, tcx, const_kind, param_env: tcx.param_env(def.did) }; let mut validator = check_consts::validation::Validator::new(&ccx); validator.check_body(); @@ -346,7 +340,7 @@ fn mir_drops_elaborated_and_const_checked<'tcx>( let mut body = body.steal(); run_post_borrowck_cleanup_passes(tcx, &mut body); - check_consts::post_drop_elaboration::check_live_drops(tcx, def.did, &body); + check_consts::post_drop_elaboration::check_live_drops(tcx, &body); tcx.alloc_steal_mir(body) } diff --git a/compiler/rustc_mir/src/transform/promote_consts.rs b/compiler/rustc_mir/src/transform/promote_consts.rs index 7ad659bddbe..5fd47128631 100644 --- a/compiler/rustc_mir/src/transform/promote_consts.rs +++ b/compiler/rustc_mir/src/transform/promote_consts.rs @@ -63,7 +63,7 @@ impl<'tcx> MirPass<'tcx> for PromoteTemps<'tcx> { let def = body.source.with_opt_param().expect_local(); let mut rpo = traversal::reverse_postorder(body); - let ccx = ConstCx::new(tcx, def.did, body); + let ccx = ConstCx::new(tcx, body); let (temps, all_candidates) = collect_temps_and_candidates(&ccx, &mut rpo); let promotable_candidates = validate_candidates(&ccx, &temps, &all_candidates); @@ -758,7 +758,7 @@ impl<'tcx> Validator<'_, 'tcx> { ty::FnDef(def_id, _) => { is_const_fn(self.tcx, def_id) || is_unstable_const_fn(self.tcx, def_id).is_some() - || is_lang_panic_fn(self.tcx, self.def_id.to_def_id()) + || is_lang_panic_fn(self.tcx, def_id) } _ => false, }; @@ -1250,7 +1250,9 @@ crate fn should_suggest_const_in_array_repeat_expressions_attribute<'tcx>( debug!( "should_suggest_const_in_array_repeat_expressions_flag: def_id={:?} \ should_promote={:?} feature_flag={:?}", - validator.ccx.def_id, should_promote, feature_flag + validator.ccx.def_id(), + should_promote, + feature_flag ); should_promote && !feature_flag } diff --git a/compiler/rustc_mir/src/transform/remove_unneeded_drops.rs b/compiler/rustc_mir/src/transform/remove_unneeded_drops.rs index cfee656eda4..aaf3ecab4dc 100644 --- a/compiler/rustc_mir/src/transform/remove_unneeded_drops.rs +++ b/compiler/rustc_mir/src/transform/remove_unneeded_drops.rs @@ -1,10 +1,9 @@ //! This pass replaces a drop of a type that does not need dropping, with a goto use crate::transform::MirPass; -use rustc_hir::def_id::LocalDefId; use rustc_middle::mir::visit::Visitor; use rustc_middle::mir::*; -use rustc_middle::ty::TyCtxt; +use rustc_middle::ty::{ParamEnv, TyCtxt}; use super::simplify::simplify_cfg; @@ -16,8 +15,8 @@ impl<'tcx> MirPass<'tcx> for RemoveUnneededDrops { let mut opt_finder = RemoveUnneededDropsOptimizationFinder { tcx, body, + param_env: tcx.param_env(body.source.def_id()), optimizations: vec![], - def_id: body.source.def_id().expect_local(), }; opt_finder.visit_body(body); let should_simplify = !opt_finder.optimizations.is_empty(); @@ -40,7 +39,7 @@ impl<'a, 'tcx> Visitor<'tcx> for RemoveUnneededDropsOptimizationFinder<'a, 'tcx> match terminator.kind { TerminatorKind::Drop { place, target, .. } => { let ty = place.ty(self.body, self.tcx); - let needs_drop = ty.ty.needs_drop(self.tcx, self.tcx.param_env(self.def_id)); + let needs_drop = ty.ty.needs_drop(self.tcx, self.param_env); if !needs_drop { self.optimizations.push((location, target)); } @@ -54,5 +53,5 @@ pub struct RemoveUnneededDropsOptimizationFinder<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, optimizations: Vec<(Location, BasicBlock)>, - def_id: LocalDefId, + param_env: ParamEnv<'tcx>, } diff --git a/compiler/rustc_mir/src/transform/rustc_peek.rs b/compiler/rustc_mir/src/transform/rustc_peek.rs index 366041581c2..205f718d6e4 100644 --- a/compiler/rustc_mir/src/transform/rustc_peek.rs +++ b/compiler/rustc_mir/src/transform/rustc_peek.rs @@ -6,7 +6,6 @@ use rustc_span::Span; use rustc_target::spec::abi::Abi; use crate::transform::MirPass; -use rustc_hir::def_id::DefId; use rustc_index::bit_set::BitSet; use rustc_middle::mir::{self, Body, Local, Location}; use rustc_middle::ty::{self, Ty, TyCtxt}; @@ -41,41 +40,40 @@ impl<'tcx> MirPass<'tcx> for SanityCheck { if has_rustc_mir_with(sess, &attributes, sym::rustc_peek_maybe_init).is_some() { let flow_inits = MaybeInitializedPlaces::new(tcx, body, &mdpe) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .iterate_to_fixpoint(); - sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_inits); + sanity_check_via_rustc_peek(tcx, body, &attributes, &flow_inits); } if has_rustc_mir_with(sess, &attributes, sym::rustc_peek_maybe_uninit).is_some() { let flow_uninits = MaybeUninitializedPlaces::new(tcx, body, &mdpe) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .iterate_to_fixpoint(); - sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_uninits); + sanity_check_via_rustc_peek(tcx, body, &attributes, &flow_uninits); } if has_rustc_mir_with(sess, &attributes, sym::rustc_peek_definite_init).is_some() { let flow_def_inits = DefinitelyInitializedPlaces::new(tcx, body, &mdpe) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .iterate_to_fixpoint(); - sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_def_inits); + sanity_check_via_rustc_peek(tcx, body, &attributes, &flow_def_inits); } if has_rustc_mir_with(sess, &attributes, sym::rustc_peek_indirectly_mutable).is_some() { let flow_mut_borrowed = MaybeMutBorrowedLocals::mut_borrows_only(tcx, body, param_env) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .iterate_to_fixpoint(); - sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_mut_borrowed); + sanity_check_via_rustc_peek(tcx, body, &attributes, &flow_mut_borrowed); } if has_rustc_mir_with(sess, &attributes, sym::rustc_peek_liveness).is_some() { - let flow_liveness = - MaybeLiveLocals.into_engine(tcx, body, def_id).iterate_to_fixpoint(); + let flow_liveness = MaybeLiveLocals.into_engine(tcx, body).iterate_to_fixpoint(); - sanity_check_via_rustc_peek(tcx, body, def_id, &attributes, &flow_liveness); + sanity_check_via_rustc_peek(tcx, body, &attributes, &flow_liveness); } if has_rustc_mir_with(sess, &attributes, sym::stop_after_dataflow).is_some() { @@ -103,12 +101,12 @@ impl<'tcx> MirPass<'tcx> for SanityCheck { pub fn sanity_check_via_rustc_peek<'tcx, A>( tcx: TyCtxt<'tcx>, body: &Body<'tcx>, - def_id: DefId, _attributes: &[ast::Attribute], results: &Results<'tcx, A>, ) where A: RustcPeekAt<'tcx>, { + let def_id = body.source.def_id(); debug!("sanity_check_via_rustc_peek def_id: {:?}", def_id); let mut cursor = ResultsCursor::new(body, results); diff --git a/compiler/rustc_mir/src/transform/validate.rs b/compiler/rustc_mir/src/transform/validate.rs index 0c3580584ff..cf51e86c5bc 100644 --- a/compiler/rustc_mir/src/transform/validate.rs +++ b/compiler/rustc_mir/src/transform/validate.rs @@ -38,7 +38,7 @@ impl<'tcx> MirPass<'tcx> for Validator { let always_live_locals = AlwaysLiveLocals::new(body); let storage_liveness = MaybeStorageLive::new(always_live_locals) - .into_engine(tcx, body, def_id) + .into_engine(tcx, body) .iterate_to_fixpoint() .into_results_cursor(body); diff --git a/compiler/rustc_mir/src/util/graphviz.rs b/compiler/rustc_mir/src/util/graphviz.rs index c10724ad404..0c32161fce5 100644 --- a/compiler/rustc_mir/src/util/graphviz.rs +++ b/compiler/rustc_mir/src/util/graphviz.rs @@ -72,16 +72,16 @@ where writeln!(w, r#" edge [{}];"#, content_attrs_str)?; // Graph label - write_graph_label(tcx, def_id, body, w)?; + write_graph_label(tcx, body, w)?; // Nodes for (block, _) in body.basic_blocks().iter_enumerated() { - write_node(def_id, block, body, dark_mode, w)?; + write_node(block, body, dark_mode, w)?; } // Edges for (source, _) in body.basic_blocks().iter_enumerated() { - write_edges(def_id, source, body, w)?; + write_edges(source, body, w)?; } writeln!(w, "}}") } @@ -145,12 +145,12 @@ where /// Write a graphviz DOT node for the given basic block. fn write_node( - def_id: DefId, block: BasicBlock, body: &Body<'_>, dark_mode: bool, w: &mut W, ) -> io::Result<()> { + let def_id = body.source.def_id(); // Start a new node with the label to follow, in one of DOT's pseudo-HTML tables. write!(w, r#" {} [shape="none", label=<"#, node(def_id, block))?; write_node_label(block, body, dark_mode, w, 1, |_| Ok(()), |_| Ok(()))?; @@ -159,12 +159,8 @@ fn write_node( } /// Write graphviz DOT edges with labels between the given basic block and all of its successors. -fn write_edges( - def_id: DefId, - source: BasicBlock, - body: &Body<'_>, - w: &mut W, -) -> io::Result<()> { +fn write_edges(source: BasicBlock, body: &Body<'_>, w: &mut W) -> io::Result<()> { + let def_id = body.source.def_id(); let terminator = body[source].terminator(); let labels = terminator.kind.fmt_successor_labels(); @@ -182,10 +178,11 @@ fn write_edges( /// all the variables and temporaries. fn write_graph_label<'tcx, W: Write>( tcx: TyCtxt<'tcx>, - def_id: DefId, body: &Body<'_>, w: &mut W, ) -> io::Result<()> { + let def_id = body.source.def_id(); + write!(w, " label=( let mut file = create_dump_file(tcx, "html", pass_num, pass_name, disambiguator, body.source)?; if body.source.def_id().is_local() { - write_mir_fn_spanview(tcx, body.source.def_id(), body, spanview, &mut file)?; + write_mir_fn_spanview(tcx, body, spanview, &mut file)?; } }; } diff --git a/compiler/rustc_mir/src/util/spanview.rs b/compiler/rustc_mir/src/util/spanview.rs index fe33fffe0ea..fdc724178b6 100644 --- a/compiler/rustc_mir/src/util/spanview.rs +++ b/compiler/rustc_mir/src/util/spanview.rs @@ -90,7 +90,6 @@ pub struct SpanViewable { /// Write a spanview HTML+CSS file to analyze MIR element spans. pub fn write_mir_fn_spanview<'tcx, W>( tcx: TyCtxt<'tcx>, - def_id: DefId, body: &Body<'tcx>, spanview: MirSpanview, w: &mut W, @@ -98,6 +97,7 @@ pub fn write_mir_fn_spanview<'tcx, W>( where W: Write, { + let def_id = body.source.def_id(); let body_span = hir_body(tcx, def_id).value.span; let mut span_viewables = Vec::new(); for (bb, data) in body.basic_blocks().iter_enumerated() { diff --git a/compiler/rustc_mir_build/src/build/mod.rs b/compiler/rustc_mir_build/src/build/mod.rs index 72814e3373b..e79f90b5f7a 100644 --- a/compiler/rustc_mir_build/src/build/mod.rs +++ b/compiler/rustc_mir_build/src/build/mod.rs @@ -205,7 +205,7 @@ fn mir_build(tcx: TyCtxt<'_>, def: ty::WithOptConstParam) -> Body<'_ build::construct_const(cx, body_id, return_ty, return_ty_span) }; - lints::check(tcx, &body, def.did); + lints::check(tcx, &body); // The borrow checker will replace all the regions here with its own // inference variables. There's no point having non-erased regions here. diff --git a/compiler/rustc_mir_build/src/lints.rs b/compiler/rustc_mir_build/src/lints.rs index a8d7c612a84..bdef02a011b 100644 --- a/compiler/rustc_mir_build/src/lints.rs +++ b/compiler/rustc_mir_build/src/lints.rs @@ -1,7 +1,6 @@ use rustc_data_structures::graph::iterate::{ ControlFlow, NodeStatus, TriColorDepthFirstSearch, TriColorVisitor, }; -use rustc_hir::def_id::LocalDefId; use rustc_hir::intravisit::FnKind; use rustc_middle::hir::map::blocks::FnLikeNode; use rustc_middle::mir::{BasicBlock, Body, Operand, TerminatorKind}; @@ -10,7 +9,8 @@ use rustc_middle::ty::{self, AssocItem, AssocItemContainer, Instance, TyCtxt}; use rustc_session::lint::builtin::UNCONDITIONAL_RECURSION; use rustc_span::Span; -crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: LocalDefId) { +crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>) { + let def_id = body.source.def_id().expect_local(); let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); if let Some(fn_like_node) = FnLikeNode::from_node(tcx.hir().get(hir_id)) { @@ -30,7 +30,7 @@ crate fn check<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, def_id: LocalDefId) { _ => &[], }; - let mut vis = Search { tcx, body, def_id, reachable_recursive_calls: vec![], trait_substs }; + let mut vis = Search { tcx, body, reachable_recursive_calls: vec![], trait_substs }; if let Some(NonRecursive) = TriColorDepthFirstSearch::new(&body).run_from_start(&mut vis) { return; } @@ -57,7 +57,6 @@ struct NonRecursive; struct Search<'mir, 'tcx> { tcx: TyCtxt<'tcx>, body: &'mir Body<'tcx>, - def_id: LocalDefId, trait_substs: &'tcx [GenericArg<'tcx>], reachable_recursive_calls: Vec, @@ -66,16 +65,17 @@ struct Search<'mir, 'tcx> { impl<'mir, 'tcx> Search<'mir, 'tcx> { /// Returns `true` if `func` refers to the function we are searching in. fn is_recursive_call(&self, func: &Operand<'tcx>) -> bool { - let Search { tcx, body, def_id, trait_substs, .. } = *self; - let param_env = tcx.param_env(def_id); + let Search { tcx, body, trait_substs, .. } = *self; + let caller = body.source.def_id(); + let param_env = tcx.param_env(caller); let func_ty = func.ty(body, tcx); - if let ty::FnDef(fn_def_id, substs) = *func_ty.kind() { - let (call_fn_id, call_substs) = - if let Ok(Some(instance)) = Instance::resolve(tcx, param_env, fn_def_id, substs) { + if let ty::FnDef(callee, substs) = *func_ty.kind() { + let (callee, call_substs) = + if let Ok(Some(instance)) = Instance::resolve(tcx, param_env, callee, substs) { (instance.def_id(), instance.substs) } else { - (fn_def_id, substs) + (callee, substs) }; // FIXME(#57965): Make this work across function boundaries @@ -84,8 +84,7 @@ impl<'mir, 'tcx> Search<'mir, 'tcx> { // calling into an entirely different method (for example, a call from the default // method in the trait to `>::method`, where `A` and/or `B` are // specific types). - return call_fn_id == def_id.to_def_id() - && &call_substs[..trait_substs.len()] == trait_substs; + return callee == caller && &call_substs[..trait_substs.len()] == trait_substs; } false