swiftlang / swift

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

[SR-4609] "is" doesn't check ObjC generics #47186

Open swift-ci opened 7 years ago

swift-ci commented 7 years ago
Previous ID SR-4609
Radar None
Original Reporter laullon (JIRA User)
Type Bug
Environment Swift 3
Additional Detail from JIRA | | | |------------------|-----------------| |Votes | 0 | |Component/s | Compiler | |Labels | Bug, TypeChecker | |Assignee | None | |Priority | Medium | md5: c52eb61cc6462c3025b48a3c62ba4312

Issue Description:

I have this code:

let o = CNLabeledValue(label: nil, value: CNInstantMessageAddress(username: "laullon", service: "Facebook"))  
print(o is CNLabeledValue<CNPhoneNumber>)  

And it will print allways "true", but it should realy print "flase", because o is "CNLabeledValue\<CNInstantMessageAddress>" and not "CNLabeledValue\<CNPhoneNumber>"

I put this on the dev forum and get this respond:
These types are imported from Objective-C and Objective-C’s lightweight generic system is only available at compile time, not runtime. From the Objective-C runtime’s perspective, CNLabeledValue\<CNInstantMessageAddress> and CNLabeledValue\<CNPhoneNumber> are the same type, that is, CNLabeledValue.

https://forums.developer.apple.com/message/221273

I think the generic should be validated, or should generate a build error, or at least a warning, it cloud lead to weird bugs on the apps.

belkadan commented 7 years ago

Yeah, it would be reasonable to have to write this as is CNLabeledValue, without the generic parameters, to underscore the fact that it can't check for that. I'm not sure what we'd do for as? though, where it needs a final type.

mattneub commented 5 years ago

A related issue: you can't declare a CNLabeledValue variable. If you try this:

        var val : CNLabeledValue?

you're told it's a generic and needs an explicit parameterized type. But what type, in general, can you supply? The correct umbrella type, according to the Contacts framework, is `NSCopying & NSSecureCoding`, and FixIt actually suggests that, correctly. But if you do in fact insert it, you get another error:

        var val : CNLabeledValue<NSCopying & NSSecureCoding>?

You are told: "'NSCopying & NSSecureCoding' cannot be used as a type conforming to protocol 'NSSecureCoding' because 'NSSecureCoding' has static requirements."