Skip to content

Commit 6c20ab7

Browse files
committed
Auto merge of rust-lang#99082 - matthiaskrgr:rollup-nouwsh7, r=matthiaskrgr
Rollup of 3 pull requests Successful merges: - rust-lang#99022 (MIR dataflow: Rename function to `always_storage_live_locals`) - rust-lang#99050 (Clarify MIR semantics of storage statements) - rust-lang#99067 (Intra-doc-link-ify reference to Clone::clone_from) Failed merges: r? `@ghost` `@rustbot` modify labels: rollup
2 parents 73443a0 + e89ed4f commit 6c20ab7

File tree

6 files changed

+28
-22
lines changed

6 files changed

+28
-22
lines changed

compiler/rustc_const_eval/src/interpret/eval_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::layout::{
1515
use rustc_middle::ty::{
1616
self, query::TyCtxtAt, subst::SubstsRef, ParamEnv, Ty, TyCtxt, TypeFoldable,
1717
};
18-
use rustc_mir_dataflow::storage::always_live_locals;
18+
use rustc_mir_dataflow::storage::always_storage_live_locals;
1919
use rustc_query_system::ich::StableHashingContext;
2020
use rustc_session::Limit;
2121
use rustc_span::{Pos, Span};
@@ -707,7 +707,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
707707
let mut locals = IndexVec::from_elem(dummy, &body.local_decls);
708708

709709
// Now mark those locals as live that have no `Storage*` annotations.
710-
let always_live = always_live_locals(self.body());
710+
let always_live = always_storage_live_locals(self.body());
711711
for local in locals.indices() {
712712
if always_live.contains(local) {
713713
locals[local].value = LocalValue::Live(Operand::Immediate(Immediate::Uninit));

compiler/rustc_const_eval/src/transform/validate.rs

+9-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ use rustc_middle::ty::fold::BottomUpFolder;
1515
use rustc_middle::ty::subst::Subst;
1616
use rustc_middle::ty::{self, InstanceDef, ParamEnv, Ty, TyCtxt, TypeFoldable, TypeVisitable};
1717
use rustc_mir_dataflow::impls::MaybeStorageLive;
18-
use rustc_mir_dataflow::storage::always_live_locals;
18+
use rustc_mir_dataflow::storage::always_storage_live_locals;
1919
use rustc_mir_dataflow::{Analysis, ResultsCursor};
2020
use rustc_target::abi::{Size, VariantIdx};
2121

@@ -49,7 +49,7 @@ impl<'tcx> MirPass<'tcx> for Validator {
4949
let param_env = tcx.param_env(def_id);
5050
let mir_phase = self.mir_phase;
5151

52-
let always_live_locals = always_live_locals(body);
52+
let always_live_locals = always_storage_live_locals(body);
5353
let storage_liveness = MaybeStorageLive::new(always_live_locals)
5454
.into_engine(tcx, body)
5555
.iterate_to_fixpoint()
@@ -206,7 +206,13 @@ impl<'a, 'tcx> Visitor<'tcx> for TypeChecker<'a, 'tcx> {
206206
}
207207

208208
if self.reachable_blocks.contains(location.block) && context.is_use() {
209-
// Uses of locals must occur while the local's storage is allocated.
209+
// We check that the local is live whenever it is used. Technically, violating this
210+
// restriction is only UB and not actually indicative of not well-formed MIR. This means
211+
// that an optimization which turns MIR that already has UB into MIR that fails this
212+
// check is not necessarily wrong. However, we have no such optimizations at the moment,
213+
// and so we include this check anyway to help us catch bugs. If you happen to write an
214+
// optimization that might cause this to incorrectly fire, feel free to remove this
215+
// check.
210216
self.storage_liveness.seek_after_primary_effect(location);
211217
let locals_with_storage = self.storage_liveness.get();
212218
if !locals_with_storage.contains(local) {

compiler/rustc_middle/src/mir/syntax.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -237,19 +237,19 @@ pub enum StatementKind<'tcx> {
237237

238238
/// `StorageLive` and `StorageDead` statements mark the live range of a local.
239239
///
240-
/// Using a local before a `StorageLive` or after a `StorageDead` is not well-formed. These
241-
/// statements are not required. If the entire MIR body contains no `StorageLive`/`StorageDead`
242-
/// statements for a particular local, the local is always considered live.
243-
///
244-
/// More precisely, the MIR validator currently does a `MaybeStorageLiveLocals` analysis to
245-
/// check validity of each use of a local. I believe this is equivalent to requiring for every
246-
/// use of a local, there exist at least one path from the root to that use that contains a
247-
/// `StorageLive` more recently than a `StorageDead`.
248-
///
249-
/// **Needs clarification**: Is it permitted to have two `StorageLive`s without an intervening
250-
/// `StorageDead`? Two `StorageDead`s without an intervening `StorageLive`? LLVM says poison,
251-
/// yes. If the answer to any of these is "no," is breaking that rule UB or is it an error to
252-
/// have a path in the CFG that might do this?
240+
/// At any point during the execution of a function, each local is either allocated or
241+
/// unallocated. Except as noted below, all locals except function parameters are initially
242+
/// unallocated. `StorageLive` statements cause memory to be allocated for the local while
243+
/// `StorageDead` statements cause the memory to be freed. Using a local in any way (not only
244+
/// reading/writing from it) while it is unallocated is UB.
245+
///
246+
/// Some locals have no `StorageLive` or `StorageDead` statements within the entire MIR body.
247+
/// These locals are implicitly allocated for the full duration of the function. There is a
248+
/// convenience method at `rustc_mir_dataflow::storage::always_storage_live_locals` for
249+
/// computing these locals.
250+
///
251+
/// If the local is already allocated, calling `StorageLive` again is UB. However, for an
252+
/// unallocated local an additional `StorageDead` all is simply a nop.
253253
StorageLive(Local),
254254

255255
/// See `StorageLive` above.

compiler/rustc_mir_dataflow/src/storage.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ use rustc_middle::mir::{self, Local};
77
//
88
// FIXME: Currently, we need to traverse the entire MIR to compute this. We should instead store it
99
// as a field in the `LocalDecl` for each `Local`.
10-
pub fn always_live_locals(body: &mir::Body<'_>) -> BitSet<Local> {
10+
pub fn always_storage_live_locals(body: &mir::Body<'_>) -> BitSet<Local> {
1111
let mut always_live_locals = BitSet::new_filled(body.local_decls.len());
1212

1313
for block in body.basic_blocks() {

compiler/rustc_mir_transform/src/generator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ use rustc_middle::ty::{self, AdtDef, Ty, TyCtxt};
6767
use rustc_mir_dataflow::impls::{
6868
MaybeBorrowedLocals, MaybeLiveLocals, MaybeRequiresStorage, MaybeStorageLive,
6969
};
70-
use rustc_mir_dataflow::storage;
70+
use rustc_mir_dataflow::storage::always_storage_live_locals;
7171
use rustc_mir_dataflow::{self, Analysis};
7272
use rustc_target::abi::VariantIdx;
7373
use rustc_target::spec::PanicStrategy;
@@ -1379,7 +1379,7 @@ impl<'tcx> MirPass<'tcx> for StateTransform {
13791379
},
13801380
);
13811381

1382-
let always_live_locals = storage::always_live_locals(&body);
1382+
let always_live_locals = always_storage_live_locals(&body);
13831383

13841384
let liveness_info =
13851385
locals_live_across_suspend_points(tcx, body, &always_live_locals, movable);

library/alloc/src/borrow.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ pub trait ToOwned {
6060

6161
/// Uses borrowed data to replace owned data, usually by cloning.
6262
///
63-
/// This is borrow-generalized version of `Clone::clone_from`.
63+
/// This is borrow-generalized version of [`Clone::clone_from`].
6464
///
6565
/// # Examples
6666
///

0 commit comments

Comments
 (0)