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-6448] Invalid Codable code generation for OptionSet in release build #48998

Closed
florianreinhart opened this issue Nov 21, 2017 · 4 comments
Closed
Labels
bug A deviation from expected or documented behavior. Also: expected but undesirable behavior. Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself

Comments

@florianreinhart
Copy link
Contributor

Previous ID SR-6448
Radar rdar://problem/35699596
Original Reporter @florianreinhart
Type Bug
Status Resolved
Resolution Done

Attachment: Download

Environment

macOS 10.12.6

Apple Swift version 4.0.2 (swiftlang-900.0.69.2 clang-900.0.38)

Additional Detail from JIRA
Votes 1
Component/s Compiler
Labels Bug, Codable, Miscompile
Assignee None
Priority Medium

md5: 8911146b2c988206beb0ee7c8b7a1a1f

Issue Description:

I've run into a pretty weird issue, where the compiler seems to generate wrong code for an OptionSet conforming to Codable. It looks like the compiler generates code that expects a [String : Any]. However, OptionSets are encoded as numbers.

This leads to an error:

Swift.DecodingError.typeMismatch(Swift.Dictionary<Swift.String, Any>, Swift.DecodingError.Context(codingPath: [CodableTestCore.MyStruct.(CodingKeys in _AC1835A39B063DF80DB968377AD3AADB).options], debugDescription: "Expected to decode Dictionary<String, Any> but found a number instead.", underlyingError: nil)): file /BuildRoot/Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-900.0.69.2/src/swift/stdlib/public/core/ErrorType.swift, line 181

I isolated the issue and created a sample package. You need to build the package with swift build -c release.

One thing I discovered is that the file names matter. Renaming A.swift to something that comes after MyOptionSet.swift in the alphabet does not trigger this issue.

@belkadan
Copy link
Contributor

The order-dependency is scary. Thanks, Florian!

@swift-ci create

@florianreinhart
Copy link
Contributor Author

I am unable to reproduce the crash with Swift version 4.2 (swiftlang-1000.0.16.9 clang-1000.10.25.3) bundled with Xcode 10 beta 2. Using Swift 4.1 I still get the crash.

@belkadan, do you have any idea if this has been fixed in Swift 4.2?

@belkadan
Copy link
Contributor

Ah, I suspect the fix for SR-6468 also ended up fixing this! Thanks for checking.

@swift-ci
Copy link
Collaborator

Comment by Justin Stofle (JIRA)

For anyone else having this issue using Xcode 9 and swift 4.1, you can fix it by reordering your swift files under compile sources and putting your codable OptionSets at the top. Swift 4.2 resolved this for me, however since Xcode 10 is still in beta that won't be an option for a few more months.

@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. Codable Area → standard library: `Codable` and co. compiler The Swift compiler in itself
Projects
None yet
Development

No branches or pull requests

3 participants