Skip to content

Commit

Permalink
fix len() on non-array but array-layout types (e.g. SIMD)
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed Aug 27, 2018
1 parent 07bdd48 commit c38cc89
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 14 deletions.
23 changes: 12 additions & 11 deletions src/librustc_mir/interpret/place.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,19 +137,20 @@ impl<'tcx> MPlaceTy<'tcx> {

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

Expand Down
6 changes: 3 additions & 3 deletions src/librustc_mir/interpret/validity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,16 +332,16 @@ impl<'a, 'mir, 'tcx, M: Machine<'mir, 'tcx>> EvalContext<'a, 'mir, 'tcx, M> {
}
}
}
ty::Array(..) | ty::Slice(..) => {
// This handles the unsized case correctly as well
_ => {
// This handles the unsized case correctly as well, as well as
// SIMD an all sorts of other array-like types.
for (i, field) in self.mplace_array_fields(dest)?.enumerate() {
let field = field?;
path.push(PathElem::ArrayElem(i));
self.validate_operand(field.into(), path, seen, todo)?;
path.truncate(path_len);
}
}
_ => bug!("Array layout for non-array type {:?}", dest.layout.ty),
}
},
layout::FieldPlacement::Array { .. } => {
Expand Down

0 comments on commit c38cc89

Please sign in to comment.