1 // min-llvm-version: 14.0
2 // ignore-debug: the debug assertions get in the way
3 // compile-flags: -O -Z merge-functions=disabled
6 // Ensure that trivial casts of vec elements are O(1)
8 pub struct Wrapper<T>(T);
18 // Going from an aggregate struct to another type currently requires Copy to
19 // enable the TrustedRandomAccess specialization. Without it optimizations do not yet
20 // reliably recognize the loops as noop for for repr(C) or non-Copy structs.
21 #[derive(Copy, Clone)]
29 // CHECK-LABEL: @vec_iterator_cast_primitive
31 pub fn vec_iterator_cast_primitive(vec: Vec<i8>) -> Vec<u8> {
34 vec.into_iter().map(|e| e as u8).collect()
37 // CHECK-LABEL: @vec_iterator_cast_wrapper
39 pub fn vec_iterator_cast_wrapper(vec: Vec<u8>) -> Vec<Wrapper<u8>> {
42 vec.into_iter().map(|e| Wrapper(e)).collect()
45 // CHECK-LABEL: @vec_iterator_cast_unwrap
47 pub fn vec_iterator_cast_unwrap(vec: Vec<Wrapper<u8>>) -> Vec<u8> {
50 vec.into_iter().map(|e| e.0).collect()
53 // CHECK-LABEL: @vec_iterator_cast_aggregate
55 pub fn vec_iterator_cast_aggregate(vec: Vec<[u64; 4]>) -> Vec<Foo> {
56 // FIXME These checks should be the same as other functions.
57 // CHECK-NOT: @__rust_alloc
58 // CHECK-NOT: @__rust_alloc
59 vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()
62 // CHECK-LABEL: @vec_iterator_cast_deaggregate
64 pub fn vec_iterator_cast_deaggregate(vec: Vec<Bar>) -> Vec<[u64; 4]> {
65 // FIXME These checks should be the same as other functions.
66 // CHECK-NOT: @__rust_alloc
67 // CHECK-NOT: @__rust_alloc
69 // Safety: For the purpose of this test we assume that Bar layout matches [u64; 4].
70 // This currently is not guaranteed for repr(Rust) types, but it happens to work here and
71 // the UCG may add additional guarantees for homogenous types in the future that would make this
73 vec.into_iter().map(|e| unsafe { std::mem::transmute(e) }).collect()