File: unavailable_from_async.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 (169 lines) | stat: -rw-r--r-- 6,737 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
// RUN: %empty-directory(%t)
// RUN: %target-swift-frontend -emit-module -emit-module-path %t/UnavailableFunction.swiftmodule -module-name UnavailableFunction -strict-concurrency=complete %S/Inputs/UnavailableFunction.swift
// RUN: %target-swift-frontend -verify -I %t %s -emit-sil -o /dev/null
// RUN: %target-swift-frontend -verify -I %t %s -emit-sil -o /dev/null -strict-concurrency=targeted
// RUN: %target-swift-frontend -verify -I %t %s -emit-sil -o /dev/null -strict-concurrency=complete

// REQUIRES: concurrency
// REQUIRES: asserts

import UnavailableFunction

@available(SwiftStdlib 5.1, *)
func okay() {}

// expected-error@+1{{'@_unavailableFromAsync' attribute cannot be applied to this declaration}}
@_unavailableFromAsync
@available(SwiftStdlib 5.1, *)
struct Foo { }

// expected-error@+1{{'@_unavailableFromAsync' attribute cannot be applied to this declaration}}
@_unavailableFromAsync
@available(SwiftStdlib 5.1, *)
extension Foo { }

// expected-error@+1{{'@_unavailableFromAsync' attribute cannot be applied to this declaration}}
@_unavailableFromAsync
@available(SwiftStdlib 5.1, *)
class Bar {
  // expected-error@+1{{'@_unavailableFromAsync' attribute cannot be applied to this declaration}}
  @_unavailableFromAsync
  deinit { }
}

// expected-error@+1{{'@_unavailableFromAsync' attribute cannot be applied to this declaration}}
@_unavailableFromAsync
@available(SwiftStdlib 5.1, *)
actor Baz { }

@available(SwiftStdlib 5.1, *)
struct Bop {
  @_unavailableFromAsync(message: "Use Bop(a: Int) instead")
  init() {}                 // expected-note 4 {{'init()' declared here}}

  init(a: Int) { }
}

@available(SwiftStdlib 5.1, *)
extension Bop {
  @_unavailableFromAsync
  func foo() {}             // expected-note 4 {{'foo()' declared here}}


  @_unavailableFromAsync
  mutating func muppet() { }  // expected-note 4 {{'muppet()' declared here}}
}

@_unavailableFromAsync
@available(SwiftStdlib 5.1, *)
func foo() {}               // expected-note 4 {{'foo()' declared here}}

@available(SwiftStdlib 5.1, *)
func makeAsyncClosuresSynchronously(bop: inout Bop) -> (() async -> Void) {
  return { () async -> Void in
    // Unavailable methods
    _ = Bop()     // expected-warning@:9{{'init' is unavailable from asynchronous contexts; Use Bop(a: Int) instead}}
    _ = Bop(a: 32)
    bop.foo()     // expected-warning@:9{{'foo' is unavailable from asynchronous contexts}}
    bop.muppet()  // expected-warning@:9{{'muppet' is unavailable from asynchronous contexts}}
    unavailableFunction() // expected-warning@:5{{'unavailableFunction' is unavailable from asynchronous contexts}}
    noasyncFunction() // expected-warning@:5{{'noasyncFunction' is unavailable from asynchronous contexts}}

    // Can use them from synchronous closures
    _ = { Bop() }()
    _ = { bop.foo() }()
    _ = { bop.muppet() }()
    _ = { noasyncFunction() }()

    // Unavailable global function
    foo()         // expected-warning{{'foo' is unavailable from asynchronous contexts}}

    // Okay function
    okay()
  }
}

@available(SwiftStdlib 5.1, *)
@_unavailableFromAsync
func asyncFunc() async { // expected-error{{asynchronous global function 'asyncFunc()' must be available from asynchronous contexts}}

  var bop = Bop(a: 32)
  _ = Bop()     // expected-warning@:7{{'init' is unavailable from asynchronous contexts; Use Bop(a: Int) instead}}
  bop.foo()     // expected-warning@:7{{'foo' is unavailable from asynchronous contexts}}
  bop.muppet()  // expected-warning@:7{{'muppet' is unavailable from asynchronous contexts}}
  unavailableFunction() // expected-warning@:3{{'unavailableFunction' is unavailable from asynchronous contexts}}
  noasyncFunction() // expected-warning@:3{{'noasyncFunction' is unavailable from asynchronous contexts}}

  // Unavailable global function
  foo()         // expected-warning{{'foo' is unavailable from asynchronous contexts}}

  // Available function
  okay()

  _ = { () -> Void in
    // Check unavailable things inside of a nested synchronous closure
    _ = Bop()
    foo()
    bop.foo()
    bop.muppet()
    unavailableFunction()
    noasyncFunction()

    _ = { () async -> Void in
      // Check Unavailable things inside of a nested async closure
      foo()           // expected-warning@:7{{'foo' is unavailable from asynchronous contexts}}
      bop.foo()       // expected-warning@:11{{'foo' is unavailable from asynchronous contexts}}
      bop.muppet()    // expected-warning@:11{{'muppet' is unavailable from asynchronous contexts}}
      _ = Bop()       // expected-warning@:11{{'init' is unavailable from asynchronous contexts; Use Bop(a: Int) instead}}
      unavailableFunction() // expected-warning@:7{{'unavailableFunction' is unavailable from asynchronous contexts}}
      noasyncFunction() // expected-warning@:7{{'noasyncFunction' is unavailable from asynchronous contexts}}
    }
  }

  _ = { () async -> Void in
    _ = Bop()     // expected-warning@:9{{'init' is unavailable from asynchronous contexts; Use Bop(a: Int) instead}}
    foo()         // expected-warning@:5{{'foo' is unavailable from asynchronous contexts}}
    bop.foo()     // expected-warning@:9{{'foo' is unavailable from asynchronous contexts}}
    bop.muppet()  // expected-warning@:9{{'muppet' is unavailable from asynchronous contexts}}
    unavailableFunction() // expected-warning@:5{{'unavailableFunction' is unavailable from asynchronous contexts}}
    noasyncFunction() // expected-warning@:5{{'noasyncFunction' is unavailable from asynchronous contexts}}

    _ = {
      foo()
      bop.foo()
      _ = Bop()
      unavailableFunction()
    }
  }

}

// Parsing tests

// expected-error@+2 {{expected declaration}}
// expected-error@+1:24{{unknown option 'nope' for attribute '_unavailableFromAsync'}}
@_unavailableFromAsync(nope: "almost right, but not quite")
func blarp1() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1 {{expected ':' after label 'message'}}
@_unavailableFromAsync(message; "almost right, but not quite")
func blarp2() {}

// expected-error@+1:31 {{'=' has been replaced with ':' in attribute arguments}}{{31-32=: }}
@_unavailableFromAsync(message="almost right, but not quite")
func blarp3() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1 {{expected string literal in '_unavailableFromAsync' attribute}}
@_unavailableFromAsync(message: 32)
func blarp4() {}

// expected-error@+2 {{expected declaration}}
// expected-error@+1 {{message cannot be an interpolated string}}
@_unavailableFromAsync(message: "blarppy blarp \(31 + 10)")
func blarp5() {}

// expected-error@+1:48 {{expected ')' in '_unavailableFromAsync' attribute}}{{48-48=)}}
@_unavailableFromAsync(message: "blarppy blarp"
func blarp6() {}