Skip to content

Commit f87bd48

Browse files
committed
add project_fields helper function
1 parent e3ed3e0 commit f87bd48

File tree

4 files changed

+22
-13
lines changed

4 files changed

+22
-13
lines changed

compiler/rustc_const_eval/src/interpret/projection.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,15 @@ where
199199
base.offset_with_meta(offset, OffsetMode::Inbounds, meta, field_layout, self)
200200
}
201201

202+
/// Projects multiple fields at once. See [`Self::project_field`] for details.
203+
pub fn project_fields<P: Projectable<'tcx, M::Provenance>, const N: usize>(
204+
&self,
205+
base: &P,
206+
fields: [FieldIdx; N],
207+
) -> InterpResult<'tcx, [P; N]> {
208+
fields.try_map(|field| self.project_field(base, field))
209+
}
210+
202211
/// Downcasting to an enum variant.
203212
pub fn project_downcast<P: Projectable<'tcx, M::Provenance>>(
204213
&self,

compiler/rustc_const_eval/src/interpret/visitor.rs

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -121,25 +121,24 @@ pub trait ValueVisitor<'tcx, M: Machine<'tcx>>: Sized {
121121

122122
// `Box` has two fields: the pointer we care about, and the allocator.
123123
assert_eq!(v.layout().fields.count(), 2, "`Box` must have exactly 2 fields");
124-
let (unique_ptr, alloc) = (
125-
self.ecx().project_field(v, FieldIdx::ZERO)?,
126-
self.ecx().project_field(v, FieldIdx::ONE)?,
127-
);
124+
let [unique_ptr, alloc] =
125+
self.ecx().project_fields(v, [FieldIdx::ZERO, FieldIdx::ONE])?;
126+
128127
// Unfortunately there is some type junk in the way here: `unique_ptr` is a `Unique`...
129128
// (which means another 2 fields, the second of which is a `PhantomData`)
130129
assert_eq!(unique_ptr.layout().fields.count(), 2);
131-
let (nonnull_ptr, phantom) = (
132-
self.ecx().project_field(&unique_ptr, FieldIdx::ZERO)?,
133-
self.ecx().project_field(&unique_ptr, FieldIdx::ONE)?,
134-
);
130+
let [nonnull_ptr, phantom] =
131+
self.ecx().project_fields(v, [FieldIdx::ZERO, FieldIdx::ONE])?;
135132
assert!(
136133
phantom.layout().ty.ty_adt_def().is_some_and(|adt| adt.is_phantom_data()),
137134
"2nd field of `Unique` should be PhantomData but is {:?}",
138135
phantom.layout().ty,
139136
);
137+
140138
// ... that contains a `NonNull`... (gladly, only a single field here)
141139
assert_eq!(nonnull_ptr.layout().fields.count(), 1);
142140
let raw_ptr = self.ecx().project_field(&nonnull_ptr, FieldIdx::ZERO)?; // the actual raw ptr
141+
143142
// ... whose only field finally is a raw ptr we can dereference.
144143
self.visit_box(ty, &raw_ptr)?;
145144

compiler/rustc_const_eval/src/lib.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#![allow(internal_features)]
33
#![allow(rustc::diagnostic_outside_of_impl)]
44
#![doc(rust_logo)]
5+
#![feature(array_try_map)]
56
#![feature(assert_matches)]
67
#![feature(box_patterns)]
78
#![feature(decl_macro)]

compiler/rustc_const_eval/src/util/caller_location.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,12 +42,12 @@ fn alloc_caller_location<'tcx>(
4242
let ___location = ecx.allocate(loc_layout, MemoryKind::CallerLocation).unwrap();
4343

4444
// Initialize fields.
45-
ecx.write_immediate(filename, &ecx.project_field(&___location, FieldIdx::from_u32(0)).unwrap())
46-
.expect("writing to memory we just allocated cannot fail");
47-
ecx.write_scalar(line, &ecx.project_field(&___location, FieldIdx::from_u32(1)).unwrap())
48-
.expect("writing to memory we just allocated cannot fail");
49-
ecx.write_scalar(col, &ecx.project_field(&___location, FieldIdx::from_u32(2)).unwrap())
45+
let [filename_field, line_field, col_field] =
46+
ecx.project_fields(&___location, [0, 1, 2].map(FieldIdx::from_u32)).unwrap();
47+
ecx.write_immediate(filename, &filename_field)
5048
.expect("writing to memory we just allocated cannot fail");
49+
ecx.write_scalar(line, &line_field).expect("writing to memory we just allocated cannot fail");
50+
ecx.write_scalar(col, &col_field).expect("writing to memory we just allocated cannot fail");
5151

5252
___location
5353
}

0 commit comments

Comments
 (0)