Skip to content

Commit ab619d5

Browse files
committed
Auto merge of #2991 - rust-lang:rustup-2023-07-26, r=RalfJung
Automatic sync from rustc
2 parents c26a7c5 + 58b7147 commit ab619d5

27 files changed

+151
-159
lines changed

rust-version

+1-1
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
cec34a43b1b14f4e39363f3b283d7ac4f593ee81
1+
98db99f5f6273d95497dd83d1b3a62c2c00292b1

src/borrow_tracker/stacked_borrows/mod.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -928,13 +928,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
928928
Ok(())
929929
}
930930
}
931-
impl<'ecx, 'mir, 'tcx> MutValueVisitor<'mir, 'tcx, MiriMachine<'mir, 'tcx>>
931+
impl<'ecx, 'mir, 'tcx> ValueVisitor<'mir, 'tcx, MiriMachine<'mir, 'tcx>>
932932
for RetagVisitor<'ecx, 'mir, 'tcx>
933933
{
934934
type V = PlaceTy<'tcx, Provenance>;
935935

936936
#[inline(always)]
937-
fn ecx(&mut self) -> &mut MiriInterpCx<'mir, 'tcx> {
937+
fn ecx(&self) -> &MiriInterpCx<'mir, 'tcx> {
938938
self.ecx
939939
}
940940

src/borrow_tracker/tree_borrows/mod.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -413,13 +413,13 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
413413
Ok(())
414414
}
415415
}
416-
impl<'ecx, 'mir, 'tcx> MutValueVisitor<'mir, 'tcx, MiriMachine<'mir, 'tcx>>
416+
impl<'ecx, 'mir, 'tcx> ValueVisitor<'mir, 'tcx, MiriMachine<'mir, 'tcx>>
417417
for RetagVisitor<'ecx, 'mir, 'tcx>
418418
{
419419
type V = PlaceTy<'tcx, Provenance>;
420420

421421
#[inline(always)]
422-
fn ecx(&mut self) -> &mut MiriInterpCx<'mir, 'tcx> {
422+
fn ecx(&self) -> &MiriInterpCx<'mir, 'tcx> {
423423
self.ecx
424424
}
425425

@@ -578,22 +578,22 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
578578
/// I.e. input is what you get from the visitor upon encountering an `adt` that is `Unique`,
579579
/// and output can be used by `retag_ptr_inplace`.
580580
fn inner_ptr_of_unique<'tcx>(
581-
ecx: &mut MiriInterpCx<'_, 'tcx>,
581+
ecx: &MiriInterpCx<'_, 'tcx>,
582582
place: &PlaceTy<'tcx, Provenance>,
583583
) -> InterpResult<'tcx, PlaceTy<'tcx, Provenance>> {
584584
// Follows the same layout as `interpret/visitor.rs:walk_value` for `Box` in
585585
// `rustc_const_eval`, just with one fewer layer.
586586
// Here we have a `Unique(NonNull(*mut), PhantomData)`
587587
assert_eq!(place.layout.fields.count(), 2, "Unique must have exactly 2 fields");
588-
let (nonnull, phantom) = (ecx.place_field(place, 0)?, ecx.place_field(place, 1)?);
588+
let (nonnull, phantom) = (ecx.project_field(place, 0)?, ecx.project_field(place, 1)?);
589589
assert!(
590590
phantom.layout.ty.ty_adt_def().is_some_and(|adt| adt.is_phantom_data()),
591591
"2nd field of `Unique` should be `PhantomData` but is `{:?}`",
592592
phantom.layout.ty,
593593
);
594594
// Now down to `NonNull(*mut)`
595595
assert_eq!(nonnull.layout.fields.count(), 1, "NonNull must have exactly 1 field");
596-
let ptr = ecx.place_field(&nonnull, 0)?;
596+
let ptr = ecx.project_field(&nonnull, 0)?;
597597
// Finally a plain `*mut`
598598
Ok(ptr)
599599
}

src/eval.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -320,7 +320,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
320320
))?;
321321
let argvs_place = ecx.allocate(argvs_layout, MiriMemoryKind::Machine.into())?;
322322
for (idx, arg) in argvs.into_iter().enumerate() {
323-
let place = ecx.mplace_field(&argvs_place, idx)?;
323+
let place = ecx.project_field(&argvs_place, idx)?;
324324
ecx.write_immediate(arg, &place.into())?;
325325
}
326326
ecx.mark_immutable(&argvs_place);
@@ -354,7 +354,7 @@ pub fn create_ecx<'mir, 'tcx: 'mir>(
354354
ecx.machine.cmd_line = Some(*cmd_place);
355355
// Store the UTF-16 string. We just allocated so we know the bounds are fine.
356356
for (idx, &c) in cmd_utf16.iter().enumerate() {
357-
let place = ecx.mplace_field(&cmd_place, idx)?;
357+
let place = ecx.project_field(&cmd_place, idx)?;
358358
ecx.write_scalar(Scalar::from_u16(c), &place.into())?;
359359
}
360360
ecx.mark_immutable(&cmd_place);

