Skip to content

Commit da95749

Browse files
authored
rdar://127199021, fix compatibility with apps that accidentally do [[[aSwiftArray class] new] mutableCopy] to work (#73755)
1 parent 6b9d96a commit da95749

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-0
lines changed

stdlib/public/stubs/SwiftNativeNSXXXBase.mm.gyb

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,19 @@ swift_stdlib_connectNSBaseClasses() {
103103
return true;
104104
}
105105

106+
@interface __SwiftNativeNSArrayBase (Compatibility)
107+
+ (id) new;
108+
@end
109+
110+
@implementation __SwiftNativeNSArrayBase (Compatibility)
111+
112+
+ (id) new {
113+
// Some apps accidentally do [[[aSwiftArray class] new] mutableCopy]
114+
return [objc_lookUpClass("NSArray") new];
115+
}
116+
117+
@end
118+
106119
#endif
107120

108121
// ${'Local Variables'}:

test/stdlib/ArrayBridge.swift.gyb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -422,6 +422,11 @@ tests.test("testThunks") {
422422
testBridgeableValue(Thunks())
423423
}
424424

425+
tests.test("testHKTFilter") {
426+
let base = ["hello", "world"]
427+
let result = testHKTFilter(base) as! NSArray as! [String]
428+
expectEqual(result, ["hello"])
429+
}
425430

426431
tests.test("testRoundTrip") {
427432
class Test : NSObject {

test/stdlib/Inputs/ArrayBridge/ArrayBridge.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ NSArray* idAsArray(id a);
1616

1717
void testSubclass(id thunks);
1818
void testBridgeableValue(id thunks);
19+
id testHKTFilter(id array);
1920

2021
@interface RDar27905230 : NSObject
2122
+ (NSDictionary<NSString *, NSArray<id> *> *)mutableDictionaryOfMutableLists;

test/stdlib/Inputs/ArrayBridge/ArrayBridge.m

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,22 @@ void testBridgeableValue(id thunks) {
6767
[thunks acceptBridgeableValueArray: toSwiftArr];
6868
}
6969

70+
static id filter(id<NSFastEnumeration, NSObject> container, BOOL (^predicate)(id)) {
71+
id result = [[[container class] new] mutableCopy];
72+
for (id object in container) {
73+
if (predicate(object)) {
74+
[result addObject:object];
75+
}
76+
}
77+
return result;
78+
}
79+
80+
id testHKTFilter(id array) {
81+
return filter(array, ^(id obj) {
82+
return [obj isEqual:@"hello"];
83+
});
84+
}
85+
7086
@implementation RDar27905230
7187

7288
+ (NSDictionary<NSString *, NSArray<id> *> *)mutableDictionaryOfMutableLists {

0 commit comments

Comments
 (0)