The following test passes for slices over non-ZST, but fails for ZST:
fn slice_test<T>(x: &[T]) {
let mut i = x.iter();
i.next();
let x1 = i.next().unwrap();
let x2 = &x[1];
let x3 = x.iter().nth(1).unwrap();
assert_eq!(x1 as *const _, x2 as *const _);
assert_eq!(x2 as *const _, x3 as *const _);
}
fn main() {
slice_test(&[1,2,3]);
slice_test(&[(),(),()]);
}
Playpen
While pointers to ZSTs don't really matter as they will never be dereferenced, I think providing consistent addresses for the same element would still make sense.
Yet, x2 turns out to be 0x55e742ee85dc, no idea where that address is coming from. The LLVM IR contains some undef, but I am not sure if that always indicates a problem.
Generally, the iterator's treatment of ZST's seems a little fishy. Below
|
impl<T> SliceIndex<[T]> for usize { |
, the
get methods defer to
.offset to compute the address of an element of the slice.
offset is only well-defined for in-bounds accesses, yet it is used here on a dangling pointer-to-ZST, with some non-0 offset.
The following test passes for slices over non-ZST, but fails for ZST:
Playpen
While pointers to ZSTs don't really matter as they will never be dereferenced, I think providing consistent addresses for the same element would still make sense.
Yet,
x2turns out to be0x55e742ee85dc, no idea where that address is coming from. The LLVM IR contains someundef, but I am not sure if that always indicates a problem.Generally, the iterator's treatment of ZST's seems a little fishy. Below
rust/src/libcore/slice/mod.rs
Line 777 in 4450779
getmethods defer to.offsetto compute the address of an element of the slice.offsetis only well-defined for in-bounds accesses, yet it is used here on a dangling pointer-to-ZST, with some non-0 offset.