|
| 1 | +// RUN: %empty-directory(%t) |
| 2 | +// RUN: %target-swiftc_driver -Xfrontend -enable-experimental-concurrency %s -o %t/out |
| 3 | +// RUN: %target-run %t/out |
| 4 | + |
| 5 | +// REQUIRES: concurrency |
| 6 | +// REQUIRES: objc_interop |
| 7 | +// REQUIRES: executable_test |
| 8 | + |
| 9 | +import ObjectiveC |
| 10 | +import _Concurrency |
| 11 | +import StdlibUnittest |
| 12 | + |
| 13 | +defer { runAllTests() } |
| 14 | + |
| 15 | +var Tests = TestSuite("Actor.AssocObject") |
| 16 | + |
| 17 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 18 | +final actor class Actor { |
| 19 | +} |
| 20 | + |
| 21 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 22 | + Tests.test("final class crash when set assoc object") |
| 23 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 24 | + .code { |
| 25 | + expectCrashLater() |
| 26 | + let x = Actor() |
| 27 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 28 | + } |
| 29 | +} |
| 30 | + |
| 31 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 32 | +actor class Actor2 { |
| 33 | +} |
| 34 | + |
| 35 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 36 | + Tests.test("non-final class crash when set assoc object") |
| 37 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 38 | + .code { |
| 39 | + expectCrashLater() |
| 40 | + let x = Actor2() |
| 41 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 42 | + } |
| 43 | +} |
| 44 | + |
| 45 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 46 | +class Actor3 : Actor2 {} |
| 47 | + |
| 48 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 49 | + Tests.test("non-final subclass crash when set assoc object") |
| 50 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 51 | + .code { |
| 52 | + expectCrashLater() |
| 53 | + let x = Actor3() |
| 54 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 55 | + } |
| 56 | +} |
| 57 | + |
| 58 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 59 | +final class Actor3Final : Actor2 {} |
| 60 | + |
| 61 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 62 | + Tests.test("final subclass crash when set assoc object") |
| 63 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 64 | + .code { |
| 65 | + expectCrashLater() |
| 66 | + let x = Actor3Final() |
| 67 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 68 | + } |
| 69 | +} |
| 70 | + |
| 71 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 72 | +class Actor4<T> : Actor2 { |
| 73 | + var state: T |
| 74 | + init(state: T) { self.state = state } |
| 75 | +} |
| 76 | + |
| 77 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 78 | + Tests.test("generic subclass crash when set assoc object") |
| 79 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 80 | + .code { |
| 81 | + expectCrashLater() |
| 82 | + let x = Actor4(state: 5) |
| 83 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 88 | +actor class Actor5<T> { |
| 89 | + var state: T |
| 90 | + init(state: T) { self.state = state } |
| 91 | +} |
| 92 | + |
| 93 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 94 | + Tests.test("base generic class crash when set assoc object") |
| 95 | + .xfail( |
| 96 | + .custom({ true }, reason: "We appear to be stomping on isa pointers during " + |
| 97 | + "actor generic class isa initialization: rdar://70589739")) |
| 98 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 99 | + .code { |
| 100 | + expectCrashLater() |
| 101 | + let x = Actor5(state: 5) |
| 102 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 103 | + } |
| 104 | + |
| 105 | + Tests.test("base generic class metatype crash when set assoc object") |
| 106 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 107 | + .code { |
| 108 | + expectCrashLater() |
| 109 | + let x = Actor5<Int>.self |
| 110 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 115 | +class Actor6<T> : Actor5<T> { |
| 116 | + override init(state: T) { super.init(state: state) } |
| 117 | +} |
| 118 | + |
| 119 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 120 | + Tests.test("sub-generic class base generic class crash when set assoc object") |
| 121 | + .xfail( |
| 122 | + .custom({ true }, reason: "We appear to be stomping on isa pointers during " + |
| 123 | + "actor generic class isa initialization: rdar://70589739")) |
| 124 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 125 | + .code { |
| 126 | + expectCrashLater() |
| 127 | + let x = Actor6(state: 5) |
| 128 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 129 | + } |
| 130 | +} |
| 131 | + |
| 132 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 133 | +final class Actor6Final<T> : Actor5<T> { |
| 134 | + override init(state: T) { super.init(state: state) } |
| 135 | +} |
| 136 | + |
| 137 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 138 | + Tests.test("final sub-generic class base generic class crash when set assoc object") |
| 139 | + .xfail( |
| 140 | + .custom({ true }, reason: "We appear to be stomping on isa pointers during " + |
| 141 | + "actor generic class isa initialization: rdar://70589739")) |
| 142 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 143 | + .code { |
| 144 | + expectCrashLater() |
| 145 | + let x = Actor6Final(state: 5) |
| 146 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 147 | + } |
| 148 | + |
| 149 | + Tests.test("final sub-generic class base generic class crash when set assoc object2") |
| 150 | + .xfail( |
| 151 | + .custom({ true }, reason: "We appear to be stomping on isa pointers during " + |
| 152 | + "actor generic class isa initialization: rdar://70589739")) |
| 153 | + .code { |
| 154 | + let x = Actor6Final(state: 5) |
| 155 | + print(type(of: x)) |
| 156 | + } |
| 157 | + |
| 158 | + Tests.test("final sub-generic class metatype, base generic class crash when set assoc object") |
| 159 | + .crashOutputMatches("objc_setAssociatedObject called on instance") |
| 160 | + .code { |
| 161 | + expectCrashLater() |
| 162 | + let x = Actor6Final<Int>.self |
| 163 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 164 | + } |
| 165 | +} |
| 166 | + |
| 167 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 168 | +actor class ActorNSObjectSubKlass : NSObject {} |
| 169 | + |
| 170 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 171 | + Tests.test("no crash when inherit from nsobject") |
| 172 | + .code { |
| 173 | + let x = ActorNSObjectSubKlass() |
| 174 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 175 | + } |
| 176 | +} |
| 177 | + |
| 178 | +@available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) |
| 179 | +actor class ActorNSObjectSubKlassGeneric<T> : NSObject { |
| 180 | + var state: T |
| 181 | + init(state: T) { self.state = state } |
| 182 | +} |
| 183 | + |
| 184 | +if #available(macOS 10.4.4, iOS 12.2, watchOS 5.2, tvOS 12.2, *) { |
| 185 | + Tests.test("no crash when generic inherit from nsobject") |
| 186 | + .code { |
| 187 | + let x = ActorNSObjectSubKlassGeneric(state: 5) |
| 188 | + objc_setAssociatedObject(x, "myKey", "myValue", .OBJC_ASSOCIATION_RETAIN) |
| 189 | + } |
| 190 | +} |
0 commit comments