diff options
| author | Yuki Okushi <jtitor@2k36.org> | 2023-05-05 12:46:26 +0900 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2023-05-05 12:46:26 +0900 |
| commit | 18d4e22e573a8b06b5a578eb2f68e78b3531d7c5 (patch) | |
| tree | a10ed3ec1b31fefb004ff03b75c227dc36eceede /compiler/rustc_mir_transform/src | |
| parent | d98e174497529883c266037a53bb03936c90bb5a (diff) | |
| parent | 1ffe9059c31d17ea1b22099b0f64de0232ebadd5 (diff) | |
| download | rust-18d4e22e573a8b06b5a578eb2f68e78b3531d7c5.tar.gz rust-18d4e22e573a8b06b5a578eb2f68e78b3531d7c5.zip | |
Rollup merge of #110954 - cjgillot:const-prop-ref, r=wesleywiser
Reject borrows of projections in ConstProp. Fixes https://github.com/rust-lang/rust/issues/110947
Diffstat (limited to 'compiler/rustc_mir_transform/src')
| -rw-r--r-- | compiler/rustc_mir_transform/src/const_prop.rs | 20 |
1 files changed, 15 insertions, 5 deletions
diff --git a/compiler/rustc_mir_transform/src/const_prop.rs b/compiler/rustc_mir_transform/src/const_prop.rs index b3a3a25ebe8..7f995c69a48 100644 --- a/compiler/rustc_mir_transform/src/const_prop.rs +++ b/compiler/rustc_mir_transform/src/const_prop.rs @@ -714,13 +714,22 @@ impl CanConstProp { } } -impl Visitor<'_> for CanConstProp { +impl<'tcx> Visitor<'tcx> for CanConstProp { + fn visit_place(&mut self, place: &Place<'tcx>, mut context: PlaceContext, loc: Location) { + use rustc_middle::mir::visit::PlaceContext::*; + + // Dereferencing just read the addess of `place.local`. + if place.projection.first() == Some(&PlaceElem::Deref) { + context = NonMutatingUse(NonMutatingUseContext::Copy); + } + + self.visit_local(place.local, context, loc); + self.visit_projection(place.as_ref(), context, loc); + } + fn visit_local(&mut self, local: Local, context: PlaceContext, _: Location) { use rustc_middle::mir::visit::PlaceContext::*; match context { - // Projections are fine, because `&mut foo.x` will be caught by - // `MutatingUseContext::Borrow` elsewhere. - MutatingUse(MutatingUseContext::Projection) // These are just stores, where the storing is not propagatable, but there may be later // mutations of the same local via `Store` | MutatingUse(MutatingUseContext::Call) @@ -751,7 +760,6 @@ impl Visitor<'_> for CanConstProp { NonMutatingUse(NonMutatingUseContext::Copy) | NonMutatingUse(NonMutatingUseContext::Move) | NonMutatingUse(NonMutatingUseContext::Inspect) - | NonMutatingUse(NonMutatingUseContext::Projection) | NonMutatingUse(NonMutatingUseContext::PlaceMention) | NonUse(_) => {} @@ -771,6 +779,8 @@ impl Visitor<'_> for CanConstProp { trace!("local {:?} can't be propagated because it's used: {:?}", local, context); self.can_const_prop[local] = ConstPropMode::NoPropagation; } + MutatingUse(MutatingUseContext::Projection) + | NonMutatingUse(NonMutatingUseContext::Projection) => bug!("visit_place should not pass {context:?} for {local:?}"), } } } |
