|
| 1 | +// RUN: %target-swift-frontend \ |
| 2 | +// RUN: -emit-sil -verify \ |
| 3 | +// RUN: %s \ |
| 4 | +// RUN: -enable-experimental-feature BuiltinModule \ |
| 5 | +// RUN: -enable-experimental-feature NoncopyableGenerics \ |
| 6 | +// RUN: -sil-verify-all |
| 7 | + |
| 8 | +// REQUIRES: asserts |
| 9 | + |
| 10 | +import Builtin |
| 11 | + |
| 12 | +@frozen |
| 13 | +enum MyLittleLayout<T : ~Copyable> { |
| 14 | + @_transparent |
| 15 | + static var size: Int { |
| 16 | + return Int(Builtin.sizeof(T.self)) |
| 17 | + } |
| 18 | + @_transparent |
| 19 | + static var stride: Int { |
| 20 | + return Int(Builtin.strideof(T.self)) |
| 21 | + } |
| 22 | +} |
| 23 | + |
| 24 | +@frozen |
| 25 | +enum MyLittleResult<Success : ~Copyable, Failure : Error> : ~Copyable { |
| 26 | + case success(Success) |
| 27 | + case failure(Failure) |
| 28 | +} |
| 29 | + |
| 30 | +@usableFromInline |
| 31 | +@_alwaysEmitIntoClient |
| 32 | +@inline(__always) |
| 33 | +func _rethrowsViaClosure<R : ~Copyable>(_ fn: () throws -> R) rethrows -> R { |
| 34 | + return try fn() |
| 35 | +} |
| 36 | + |
| 37 | +func _withUnprotectedUnsafeTemporaryAllocation<T: ~Copyable, R: ~Copyable>( |
| 38 | + of type: T.Type, |
| 39 | + capacity: Int, |
| 40 | + alignment: Int, |
| 41 | + _ body: (Builtin.RawPointer) throws -> R |
| 42 | +) rethrows -> R { |
| 43 | + let result: MyLittleResult<R, Error> |
| 44 | +#if $BuiltinUnprotectedStackAlloc |
| 45 | + let stackAddress = Builtin.unprotectedStackAlloc( |
| 46 | + capacity._builtinWordValue, |
| 47 | + MyLittleLayout<T>.stride._builtinWordValue, |
| 48 | + alignment._builtinWordValue |
| 49 | + ) |
| 50 | +#else |
| 51 | + let stackAddress = Builtin.stackAlloc( |
| 52 | + capacity._builtinWordValue, |
| 53 | + MyLittleLayout<T>.stride._builtinWordValue, |
| 54 | + alignment._builtinWordValue |
| 55 | + ) |
| 56 | +#endif |
| 57 | + do { |
| 58 | + result = .success(try body(stackAddress)) |
| 59 | + Builtin.stackDealloc(stackAddress) |
| 60 | + } catch { |
| 61 | + result = .failure(error) |
| 62 | + Builtin.stackDealloc(stackAddress) |
| 63 | + } |
| 64 | + switch consume result { |
| 65 | + // FIXME: This shouldn't be diagnosed. But it's better than miscompiling. |
| 66 | + case .success(let success): return success // expected-error{{cannot partially consume 'unknown'}} |
| 67 | + case .failure(let error): return try _rethrowsViaClosure { throw error } |
| 68 | + } |
| 69 | +} |
0 commit comments