rawValue initialization of imported closed enums should check cases #81696
Labels
bug
A deviation from expected or documented behavior. Also: expected but undesirable behavior.
crash
Bug: A crash, i.e., an abnormal termination of software
triage needed
This issue needs more specific labels
Description
C
enum
s marked as__attribute__((enum_extensibility(closed)))
(or usingNS_CLOSED_ENUM
) will be imported as@frozen
Swiftenum
s, not requiring default branches forswitch
es. However, constructing suchenum
s withinit?(rawValue:)
will not fail. Switching on suchenum
s will reproducibly trap at runtime with "unexpected enum case while switching".Reproduction
Stack dump
Expected behavior
init?(rawValue:)
should producenil
when givenrawValue
s which do not correspond to the values associated with the enumerators listed in the importedenum
's definition. This is consistent with Swift's behavior for initializingenum
s. According to old documentation, this "probably should" happening.Environment
$ swiftc -version
Apple Swift version 6.1 (swift-6.1-RELEASE)
Target: arm64-apple-macosx15.0
Additional information
I came across this bug while playing around with
-strict-memory-safety
onApple Swift version 6.2-dev (LLVM 81ab6d9f7e4810f, Swift 9cc1947527bacea)
. It seems important to ensure safe interoperability.This documentation explicitly talks about how imported
enum
s interacting withswitch
exhaustiveness checks is hazardous in the presence of unexpected bit patterns.The text was updated successfully, but these errors were encountered: