Skip to content

Commit 7222506

Browse files
committed
clarify current MIR semantics re: overlapping assignment
and double-check that we match it in codegen
1 parent 8ecda4b commit 7222506

File tree

2 files changed

+15
-4
lines changed

2 files changed

+15
-4
lines changed

compiler/rustc_codegen_ssa/src/mir/rvalue.rs

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use itertools::Itertools as _;
2-
use rustc_abi::{self as abi, FIRST_VARIANT};
2+
use rustc_abi::{self as abi, BackendRepr, FIRST_VARIANT};
33
use rustc_middle::ty::adjustment::PointerCoercion;
44
use rustc_middle::ty::layout::{HasTyCtxt, HasTypingEnv, LayoutOf, TyAndLayout};
55
use rustc_middle::ty::{self, Instance, Ty, TyCtxt};
@@ -25,6 +25,15 @@ impl<'a, 'tcx, Bx: BuilderMethods<'a, 'tcx>> FunctionCx<'a, 'tcx, Bx> {
2525
match *rvalue {
2626
mir::Rvalue::Use(ref operand) => {
2727
let cg_operand = self.codegen_operand(bx, operand);
28+
// Crucially, we do *not* use `OperandValue::Ref` for types with
29+
// `BackendRepr::Scalar | BackendRepr::ScalarPair`. This ensures we match the MIR
30+
// semantics regarding when assignment operators allow overlap of LHS and RHS.
31+
if matches!(
32+
cg_operand.layout.backend_repr,
33+
BackendRepr::Scalar(..) | BackendRepr::ScalarPair(..),
34+
) {
35+
debug_assert!(!matches!(cg_operand.val, OperandValue::Ref(..)));
36+
}
2837
// FIXME: consider not copying constants through stack. (Fixable by codegen'ing
2938
// constants into `OperandValue::Ref`; why don’t we do that yet if we don’t?)
3039
cg_operand.val.store(bx, dest);

compiler/rustc_middle/src/mir/syntax.rs

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -327,9 +327,11 @@ pub enum StatementKind<'tcx> {
327327
/// interesting for optimizations? Do we want to allow such optimizations?
328328
///
329329
/// **Needs clarification**: We currently require that the LHS place not overlap with any place
330-
/// read as part of computation of the RHS for some rvalues (generally those not producing
331-
/// primitives). This requirement is under discussion in [#68364]. As a part of this discussion,
332-
/// it is also unclear in what order the components are evaluated.
330+
/// read as part of computation of the RHS for some rvalues. This requirement is under
331+
/// discussion in [#68364]. Specifically, overlap is permitted only for assignments of a type
332+
/// with `BackendRepr::Scalar | BackendRepr::ScalarPair` where all the scalar fields are
333+
/// [`Scalar::Initialized`][rustc_abi::Scalar::Initialized]. As a part of this discussion, it is
334+
/// also unclear in what order the components are evaluated.
333335
///
334336
/// [#68364]: https://github.com/rust-lang/rust/issues/68364
335337
///

0 commit comments

Comments
 (0)