|
| 1 | +// RUN: %target-typecheck-verify-swift -enable-experimental-feature StaticExclusiveOnly |
| 2 | + |
| 3 | +@_staticExclusiveOnly // expected-error {{@_staticExclusiveOnly can only be applied to noncopyable types}} |
| 4 | +struct A {} |
| 5 | + |
| 6 | +@_staticExclusiveOnly // OK |
| 7 | +struct B: ~Copyable { // expected-note {{'B' is a non-mutable type}} |
| 8 | + // expected-note@-1 {{'B' is a non-mutable type}} |
| 9 | + // expected-note@-2 {{'B' is a non-mutable type}} |
| 10 | + // expected-note@-3 {{'B' is a non-mutable type}} |
| 11 | + // expected-note@-4 {{'B' is a non-mutable type}} |
| 12 | + mutating func change() { // expected-error {{type 'B' cannot have mutating function 'change()'}} |
| 13 | + print("123") |
| 14 | + } |
| 15 | +} |
| 16 | + |
| 17 | +let b0 = B() // OK |
| 18 | + |
| 19 | +var b1 = B() // expected-error {{variable of type 'B' must be declared with a 'let'}} |
| 20 | + |
| 21 | +class C { |
| 22 | + var b2 = B() // expected-error {{variable of type 'B' must be declared with a 'let'}} |
| 23 | + let b3 = B() // OK |
| 24 | +} |
| 25 | + |
| 26 | +struct D: ~Copyable { |
| 27 | + var b4 = B() // expected-error {{variable of type 'B' must be declared with a 'let'}} |
| 28 | + let b5 = B() // OK |
| 29 | +} |
| 30 | + |
| 31 | +func e(_: borrowing B) {} // OK |
| 32 | + |
| 33 | +func f(_: inout B) {} // expected-error {{parameter of type 'B' must be declared as either 'borrowing' or 'consuming'}} |
| 34 | + |
| 35 | +func g(_: (inout B) -> ()) {} // expected-error {{parameter of type 'B' must be declared as either 'borrowing' or 'consuming'}} |
| 36 | + |
| 37 | +func h(_: (borrowing B) -> ()) {} // OK |
| 38 | + |
| 39 | +func i() { |
| 40 | + let _: (Int, Int) -> Int = { |
| 41 | + var b6 = B() // expected-error {{variable of type 'B' must be declared with a 'let'}} |
| 42 | + // expected-warning@-1 {{initialization of variable 'b6' was never used; consider replacing with assignment to '_' or removing it}} |
| 43 | + |
| 44 | + return $0 + $1 |
| 45 | + } |
| 46 | +} |
| 47 | + |
| 48 | +@_staticExclusiveOnly // expected-error {{@_staticExclusiveOnly may only be used on 'struct' declarations}} |
| 49 | +enum J {} |
| 50 | + |
| 51 | +@_staticExclusiveOnly // expected-error {{@_staticExclusiveOnly may only be used on 'struct' declarations}} |
| 52 | +class K {} |
| 53 | + |
| 54 | +@_staticExclusiveOnly // expected-error {{@_staticExclusiveOnly may only be used on 'struct' declarations}} |
| 55 | +func l() {} |
| 56 | + |
| 57 | +@_staticExclusiveOnly // expected-error {{@_staticExclusiveOnly may only be used on 'struct' declarations}} |
| 58 | +let m = 123 |
| 59 | + |
| 60 | +@_staticExclusiveOnly // expected-error {{@_staticExclusiveOnly may only be used on 'struct' declarations}} |
| 61 | +protocol N {} |
| 62 | + |
| 63 | +func o(_: consuming B) {} // OK |
| 64 | + |
| 65 | +func p(_: (consuming B) -> ()) {} // OK |
| 66 | + |
| 67 | +@_staticExclusiveOnly |
| 68 | +struct Q<T>: ~Copyable {} // expected-note {{'Q<T>' is a non-mutable type}} |
| 69 | + |
| 70 | +var r0 = Q<Int>() // expected-error {{variable of type 'Q<Int>' must be declared with a 'let'}} |
0 commit comments