diff options
Diffstat (limited to 'compiler/rustc_mir_transform/src')
15 files changed, 55 insertions, 70 deletions
diff --git a/compiler/rustc_mir_transform/src/check_packed_ref.rs b/compiler/rustc_mir_transform/src/check_packed_ref.rs index f0367958ef8..4bf66cd4c9f 100644 --- a/compiler/rustc_mir_transform/src/check_packed_ref.rs +++ b/compiler/rustc_mir_transform/src/check_packed_ref.rs @@ -1,10 +1,9 @@ -use rustc_hir::def_id::{DefId, LocalDefId}; +use rustc_hir::def_id::LocalDefId; use rustc_middle::mir::visit::{PlaceContext, Visitor}; use rustc_middle::mir::*; use rustc_middle::ty::query::Providers; use rustc_middle::ty::{self, TyCtxt}; use rustc_session::lint::builtin::UNALIGNED_REFERENCES; -use rustc_span::symbol::sym; use crate::util; use crate::MirLint; @@ -50,22 +49,6 @@ fn unsafe_derive_on_repr_packed(tcx: TyCtxt<'_>, def_id: LocalDefId) { }); } -fn builtin_derive_def_id(tcx: TyCtxt<'_>, def_id: DefId) -> Option<DefId> { - debug!("builtin_derive_def_id({:?})", def_id); - if let Some(impl_def_id) = tcx.impl_of_method(def_id) { - if tcx.has_attr(impl_def_id, sym::automatically_derived) { - debug!("builtin_derive_def_id({:?}) - is {:?}", def_id, impl_def_id); - Some(impl_def_id) - } else { - debug!("builtin_derive_def_id({:?}) - not automatically derived", def_id); - None - } - } else { - debug!("builtin_derive_def_id({:?}) - not a method", def_id); - None - } -} - impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> { fn visit_terminator(&mut self, terminator: &Terminator<'tcx>, location: Location) { // Make sure we know where in the MIR we are. @@ -83,7 +66,11 @@ impl<'tcx> Visitor<'tcx> for PackedRefChecker<'_, 'tcx> { if context.is_borrow() { if util::is_disaligned(self.tcx, self.body, self.param_env, *place) { let def_id = self.body.source.instance.def_id(); - if let Some(impl_def_id) = builtin_derive_def_id(self.tcx, def_id) { + if let Some(impl_def_id) = self + .tcx + .impl_of_method(def_id) + .filter(|&def_id| self.tcx.is_builtin_derive(def_id)) + { // If a method is defined in the local crate, // the impl containing that method should also be. self.tcx.ensure().unsafe_derive_on_repr_packed(impl_def_id.expect_local()); diff --git a/compiler/rustc_mir_transform/src/check_unsafety.rs b/compiler/rustc_mir_transform/src/check_unsafety.rs index 8de05990cdf..f5d82315c4e 100644 --- a/compiler/rustc_mir_transform/src/check_unsafety.rs +++ b/compiler/rustc_mir_transform/src/check_unsafety.rs @@ -149,7 +149,7 @@ impl<'tcx> Visitor<'tcx> for UnsafetyChecker<'_, 'tcx> { self.check_mut_borrowing_layout_constrained_field(*place, context.is_mutating_use()); } - // Some checks below need the extra metainfo of the local declaration. + // Some checks below need the extra meta info of the local declaration. let decl = &self.body.local_decls[place.local]; // Check the base local: it might be an unsafe-to-access static. We only check derefs of the diff --git a/compiler/rustc_mir_transform/src/const_goto.rs b/compiler/rustc_mir_transform/src/const_goto.rs index 905173b0457..5acf939f06b 100644 --- a/compiler/rustc_mir_transform/src/const_goto.rs +++ b/compiler/rustc_mir_transform/src/const_goto.rs @@ -39,7 +39,9 @@ impl<'tcx> MirPass<'tcx> for ConstGoto { opt_finder.visit_body(body); let should_simplify = !opt_finder.optimizations.is_empty(); for opt in opt_finder.optimizations { - let terminator = body.basic_blocks_mut()[opt.bb_with_goto].terminator_mut(); + let block = &mut body.basic_blocks_mut()[opt.bb_with_goto]; + block.statements.extend(opt.stmts_move_up); + let terminator = block.terminator_mut(); let new_goto = TerminatorKind::Goto { target: opt.target_to_use_in_goto }; debug!("SUCCESS: replacing `{:?}` with `{:?}`", terminator.kind, new_goto); terminator.kind = new_goto; @@ -68,12 +70,15 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { // Now check that the target of this Goto switches on this place. let target_bb = &self.body.basic_blocks()[target]; - // FIXME(simonvandel): We are conservative here when we don't allow - // any statements in the target basic block. - // This could probably be relaxed to allow `StorageDead`s which could be - // copied to the predecessor of this block. - if !target_bb.statements.is_empty() { - None? + // The `StorageDead(..)` statement does not affect the functionality of mir. + // We can move this part of the statement up to the predecessor. + let mut stmts_move_up = Vec::new(); + for stmt in &target_bb.statements { + if let StatementKind::StorageDead(..) = stmt.kind { + stmts_move_up.push(stmt.clone()) + } else { + None?; + } } let target_bb_terminator = target_bb.terminator(); @@ -87,6 +92,7 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { self.optimizations.push(OptimizationToApply { bb_with_goto: location.block, target_to_use_in_goto, + stmts_move_up, }); } } @@ -97,14 +103,15 @@ impl<'tcx> Visitor<'tcx> for ConstGotoOptimizationFinder<'_, 'tcx> { } } -struct OptimizationToApply { +struct OptimizationToApply<'tcx> { bb_with_goto: BasicBlock, target_to_use_in_goto: BasicBlock, + stmts_move_up: Vec<Statement<'tcx>>, } pub struct ConstGotoOptimizationFinder<'a, 'tcx> { tcx: TyCtxt<'tcx>, body: &'a Body<'tcx>, param_env: ParamEnv<'tcx>, - optimizations: Vec<OptimizationToApply>, + optimizations: Vec<OptimizationToApply<'tcx>>, } diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index 4f5fc38917f..a342aeed905 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -184,8 +184,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> type MemoryKind = !; - type MemoryExtra = (); - fn load_mir( _ecx: &InterpCx<'mir, 'tcx, Self>, _instance: ty::InstanceDef<'tcx>, @@ -267,7 +265,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> } fn before_access_global( - _memory_extra: &(), + _tcx: TyCtxt<'tcx>, + _machine: &Self, _alloc_id: AllocId, alloc: ConstAllocation<'tcx, Self::PointerTag, Self::AllocExtra>, _static_def_id: Option<DefId>, @@ -377,7 +376,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { span, param_env, ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop), - (), ); let ret = ecx @@ -634,7 +632,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { } } - // Attempt to use albegraic identities to eliminate constant expressions + // Attempt to use algebraic identities to eliminate constant expressions fn eval_rvalue_with_identities( &mut self, rvalue: &Rvalue<'tcx>, diff --git a/compiler/rustc_mir_transform/src/const_prop_lint.rs b/compiler/rustc_mir_transform/src/const_prop_lint.rs index 5be745bc1de..159503ad2d3 100644 --- a/compiler/rustc_mir_transform/src/const_prop_lint.rs +++ b/compiler/rustc_mir_transform/src/const_prop_lint.rs @@ -180,8 +180,6 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> type MemoryKind = !; - type MemoryExtra = (); - fn load_mir( _ecx: &InterpCx<'mir, 'tcx, Self>, _instance: ty::InstanceDef<'tcx>, @@ -263,7 +261,8 @@ impl<'mir, 'tcx> interpret::Machine<'mir, 'tcx> for ConstPropMachine<'mir, 'tcx> } fn before_access_global( - _memory_extra: &(), + _tcx: TyCtxt<'tcx>, + _machine: &Self, _alloc_id: AllocId, alloc: ConstAllocation<'tcx, Self::PointerTag, Self::AllocExtra>, _static_def_id: Option<DefId>, @@ -374,7 +373,6 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> { span, param_env, ConstPropMachine::new(only_propagate_inside_block_locals, can_const_prop), - (), ); let ret = ecx diff --git a/compiler/rustc_mir_transform/src/coverage/query.rs b/compiler/rustc_mir_transform/src/coverage/query.rs index da921904523..9d02f58ae65 100644 --- a/compiler/rustc_mir_transform/src/coverage/query.rs +++ b/compiler/rustc_mir_transform/src/coverage/query.rs @@ -66,7 +66,7 @@ impl CoverageVisitor { // The operand ID is outside the known range of counter IDs and also outside the // known range of expression IDs. In either case, the result of a missing operand // (if and when used in an expression) will be zero, so from a computation - // perspective, it doesn't matter whether it is interepretted as a counter or an + // perspective, it doesn't matter whether it is interpreted as a counter or an // expression. // // However, the `num_counters` and `num_expressions` query results are used to diff --git a/compiler/rustc_mir_transform/src/coverage/spans.rs b/compiler/rustc_mir_transform/src/coverage/spans.rs index a36ba9300e4..5e366d7fb7d 100644 --- a/compiler/rustc_mir_transform/src/coverage/spans.rs +++ b/compiler/rustc_mir_transform/src/coverage/spans.rs @@ -694,7 +694,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> { /// If prev.span() was split off to the right of a closure, prev.span().lo() will be /// greater than prev_original_span.lo(). The actual span of `prev_original_span` is /// not as important as knowing that `prev()` **used to have the same span** as `curr(), - /// which means their sort order is still meaningful for determinating the dominator + /// which means their sort order is still meaningful for determining the dominator /// relationship. /// /// When two `CoverageSpan`s have the same `Span`, dominated spans can be discarded; but if @@ -726,7 +726,7 @@ impl<'a, 'tcx> CoverageSpans<'a, 'tcx> { self.prev() ); self.cutoff_prev_at_overlapping_curr(); - // If one span dominates the other, assocate the span with the code from the dominated + // If one span dominates the other, associate the span with the code from the dominated // block only (`curr`), and discard the overlapping portion of the `prev` span. (Note // that if `prev.span` is wider than `prev_original_span`, a `CoverageSpan` will still // be created for `prev`s block, for the non-overlapping portion, left of `curr.span`.) diff --git a/compiler/rustc_mir_transform/src/early_otherwise_branch.rs b/compiler/rustc_mir_transform/src/early_otherwise_branch.rs index d72e8d16105..5bde0c01412 100644 --- a/compiler/rustc_mir_transform/src/early_otherwise_branch.rs +++ b/compiler/rustc_mir_transform/src/early_otherwise_branch.rs @@ -260,7 +260,7 @@ fn may_hoist<'tcx>(tcx: TyCtxt<'tcx>, body: &Body<'tcx>, place: Place<'tcx>) -> for (place, proj) in place.iter_projections() { match proj { // Dereferencing in the computation of `place` might cause issues from one of two - // cateogires. First, the referrent might be invalid. We protect against this by + // categories. First, the referent might be invalid. We protect against this by // dereferencing references only (not pointers). Second, the use of a reference may // invalidate other references that are used later (for aliasing reasons). Consider // where such an invalidated reference may appear: diff --git a/compiler/rustc_mir_transform/src/generator.rs b/compiler/rustc_mir_transform/src/generator.rs index ad96bf544cb..04b5c4e0919 100644 --- a/compiler/rustc_mir_transform/src/generator.rs +++ b/compiler/rustc_mir_transform/src/generator.rs @@ -500,7 +500,7 @@ fn locals_live_across_suspend_points<'tcx>( // The `liveness` variable contains the liveness of MIR locals ignoring borrows. // This is correct for movable generators since borrows cannot live across // suspension points. However for immovable generators we need to account for - // borrows, so we conseratively assume that all borrowed locals are live until + // borrows, so we conservatively assume that all borrowed locals are live until // we find a StorageDead statement referencing the locals. // To do this we just union our `liveness` result with `borrowed_locals`, which // contains all the locals which has been borrowed before this suspension point. diff --git a/compiler/rustc_mir_transform/src/inline.rs b/compiler/rustc_mir_transform/src/inline.rs index 23e5f0b4f30..2f0673b9a76 100644 --- a/compiler/rustc_mir_transform/src/inline.rs +++ b/compiler/rustc_mir_transform/src/inline.rs @@ -1,7 +1,6 @@ //! Inlining pass for MIR functions use rustc_attr::InlineAttr; -use rustc_hir as hir; use rustc_index::bit_set::BitSet; use rustc_index::vec::Idx; use rustc_middle::middle::codegen_fn_attrs::{CodegenFnAttrFlags, CodegenFnAttrs}; @@ -59,11 +58,10 @@ impl<'tcx> MirPass<'tcx> for Inline { } fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool { - let def_id = body.source.def_id(); - let hir_id = tcx.hir().local_def_id_to_hir_id(def_id.expect_local()); + let def_id = body.source.def_id().expect_local(); // Only do inlining into fn bodies. - if !tcx.hir().body_owner_kind(hir_id).is_fn_or_closure() { + if !tcx.hir().body_owner_kind(def_id).is_fn_or_closure() { return false; } if body.source.promoted.is_some() { @@ -77,9 +75,10 @@ fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool { } let param_env = tcx.param_env_reveal_all_normalized(def_id); + let hir_id = tcx.hir().local_def_id_to_hir_id(def_id); let param_env = rustc_trait_selection::traits::normalize_param_env_or_error( tcx, - def_id, + def_id.to_def_id(), param_env, ObligationCause::misc(body.span, hir_id), ); @@ -88,7 +87,6 @@ fn inline<'tcx>(tcx: TyCtxt<'tcx>, body: &mut Body<'tcx>) -> bool { tcx, param_env, codegen_fn_attrs: tcx.codegen_fn_attrs(def_id), - hir_id, history: Vec::new(), changed: false, }; @@ -102,8 +100,6 @@ struct Inliner<'tcx> { param_env: ParamEnv<'tcx>, /// Caller codegen attributes. codegen_fn_attrs: &'tcx CodegenFnAttrs, - /// Caller HirID. - hir_id: hir::HirId, /// Stack of inlined Instances. history: Vec<ty::Instance<'tcx>>, /// Indicates that the caller body has been modified. @@ -179,7 +175,9 @@ impl<'tcx> Inliner<'tcx> { caller_body: &Body<'tcx>, callee: &Instance<'tcx>, ) -> Result<(), &'static str> { - if callee.def_id() == caller_body.source.def_id() { + let caller_def_id = caller_body.source.def_id(); + let callee_def_id = callee.def_id(); + if callee_def_id == caller_def_id { return Err("self-recursion"); } @@ -188,7 +186,7 @@ impl<'tcx> Inliner<'tcx> { // If there is no MIR available (either because it was not in metadata or // because it has no MIR because it's an extern function), then the inliner // won't cause cycles on this. - if !self.tcx.is_mir_available(callee.def_id()) { + if !self.tcx.is_mir_available(callee_def_id) { return Err("item MIR unavailable"); } } @@ -208,29 +206,26 @@ impl<'tcx> Inliner<'tcx> { | InstanceDef::CloneShim(..) => return Ok(()), } - if self.tcx.is_constructor(callee.def_id()) { + if self.tcx.is_constructor(callee_def_id) { trace!("constructors always have MIR"); // Constructor functions cannot cause a query cycle. return Ok(()); } - if let Some(callee_def_id) = callee.def_id().as_local() { - let callee_hir_id = self.tcx.hir().local_def_id_to_hir_id(callee_def_id); + if callee_def_id.is_local() { // Avoid a cycle here by only using `instance_mir` only if we have - // a lower `HirId` than the callee. This ensures that the callee will - // not inline us. This trick only works without incremental compilation. - // So don't do it if that is enabled. - if !self.tcx.dep_graph.is_fully_enabled() && self.hir_id.index() < callee_hir_id.index() + // a lower `DefPathHash` than the callee. This ensures that the callee will + // not inline us. This trick even works with incremental compilation, + // since `DefPathHash` is stable. + if self.tcx.def_path_hash(caller_def_id).local_hash() + < self.tcx.def_path_hash(callee_def_id).local_hash() { return Ok(()); } // If we know for sure that the function we're calling will itself try to // call us, then we avoid inlining that function. - if self - .tcx - .mir_callgraph_reachable((*callee, caller_body.source.def_id().expect_local())) - { + if self.tcx.mir_callgraph_reachable((*callee, caller_def_id.expect_local())) { return Err("caller might be reachable from callee (query cycle avoidance)"); } @@ -698,7 +693,7 @@ impl<'tcx> Inliner<'tcx> { // The `closure_ref` in our example above. let closure_ref_arg = iter::once(self_); - // The `tmp0`, `tmp1`, and `tmp2` in our example abonve. + // The `tmp0`, `tmp1`, and `tmp2` in our example above. let tuple_tmp_args = tuple_tys.iter().enumerate().map(|(i, ty)| { // This is e.g., `tuple_tmp.0` in our example above. let tuple_field = Operand::Move(tcx.mk_place_field(tuple, Field::new(i), ty)); diff --git a/compiler/rustc_mir_transform/src/inline/cycle.rs b/compiler/rustc_mir_transform/src/inline/cycle.rs index de93ab7059f..ea1ec6249bc 100644 --- a/compiler/rustc_mir_transform/src/inline/cycle.rs +++ b/compiler/rustc_mir_transform/src/inline/cycle.rs @@ -8,7 +8,7 @@ use rustc_middle::ty::{self, subst::SubstsRef, InstanceDef, TyCtxt}; use rustc_session::Limit; // FIXME: check whether it is cheaper to precompute the entire call graph instead of invoking -// this query riddiculously often. +// this query ridiculously often. #[instrument(level = "debug", skip(tcx, root, target))] crate fn mir_callgraph_reachable<'tcx>( tcx: TyCtxt<'tcx>, diff --git a/compiler/rustc_mir_transform/src/nrvo.rs b/compiler/rustc_mir_transform/src/nrvo.rs index ec25f298d48..444b4126e88 100644 --- a/compiler/rustc_mir_transform/src/nrvo.rs +++ b/compiler/rustc_mir_transform/src/nrvo.rs @@ -64,7 +64,7 @@ impl<'tcx> MirPass<'tcx> for RenameReturnPlace { let (renamed_decl, ret_decl) = body.local_decls.pick2_mut(returned_local, mir::RETURN_PLACE); - // Sometimes, the return place is assigned a local of a different but coercable type, for + // Sometimes, the return place is assigned a local of a different but coercible type, for // example `&mut T` instead of `&T`. Overwriting the `LocalInfo` for the return place means // its type may no longer match the return type of its function. This doesn't cause a // problem in codegen because these two types are layout-compatible, but may be unexpected. diff --git a/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs b/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs index 77fb092d580..03b9ecc9596 100644 --- a/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs +++ b/compiler/rustc_mir_transform/src/remove_noop_landing_pads.rs @@ -36,7 +36,7 @@ impl RemoveNoopLandingPads { | StatementKind::AscribeUserType(..) | StatementKind::Coverage(..) | StatementKind::Nop => { - // These are all nops in a landing pad + // These are all noops in a landing pad } StatementKind::Assign(box (place, Rvalue::Use(_) | Rvalue::Discriminant(_))) => { diff --git a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs index d7fb7063114..efa45883eab 100644 --- a/compiler/rustc_mir_transform/src/remove_uninit_drops.rs +++ b/compiler/rustc_mir_transform/src/remove_uninit_drops.rs @@ -12,7 +12,7 @@ use crate::MirPass; /// that point. /// /// This is redundant with drop elaboration, but we need to do it prior to const-checking, and -/// running const-checking after drop elaboration makes it opimization dependent, causing issues +/// running const-checking after drop elaboration makes it optimization dependent, causing issues /// like [#90770]. /// /// [#90770]: https://github.com/rust-lang/rust/issues/90770 diff --git a/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs b/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs index 39f78e9555e..921a11a3a06 100644 --- a/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs +++ b/compiler/rustc_mir_transform/src/remove_unneeded_drops.rs @@ -2,7 +2,7 @@ //! //! When the MIR is built, we check `needs_drop` before emitting a `Drop` for a place. This pass is //! useful because (unlike MIR building) it runs after type checking, so it can make use of -//! `Reveal::All` to provide more precies type information. +//! `Reveal::All` to provide more precise type information. use crate::MirPass; use rustc_middle::mir::*; |
