3 // ignore-debug: the debug assertions get in the way
6 #![feature(iter_repeat_n)]
9 pub struct NotCopy(u16);
11 impl Drop for NotCopy {
15 // For a type where `Drop::drop` doesn't do anything observable and a clone is the
16 // same as a move, make sure that the extra case for the last item disappears.
19 // CHECK-LABEL: @iter_repeat_n_next
20 pub fn iter_repeat_n_next(it: &mut std::iter::RepeatN<NotCopy>) -> Option<NotCopy> {
23 // CHECK: %[[COUNT:.+]] = load i64
24 // CHECK-NEXT: %[[COUNT_ZERO:.+]] = icmp eq i64 %[[COUNT]], 0
25 // CHECK-NEXT: br i1 %[[COUNT_ZERO]], label %[[EMPTY:.+]], label %[[NOT_EMPTY:.+]]
27 // CHECK: [[NOT_EMPTY]]:
28 // CHECK-NEXT: %[[DEC:.+]] = add i64 %[[COUNT]], -1
29 // CHECK-NEXT: store i64 %[[DEC]]
31 // CHECK: %[[VAL:.+]] = load i16
32 // CHECK-NEXT: br label %[[EMPTY]]
36 // CHECK: phi i16 [ undef, %start ], [ %[[VAL]], %[[NOT_EMPTY]] ]
43 // And as a result, using the iterator can optimize without special cases for
44 // the last iteration, like `memset`ing all the items in one call.
47 // CHECK-LABEL: @vec_extend_via_iter_repeat_n
48 pub fn vec_extend_via_iter_repeat_n() -> Vec<u8> {
49 // CHECK: %[[ADDR:.+]] = tail call dereferenceable_or_null(1234) ptr @__rust_alloc(i64 1234, i64 1)
50 // CHECK: tail call void @llvm.memset.p0.i64(ptr noundef nonnull align 1 dereferenceable(1234) %[[ADDR]], i8 42, i64 1234,
53 let mut v = Vec::with_capacity(n);
54 v.extend(std::iter::repeat_n(42_u8, n));