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
[SR-2420] AnyObject not working in Xcode8 beta6 #45025
Comments
Comment by Ling Guo (JIRA) I came across the same issue: protocol AProtocol {
mutating func getPtr() -> UnsafeMutableRawPointer
}
extension AProtocol {
mutating func getPtr() -> UnsafeMutableRawPointer {
if self is AnyObject {
return withUnsafePointer(to: &self, { unsafeBitCast($0.pointee, to: UnsafeMutableRawPointer.self) })
} else {
return withUnsafePointer(to: &self, { unsafeBitCast($0, to: UnsafeMutableRawPointer.self) })
}
}
} |
cc @jckarter |
This is correct, because everything bridges to |
Ling's example is invalid, but 100mango's isn't. |
|
Right, but the report says the runtime is behaving differently. |
Comment by 100mango (JIRA) Thanks for your quick reply. It make me wonder how to detect value type and reference type in Swift now. One of my framework SwiftNotificationCenter need to detect value type and reference type. Because value type is copied when it is assigned to a variable or constant, or when it is passed to a function.So it can't be participated in one to many communication.Cocoa' NotificationCenter can't add value type as observer too. |
You can still statically constrain parameters to |
Does the PR fix the case reported in my dupe SR-2498, wherein the compiler claims that |
Eridius (JIRA User) Yeah, John's fix should address that. |
Comment by Rudolf Adamkovič (JIRA) This bug is still present in Xcode 8.3.1 (8E1000a): struct SwiftBug {
struct TestStruct {}
func test() {
//warning: 'is' test is always true
if TestStruct() is AnyObject {
print("pass")
}
}
} |
@jckarter Can this behaviour be made explicit? Something like `ObjCRefBox(TestStruct())` (because IMO, `SwiftValue` is a poor class name to expose publicly)? It's really confusing to new people, and relatively hard to explain. It completely muddies the waters on `Any` vs `AnyObject`. There were many other implicit bridging behaviours, like `NSString` to `String`, that were way more straight-forward than this one, but even they were changed to require an explicit coercion. |
"SwiftValue" is an implementation detail; the name of the class Swift happens to use to box values when bridging to ObjC could change at any time. It's exactly as explicit as the other bridging behaviors; Swift won't just convert something to AnyObject unless you ask it to with `as AnyObject`. |
@jckarter Thanks for your response. " It's exactly as explicit as the other bridging behaviors; Swift won't just convert something to AnyObject unless you ask it to with `as AnyObject`." Ah yes, I didn't realize that. I guess my issue is that `is` doesn't operate on the value as it already exists, but it considers potential bridging that's available. If you have a `testStruct: TestStruct`, or a `str: Swift.String`, then `testStruct is AnyObject` and `str is NSString` both counterintuitively return true. Not because those values are those types, but because they could be bridged to be. I think a lot of the confusion around this could be alleviated if `is` didn't consider bridging behaviour, and answers more directly about precisely what it was asked about. |
Environment
Xcode8 beta6 on OS X 10.11.5
Additional Detail from JIRA
md5: f51cb8c5830727793b8f598a7fa1424e
is duplicated by:
Foo.self is AnyObject
incorrectly claims it always returns trueIssue Description:
In Xcode8 beta6, the following code will cause a warning: 'is' test is always true. But it won't print pass.
And the following code will cause a warning: Conditional cast from 'T' to 'AnyObject' always succeeds
The text was updated successfully, but these errors were encountered: