Skip to content

Sema: Avoid decoding unavailable enum elements in derived Codable conformances #67556

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

Conversation

tshortli
Copy link
Contributor

The compiler derived implementations of Codable conformances for enums did not take enum element unavailability into account. This could result in unavailable values being instantiated at runtime, leading to a general violation of the invariant that unavailable code is unreachable at runtime. This problem is possible because synthesized code is not type checked; had the conformances been hand-written, they would have been rejected for referencing unavailable declarations inside of available declarations.

This change specifically alters derivation for the following declarations:

  • Decodable.init(from:)
  • Encodable.encode(to:)
  • CodingKey.init(stringValue:)

Resolves rdar://110098469

@tshortli
Copy link
Contributor Author

@swift-ci please test

@tshortli tshortli requested a review from beccadax July 26, 2023 23:48
@tshortli
Copy link
Contributor Author

This was discussed on the Evolution forums here: https://forums.swift.org/t/codable-synthesis-and-decoding-unavailable-values/64926

@tshortli
Copy link
Contributor Author

@swift-ci please test source compatibility

…formances.

The compiler derived implementations of `Codable` conformances for enums did
not take enum element unavailability into account. This could result in
unavailable values being instantiated at runtime, leading to a general
violation of the invariant that unavailable code is unreachable at runtime.
This problem is possible because synthesized code is not type checked; had the
conformances been hand-written, they would have been rejected for referencing
unavailable declarations inside of available declarations.

This change specifically alters derivation for the following declarations:
- `Decodable.init(from:)`
- `Encodable.encode(to:)`
- `CodingKey.init(stringValue:)`

Resolves rdar://110098469
@tshortli
Copy link
Contributor Author

Source compatibility failures are a known issue: #67534

@tshortli tshortli force-pushed the derived-codable-conformance-unavailable-enum-element branch from 2a61f54 to 7996c5b Compare July 27, 2023 05:49
@tshortli
Copy link
Contributor Author

@swift-ci please smoke test

@tshortli tshortli changed the title Sema: Avoid decoding unavailable enum elements in derived Codable con formances Sema: Avoid decoding unavailable enum elements in derived Codable conformances Jul 27, 2023
@tshortli
Copy link
Contributor Author

@swift-ci please smoke test Linux

@tshortli tshortli merged commit 7c93887 into swiftlang:main Jul 27, 2023
@tshortli tshortli deleted the derived-codable-conformance-unavailable-enum-element branch July 27, 2023 20:07
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.

1 participant