swiftlang / swift

The Swift Programming Language
https://swift.org
Apache License 2.0
67.57k stars 10.36k forks source link

[SR-8750] Segmentation Fault 11 when bridging `NSSet<id<MyObjCProtocol>> *` to swift #51258

Open swift-ci opened 6 years ago

swift-ci commented 6 years ago
Previous ID SR-8750
Radar rdar://problem/44460364
Original Reporter hannesoid (JIRA User)
Type Bug

Attachment: Download

Environment Xcode 10 GM, Swift 4, Swift 4.2
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, CompilerCrash | |Assignee | None | |Priority | Medium | md5: 1e58ba560df7d4fb0d63d2bf82937816

Issue Description:

This code causes a Segmentation Fault 11


@objc class MyObjCObject1: NSObject, MyObjCProtocol {}
@objc class MyObjCObject2: NSObject, MyObjCProtocol {}

// Compiler crash:
let producesSegfault = MyObjCController(set: [MyObjCObject1(), MyObjCObject2()]) 

// MyObjCController initializer is defined in ObjC:
// - (instancetype)initWithSet:(NSSet<id<MyObjCProtocol>> *)selectedObjects
// mapped to swift: init(set selectedObjects: Set<AnyHashable>)

An example project is attached.

belkadan commented 6 years ago

With assertions:

Unhandled coercion
UNREACHABLE executed at /Volumes/Data/swift-public/swift/lib/Sema/CSApply.cpp:6925!

1.  While type-checking declaration 0x7f81dfe2f8a8 at /Users/jrose/Downloads/Segfault11Example/SegmentationFaultExample/SegfaultingSwift.swift:24:1
2.  While type-checking expression at [/Users/jrose/Downloads/Segfault11Example/SegmentationFaultExample/SegfaultingSwift.swift:24:24 - line:24:62] RangeText="MyObjCController(set: [myObj1, myObj2])"
belkadan commented 6 years ago

Weirdly and somewhat worryingly, adding an explicit as Set<AnyHashable> makes the crash go away for me. At least there's a workaround!

@swift-ci create