Skip to content

Commit f23fb19

Browse files
committed
vec: avoid ptrtoint/inttoptr in the iterators
This results in throwing away alias analysis information, because LLVM does *not* implement reasoning about these conversions yet. We specialize zero-size types since a `getelementptr` offset will return us the same pointer, making it broken as a simple counter.
1 parent 8f9bbc4 commit f23fb19

File tree

1 file changed

+18
-8
lines changed

1 file changed

+18
-8
lines changed

src/libstd/vec.rs

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -849,10 +849,15 @@ impl<'self,T> ImmutableVector<'self, T> for &'self [T] {
849849
fn iter(self) -> VecIterator<'self, T> {
850850
unsafe {
851851
let p = vec::raw::to_ptr(self);
852-
VecIterator{ptr: p,
853-
end: (p as uint + self.len() *
854-
sys::nonzero_size_of::<T>()) as *T,
855-
lifetime: cast::transmute(p)}
852+
if sys::size_of::<T>() == 0 {
853+
VecIterator{ptr: p,
854+
end: (p as uint + self.len()) as *T,
855+
lifetime: cast::transmute(p)}
856+
} else {
857+
VecIterator{ptr: p,
858+
end: p.offset(self.len() as int),
859+
lifetime: cast::transmute(p)}
860+
}
856861
}
857862
}
858863

@@ -1826,10 +1831,15 @@ impl<'self,T> MutableVector<'self, T> for &'self mut [T] {
18261831
fn mut_iter(self) -> VecMutIterator<'self, T> {
18271832
unsafe {
18281833
let p = vec::raw::to_mut_ptr(self);
1829-
VecMutIterator{ptr: p,
1830-
end: (p as uint + self.len() *
1831-
sys::nonzero_size_of::<T>()) as *mut T,
1832-
lifetime: cast::transmute(p)}
1834+
if sys::size_of::<T>() == 0 {
1835+
VecMutIterator{ptr: p,
1836+
end: (p as uint + self.len()) as *mut T,
1837+
lifetime: cast::transmute(p)}
1838+
} else {
1839+
VecMutIterator{ptr: p,
1840+
end: p.offset(self.len() as int),
1841+
lifetime: cast::transmute(p)}
1842+
}
18331843
}
18341844
}
18351845

0 commit comments

Comments
 (0)