Skip to content
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-8894] Objective-C/Swift 4.2 enums interoperability: unexpected enum case #51400

Closed
swift-ci opened this issue Oct 2, 2018 · 2 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself standard library Area: Standard library umbrella

Comments

@swift-ci
Copy link
Collaborator

swift-ci commented Oct 2, 2018

Previous ID SR-8894
Radar https://bugreport.apple.com/web/?problemID=44907108
Original Reporter t.camin (JIRA User)
Type Bug
Status Resolved
Resolution Won't Do
Additional Detail from JIRA
Votes 0
Component/s Compiler, Standard Library
Labels Bug
Assignee None
Priority Medium

md5: 056832b4eeae1b79a9d19a93a9213abb

Issue Description:

Description:

An Objective-C defined enum isn't returning nil in Swift 4.2 when initialised with an invalid rawValue.

Create an Objective-C enum:

typedef NS_ENUM(NSInteger, MyEnum) {
  MyEnumValue1,
  MyEnumValue2
};

Now in a swift file

let e = MyEnum(rawValue: 99)

e is not `nil` instead it's initialized with an <invalid> case.

This causes a "Fatal error: unexpected enum case 'MyEnum(rawValue: 44)' when switching over an exhaustive switch.

The only workaround to this unexpected behaviour is to add a `default` case (even on exhaustive switches) which will catch this case.

Expected result:

I would expect that init?(rawValue🙂 returns nil when providing invalid rawValues to initializer.

Actual result:

A crash occurs in exhaustive switches

Notes:

This might be related to https://bugs.swift.org/browse/SR-1419 but with Swift 4.2 some major changes were introduced which are causing the crash.

@belkadan
Copy link
Contributor

belkadan commented Oct 2, 2018

Unfortunately, init(rawValue:) can't actually know all the possible values for your enum, because Objective-C says it's legal to stuff any values in as long as they fit in the underlying type. Some people use this to create "private" enum cases, and those need to round-trip through their raw value.

@belkadan
Copy link
Contributor

belkadan commented Oct 2, 2018

Check out SE-0192 for (a bit) more information.

@swift-ci swift-ci transferred this issue from apple/swift-issues Apr 25, 2022
This issue was closed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. compiler The Swift compiler in itself standard library Area: Standard library umbrella
Projects
None yet
Development

No branches or pull requests

2 participants