1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223
|
// RUN: %target-swift-emit-silgen %s -module-name foreach_async -swift-version 5 -disable-availability-checking | %FileCheck %s
// REQUIRES: concurrency
//////////////////
// Declarations //
//////////////////
class C {}
@_silgen_name("loopBodyEnd")
func loopBodyEnd() -> ()
@_silgen_name("condition")
func condition() -> Bool
@_silgen_name("loopContinueEnd")
func loopContinueEnd() -> ()
@_silgen_name("loopBreakEnd")
func loopBreakEnd() -> ()
@_silgen_name("funcEnd")
func funcEnd() -> ()
struct TrivialStruct {
var value: Int32
}
struct NonTrivialStruct {
var value: C
}
struct GenericStruct<T> {
var value: T
var value2: C
}
protocol P {}
protocol ClassP : AnyObject {}
protocol GenericCollection : Collection {
}
struct AsyncLazySequence<S: Sequence>: AsyncSequence {
typealias Element = S.Element
typealias AsyncIterator = Iterator
struct Iterator: AsyncIteratorProtocol {
typealias Element = S.Element
var iterator: S.Iterator?
mutating func next() async -> S.Element? {
return iterator?.next()
}
}
var sequence: S
func makeAsyncIterator() -> Iterator {
return Iterator(iterator: sequence.makeIterator())
}
}
///////////
// Tests //
///////////
//===----------------------------------------------------------------------===//
// Trivial Struct
//===----------------------------------------------------------------------===//
// CHECK-LABEL: sil hidden [ossa] @$s13foreach_async13trivialStructyyAA17AsyncLazySequenceVySaySiGGYaF : $@convention(thin) @async (@guaranteed AsyncLazySequence<Array<Int>>) -> () {
// CHECK: bb0([[SOURCE:%.*]] : @guaranteed $AsyncLazySequence<Array<Int>>):
// CHECK: [[ITERATOR_BOX:%.*]] = alloc_box ${ var AsyncLazySequence<Array<Int>>.Iterator }, var, name "$x$generator"
// CHECK: [[ITERATOR_LIFETIME:%.*]] = begin_borrow [var_decl] [[ITERATOR_BOX]]
// CHECK: [[PROJECT_ITERATOR_BOX:%.*]] = project_box [[ITERATOR_LIFETIME]]
// CHECK: br [[LOOP_DEST:bb[0-9]+]]
// CHECK: [[LOOP_DEST]]:
// CHECK: [[NEXT_RESULT:%.*]] = alloc_stack $Optional<Int>
// CHECK: [[ACTOR:%.*]] = enum $Optional<any Actor>, #Optional.none!enumelt
// CHECK: [[MUTATION:%.*]] = begin_access
// CHECK: [[WITNESS_METHOD:%.*]] = function_ref @$sScIsE4next9isolation7ElementQzSgScA_pSgYi_tYa7FailureQzYKF : $@convention(method) @async <τ_0_0 where τ_0_0 : AsyncIteratorProtocol> (@sil_isolated @guaranteed Optional<any Actor>, @inout τ_0_0) -> (@out Optional<τ_0_0.Element>, @error_indirect τ_0_0.Failure)
// CHECK: [[ERROR_SLOT:%[0-9]+]] = alloc_stack $Never
// CHECK: try_apply [[WITNESS_METHOD]]<AsyncLazySequence<[Int]>.Iterator>([[NEXT_RESULT]], [[ERROR_SLOT]], [[ACTOR]], [[MUTATION]]) : $@convention(method) @async <τ_0_0 where τ_0_0 : AsyncIteratorProtocol> (@sil_isolated @guaranteed Optional<any Actor>, @inout τ_0_0) -> (@out Optional<τ_0_0.Element>, @error_indirect τ_0_0.Failure), normal [[NORMAL_BB:bb[0-2]+]], error [[ERROR_BB:bb[0-9]+]]
// CHECK: [[NORMAL_BB]](
// CHECK: end_access [[MUTATION]]
// CHECK: switch_enum [[IND_VAR:%.*]] : $Optional<Int>, case #Optional.some!enumelt: [[SOME_BB:bb[0-9]+]], case #Optional.none!enumelt: [[NONE_BB:bb[0-9]+]]
// CHECK: [[SOME_BB]]([[VAR:%.*]] : $Int):
// CHECK: loopBodyEnd
// CHECK: br [[LOOP_DEST]]
// CHECK: [[NONE_BB]]:
// CHECK: funcEnd
// CHECK: return
// CHECK: } // end sil function '$s13foreach_async13trivialStructyyAA17AsyncLazySequenceVySaySiGGYaF'
func trivialStruct(_ xx: AsyncLazySequence<[Int]>) async {
for await x in xx {
loopBodyEnd()
}
funcEnd()
}
// CHECK-LABEL: sil hidden [ossa] @$s13foreach_async21trivialStructContinueyyAA17AsyncLazySequenceVySaySiGGYaF : $@convention(thin) @async (@guaranteed AsyncLazySequence<Array<Int>>) -> () {
// CHECK: bb0([[SOURCE:%.*]] : @guaranteed $AsyncLazySequence<Array<Int>>):
// CHECK: [[ITERATOR_BOX:%.*]] = alloc_box ${ var AsyncLazySequence<Array<Int>>.Iterator }, var, name "$x$generator"
// CHECK: [[PROJECT_ITERATOR_LIFETIME:%.*]] = begin_borrow [var_decl] [[ITERATOR_BOX]]
// CHECK: [[PROJECT_ITERATOR_BOX:%.*]] = project_box [[ITERATOR_LIFETIME]]
// CHECK: br [[LOOP_DEST:bb[0-9]+]]
// CHECK: [[LOOP_DEST]]:
// CHECK: [[NEXT_RESULT:%.*]] = alloc_stack $Optional<Int>
// CHECK: [[ACTOR:%.*]] = enum $Optional<any Actor>, #Optional.none!enumelt
// CHECK: [[MUTATION:%.*]] = begin_access
// CHECK: [[WITNESS_METHOD:%.*]] = function_ref @$sScIsE4next9isolation7ElementQzSgScA_pSgYi_tYa7FailureQzYKF : $@convention(method) @async <τ_0_0 where τ_0_0 : AsyncIteratorProtocol> (@sil_isolated @guaranteed Optional<any Actor>, @inout τ_0_0) -> (@out Optional<τ_0_0.Element>, @error_indirect τ_0_0.Failure)
// CHECK: [[ERROR_SLOT:%[0-9]+]] = alloc_stack $Never
// CHECK: try_apply [[WITNESS_METHOD]]<AsyncLazySequence<[Int]>.Iterator>([[NEXT_RESULT]], [[ERROR_SLOT]], [[ACTOR]], [[MUTATION]]) : $@convention(method) @async <τ_0_0 where τ_0_0 : AsyncIteratorProtocol> (@sil_isolated @guaranteed Optional<any Actor>, @inout τ_0_0) -> (@out Optional<τ_0_0.Element>, @error_indirect τ_0_0.Failure), normal [[NORMAL_BB:bb[0-2]+]], error [[ERROR_BB:bb[0-9]+]]
// CHECK: [[NORMAL_BB]](
// CHECK: end_access [[MUTATION]]
// CHECK: switch_enum [[IND_VAR:%.*]] : $Optional<Int>, case #Optional.some!enumelt: [[SOME_BB:bb[0-9]+]], case #Optional.none!enumelt: [[NONE_BB:bb[0-9]+]]
// CHECK: [[SOME_BB]]([[VAR:%.*]] : $Int):
// CHECK: condition
// CHECK: cond_br [[VAR:%.*]], [[COND_TRUE:bb[0-9]+]], [[COND_FALSE:bb[0-9]+]]
// CHECK: [[COND_TRUE]]:
// CHECK: loopContinueEnd
// CHECK: br [[LOOP_DEST]]
// CHECK: [[COND_FALSE]]:
// CHECK: loopBodyEnd
// CHECK: br [[LOOP_DEST]]
// CHECK: [[NONE_BB]]:
// CHECK: funcEnd
// CHECK: return
// CHECK: } // end sil function '$s13foreach_async21trivialStructContinueyyAA17AsyncLazySequenceVySaySiGGYaF'
func trivialStructContinue(_ xx: AsyncLazySequence<[Int]>) async {
for await x in xx {
if (condition()) {
loopContinueEnd()
continue
}
loopBodyEnd()
}
funcEnd()
}
// TODO: Write this test
func trivialStructBreak(_ xx: AsyncLazySequence<[Int]>) async {
for await x in xx {
if (condition()) {
loopBreakEnd()
break
}
loopBodyEnd()
}
funcEnd()
}
// CHECK-LABEL: sil hidden [ossa] @$s13foreach_async26trivialStructContinueBreakyyAA17AsyncLazySequenceVySaySiGGYaF : $@convention(thin) @async (@guaranteed AsyncLazySequence<Array<Int>>) -> ()
// CHECK: bb0([[SOURCE:%.*]] : @guaranteed $AsyncLazySequence<Array<Int>>):
// CHECK: [[ITERATOR_BOX:%.*]] = alloc_box ${ var AsyncLazySequence<Array<Int>>.Iterator }, var, name "$x$generator"
// CHECK: [[ITERATOR_BOX_LIFETIME:%.*]] = begin_borrow [var_decl] [[ITERATOR_BOX]]
// CHECK: [[PROJECT_ITERATOR_BOX:%.*]] = project_box [[ITERATOR_BOX_LIFETIME]]
// CHECK: br [[LOOP_DEST:bb[0-9]+]]
// CHECK: [[LOOP_DEST]]:
// CHECK: [[NEXT_RESULT:%.*]] = alloc_stack $Optional<Int>
// CHECK: [[ACTOR:%.*]] = enum $Optional<any Actor>, #Optional.none!enumelt
// CHECK: [[MUTATION:%.*]] = begin_access
// CHECK: [[WITNESS_METHOD:%.*]] = function_ref @$sScIsE4next9isolation7ElementQzSgScA_pSgYi_tYa7FailureQzYKF : $@convention(method) @async <τ_0_0 where τ_0_0 : AsyncIteratorProtocol> (@sil_isolated @guaranteed Optional<any Actor>, @inout τ_0_0) -> (@out Optional<τ_0_0.Element>, @error_indirect τ_0_0.Failure)
// CHECK: [[ERROR_SLOT:%[0-9]+]] = alloc_stack $Never
// CHECK: try_apply [[WITNESS_METHOD]]<AsyncLazySequence<[Int]>.Iterator>([[NEXT_RESULT]], [[ERROR_SLOT]], [[ACTOR]], [[MUTATION]]) : $@convention(method) @async <τ_0_0 where τ_0_0 : AsyncIteratorProtocol> (@sil_isolated @guaranteed Optional<any Actor>, @inout τ_0_0) -> (@out Optional<τ_0_0.Element>, @error_indirect τ_0_0.Failure), normal [[NORMAL_BB:bb[0-2]+]], error [[ERROR_BB:bb[0-9]+]]
// CHECK: [[NORMAL_BB]](
// CHECK: end_access [[MUTATION]]
// CHECK: switch_enum [[IND_VAR:%.*]] : $Optional<Int>, case #Optional.some!enumelt: [[SOME_BB:bb[0-9]+]], case #Optional.none!enumelt: [[NONE_BB:bb[0-9]+]]
// CHECK: [[SOME_BB]]([[VAR:%.*]] : $Int):
// CHECK: condition
// CHECK: cond_br [[VAR:%.*]], [[COND_TRUE:bb[0-9]+]], [[COND_FALSE:bb[0-9]+]]
// CHECK: [[COND_TRUE]]:
// CHECK: loopBreakEnd
// CHECK: br [[LOOP_EXIT:bb[0-9]+]]
// CHECK: [[COND_FALSE]]:
// CHECK: condition
// CHECK: cond_br [[VAR:%.*]], [[COND_TRUE2:bb[0-9]+]], [[COND_FALSE2:bb[0-9]+]]
// CHECK: [[COND_TRUE2]]:
// CHECK: loopContinueEnd
// CHECK: br [[LOOP_DEST]]
// CHECK: [[COND_FALSE2]]:
// CHECK: br [[LOOP_DEST]]
// CHECK: [[LOOP_EXIT]]:
// CHECK: return
// CHECK: } // end sil function '$s13foreach_async26trivialStructContinueBreakyyAA17AsyncLazySequenceVySaySiGGYaF'
func trivialStructContinueBreak(_ xx: AsyncLazySequence<[Int]>) async {
for await x in xx {
if (condition()) {
loopBreakEnd()
break
}
if (condition()) {
loopContinueEnd()
continue
}
loopBodyEnd()
}
funcEnd()
}
|