Skip to content

Commit 213a25d

Browse files
committed
interpret: make some large types not Copy
1 parent 388971b commit 213a25d

File tree

9 files changed

+45
-39
lines changed

9 files changed

+45
-39
lines changed

Diff for: compiler/rustc_const_eval/src/interpret/eval_context.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -674,7 +674,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
674674
body,
675675
loc: Err(body.span), // Span used for errors caused during preamble.
676676
return_to_block,
677-
return_place: *return_place,
677+
return_place: return_place.clone(),
678678
// empty local array, we fill it in below, after we are inside the stack frame and
679679
// all methods actually know about the frame
680680
locals: IndexVec::new(),
@@ -795,7 +795,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
795795
let op = self
796796
.local_to_op(self.frame(), mir::RETURN_PLACE, None)
797797
.expect("return place should always be live");
798-
let dest = self.frame().return_place;
798+
let dest = self.frame().return_place.clone();
799799
let err = self.copy_op(&op, &dest, /*allow_transmute*/ true);
800800
trace!("return value: {:?}", self.dump_place(*dest));
801801
// We delay actually short-circuiting on this error until *after* the stack frame is

Diff for: compiler/rustc_const_eval/src/interpret/intrinsics.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -457,8 +457,11 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
457457

458458
for i in 0..dest_len {
459459
let place = self.mplace_index(&dest, i)?;
460-
let value =
461-
if i == index { *elem } else { self.mplace_index(&input, i)?.into() };
460+
let value = if i == index {
461+
elem.clone()
462+
} else {
463+
self.mplace_index(&input, i)?.into()
464+
};
462465
self.copy_op(&value, &place.into(), /*allow_transmute*/ false)?;
463466
}
464467
}

Diff for: compiler/rustc_const_eval/src/interpret/operand.rs

+5-2
Original file line numberDiff line numberDiff line change
@@ -111,7 +111,7 @@ impl<'tcx, Tag: Provenance> Immediate<Tag> {
111111

112112
// ScalarPair needs a type to interpret, so we often have an immediate and a type together
113113
// as input for binary and cast operations.
114-
#[derive(Copy, Clone, Debug)]
114+
#[derive(Clone, Debug)]
115115
pub struct ImmTy<'tcx, Tag: Provenance = AllocId> {
116116
imm: Immediate<Tag>,
117117
pub layout: TyAndLayout<'tcx>,
@@ -187,7 +187,10 @@ pub enum Operand<Tag: Provenance = AllocId> {
187187
Indirect(MemPlace<Tag>),
188188
}
189189

190-
#[derive(Copy, Clone, Debug)]
190+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
191+
rustc_data_structures::static_assert_size!(Operand, 64);
192+
193+
#[derive(Clone, Debug)]
191194
pub struct OpTy<'tcx, Tag: Provenance = AllocId> {
192195
op: Operand<Tag>, // Keep this private; it helps enforce invariants.
193196
pub layout: TyAndLayout<'tcx>,

Diff for: compiler/rustc_const_eval/src/interpret/place.rs

