Skip to content

Commit 7cdeff2

Browse files
committed
a bit of meta-related cleanup on Projectable
1 parent a09df43 commit 7cdeff2

File tree

3 files changed

+32
-36
lines changed

3 files changed

+32
-36
lines changed

compiler/rustc_const_eval/src/interpret/operand.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -295,9 +295,9 @@ impl<'tcx, Prov: Provenance> Projectable<'tcx, Prov> for ImmTy<'tcx, Prov> {
295295
}
296296

297297
#[inline(always)]
298-
fn meta(&self) -> InterpResult<'tcx, MemPlaceMeta<Prov>> {
298+
fn meta(&self) -> MemPlaceMeta<Prov> {
299299
debug_assert!(self.layout.is_sized()); // unsized ImmTy can only exist temporarily and should never reach this here
300-
Ok(MemPlaceMeta::None)
300+
MemPlaceMeta::None
301301
}
302302

303303
fn offset_with_meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
@@ -326,14 +326,14 @@ impl<'tcx, Prov: Provenance + 'static> Projectable<'tcx, Prov> for OpTy<'tcx, Pr
326326
}
327327

328328
#[inline]
329-
fn meta(&self) -> InterpResult<'tcx, MemPlaceMeta<Prov>> {
330-
Ok(match self.as_mplace_or_imm() {
329+
fn meta(&self) -> MemPlaceMeta<Prov> {
330+
match self.as_mplace_or_imm() {
331331
Left(mplace) => mplace.meta,
332332
Right(_) => {
333333
debug_assert!(self.layout.is_sized(), "unsized immediates are not a thing");
334334
MemPlaceMeta::None
335335
}
336-
})
336+
}
337337
}
338338

339339
fn offset_with_meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(

compiler/rustc_const_eval/src/interpret/place.rs

+6-27
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use rustc_middle::mir::interpret::PointerArithmetic;
1313
use rustc_middle::ty;
1414
use rustc_middle::ty::layout::{LayoutOf, TyAndLayout};
1515
use rustc_middle::ty::Ty;
16-
use rustc_target::abi::{self, Abi, Align, FieldIdx, HasDataLayout, Size, FIRST_VARIANT};
16+
use rustc_target::abi::{Abi, Align, FieldIdx, HasDataLayout, Size, FIRST_VARIANT};
1717

1818
use super::{
1919
alloc_range, mir_assign_valid_types, AllocId, AllocRef, AllocRefMut, CheckInAllocMsg,
@@ -48,27 +48,6 @@ impl<Prov: Provenance> MemPlaceMeta<Prov> {
4848
Self::None => false,
4949
}
5050
}
51-
52-
pub(crate) fn len<'tcx>(
53-
&self,
54-
layout: TyAndLayout<'tcx>,
55-
cx: &impl HasDataLayout,
56-
) -> InterpResult<'tcx, u64> {
57-
if layout.is_unsized() {
58-
// We need to consult `meta` metadata
59-
match layout.ty.kind() {
60-
ty::Slice(..) | ty::Str => self.unwrap_meta().to_target_usize(cx),
61-
_ => bug!("len not supported on unsized type {:?}", layout.ty),
62-
}
63-
} else {
64-
// Go through the layout. There are lots of types that support a length,
65-
// e.g., SIMD types. (But not all repr(simd) types even have FieldsShape::Array!)
66-
match layout.fields {
67-
abi::FieldsShape::Array { count, .. } => Ok(count),
68-
_ => bug!("len not supported on sized type {:?}", layout.ty),
69-
}
70-
}
71-
}
7251
}
7352

7453
#[derive(Copy, Clone, Hash, PartialEq, Eq, Debug)]
@@ -224,8 +203,8 @@ impl<'tcx, Prov: Provenance + 'static> Projectable<'tcx, Prov> for MPlaceTy<'tcx
224203
}
225204

226205
#[inline(always)]
227-
fn meta(&self) -> InterpResult<'tcx, MemPlaceMeta<Prov>> {
228-
Ok(self.meta)
206+
fn meta(&self) -> MemPlaceMeta<Prov> {
207+
self.meta
229208
}
230209

