Skip to content

Commit 3cdbe9b

Browse files
committed
rdar://127199021, fix compatibility with apps that accidentally do [[[aSwiftArray class] new] mutableCopy] to work
1 parent dbc8c4b commit 3cdbe9b

File tree

4 files changed

+31
-0
lines changed

4 files changed

+31
-0
lines changed

stdlib/public/stubs/SwiftNativeNSXXXBase.mm.gyb

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

106+
@interface __SwiftNativeNSArrayBase ()
107+
108+
+ (id) new {
109+
// Some apps accidentally do [[[aSwiftArray class] new] mutableCopy]
110+
return [objc_lookUpClass("NSArray") new];
111+
}
112+
113+
@end
114+
106115
#endif
107116

108117
// ${'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 Array
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)