File: typed_throws.swift

package info (click to toggle)
swiftlang 6.1.3-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,791,532 kB
  • sloc: cpp: 9,901,743; ansic: 2,201,431; asm: 1,091,827; python: 308,252; objc: 82,166; f90: 80,126; lisp: 38,358; pascal: 25,559; sh: 20,429; ml: 5,058; perl: 4,745; makefile: 4,484; awk: 3,535; javascript: 3,018; xml: 918; fortran: 664; cs: 573; ruby: 396
file content (361 lines) | stat: -rw-r--r-- 10,264 bytes parent folder | download
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
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
// RUN: %target-swift-frontend -primary-file %s -emit-ir -disable-availability-checking -runtime-compatibility-version none -target %module-target-future | %FileCheck %s --check-prefix=CHECK-MANGLE

// RUN: %target-swift-frontend -primary-file %s -emit-ir -disable-availability-checking -runtime-compatibility-version 5.8 -disable-concrete-type-metadata-mangled-name-accessors | %FileCheck %s --check-prefix=CHECK-NOMANGLE

// RUN: %target-swift-frontend -primary-file %s -emit-ir  | %FileCheck %s --check-prefix=CHECK

// RUN: %target-swift-frontend -primary-file %s -emit-ir -enable-library-evolution

// RUN: %target-swift-frontend -primary-file %s -emit-ir -O

// XFAIL: CPU=arm64e
// REQUIRES: PTRSIZE=64

public enum MyBigError: Error {
  case epicFail
  case evenBiggerFail
}

// CHECK-MANGLE: @"$s12typed_throws1XVAA1PAAWP" = hidden global [2 x ptr] [ptr @"$s12typed_throws1XVAA1PAAMc", ptr getelementptr inbounds (i8, ptr @"symbolic ySi_____YKc 12typed_throws10MyBigErrorO", {{i32|i64}} 1)]
@available(SwiftStdlib 6.0, *)
struct X: P {
  typealias A = (Int) throws(MyBigError) -> Void
}

func requiresP<T: P>(_: T.Type) { }

@available(SwiftStdlib 6.0, *)
func createsP() {
  requiresP(X.self)
}

// This is for TypeH.method
// CHECK-NOMANGLE: @"$s12typed_throws5TypeHV6methodySSSiAA10MyBigErrorOYKcvpMV" =


// CHECK-LABEL: define {{.*}}hidden swiftcc ptr @"$s12typed_throws13buildMetatypeypXpyF"()
@available(SwiftStdlib 6.0, *)
func buildMetatype() -> Any.Type {
  typealias Fn = (Int) throws(MyBigError) -> Void

  // CHECK-MANGLE: __swift_instantiateConcreteTypeFromMangledName(ptr @"$sySi12typed_throws10MyBigErrorOYKcMD

  // CHECK-NOMANGLE: call swiftcc %swift.metadata_response @"$sySi12typed_throws10MyBigErrorOYKcMa"
  return Fn.self
}

// // CHECK-NOMANGLE: define linkonce_odr hidden swiftcc %swift.metadata_response @"$sySi12typed_throws10MyBigErrorOYKcMa"
// // CHECK-NOMANGLE: @swift_getExtendedFunctionTypeMetadata({{.*}}@"$s12typed_throws10MyBigErrorOMf"

protocol P {
  associatedtype A
}

// The following ensures that we lower
func passthroughCall<T, E>(_ body: () throws(E) -> T) throws(E) -> T {
  try body()
}

func five() -> Int { 5 }

func fiveOrBust() throws -> Int { 5 }

func fiveOrTypedBust() throws(MyBigError) -> Int { throw MyBigError.epicFail }

func reabstractAsNonthrowing() -> Int {
  passthroughCall(five)
}

func reabstractAsThrowing() throws -> Int {
  try passthroughCall(fiveOrBust)
}


func reabstractAsConcreteThrowing() throws -> Int {
  try passthroughCall(fiveOrTypedBust)
}

// CHECK-LABEL: define {{.*}} swiftcc void @"$sSi12typed_throws10MyBigErrorOIgdzo_SiACIegrzr_TR"(ptr noalias nocapture sret(%TSi) %0, ptr %1, ptr %2, ptr swiftself %3, ptr noalias nocapture swifterror dereferenceable(8) %4, ptr %5)
// CHECK: call swiftcc {{i32|i64}} %1
// CHECK: [[CMP:%.*]] = icmp ne ptr {{%.*}}, null
// CHECK: br i1 [[CMP]], label %typed.error.load


struct S : Error { }

func callee() throws (S) {
  throw S()
}

// This used to crash at compile time.
func testit() throws (S) {
  try callee()
}