+16-16
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,21 @@ pub struct MemPlace<Tag: Provenance = AllocId> {
5959
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
6060
rustc_data_structures::static_assert_size!(MemPlace, 40);
6161

62+
/// A MemPlace with its layout. Constructing it is only possible in this module.
63+
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
64+
pub struct MPlaceTy<'tcx, Tag: Provenance = AllocId> {
65+
mplace: MemPlace<Tag>,
66+
pub layout: TyAndLayout<'tcx>,
67+
/// rustc does not have a proper way to represent the type of a field of a `repr(packed)` struct:
68+
/// it needs to have a different alignment than the field type would usually have.
69+
/// So we represent this here with a separate field that "overwrites" `layout.align`.
70+
/// This means `layout.align` should never be used for a `MPlaceTy`!
71+
pub align: Align,
72+
}
73+
74+
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
75+
rustc_data_structures::static_assert_size!(MPlaceTy<'_>, 64);
76+
6277
#[derive(Copy, Clone, Debug)]
6378
pub enum Place<Tag: Provenance = AllocId> {
6479
/// A place referring to a value allocated in the `Memory` system.
@@ -72,7 +87,7 @@ pub enum Place<Tag: Provenance = AllocId> {
7287
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
7388
rustc_data_structures::static_assert_size!(Place, 48);
7489

75-
#[derive(Copy, Clone, Debug)]
90+
#[derive(Clone, Debug)]
7691
pub struct PlaceTy<'tcx, Tag: Provenance = AllocId> {
7792
place: Place<Tag>, // Keep this private; it helps enforce invariants.
7893
pub layout: TyAndLayout<'tcx>,
@@ -94,21 +109,6 @@ impl<'tcx, Tag: Provenance> std::ops::Deref for PlaceTy<'tcx, Tag> {
94109
}
95110
}
96111

97-
/// A MemPlace with its layout. Constructing it is only possible in this module.
98-
#[derive(Copy, Clone, Hash, Eq, PartialEq, Debug)]
99-
pub struct MPlaceTy<'tcx, Tag: Provenance = AllocId> {
100-
mplace: MemPlace<Tag>,
101-
pub layout: TyAndLayout<'tcx>,
102-
/// rustc does not have a proper way to represent the type of a field of a `repr(packed)` struct:
103-
/// it needs to have a different alignment than the field type would usually have.
104-
/// So we represent this here with a separate field that "overwrites" `layout.align`.
105-
/// This means `layout.align` should never be used for a `MPlaceTy`!
106-
pub align: Align,
107-
}
108-
109-
#[cfg(all(target_arch = "x86_64", target_pointer_width = "64"))]
110-
rustc_data_structures::static_assert_size!(MPlaceTy<'_>, 64);
111-
112112
impl<'tcx, Tag: Provenance> std::ops::Deref for MPlaceTy<'tcx, Tag> {
113113
type Target = MemPlace<Tag>;
114114
#[inline(always)]

Diff for: compiler/rustc_const_eval/src/interpret/projection.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -157,7 +157,7 @@ where
157157
variant: VariantIdx,
158158
) -> InterpResult<'tcx, PlaceTy<'tcx, M::PointerTag>> {
159159
// Downcast just changes the layout
160-
let mut base = *base;
160+
let mut base = base.clone();
161161
base.layout = base.layout.for_variant(self, variant);
162162
Ok(base)
163163
}
@@ -168,7 +168,7 @@ where
168168
variant: VariantIdx,
169169
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
170170
// Downcast just changes the layout
171-
let mut base = *base;
171+
let mut base = base.clone();
172172
base.layout = base.layout.for_variant(self, variant);
173173
Ok(base)
174174
}
@@ -350,7 +350,7 @@ where
350350
use rustc_middle::mir::ProjectionElem::*;
351351
Ok(match proj_elem {
352352
OpaqueCast(ty) => {
353-
let mut place = *base;
353+
let mut place = base.clone();
354354
place.layout = self.layout_of(ty)?;
355355
place
356356
}
@@ -379,7 +379,7 @@ where
379379
use rustc_middle::mir::ProjectionElem::*;
380380
Ok(match proj_elem {
381381
OpaqueCast(ty) => {
382-
let mut op = *base;
382+
let mut op = base.clone();
383383
op.layout = self.layout_of(ty)?;
384384
op
385385
}

Diff for: compiler/rustc_const_eval/src/interpret/terminator.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -444,7 +444,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
444444
trace!("eval_fn_call: Will pass last argument by untupling");
445445
Cow::from(
446446
args.iter()
447-
.map(|&a| Ok(a))
447+
.map(|a| Ok(a.clone()))
448448
.chain(
449449
(0..untuple_arg.layout.fields.count())
450450
.map(|i| self.operand_field(untuple_arg, i)),
@@ -525,7 +525,7 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> InterpCx<'mir, 'tcx, M> {
525525
// We have to implement all "object safe receivers". So we have to go search for a
526526
// pointer or `dyn Trait` type, but it could be wrapped in newtypes. So recursively
527527
// unwrap those newtypes until we are there.
528-
let mut receiver = args[0];
528+
let mut receiver = args[0].clone();
529529
let receiver_place = loop {
530530
match receiver.layout.ty.kind() {
531531
ty::Ref(..) | ty::RawPtr(..) => break self.deref_operand(&receiver)?,

Diff for: compiler/rustc_const_eval/src/interpret/visitor.rs

+7-7
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use super::{InterpCx, MPlaceTy, Machine, OpTy, PlaceTy};
1313
/// A thing that we can project into, and that has a layout.
1414
/// This wouldn't have to depend on `Machine` but with the current type inference,
1515
/// that's just more convenient to work with (avoids repeating all the `Machine` bounds).
16-
pub trait Value<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Copy {
16+
pub trait Value<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Sized {
1717
/// Gets this value's layout.
1818
fn layout(&self) -> TyAndLayout<'tcx>;
1919

@@ -54,7 +54,7 @@ pub trait Value<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Copy {
5454
/// A thing that we can project into given *mutable* access to `ecx`, and that has a layout.
5555
/// This wouldn't have to depend on `Machine` but with the current type inference,
5656
/// that's just more convenient to work with (avoids repeating all the `Machine` bounds).
57-
pub trait ValueMut<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Copy {
57+
pub trait ValueMut<'mir, 'tcx, M: Machine<'mir, 'tcx>>: Sized {
5858
/// Gets this value's layout.
5959
fn layout(&self) -> TyAndLayout<'tcx>;
6060

@@ -106,12 +106,12 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> Value<'mir, 'tcx, M> for OpTy<'tc
106106
&self,
107107
_ecx: &InterpCx<'mir, 'tcx, M>,
108108
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
109-
Ok(*self)
109+
Ok(self.clone())
110110
}
111111

112112
#[inline(always)]
113113
fn from_op(op: &OpTy<'tcx, M::PointerTag>) -> Self {
114-
*op
114+
op.clone()
115115
}
116116

117117
#[inline(always)]
@@ -146,20 +146,20 @@ impl<'mir, 'tcx: 'mir, M: Machine<'mir, 'tcx>> ValueMut<'mir, 'tcx, M>
146146
&self,
147147
_ecx: &InterpCx<'mir, 'tcx, M>,
148148
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
149-
Ok(*self)
149+
Ok(self.clone())
150150
}
151151

152152
#[inline(always)]
153153
fn to_op_for_proj(
154154
&self,
155155
_ecx: &mut InterpCx<'mir, 'tcx, M>,
156156
) -> InterpResult<'tcx, OpTy<'tcx, M::PointerTag>> {
157-
Ok(*self)
157+
Ok(self.clone())
158158
}
159159

160160
#[inline(always)]
161161
fn from_op(op: &OpTy<'tcx, M::PointerTag>) -> Self {
162-
*op
162+
op.clone()
163163
}
164164

165165
#[inline(always)]

Diff for: compiler/rustc_mir_transform/src/const_prop.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -516,7 +516,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
516516
let l = self.use_ecx(|this| this.ecx.read_immediate(&this.ecx.eval_operand(left, None)?));
517517
// Check for exceeding shifts *even if* we cannot evaluate the LHS.
518518
if op == BinOp::Shr || op == BinOp::Shl {
519-
let r = r?;
519+
let r = r.clone()?;
520520
// We need the type of the LHS. We cannot use `place_layout` as that is the type
521521
// of the result, which for checked binops is not the same!
522522
let left_ty = left.ty(self.local_decls, self.tcx);

Diff for: compiler/rustc_mir_transform/src/const_prop_lint.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -584,7 +584,7 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
584584
});
585585
// Check for exceeding shifts *even if* we cannot evaluate the LHS.
586586
if op == BinOp::Shr || op == BinOp::Shl {
587-
let r = r?;
587+
let r = r.clone()?;
588588
// We need the type of the LHS. We cannot use `place_layout` as that is the type
589589
// of the result, which for checked binops is not the same!
590590
let left_ty = left.ty(self.local_decls, self.tcx);
@@ -616,10 +616,10 @@ impl<'mir, 'tcx> ConstPropagator<'mir, 'tcx> {
616616
}
617617
}
618618

619-
if let (Some(l), Some(r)) = (&l, &r) {
619+
if let (Some(l), Some(r)) = (l, r) {
620620
// The remaining operators are handled through `overflowing_binary_op`.
621621
if self.use_ecx(source_info, |this| {
622-
let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, l, r)?;
622+
let (_res, overflow, _ty) = this.ecx.overflowing_binary_op(op, &l, &r)?;
623623
Ok(overflow)
624624
})? {
625625
self.report_assert_as_lint(

0 commit comments

Comments
 (0)