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
|
// RUN: %target-typecheck-verify-swift -enable-experimental-move-only -parse-stdlib -disable-availability-checking
import Swift
class Klass {}
func argumentsAndReturns(@_noImplicitCopy _ x: Klass) -> Klass {
return x
}
func letDecls(_ x: Klass) -> () {
@_noImplicitCopy let y: Klass = x
print(y)
}
func varDecls(_ x: Klass, _ x2: Klass) -> () {
@_noImplicitCopy var y: Klass = x // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
y = x2
print(y)
}
func getKlass() -> Builtin.NativeObject {
let k = Klass()
return Builtin.unsafeCastToNativeObject(k)
}
@_noImplicitCopy var g: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy let g2: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy var g3: Builtin.NativeObject { getKlass() } // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
struct MyStruct {
// Error if @_noImplicitCopy on struct fields. We do not have move only types and
// these are part of MyStruct.
//
// TODO: Make error specific for move only on struct/enum.
@_noImplicitCopy var x: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy let y: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return getKlass()
}
func foo<T>(@_noImplicitCopy _ t: T) {
}
}
struct MyGenericStruct<T> {
func foo(@_noImplicitCopy _ t: T) {
}
}
protocol P {
@_noImplicitCopy var x: Builtin.NativeObject { get } // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
}
func foo<T>(@_noImplicitCopy _ t: T) {
}
// Do not error on class fields. The noImplicitCopy field is separate from the
// underlying class itself so the fact the class is not move only does not
// suggest that the binding inside the class can be.
class MyClass {
@_noImplicitCopy var x: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy let y: Builtin.NativeObject = getKlass() // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return getKlass()
}
func foo<T>(@_noImplicitCopy _ t: T) {
}
}
class MyGenericClass<T> {
@_noImplicitCopy var x: T? = nil // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy let y: T? = nil // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
@_noImplicitCopy var myMoveOnly: T? { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return nil
}
@_noImplicitCopy var myMoveOnly2: Builtin.NativeObject? { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return nil
}
func foo(@_noImplicitCopy _ t: T) {
}
}
// We need to error on Enums since the case is part of the value and we do not
// support move only types.
enum MyEnum {
case none
case noImplicitCopyCase(Klass)
// We support doing it on computed properties though.
@_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return getKlass()
}
}
// We need to error on Enums since the case is part of the value and we do not
// support move only types.
enum MyGenericEnum<T> {
case none
case noImplicitCopyCase(Klass)
// We support doing it on computed properties though.
@_noImplicitCopy var myMoveOnly: Builtin.NativeObject { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return getKlass()
}
// We support doing it on computed properties though.
@_noImplicitCopy var myMoveOnly2: T? { // expected-error {{'@_noImplicitCopy' attribute can only be applied to local lets}}
return nil
}
}
struct UnsafePointerWithOwner<T> {
var owner: AnyObject? = nil
var data: UnsafePointer<T>? = nil
func doNothing() {}
}
func useUnsafePointerWithOwner<T>(_ x: UnsafePointerWithOwner<T>) {
// We allow for this here (even without opaque values, since we check this
// at the SIL level in SILGen).
@_noImplicitCopy let y = x
y.doNothing()
let z = y
print(z)
}
func useGeneric<T>(_ x: T) {
// We allow for this here (even without opaque values, since we check this
// at the SIL level in SILGen).
@_noImplicitCopy let y = x
let z = y
print(z)
}
@_moveOnly
struct MoveOnly {
var k = Klass()
}
func useMoveOnly(@_noImplicitCopy _ x: __shared MoveOnly) -> MoveOnly { // expected-error {{'@_noImplicitCopy' has no effect when applied to a noncopyable type}}
return x
}
func useMoveOnly2(_ x: __shared MoveOnly) {
@_noImplicitCopy let y = x // expected-error {{'@_noImplicitCopy' has no effect when applied to a noncopyable type}}
let _ = y
}
|