File: specialize_self_conforming_error.swift

package info (click to toggle)
swiftlang 6.0.3-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 2,519,992 kB
  • sloc: cpp: 9,107,863; ansic: 2,040,022; asm: 1,135,751; python: 296,500; objc: 82,456; f90: 60,502; lisp: 34,951; pascal: 19,946; sh: 18,133; perl: 7,482; ml: 4,937; javascript: 4,117; makefile: 3,840; awk: 3,535; xml: 914; fortran: 619; cs: 573; ruby: 573
file content (101 lines) | stat: -rw-r--r-- 3,441 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
// RUN: %target-swift-frontend -module-name specialize_self_conforming -emit-sil -O -primary-file %s | %FileCheck %s

// Test 1: apply the substitution
//   [U:Error => Error:Error]
// to the type argument map
//   [T:Error => U:Error]
// on the call to takesError.

@_optimize(none)
func takesError<T : Error>(_ t: T) {
  print(t)
}

@inline(__always)
func callsTakesError<U : Error>(_ error: U) {
  takesError(error)
}

// CHECK-LABEL: sil hidden @$s26specialize_self_conforming5test1yys5Error_pF : $@convention(thin) (@guaranteed any Error) -> () {
// CHECK: [[TEMP:%.*]] = alloc_stack $any Error
// CHECK: store %0 to [[TEMP]]
// CHECK: [[FN:%.*]] = function_ref @$s26specialize_self_conforming10takesErroryyxs0E0RzlF : $@convention(thin) <τ_0_0 where τ_0_0 : Error> (@in_guaranteed τ_0_0) -> ()
// CHECK: apply [[FN]]<any Error>([[TEMP]]) : $@convention(thin) <τ_0_0 where τ_0_0 : Error> (@in_guaranteed τ_0_0) -> ()
// CHECK: dealloc_stack [[TEMP]]
// CHECK: return

func test1(_ error: Error) {
  callsTakesError(error)
}

// Test 2: apply the substitution
//   [U => Int]
// to the type argument map
//   [T:Error => Error:Error]
// on the call to takesError.

@inline(__always)
func callsTakesErrorWithError<U>(_ error: Error, _ value : U) {
  takesError(error)
}

// CHECK-LABEL: sil hidden @$s26specialize_self_conforming5test2yys5Error_pF : $@convention(thin) (@guaranteed any Error) -> () {
// CHECK: [[TEMP:%.*]] = alloc_stack $any Error
// CHECK: store %0 to [[TEMP]]
// CHECK: [[FN:%.*]] = function_ref @$s26specialize_self_conforming10takesErroryyxs0E0RzlF : $@convention(thin) <τ_0_0 where τ_0_0 : Error> (@in_guaranteed τ_0_0) -> ()
// CHECK: apply [[FN]]<any Error>([[TEMP]]) : $@convention(thin) <τ_0_0 where τ_0_0 : Error> (@in_guaranteed τ_0_0) -> ()
// CHECK: dealloc_stack [[TEMP]]
// CHECK: return

func test2(_ error: Error) {
  callsTakesErrorWithError(error, 0)
}

// Test 3: apply the substitution
//   [V => Int]
// to the type argument map
//   [T:Error => Error:Error, U => V]
// on the call to takesErrorAndValue.

@_optimize(none)
func takesErrorAndValue<T : Error, U>(_ t: T, _ u: U) {
  print(t, u)
}

@inline(__always)
func callsTakesErrorAndValueWithError<U>(_ error: Error, _ value : U) {
  takesErrorAndValue(error, value)
}

// CHECK-LABEL: sil hidden @$s26specialize_self_conforming5test3yys5Error_pF : $@convention(thin) (@guaranteed any Error) -> () {
// CHECK: [[TEMP:%.*]] = alloc_stack $any Error
// CHECK: store %0 to [[TEMP]]
// CHECK: [[FN:%.*]] = function_ref @$s26specialize_self_conforming18takesErrorAndValueyyx_q_ts0E0Rzr0_lF : $@convention(thin) <τ_0_0, τ_0_1 where τ_0_0 : Error> (@in_guaranteed τ_0_0, @in_guaranteed τ_0_1) -> ()
// CHECK: apply [[FN]]<any Error, Int>([[TEMP]], {{%.*}}) :
// CHECK: dealloc_stack [[TEMP]]
// CHECK: return

func test3(_ error: Error) {
  callsTakesErrorAndValueWithError(error, 0)
}

// Test 4: just clone the type argument map
//   [Self:P => opened:P, T:Error => Error:Error]
// When the inliner is cloning a substitution map that includes an opened
// archetype, it uses a substitution function that expects not to be called
// on types it's not expecting.

protocol P {}
extension P {
  @_optimize(none)
  func exTakesError<T: Error>(_: T) {}
}

@inline(__always)
func callsExTakesErrorWithError(_ p: P, _ error: Error) {
  p.exTakesError(error)
}

func test4(_ p: P, _ error: Error) {
  callsExTakesErrorWithError(p, error)
}