Skip to content

Commit 0ac8ebd

Browse files
authored
Rollup merge of #110826 - cjgillot:place-mention-use, r=JakobDegen,lcnr
Make PlaceMention a non-mutating use. Fixes #110781 r? `@JakobDegen` I don't agree with your statement in #110781 (comment). I suggest that we start fixing `PlaceContext` to be accurate enough for optimizations to use it. This structure is very convenient to use in visitors, and we perhaps have an opportunity to make it less of a footgun.
2 parents bf72b64 + 4ec76df commit 0ac8ebd

File tree

8 files changed

+49
-10
lines changed

8 files changed

+49
-10
lines changed

compiler/rustc_borrowck/src/def_use.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ pub fn categorize(context: PlaceContext) -> Option<DefUse> {
5454

5555
// `PlaceMention` and `AscribeUserType` both evaluate the place, which must not
5656
// contain dangling references.
57-
PlaceContext::NonUse(NonUseContext::PlaceMention) |
57+
PlaceContext::NonMutatingUse(NonMutatingUseContext::PlaceMention) |
5858
PlaceContext::NonUse(NonUseContext::AscribeUserTy) |
5959

6060
PlaceContext::MutatingUse(MutatingUseContext::AddressOf) |

compiler/rustc_borrowck/src/type_check/mod.rs

+3-5
Original file line numberDiff line numberDiff line change
@@ -772,12 +772,10 @@ impl<'a, 'b, 'tcx> TypeVerifier<'a, 'b, 'tcx> {
772772

773773
match context {
774774
PlaceContext::MutatingUse(_) => ty::Invariant,
775-
PlaceContext::NonUse(StorageDead | StorageLive | PlaceMention | VarDebugInfo) => {
776-
ty::Invariant
777-
}
775+
PlaceContext::NonUse(StorageDead | StorageLive | VarDebugInfo) => ty::Invariant,
778776
PlaceContext::NonMutatingUse(
779-
Inspect | Copy | Move | SharedBorrow | ShallowBorrow | UniqueBorrow | AddressOf
780-
| Projection,
777+
Inspect | Copy | Move | PlaceMention | SharedBorrow | ShallowBorrow | UniqueBorrow
778+
| AddressOf | Projection,
781779
) => ty::Covariant,
782780
PlaceContext::NonUse(AscribeUserTy) => ty::Covariant,
783781
}

compiler/rustc_codegen_ssa/src/mir/analyze.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -203,7 +203,9 @@ impl<'mir, 'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> Visitor<'tcx>
203203
self.assign(local, DefLocation::Body(location));
204204
}
205205

206-
PlaceContext::NonUse(_) | PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
206+
PlaceContext::NonUse(_)
207+
| PlaceContext::NonMutatingUse(NonMutatingUseContext::PlaceMention)
208+
| PlaceContext::MutatingUse(MutatingUseContext::Retag) => {}
207209

208210
PlaceContext::NonMutatingUse(
209211
NonMutatingUseContext::Copy | NonMutatingUseContext::Move,

compiler/rustc_middle/src/mir/visit.rs

+6-3
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ macro_rules! make_mir_visitor {
410410
StatementKind::PlaceMention(place) => {
411411
self.visit_place(
412412
place,
413-
PlaceContext::NonUse(NonUseContext::PlaceMention),
413+
PlaceContext::NonMutatingUse(NonMutatingUseContext::PlaceMention),
414414
location
415415
);
416416
}
@@ -1251,6 +1251,11 @@ pub enum NonMutatingUseContext {
12511251
UniqueBorrow,
12521252
/// AddressOf for *const pointer.
12531253
AddressOf,
1254+
/// PlaceMention statement.
1255+
///
1256+
/// This statement is executed as a check that the `Place` is live without reading from it,
1257+
/// so it must be considered as a non-mutating use.
1258+
PlaceMention,
12541259
/// Used as base for another place, e.g., `x` in `x.y`. Will not mutate the place.
12551260
/// For example, the projection `x.y` is not marked as a mutation in these cases:
12561261
/// ```ignore (illustrative)
@@ -1301,8 +1306,6 @@ pub enum NonUseContext {
13011306
AscribeUserTy,
13021307
/// The data of a user variable, for debug info.
13031308
VarDebugInfo,
1304-
/// PlaceMention statement.
1305-
PlaceMention,
13061309
}
13071310

13081311
#[derive(Copy, Clone, Debug, PartialEq, Eq)]

compiler/rustc_mir_dataflow/src/impls/liveness.rs

+1
Original file line numberDiff line numberDiff line change
@@ -197,6 +197,7 @@ impl DefUse {
197197
| NonMutatingUseContext::Copy
198198
| NonMutatingUseContext::Inspect
199199
| NonMutatingUseContext::Move
200+
| NonMutatingUseContext::PlaceMention
200201
| NonMutatingUseContext::ShallowBorrow
201202
| NonMutatingUseContext::SharedBorrow
202203
| NonMutatingUseContext::UniqueBorrow,

compiler/rustc_mir_transform/src/const_prop.rs

+1
Original file line numberDiff line numberDiff line change
@@ -752,6 +752,7 @@ impl Visitor<'_> for CanConstProp {
752752
| NonMutatingUse(NonMutatingUseContext::Move)
753753
| NonMutatingUse(NonMutatingUseContext::Inspect)
754754
| NonMutatingUse(NonMutatingUseContext::Projection)
755+
| NonMutatingUse(NonMutatingUseContext::PlaceMention)
755756
| NonUse(_) => {}
756757

757758
// These could be propagated with a smarter analysis or just some careful thinking about
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
- // MIR for `main` before DeadStoreElimination
2+
+ // MIR for `main` after DeadStoreElimination
3+
4+
fn main() -> () {
5+
let mut _0: (); // return place in scope 0 at $DIR/place_mention.rs:+0:11: +0:11
6+
let mut _1: (&str, &str); // in scope 0 at $DIR/place_mention.rs:+3:18: +3:36
7+
scope 1 {
8+
}
9+
10+
bb0: {
11+
StorageLive(_1); // scope 0 at $DIR/place_mention.rs:+3:18: +3:36
12+
_1 = (const "Hello", const "World"); // scope 0 at $DIR/place_mention.rs:+3:18: +3:36
13+
// mir::Constant
14+
// + span: $DIR/place_mention.rs:8:19: 8:26
15+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
16+
// mir::Constant
17+
// + span: $DIR/place_mention.rs:8:28: 8:35
18+
// + literal: Const { ty: &str, val: Value(Slice(..)) }
19+
PlaceMention(_1); // scope 0 at $DIR/place_mention.rs:+3:18: +3:36
20+
StorageDead(_1); // scope 0 at $DIR/place_mention.rs:+3:36: +3:37
21+
_0 = const (); // scope 0 at $DIR/place_mention.rs:+0:11: +4:2
22+
return; // scope 0 at $DIR/place_mention.rs:+4:2: +4:2
23+
}
24+
}
25+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
// unit-test: DeadStoreElimination
2+
// compile-flags: -Zmir-keep-place-mention
3+
4+
// EMIT_MIR place_mention.main.DeadStoreElimination.diff
5+
fn main() {
6+
// Verify that we account for the `PlaceMention` statement as a use of the tuple,
7+
// and don't remove it as a dead store.
8+
let (_, _) = ("Hello", "World");
9+
}

0 commit comments

Comments
 (0)