src/helpers.rs

+27-42
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,15 @@ use log::trace;
1010

1111
use rustc_hir::def::{DefKind, Namespace};
1212
use rustc_hir::def_id::{DefId, CRATE_DEF_INDEX};
13+
use rustc_index::IndexVec;
1314
use rustc_middle::mir;
1415
use rustc_middle::ty::{
1516
self,
1617
layout::{LayoutOf, TyAndLayout},
1718
List, TyCtxt,
1819
};
1920
use rustc_span::{def_id::CrateNum, sym, Span, Symbol};
20-
use rustc_target::abi::{Align, FieldsShape, Size, Variants};
21+
use rustc_target::abi::{Align, FieldIdx, FieldsShape, Size, Variants};
2122
use rustc_target::spec::abi::Abi;
2223

2324
use rand::RngCore;
@@ -229,20 +230,20 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
229230
this.layout_of(ty).unwrap()
230231
}
231232

232-
/// Project to the given *named* field of the mplace (which must be a struct or union type).
233-
fn mplace_field_named(
233+
/// Project to the given *named* field (which must be a struct or union type).
234+
fn project_field_named<P: Projectable<'mir, 'tcx, Provenance>>(
234235
&self,
235-
mplace: &MPlaceTy<'tcx, Provenance>,
236+
base: &P,
236237
name: &str,
237-
) -> InterpResult<'tcx, MPlaceTy<'tcx, Provenance>> {
238+
) -> InterpResult<'tcx, P> {
238239
let this = self.eval_context_ref();
239-
let adt = mplace.layout.ty.ty_adt_def().unwrap();
240+
let adt = base.layout().ty.ty_adt_def().unwrap();
240241
for (idx, field) in adt.non_enum_variant().fields.iter().enumerate() {
241242
if field.name.as_str() == name {
242-
return this.mplace_field(mplace, idx);
243+
return this.project_field(base, idx);
243244
}
244245
}
245-
bug!("No field named {} in type {}", name, mplace.layout.ty);
246+
bug!("No field named {} in type {}", name, base.layout().ty);
246247
}
247248

248249
/// Write an int of the appropriate size to `dest`. The target type may be signed or unsigned,
@@ -270,7 +271,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
270271
) -> InterpResult<'tcx> {
271272
let this = self.eval_context_mut();
272273
for (idx, &val) in values.iter().enumerate() {
273-
let field = this.mplace_field(dest, idx)?;
274+
let field = this.project_field(dest, idx)?;
274275
this.write_int(val, &field.into())?;
275276
}
276277
Ok(())
@@ -284,7 +285,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
284285
) -> InterpResult<'tcx> {
285286
let this = self.eval_context_mut();
286287
for &(name, val) in values.iter() {
287-
let field = this.mplace_field_named(dest, name)?;
288+
let field = this.project_field_named(dest, name)?;
288289
this.write_int(val, &field.into())?;
289290
}
290291
Ok(())
@@ -301,8 +302,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
301302
}
302303