// Used to crash in abstract pattern creation.
public struct TypeH {
  public var method: (Int) throws(MyBigError) -> String
}

struct SmallError: Error {
  let x: Int
}

@inline(never)
func throwsSmallError() throws(SmallError) -> (Float, Int) {
  throw SmallError(x: 1)
}

// CHECK: define hidden swiftcc i64 @"$s12typed_throws17catchesSmallErrorSiyF"()
// CHECK:   [[RES:%.*]] = call swiftcc { float, i64 } @"$s12typed_throws0B10SmallErrorSf_SityAA0cD0VYKF"(ptr swiftself undef, ptr noalias nocapture swifterror dereferenceable(8) %swifterror)
// CHECK:   [[R0:%.*]] = extractvalue { float, i64 } [[RES]], 0
// CHECK:   [[R1:%.*]] = extractvalue { float, i64 } [[RES]], 1
// CHECK:   phi i64 [ [[R1]], %typed.error.load ]
// CHECK: }
func catchesSmallError() -> Int {
  do {
    return try throwsSmallError().1
  } catch {
    return error.x
  }
}

struct MyError: Error {
  let x: AnyObject
}

// CHECK: define hidden swiftcc { float, i64, float } @"$s12typed_throws8mayThrow1x1ySf_s5Int32VSftSb_yXltAA7MyErrorVYKF"
// CHECK:   [[CONVERTED:%.*]] = ptrtoint ptr {{%.*}} to i64
// CHECK:   insertvalue { float, i64, float } undef, i64 [[CONVERTED]], 1
// CHECK: }
@inline(never)
func mayThrow(x: Bool, y: AnyObject) throws(MyError) -> (Float, Int32, Float) {
  guard x else {
    throw MyError(x: y)
  }
  return (3.0, 4, 5.0)
}

// CHECK: define hidden swiftcc { i64, i64 } @"$s12typed_throws25directErrorMergePtrAndInt1x1yyXl_SitSb_yXltAA05SmallD0VYKF"
// CHECK:   [[RES:%.*]] = call swiftcc { i64, i64 } @"$s12typed_throws25directErrorMergePtrAndInt1x1yyXl_SitSb_yXltAA05SmallD0VYKF"
// CHECK:   [[R0:%.*]] = extractvalue { i64, i64 } [[RES]], 0
// CHECK:   inttoptr i64 [[R0]] to ptr
// CHECK: }
func directErrorMergePtrAndInt(x: Bool, y: AnyObject) throws(SmallError) -> (AnyObject, Int) {
  guard x else {
    throw SmallError(x: 1)
  }

  return try directErrorMergePtrAndInt(x: !x, y: y)
}

// This used to crash at compile time, because it was trying to use a direct
// error return in combination with an indirect result, which is illegal.
func genericThrows<T>(x: Bool, y: T) throws(SmallError) -> T {
  guard x else {
    throw SmallError(x: 1)
  }

  return y
}

func throwsGeneric<T: Error>(x: Bool, y: T) throws(T) -> Int {
  guard x else {
    throw y
  }

  return 32
}

@available(SwiftStdlib 6.0, *)
func mayThrowAsync(x: Bool, y: AnyObject) async throws(MyError) -> (Float, Int32, Float) {
  guard x else {
    throw MyError(x: y)
  }
  return (3.0, 4, 5.0)
}

@available(SwiftStdlib 6.0, *)
func genericThrowsAsync<T>(x: Bool, y: T) async throws(SmallError) -> T {
  guard x else {
    throw SmallError(x: 1)
  }

  return y
}

@available(SwiftStdlib 6.0, *)
func throwsGenericAsync<T: Error>(x: Bool, y: T) async throws(T) -> Int {
  guard x else {
    throw y
  }

  return 32
}

enum TinyError: Error {
  case a
}

@available(SwiftStdlib 6.0, *)
func mayThrowAsyncTiny(x: Bool) async throws(TinyError) -> Bool {
  guard x else {
    throw .a
  }
  return false
}

@available(SwiftStdlib 6.0, *)
func callsMayThrowAsyncTiny(x: Bool) async {
  _ = try! await mayThrowAsyncTiny(x: x)
}

struct EmptyError: Error {}

@available(SwiftStdlib 6.0, *)
func mayThrowEmptyErrorAsync(x: Bool) async throws(EmptyError) -> String? {
  guard x else {
    throw EmptyError()
  }

  return ""
}


enum SP: Error {
    case a
    case b(Int32)
}

protocol Proto {
  // This used to crash.
  static func f() throws(SP) -> Self

  // This used to crash.
  static func f2() throws(SP) -> Int64
}

@inline(never)
@available(SwiftStdlib 6.0, *)
public func passthroughAsync<T, E: Error>(f: () async throws(E) -> T) async throws(E) -> T {
  try await f()
}

