Skip to content

Introduce @unsafe and the ability to prohibit use of unsafe entities #75413

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
Aug 20, 2024

Conversation

DougGregor
Copy link
Member

@DougGregor DougGregor commented Jul 23, 2024

Allow any declaration to be marked with @unsafe, meaning that it involves unsafe code. This also extends to C declarations marked with the swift_attr("unsafe") attribute.

Under a separate experimental flag (WarnUnsafe), warn about any attempt to use an @unsafe declaration, type, or any unsafe language feature (such as unowned(unsafe), @unchecked Sendable). This defines a "safe" mode in Swift that prohibits memory-unsafe constructs. These language features are considered unsafe:

  • @unchecked Sendable: diagnosed only when strict concurrency is enabled.
  • unowned(unsafe)
  • nonisolated(unsafe): diagnosed only when strict concurrency is enabled.

An @unsafe declaration cannot:

  • Override a safe declaration.
  • Witness a safe requirement.
  • Be referenced from code that has strict checking enabled.

Annotate all of the Unsafe* types and unsafe functions in the standard library (including concurrency, synchronization, etc.) as @unsafe. This includes:

  • Unsafe(Mutable)(Raw)(Buffer)Pointer
  • OpaquePointer
  • Unmanaged
  • unsafeBitCast, unsafeDowncast
  • Optional.unsafelyUnwrapped
  • UnsafeContinuation, withUnsafe(Throwing)Continuation
  • UnsafeCurrentTask
  • Mutex's unsafeTryLock, unsafeLock, unsafeUnlock
  • VolatileMappedRegister.init(unsafeBitPattern:)

There are a large number of APIs that could be explicitly marked @unsafe because they traffic in unsafe types. This isn't strictly necessary, because the language design will diagnose attempts to use these entities when in the safe mode, so I've opted to keep the set of changes smaller by omitting them.

I'm tracking this idea via rdar://133963224

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@DougGregor
Copy link
Member Author

@swift-ci please smoke test Windows

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please build toolchain

@DougGregor
Copy link
Member Author

@swift-ci please smoke test

1 similar comment
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor
Copy link
Member Author

@swift-ci please test

@DougGregor
Copy link
Member Author

@swift-ci please test Linux

@egorzhdan egorzhdan requested a review from Xazax-hun August 15, 2024 13:57
@ahoppen ahoppen removed their request for review August 15, 2024 15:48
…tions

Allow any declaration to be marked with `@unsafe`, meaning that it
involves unsafe code. This also extends to C declarations marked with
the `swift_attr("unsafe")` attribute.

Under a separate experimental flag (`DisallowUnsafe`), diagnose any
attempt to use an `@unsafe` declaration or any unsafe language feature
(such as `unowned(unsafe)`, `@unchecked Sendable`). This begins to
define a "safe" mode in Swift that prohibits memory-unsafe constructs.
When referencing a declaration, check whether any of the types in that
reference are unsafe. This can diagnose cases where the original
declaration either wasn't actually unsafe, or is being provided with
unsafe types via generics.
Annotate all of the `Unsafe*` types and `unsafe` functions in the standard
library (including concurrency, synchronization, etc.) as `@unsafe`. Add a
few tests to ensure that we detect uses of these types in clients that
have disabled unsafe code.
Warnings fit better with the approach we're going for, and can be
escalated to errors by `-warnings-as-errors` for clients that need it.
@DougGregor
Copy link
Member Author

@swift-ci please smoke test

@DougGregor DougGregor merged commit d9f7d94 into swiftlang:main Aug 20, 2024
3 checks passed
@DougGregor DougGregor deleted the unsafe branch August 20, 2024 13:14
@DougGregor
Copy link
Member Author

Merging as experimental, details will change

stmontgomery added a commit to swiftlang/swift-testing that referenced this pull request Apr 19, 2025
…ting 6.1 development snapshot toolchains (#1084)

This fixes a build failure when attempting to build the `main` branch
using a 6.1 development snapshot toolchain. This failure was introduced
by #1069, which added usage of the new `@unsafe` attribute, and the
failure was revealed when we set up the 6.1 CI jobs in #1083.

Here are some relevant related Swift PRs which give context around these
changes:

- swiftlang/swift#75413
- swiftlang/swift#79645

See the code comment for more details.

### Checklist:

- [x] Code and documentation should follow the style of the [Style
Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md).
- [x] If public symbols are renamed or modified, DocC references should
be updated.
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