Skip to content

Sema: Fix unavailable enum element cases in derived hashable/equatable impls #67636

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

@tshortli tshortli commented Aug 1, 2023

When deriving the hash(into:) and == witnesses for Hashable/Equatable enums, call _diagnoseUnavailableCodeReached() in the case bodies for unavailable enum elements. This is needed because the previously derived code would operate on the values associated with unavailable enum elements, which is illegal if the types of those associated values are also unavailable (these case bodies would have failed typechecking had they been hand-written). The new structure also more explicitly documents that reaching these cases is unexpected, since unavailable enum elements should not be instantiated at run time.

Includes some drive-by fixes for other tests exercising enums with unavailable elements.

tshortli added 3 commits July 31, 2023 17:26
…cases.

Also, add a FIXME to track improving the diagnostics when derivation of a
Comparable conformance is blocked by unavailable enum cases.
The `-unavailable-decl-optimization` flag is a frontend flag and therefore
requires `-Xfrontend` when passed through the driver.
Unavailable cases _can_ be written in switch statements. However, they should
not be required to be written for exhaustive matching.
@tshortli
Copy link
Contributor Author

tshortli commented Aug 1, 2023

@swift-ci please test

tshortli added 3 commits July 31, 2023 21:27
No test changes because the tests for `-print-ast` don't match whitespace.
…e impls.

When deriving the `hash(into:)` and `==` witnesses for `Hashable`/`Equatable`
enums, call `_diagnoseUnavailableCodeReached()` in the case bodies for
unavailable enum elements. This is needed because the previously derived code
would operate on the values associated with unavailable enum elements, which is
illegal if the types of those associated values are also unavailable (these
case bodies would have failed typechecking had they been hand-written). The new
structure also more explicitly documents that reaching these cases is
unexpected, since unavailable enum elements should not be instantiated at run
time.
@tshortli tshortli force-pushed the enum-unavailable-element-derived-equatable-hashable branch from 835e142 to e4810f5 Compare August 1, 2023 05:48
@tshortli
Copy link
Contributor Author

tshortli commented Aug 1, 2023

@swift-ci please test

@tshortli
Copy link
Contributor Author

tshortli commented Aug 1, 2023

@swift-ci please test source compatibility

@ahoppen ahoppen removed their request for review August 1, 2023 14:52
@tshortli tshortli merged commit 7c0a9cc into swiftlang:main Aug 1, 2023
@tshortli tshortli deleted the enum-unavailable-element-derived-equatable-hashable branch August 1, 2023 23:22
tshortli added a commit to tshortli/swift that referenced this pull request Aug 24, 2023
…ormance.

Fixes a regression caused by swiftlang#67636 in which
the patterns for case statements generated to match unavailable enum elements
were missing a type. The missing type caused a crash during SILGen if the
unavailable element case was the first one in the swtich statement.

Resolves rdar://113761850
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