@available(SwiftStdlib 6.0, *)
public func reabstractAsyncVoidThrowsNever() async {
  await passthroughAsync {
    ()
  }
}

// Used to crash with null GenericSignature -- https://github.com/swiftlang/swift/issues/77297
struct LoadableGeneric<E>: Error {}

func throwsLoadableGeneric<E>(_: E) throws(LoadableGeneric<E>) {}

@inline(never)
func throwError() throws(SmallError) -> Never {
  throw SmallError(x: 1)
}

func conditionallyCallsThrowError(b: Bool) throws(SmallError) -> Int {
  if b {
    try throwError()
  } else {
    return 0
  }
}

func passthroughFixedErrorCall<T>(_ body: () throws(TinyError) -> T) throws(TinyError) -> T {
  try body()
}

func passthroughFixedErrorAsync<T>(_ body: () async throws(TinyError) -> T) async throws(TinyError) -> T {
  try await body()
}

func callClosureSync<T>(t: T) {
  _ = try! passthroughFixedErrorCall { () throws(TinyError) -> T in
    return t
  }
}

func callClosureAsync<T>(t: T) async {
  _ = try! await passthroughFixedErrorAsync { () async throws(TinyError) -> T in
    return t
  }
}

enum LargeError: Error {
  case x
  case y(Int64, Int64, Int64, Int64, Int64)
}

// Used to crash the compiler because
func callClosureAsyncIndirectError(f: () async throws(LargeError) -> Int) async throws(LargeError) -> Int {
  return try await f()
}

protocol AsyncGenProto<A> {
  associatedtype A
  func fn(arg: Int) async throws(SmallError) -> A
}

// CHECK: define internal swifttailcc void @"$s12typed_throws23callAsyncIndirectResult1p1xxAA0D8GenProto_px1ARts_XP_SitYaAA10SmallErrorVYKlFTY0_"(ptr swiftasync %0)
// CHECK:   musttail call swifttailcc void {{%.*}}(ptr noalias {{%.*}}, ptr swiftasync {{%.*}}, i64 {{%.*}}, ptr noalias swiftself {{%.*}}, ptr %swifterror, ptr {{%.*}}, ptr {{%.*}})
// CHECK:   ret void
// CHECK: }
@inline(never)
func callAsyncIndirectResult<A>(p: any AsyncGenProto<A>, x: Int) async throws(SmallError) -> A {
  return try await p.fn(arg: x)
}

@inline(never)
func smallResultLargerError() throws(SmallError) -> Int8? {
  return 10
}

// CHECK:  [[COERCED:%.*]] = alloca { i16 }, align 2
// CHECK:  [[RES:%.*]] = call swiftcc i64 @"$s12typed_throws22smallResultLargerErrors4Int8VSgyAA05SmallF0VYKF"(ptr swiftself undef, ptr noalias nocapture swifterror dereferenceable(8) %swifterror)
// CHECK:  [[TRUNC:%.*]] = trunc i64 [[RES]] to i16
// CHECK:  [[COERCED_PTR:%.*]] = getelementptr inbounds { i16 }, ptr [[COERCED]], i32 0, i32 0
// CHECK:  store i16 [[TRUNC]], ptr [[COERCED_PTR]], align 2
func callSmallResultLargerError() {
  let res = try! smallResultLargerError()
  precondition(res! == 10)
}

enum UInt8OptSingletonError: Error {
  case a(Int8?)
}

@inline(never)
func smallErrorLargerResult() throws(UInt8OptSingletonError) -> Int {
  throw .a(10)
}

// CHECK:  [[COERCED:%.*]] = alloca { i16 }, align 2
// CHECK:  [[RES:%.*]] = call swiftcc i64 @"$s12typed_throws22smallErrorLargerResultSiyAA017UInt8OptSingletonD0OYKF"(ptr swiftself undef, ptr noalias nocapture swifterror dereferenceable(8) %swifterror)
// CHECK:  [[TRUNC:%.*]] = trunc i64 [[RES]] to i16
// CHECK:  [[COERCED_PTR:%.*]] = getelementptr inbounds { i16 }, ptr [[COERCED]], i32 0, i32 0
// CHECK:  store i16 [[TRUNC]], ptr [[COERCED_PTR]], align 2
func callSmallErrorLargerResult() {
  do {
    _ = try smallErrorLargerResult()
  } catch {
    switch error {
      case .a(let x):
        precondition(x! == 10)
    }
  }
}

struct SomeStruct {
    let x: Int
    let y: UInt32
    let z: UInt32
}

func someFunc() async throws(SmallError) -> SomeStruct {
    SomeStruct(x: 42, y: 23, z: 25)
}