231210
fn offset_with_meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
@@ -257,14 +236,14 @@ impl<'tcx, Prov: Provenance + 'static> Projectable<'tcx, Prov> for PlaceTy<'tcx,
257236
}
258237

259238
#[inline]
260-
fn meta(&self) -> InterpResult<'tcx, MemPlaceMeta<Prov>> {
261-
Ok(match self.as_mplace_or_local() {
239+
fn meta(&self) -> MemPlaceMeta<Prov> {
240+
match self.as_mplace_or_local() {
262241
Left(mplace) => mplace.meta,
263242
Right(_) => {
264243
debug_assert!(self.layout.is_sized(), "unsized locals should live in memory");
265244
MemPlaceMeta::None
266245
}
267-
})
246+
}
268247
}
269248

270249
fn offset_with_meta<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(

compiler/rustc_const_eval/src/interpret/projection.rs

+21-4
Original file line numberDiff line numberDiff line change
@@ -25,13 +25,28 @@ pub trait Projectable<'tcx, Prov: Provenance>: Sized + std::fmt::Debug {
2525
fn layout(&self) -> TyAndLayout<'tcx>;
2626

2727
/// Get the metadata of a wide value.
28-
fn meta(&self) -> InterpResult<'tcx, MemPlaceMeta<Prov>>;
28+
fn meta(&self) -> MemPlaceMeta<Prov>;
2929

30+
/// Get the length of a slice/string/array stored here.
3031
fn len<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
3132
&self,
3233
ecx: &InterpCx<'mir, 'tcx, M>,
3334
) -> InterpResult<'tcx, u64> {
34-
self.meta()?.len(self.layout(), ecx)
35+
let layout = self.layout();
36+
if layout.is_unsized() {
37+
// We need to consult `meta` metadata
38+
match layout.ty.kind() {
39+
ty::Slice(..) | ty::Str => self.meta().unwrap_meta().to_target_usize(ecx),
40+
_ => bug!("len not supported on unsized type {:?}", layout.ty),
41+
}
42+
} else {
43+
// Go through the layout. There are lots of types that support a length,
44+
// e.g., SIMD types. (But not all repr(simd) types even have FieldsShape::Array!)
45+
match layout.fields {
46+
abi::FieldsShape::Array { count, .. } => Ok(count),
47+
_ => bug!("len not supported on sized type {:?}", layout.ty),
48+
}
49+
}
3550
}
3651

3752
/// Offset the value by the given amount, replacing the layout and metadata.
@@ -43,6 +58,7 @@ pub trait Projectable<'tcx, Prov: Provenance>: Sized + std::fmt::Debug {
4358
ecx: &InterpCx<'mir, 'tcx, M>,
4459
) -> InterpResult<'tcx, Self>;
4560

61+
#[inline]
4662
fn offset<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
4763
&self,
4864
offset: Size,
@@ -53,6 +69,7 @@ pub trait Projectable<'tcx, Prov: Provenance>: Sized + std::fmt::Debug {
5369
self.offset_with_meta(offset, MemPlaceMeta::None, layout, ecx)
5470
}
5571

72+
#[inline]
5673
fn transmute<'mir, M: Machine<'mir, 'tcx, Provenance = Prov>>(
5774
&self,
5875
layout: TyAndLayout<'tcx>,
@@ -125,7 +142,7 @@ where
125142
// But const-prop actually feeds us such nonsense MIR! (see test `const_prop/issue-86351.rs`)
126143
throw_inval!(ConstPropNonsense);
127144
}
128-
let base_meta = base.meta()?;
145+
let base_meta = base.meta();
129146
// Re-use parent metadata to determine dynamic field layout.
130147
// With custom DSTS, this *will* execute user-defined code, but the same
131148
// happens at run-time so that's okay.
@@ -153,7 +170,7 @@ where
153170
base: &P,
154171
variant: VariantIdx,
155172
) -> InterpResult<'tcx, P> {
156-
assert!(!base.meta()?.has_meta());
173+
assert!(!base.meta().has_meta());
157174
// Downcasts only change the layout.
158175
// (In particular, no check about whether this is even the active variant -- that's by design,
159176
// see https://github.com/rust-lang/rust/issues/93688#issuecomment-1032929496.)

0 commit comments

Comments
 (0)