mirror of
https://github.com/apple/swift.git
synced 2025-12-14 20:36:38 +01:00
This is a new feature of Swift 5 mode, so it deserves at least a
little bit of explanation right in the diagnostic. If you have an
otherwise-fully-covered switch but can't assume the enum is frozen,
you'll now get this message:
switch covers known cases, but 'MusicGenre' may have additional
unknown values
Furthermore, if the enum comes from a system header, it looks like
this:
switch covers known cases, but 'NSMusicGenre' may have additional
unknown values, possibly added in future versions
...to further suggest the idea that even though your switch is covered
/now/, it might not handle everything in the /future/. This extra bit
is limited to system headers to avoid showing up on C enums defined in
your own project, for which it sounds silly. (The main message is
still valid though, since you can cram whatever you want into a C
enum, and people use this pattern to implement "private cases".)
rdar://problem/39367045
20 lines
1.1 KiB
Swift
20 lines
1.1 KiB
Swift
// RUN: %target-swift-frontend -typecheck %s -enable-objc-interop -import-objc-header %S/Inputs/enum-inferred-exhaustivity.h -verify -enable-nonfrozen-enum-exhaustivity-diagnostics -warnings-as-errors
|
|
|
|
// This is testing what happens with a CF_ENUM definition that doesn't include
|
|
// any enum_extensibility attributes. As such, the test deliberately avoids
|
|
// importing anything that might pull in CoreFoundation, even from the mock SDK.
|
|
|
|
func test(_ value: EnumWithDefaultExhaustivity) {
|
|
// We want to assume such enums are non-frozen.
|
|
switch value { // expected-error {{switch covers known cases, but 'EnumWithDefaultExhaustivity' may have additional unknown values}} expected-note {{handle unknown values using "@unknown default"}}
|
|
case .loneCase: break
|
|
}
|
|
}
|
|
|
|
func test(_ value: EnumWithSpecialAttributes) {
|
|
// Same, but with the attributes macro shipped in the Xcode 9 SDKs.
|
|
switch value { // expected-error {{switch covers known cases, but 'EnumWithSpecialAttributes' may have additional unknown values}} expected-note {{handle unknown values using "@unknown default"}}
|
|
case .loneCase: break
|
|
}
|
|
}
|