Skip to content

rdar://127199021, fix compatibility with apps that accidentally do [[[aSwiftArray class] new] mutableCopy] #73755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions stdlib/public/stubs/SwiftNativeNSXXXBase.mm.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,19 @@ swift_stdlib_connectNSBaseClasses() {
return true;
}

@interface __SwiftNativeNSArrayBase (Compatibility)
+ (id) new;
@end

@implementation __SwiftNativeNSArrayBase (Compatibility)

+ (id) new {
// Some apps accidentally do [[[aSwiftArray class] new] mutableCopy]
return [objc_lookUpClass("NSArray") new];
}

@end

#endif

// ${'Local Variables'}:
Expand Down
5 changes: 5 additions & 0 deletions test/stdlib/ArrayBridge.swift.gyb
Original file line number Diff line number Diff line change
Expand Up @@ -422,6 +422,11 @@ tests.test("testThunks") {
testBridgeableValue(Thunks())
}

tests.test("testHKTFilter") {
let base = ["hello", "world"]
let result = testHKTFilter(base) as! NSArray as! [String]
expectEqual(result, ["hello"])
}

tests.test("testRoundTrip") {
class Test : NSObject {
Expand Down
1 change: 1 addition & 0 deletions test/stdlib/Inputs/ArrayBridge/ArrayBridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ NSArray* idAsArray(id a);

void testSubclass(id thunks);
void testBridgeableValue(id thunks);
id testHKTFilter(id array);

@interface RDar27905230 : NSObject
+ (NSDictionary<NSString *, NSArray<id> *> *)mutableDictionaryOfMutableLists;
Expand Down
16 changes: 16 additions & 0 deletions test/stdlib/Inputs/ArrayBridge/ArrayBridge.m
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,22 @@ void testBridgeableValue(id thunks) {
[thunks acceptBridgeableValueArray: toSwiftArr];
}

static id filter(id<NSFastEnumeration, NSObject> container, BOOL (^predicate)(id)) {
id result = [[[container class] new] mutableCopy];
for (id object in container) {
if (predicate(object)) {
[result addObject:object];
}
}
return result;
}

id testHKTFilter(id array) {
return filter(array, ^(id obj) {
return [obj isEqual:@"hello"];
});
}

@implementation RDar27905230

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