303304
/// Get the `Place` for a local
304-
fn local_place(&mut self, local: mir::Local) -> InterpResult<'tcx, PlaceTy<'tcx, Provenance>> {
305-
let this = self.eval_context_mut();
305+
fn local_place(&self, local: mir::Local) -> InterpResult<'tcx, PlaceTy<'tcx, Provenance>> {
306+
let this = self.eval_context_ref();
306307
let place = mir::Place { local, projection: List::empty() };
307308
this.eval_place(place)
308309
}
@@ -479,6 +480,16 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
479480
self.ecx
480481
}
481482

483+
fn aggregate_field_order(memory_index: &IndexVec<FieldIdx, u32>, idx: usize) -> usize {
484+
// We need to do an *inverse* lookup: find the field that has position `idx` in memory order.
485+
for (src_field, &mem_pos) in memory_index.iter_enumerated() {
486+
if mem_pos as usize == idx {
487+
return src_field.as_usize();
488+
}
489+
}
490+
panic!("invalid `memory_index`, could not find {}-th field in memory order", idx);
491+
}
492+
482493
// Hook to detect `UnsafeCell`.
483494
fn visit_value(&mut self, v: &MPlaceTy<'tcx, Provenance>) -> InterpResult<'tcx> {
484495
trace!("UnsafeCellVisitor: {:?} {:?}", *v, v.layout.ty);
@@ -524,33 +535,6 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
524535
}
525536
}
526537

527-
// Make sure we visit aggregates in increasing offset order.
528-
fn visit_aggregate(
529-
&mut self,
530-
place: &MPlaceTy<'tcx, Provenance>,
531-
fields: impl Iterator<Item = InterpResult<'tcx, MPlaceTy<'tcx, Provenance>>>,
532-
) -> InterpResult<'tcx> {
533-
match place.layout.fields {
534-
FieldsShape::Array { .. } => {
535-
// For the array layout, we know the iterator will yield sorted elements so
536-
// we can avoid the allocation.
537-
self.walk_aggregate(place, fields)
538-
}
539-
FieldsShape::Arbitrary { .. } => {
540-
// Gather the subplaces and sort them before visiting.
541-
let mut places = fields
542-
.collect::<InterpResult<'tcx, Vec<MPlaceTy<'tcx, Provenance>>>>()?;
543-
// we just compare offsets, the abs. value never matters
544-
places.sort_by_key(|place| place.ptr.addr());
545-
self.walk_aggregate(place, places.into_iter().map(Ok))
546-
}
547-
FieldsShape::Union { .. } | FieldsShape::Primitive => {
548-
// Uh, what?
549-
bug!("unions/primitives are not aggregates we should ever visit")
550-
}
551-
}
552-
}
553-
554538
fn visit_union(
555539
&mut self,
556540
_v: &MPlaceTy<'tcx, Provenance>,
@@ -746,7 +730,8 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
746730
Ok(mplace)
747731
}
748732

