Skip to content

[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

Merged

Conversation

jrose-apple
Copy link
Contributor

@jrose-apple jrose-apple commented Jul 11, 2018

  • 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 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. Implement hash(into:) in terms of rawValue and rawValue in terms of unsafeBitCast.

  • Scope: Affects hash(into:) and rawValue 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

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@jrose-apple
Copy link
Contributor Author

@swift-ci Please nominate

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - f409cd5c49f13514bad9d4f3e4589b697a2f3c55

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f409cd5c49f13514bad9d4f3e4589b697a2f3c55

…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)
@jrose-apple jrose-apple force-pushed the 4.2-ready-for-anything branch from f409cd5 to 5b05e42 Compare July 11, 2018 19:53
@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - f409cd5c49f13514bad9d4f3e4589b697a2f3c55

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - f409cd5c49f13514bad9d4f3e4589b697a2f3c55

@jrose-apple jrose-apple force-pushed the 4.2-ready-for-anything branch from 5b05e42 to ec0b0b8 Compare July 11, 2018 20:19
@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - 5b05e4217166686db4482d85f211c69a99b8b312

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - 5b05e4217166686db4482d85f211c69a99b8b312

@jrose-apple
Copy link
Contributor Author

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.)
@jrose-apple jrose-apple force-pushed the 4.2-ready-for-anything branch from ec0b0b8 to 3bd1a55 Compare July 11, 2018 23:14
@jrose-apple
Copy link
Contributor Author

@swift-ci Please test

@jrose-apple
Copy link
Contributor Author

@swift-ci Please test source compatibility

@swift-ci
Copy link
Contributor

Build failed
Swift Test Linux Platform
Git Sha - ec0b0b89cd87a75d33fe6033f76dc6dd9037c6e5

@swift-ci
Copy link
Contributor

Build failed
Swift Test OS X Platform
Git Sha - ec0b0b89cd87a75d33fe6033f76dc6dd9037c6e5

@jrose-apple jrose-apple merged commit b27a34b into swiftlang:swift-4.2-branch Jul 12, 2018
@jrose-apple jrose-apple deleted the 4.2-ready-for-anything branch July 12, 2018 15:49
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants