|
| 1 | +// RUN: %target-typecheck-verify-swift -parse-stdlib -disable-availability-checking -verify-syntax-tree |
| 2 | + |
| 3 | +import Swift |
| 4 | + |
| 5 | +class Klass {} |
| 6 | + |
| 7 | +func argumentsAndReturns(@_noImplicitCopy _ x: Klass) -> Klass { // expected-error {{@_noImplicitCopy may only be used on 'var' declarations}} |
| 8 | + return x |
| 9 | +} |
| 10 | +func letDecls(_ x: Klass) -> () { |
| 11 | + @_noImplicitCopy let y: Klass = x |
| 12 | + print(y) |
| 13 | +} |
| 14 | + |
| 15 | +func varDecls(_ x: Klass, _ x2: Klass) -> () { |
| 16 | + @_noImplicitCopy var y: Klass = x // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 17 | + y = x2 |
| 18 | + print(y) |
| 19 | +} |
| 20 | + |
| 21 | +func getKlass() -> Builtin.NativeObject { |
| 22 | + let k = Klass() |
| 23 | + let b = Builtin.unsafeCastToNativeObject(k) |
| 24 | + return Builtin.move(b) |
| 25 | +} |
| 26 | + |
| 27 | +@_noImplicitCopy var g: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 28 | +@_noImplicitCopy let g2: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 29 | +@_noImplicitCopy var g3: Builtin.NativeObject { getKlass() } // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 30 | + |
| 31 | +struct MyStruct { |
| 32 | + // Error if @_noImplicitCopy on struct fields. We do not have move only types and |
| 33 | + // these are part of MyStruct. |
| 34 | + // |
| 35 | + // TODO: Make error specific for move only on struct/enum. |
| 36 | + @_noImplicitCopy var x: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 37 | + @_noImplicitCopy let y: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 38 | + |
| 39 | + @_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 40 | + return getKlass() |
| 41 | + } |
| 42 | + |
| 43 | + func foo<T>(@_noImplicitCopy _ t: T) { // expected-error {{@_noImplicitCopy may only be used on 'var' declarations}} |
| 44 | + } |
| 45 | +} |
| 46 | + |
| 47 | +struct MyGenericStruct<T> { |
| 48 | + func foo(@_noImplicitCopy _ t: T) { // expected-error {{@_noImplicitCopy may only be used on 'var' declarations}} |
| 49 | + } |
| 50 | +} |
| 51 | + |
| 52 | +protocol P { |
| 53 | + @_noImplicitCopy var x: Builtin.NativeObject { get } // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 54 | +} |
| 55 | + |
| 56 | +func foo<T>(@_noImplicitCopy _ t: T) { // expected-error {{@_noImplicitCopy may only be used on 'var' declarations}} |
| 57 | +} |
| 58 | + |
| 59 | +// Do not error on class fields. The noImplicitCopy field is separate from the |
| 60 | +// underlying class itself so the fact the class is not move only does not |
| 61 | +// suggest that the binding inside the class can be. |
| 62 | +class MyClass { |
| 63 | + @_noImplicitCopy var x: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 64 | + @_noImplicitCopy let y: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 65 | + |
| 66 | + @_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 67 | + return getKlass() |
| 68 | + } |
| 69 | + |
| 70 | + func foo<T>(@_noImplicitCopy _ t: T) { // expected-error {{@_noImplicitCopy may only be used on 'var' declarations}} |
| 71 | + } |
| 72 | +} |
| 73 | + |
| 74 | +class MyGenericClass<T> { |
| 75 | + @_noImplicitCopy var x: T? = nil // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 76 | + @_noImplicitCopy let y: T? = nil // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 77 | + |
| 78 | + @_noImplicitCopy var myMoveOnly: T? { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 79 | + return nil |
| 80 | + } |
| 81 | + |
| 82 | + @_noImplicitCopy var myMoveOnly2: Builtin.NativeObject? { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 83 | + return nil |
| 84 | + } |
| 85 | + |
| 86 | + func foo(@_noImplicitCopy _ t: T) { // expected-error {{@_noImplicitCopy may only be used on 'var' declarations}} |
| 87 | + } |
| 88 | +} |
| 89 | + |
| 90 | +// We need to error on Enums since the case is part of the value and we do not |
| 91 | +// support move only types. |
| 92 | +enum MyEnum { |
| 93 | + case none |
| 94 | + case noImplicitCopyCase(Klass) |
| 95 | + |
| 96 | + // We suport doing it on computed properties though. |
| 97 | + @_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 98 | + return getKlass() |
| 99 | + } |
| 100 | +} |
| 101 | + |
| 102 | +// We need to error on Enums since the case is part of the value and we do not |
| 103 | +// support move only types. |
| 104 | +enum MyGenericEnum<T> { |
| 105 | + case none |
| 106 | + case noImplicitCopyCase(Klass) |
| 107 | + |
| 108 | + // We suport doing it on computed properties though. |
| 109 | + @_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 110 | + return getKlass() |
| 111 | + } |
| 112 | + |
| 113 | + // We suport doing it on computed properties though. |
| 114 | + @_noImplicitCopy var myMoveOnly2: T? { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}} |
| 115 | + return nil |
| 116 | + } |
| 117 | +} |
| 118 | + |
| 119 | +struct UnsafePointerWithOwner<T> { |
| 120 | + var owner: AnyObject? = nil |
| 121 | + var data: UnsafePointer<T>? = nil |
| 122 | + |
| 123 | + func doNothing() {} |
| 124 | +} |
| 125 | + |
| 126 | +func useUnsafePointerWithOwner<T>(_ x: UnsafePointerWithOwner<T>) { |
| 127 | + // We allow for this here (even without opaque values, since we check this |
| 128 | + // at the SIL level in SILGen). |
| 129 | + @_noImplicitCopy let y = x |
| 130 | + y.doNothing() |
| 131 | + let z = y |
| 132 | + print(z) |
| 133 | +} |
| 134 | + |
| 135 | +func useGeneric<T>(_ x: T) { |
| 136 | + // We allow for this here (even without opaque values, since we check this |
| 137 | + // at the SIL level in SILGen). |
| 138 | + @_noImplicitCopy let y = x |
| 139 | + let z = y |
| 140 | + print(z) |
| 141 | +} |
0 commit comments