749-
fn deref_pointer_as(
733+
/// Deref' a pointer *without* checking that the place is dereferenceable.
734+
fn deref_pointer_unchecked(
750735
&self,
751736
val: &ImmTy<'tcx, Provenance>,
752737
layout: TyAndLayout<'tcx>,
@@ -811,10 +796,10 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
811796
tp: &MPlaceTy<'tcx, Provenance>,
812797
) -> InterpResult<'tcx, Option<Duration>> {
813798
let this = self.eval_context_mut();
814-
let seconds_place = this.mplace_field(tp, 0)?;
799+
let seconds_place = this.project_field(tp, 0)?;
815800
let seconds_scalar = this.read_scalar(&seconds_place.into())?;
816801
let seconds = seconds_scalar.to_target_isize(this)?;
817-
let nanoseconds_place = this.mplace_field(tp, 1)?;
802+
let nanoseconds_place = this.project_field(tp, 1)?;
818803
let nanoseconds_scalar = this.read_scalar(&nanoseconds_place.into())?;
819804
let nanoseconds = nanoseconds_scalar.to_target_isize(this)?;
820805

src/shims/backtrace.rs

+8-8
Original file line numberDiff line numberDiff line change
@@ -83,7 +83,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
8383

8484
// Write pointers into array
8585
for (i, ptr) in ptrs.into_iter().enumerate() {
86-
let place = this.mplace_index(&alloc, i as u64)?;
86+
let place = this.project_index(&alloc, i as u64)?;
8787

8888
this.write_pointer(ptr, &place.into())?;
8989
}
@@ -196,33 +196,33 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
196196

197197
this.write_immediate(
198198
name_alloc.to_ref(this),
199-
&this.mplace_field(&dest, 0)?.into(),
199+
&this.project_field(&dest, 0)?.into(),
200200
)?;
201201
this.write_immediate(
202202
filename_alloc.to_ref(this),
203-
&this.mplace_field(&dest, 1)?.into(),
203+
&this.project_field(&dest, 1)?.into(),
204204
)?;
205205
}
206206
1 => {
207207
this.write_scalar(
208208
Scalar::from_target_usize(name.len().try_into().unwrap(), this),
209-
&this.mplace_field(&dest, 0)?.into(),
209+
&this.project_field(&dest, 0)?.into(),
210210
)?;
211211
this.write_scalar(
212212
Scalar::from_target_usize(filename.len().try_into().unwrap(), this),
213-
&this.mplace_field(&dest, 1)?.into(),
213+
&this.project_field(&dest, 1)?.into(),
214214
)?;
215215
}
216216
_ => throw_unsup_format!("unknown `miri_resolve_frame` flags {}", flags),
217217
}
218218

219-
this.write_scalar(Scalar::from_u32(lineno), &this.mplace_field(&dest, 2)?.into())?;
220-
this.write_scalar(Scalar::from_u32(colno), &this.mplace_field(&dest, 3)?.into())?;
219+
this.write_scalar(Scalar::from_u32(lineno), &this.project_field(&dest, 2)?.into())?;
220+
this.write_scalar(Scalar::from_u32(colno), &this.project_field(&dest, 3)?.into())?;
221221

222222
// Support a 4-field struct for now - this is deprecated
223223
// and slated for removal.
224224
if num_fields == 5 {
225-
this.write_pointer(fn_ptr, &this.mplace_field(&dest, 4)?.into())?;
225+
this.write_pointer(fn_ptr, &this.project_field(&dest, 4)?.into())?;
226226
}
227227

228228
Ok(())

src/shims/env.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -456,7 +456,7 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
456456
))?;
457457
let vars_place = this.allocate(vars_layout, MiriMemoryKind::Runtime.into())?;
458458
for (idx, var) in vars.into_iter().enumerate() {
459-
let place = this.mplace_field(&vars_place, idx)?;
459+
let place = this.project_field(&vars_place, idx)?;
460460
this.write_pointer(var, &place.into())?;
461461
}
462462
this.write_pointer(vars_place.ptr, &this.machine.env_vars.environ.unwrap().into())?;

src/shims/foreign_items.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -942,9 +942,9 @@ pub trait EvalContextExt<'mir, 'tcx: 'mir>: crate::MiriInterpCxExt<'mir, 'tcx> {
942942
#[allow(clippy::arithmetic_side_effects)] // it's a u128, we can shift by 64
943943
let (c_out, sum) = ((wide_sum >> 64).truncate::<u8>(), wide_sum.truncate::<u64>());
944944

945-
let c_out_field = this.place_field(dest, 0)?;
945+
let c_out_field = this.project_field(dest, 0)?;
946946
this.write_scalar(Scalar::from_u8(c_out), &c_out_field)?;
947-
let sum_field = this.place_field(dest, 1)?;
947+
let sum_field = this.project_field(dest, 1)?;
948948
this.write_scalar(Scalar::from_u64(sum), &sum_field)?;
949949
}
950950
"llvm.x86.sse2.pause"

0 commit comments

Comments
 (0)