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

SILGen/Sema: Avoid diagnosing @unknown default switch cases as unreachable #72213

Conversation

tshortli
Copy link
Contributor

@tshortli tshortli commented Mar 9, 2024

Suppose you have an exhaustive switch statement which matches all the cases of a Swift enum defined in a different module named External:

import External

var e: External.SomeEnum = //...

switch e {
case .a: break
}

If External is compiled with library evolution and SomeEnum is not frozen, then the compiler will warn:

warning: switch covers known cases, but 'SomeEnum' may have additional unknown values

You add an @unknown default to the switch to resolve this warning. Now suppose in another build configuration, External is built without library evolution. The compiler will complain about the unreachability of the default case:

warning: Default will never be executed

These contradictory compiler diagnostics encourage the developer to change the code in a way that will cause a diagnostic in the other configuration. Developers should have the tools to address all warning diagnostics in a reasonable fashion and this is a case where the compiler makes that especially difficult. Given that writing @unknown default instead of default is a very intentional action that would be the result of addressing the library evolution configuration, it seems reasonable to suppress the Default will never be executed diagnostic.

…hable.

Suppose you have an exhaustive switch statement which matches all the cases of
a Swift enum defined in a different module named `External`:

```
import External

var e: External.SomeEnum = //...

switch e {
case .a: break
}
```

If `External` is compiled with library evolution and `SomeEnum` is not frozen,
then the compiler will warn:

```
warning: switch covers known cases, but 'SomeEnum' may have additional unknown values
```

You add an `@unknown default` to the switch to resolve this warning. Now
suppose in another build configuration, `External` is built _without_ library
evolution. The compiler will complain about the unreachability of the default
case:

```
warning: Default will never be executed
```

These contradictory compiler diagnostics encourage the developer to change the
code in a way that will cause a diagnostic in the other configuration.
Developers should have the tools to address all warning diagnostics in a
reasonable fashion and this is a case where the compiler makes that especially
difficult. Given that writing `@unknown default` instead of `default` is a very
intentional action that would be the result of addressing the library evolution
configuration, it seems reasonable to suppress the `Default will never be
executed` diagnostic.
@tshortli
Copy link
Contributor Author

tshortli commented Mar 9, 2024

@swift-ci please test

@tshortli tshortli merged commit c0c24c1 into swiftlang:main Mar 11, 2024
5 checks passed
@tshortli tshortli deleted the unknown-default-suppresses-unreachable-default-warning branch March 11, 2024 17:38
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