-
Notifications
You must be signed in to change notification settings - Fork 10.5k
[4.2] Handle unexpected raw values for @objc enums in derived conformances #17881
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
[4.2] Handle unexpected raw values for @objc enums in derived conformances #17881
Conversation
@swift-ci Please test |
@swift-ci Please nominate |
@swift-ci Please test |
Build failed |
Build failed |
…wiftlang#17836) Because people put all sorts of nonsense into @objc enums (most reasonably, "private cases", which represent valid values that are not API), the Swift-synthesized implementation of 'hash(into:)' needs to not expect a switch statement to be exhaustive. And since Swift-defined @objc enums are supposed to behave enough like C-defined enums, they should at least handle simple method calls with an invalid raw value, which means that 'rawValue' likewise should not use a switch. This patch provides alternate implementations that look like this: extension ImportedEnum { public var rawValue: Int { return unsafeBitCast(self, to: Int.self) } public func hash(into hasher: inout Hasher) { hasher.combine(self.rawValue) } } rdar://problem/41913284 (cherry picked from commit 357c7d6)
f409cd5
to
5b05e42
Compare
@swift-ci Please test |
Build failed |
Build failed |
5b05e42
to
ec0b0b8
Compare
@swift-ci Please test |
Build failed |
Build failed |
Aargh. Guess the simplest fix wasn't enough. |
This check is superfluous in correct code, but necessary in the 4.2 branch to avoid failing in incorrect code. (On master, the error is not reported because the enum is no longer considered @objc at this point.)
ec0b0b8
to
3bd1a55
Compare
@swift-ci Please test |
@swift-ci Please test source compatibility |
Build failed |
Build failed |
Explanation: Because people put all sorts of nonsense into
@objc
enums (most reasonably, "private cases", which represent valid values that are not API), the Swift-synthesized implementation ofhash(into:)
needs to not expect a switch statement to be exhaustive. And since Swift-defined@objc
enums are supposed to behave enough like C-defined enums, they should at least handle simple method calls with an invalid raw value, which means thatrawValue
likewise should not use a switch. Implementhash(into:)
in terms ofrawValue
andrawValue
in terms ofunsafeBitCast
.Scope: Affects
hash(into:)
andrawValue
for both imported and user-defined@objc
enums.Issue: rdar://problem/41913284
Risk: Medium-low. The code change is very straightforward and we have pretty good test coverage, but it is a broad scope.
Testing: Added compiler regression tests.
Reviewed by: @jckarter, @itaiferber, @slavapestov