Skip to content

Commit d3c3e3c

Browse files
committed
fix len() on non-array but array-layout types (e.g. SIMD)
1 parent 039f53f commit d3c3e3c

File tree

2 files changed

+15
-14
lines changed

2 files changed

+15
-14
lines changed

src/librustc_mir/interpret/place.rs

+12-11
Original file line numberDiff line numberDiff line change
@@ -137,19 +137,20 @@ impl<'tcx> MPlaceTy<'tcx> {
137137

138138
#[inline]
139139
pub(super) fn len(self, cx: impl HasDataLayout) -> EvalResult<'tcx, u64> {
140-
match self.layout.ty.sty {
141-
ty::Array(..) => {
142-
// Sized, get length from layout.
143-
debug_assert!(self.extra.is_none());
144-
match self.layout.fields {
145-
layout::FieldPlacement::Array { count, .. } => Ok(count),
146-
_ => bug!("Length for non-array layout {:?} requested", self.layout),
147-
}
140+
if self.layout.is_unsized() {
141+
// We need to consult `extra` metadata
142+
match self.layout.ty.sty {
143+
ty::Slice(..) | ty::Str =>
144+
return self.extra.unwrap().to_usize(cx),
145+
_ => bug!("len not supported on unsized type {:?}", self.layout.ty),
148146
}
149-
ty::Slice(..) | ty::Str => {
150-
self.extra.unwrap().to_usize(cx)
147+
} else {
148+
// Go through the layout. There are lots of types that support a length,
149+
// e.g. SIMD types.
150+
match self.layout.fields {
151+
layout::FieldPlacement::Array { count, .. } => Ok(count),
152+
_ => bug!("len not supported on sized type {:?}", self.layout.ty),
151153
}
152-
_ => bug!("len not supported on type {:?}", self.layout.ty),
153154
}
154155
}
155156

src/librustc_mir/interpret/validity.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -332,16 +332,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
332332
}
333333
}
334334
}
335-
ty::Array(..) | ty::Slice(..) => {
336-
// This handles the unsized case correctly as well
335+
_ => {
336+
// This handles the unsized case correctly as well, as well as
337+
// SIMD an all sorts of other array-like types.
337338
for (i, field) in self.mplace_array_fields(dest)?.enumerate() {
338339
let field = field?;
339340
path.push(PathElem::ArrayElem(i));
340341
self.validate_operand(field.into(), path, seen, todo)?;
341342
path.truncate(path_len);
342343
}
343344
}
344-
_ => bug!("Array layout for non-array type {:?}", dest.layout.ty),
345345
}
346346
},
347347
layout::FieldPlacement::Array { .. } => {

0 commit comments

Comments
 (0)