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-2867] With optimization on, type(of:) returns protocol instead of concrete type #45461
Comments
cc @swiftix |
I discussed it with @jckarter and he explained that the optimized version is correct and the non-optimized version is wrong. This is because "init" gets a C value boxed into an existential of type P. Therefore, the type of this boxed value is P.Protocol and not C. The runtime produces a wrong result in the non-optimized case. I'll create a radar for it. |
That would be P.Type, not P.Protocol. But |
Yes, what Jordan wrote was also my understanding: |
Comment by 100mango (JIRA) cc@swiftix protocol: protocol UpdateTitle: class {
func updateWithNewTitle(title: String)
} in view controller class FirstViewController: UIViewController, UpdateTitle {
override func viewDidLoad() {
super.viewDidLoad()
// Crash here
Broadcaster.register(UpdateTitle.self, observer: self)
}
} and with optimization on: public class Broadcaster {
public static func register<T>(_ protocolType: T.Type, observer: T) {
let key = "\(protocolType)"
guard type(of: observer) is AnyClass else {
//with optimization on. it will always goes here
fatalError("expecting reference type but found value type: \(observer)")
return
}
//..........
}
} |
The inconsistency was addressed in SR-3304. |
I don't think this should be closed…? As of Xcode 9 beta 5, it now always gives “P / false”. That is the wrong answer, per Jordan above:
|
It's correct as the language is defined today, as discussed in detail in SR-3304. |
Additional Detail from JIRA
md5: feb48b9cac66e6d4a7b332444ba939cb
duplicates:
relates to:
Issue Description:
With an unoptimized build, it prints:
However, in an optimized build (release configuration in Xcode), it prints:
The optimization discrepancy is definitely a bug. I think the unoptimized output is the correct one — type(of:) should always return a concrete type, never a protocol … right?
(Apologies if this is a duplicate. I found it a difficult one to search for.)
The text was updated successfully, but these errors were encountered: