Skip to content

Add -require-explicit-sendable to warn about non-Sendable public types #39550

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

Merged
merged 6 commits into from
Oct 8, 2021

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Oct 1, 2021

Introduce a compiler flag that warnings about any public types defined in
a module that are neither explicitly Sendable nor explicitly
non-Sendable (the latter of which has no spelling currently), which
is intended to help with auditing a module for Sendable conformances.

Try to make the Fix-Its smart enough to provide a good workflow. For example,
given this type:

public struct DictionaryHolder<T: Hashable, U, V> {
  var member: [T: (U, V?)]
}

we'll provide a warning like this:

warning: Public generic struct 'DictionaryHolder' does not specify whether it is 'Sendable' or not

along with two notes. The first note makes the type Sendable via a conditional conformance introduced with a Fix-It:

note: Consider making generic struct 'DictionaryHolder' conform to the 'Sendable' protocol

extension DictionaryHolder: Sendable where T: Sendable, U: Sendable, V: Sendable { }

and the second note silences the warning using an unavailable extension with the Sendable conformance:

note: Make generic struct 'DictionaryHolder' explicitly non-Sendable to suppress this warning

@available(*, unavailable)
extension DictionaryHolder: Sendable { }

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@Saklad5
Copy link

Saklad5 commented Oct 4, 2021

This seems quite handy.

Introduce a compiler flag that warnings about any public types defined in
a module that are neither explicitly `Sendable` nor explicitly
non-`Sendable` (the latter of which has no spelling currently), which
is intended to help with auditing a module for Sendable conformances.
…warning

Becca noticed that one can already write an unavailable `Sendable`
conformance in language:

    @available(*, unavailable)
    extension MyType: Sendable { ... }

Add tests to verify that this suppresses the "missing Sendable
annotation" warning (it does) and suggest this spelling in a Fix-It to
help users find it.
@DougGregor DougGregor force-pushed the require-explicit-sendable branch from 477a30a to 2e81274 Compare October 6, 2021 21:01
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

…rage.

When proposing to annotate a public type as `Sendable` due to the
`-require-explicit-sendable` command-line parameter, suggest a
conditional conformance when it can be determined that a generic type
would be `Sendable` when certain type parameters are `Sendable`.

For example, given this type:

    public struct DictionaryHolder<T: Hashable, U, V> {
      var member: [T: (U, V?)]
    }

We will now produce a Fix-It that suggests that one add:

    extension DictionaryHolder: Sendable
      where T: Sendable, U: Sendable, V: Sendable { }
When a type has provided an unavailable `Sendable` conformance, don't
check its instance storage for `Sendable`. Additionally, teach
`-require-explicit-sendable` to avoid trying to add a `Sendable`
conformance when some of the instance storage relies on unavailable
`Sendable` conformances.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit e845af8 into swiftlang:main Oct 8, 2021
@DougGregor DougGregor deleted the require-explicit-sendable branch October 8, 2021 23:13
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.

2 participants