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

2024: Add updates for unsafe extern blocks #1565

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 8 additions & 1 deletion src/items/external-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

> **<sup>Syntax</sup>**\
> _ExternBlock_ :\
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `extern` [_Abi_]<sup>?</sup> `{`\
> &nbsp;&nbsp; `unsafe`<sup>?</sup>[^unsafe-2024] `extern` [_Abi_]<sup>?</sup> `{`\
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
> &nbsp;&nbsp; &nbsp;&nbsp; _ExternalItem_<sup>\*</sup>\
> &nbsp;&nbsp; `}`
Expand All @@ -12,6 +12,8 @@
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_StaticItem_] | [_Function_] ) )\
> &nbsp;&nbsp; )
>
> [^unsafe-2024]: Starting with the 2024 Edition, the `unsafe` keyword is required semantically.

External blocks provide _declarations_ of items that are not _defined_ in the
current crate and are the basis of Rust's foreign function interface. These are
Expand All @@ -23,6 +25,11 @@ blocks is only allowed in an `unsafe` context.

The external block defines its functions and statics in the [value namespace] of the module or block where it is located.

**Edition differences**: Starting in the 2024 edition, the `unsafe` keyword is
required to appear before the `extern` keyword on external blocks. In previous
editions, it is accepted but not required. The `safe` and `unsafe` item qualifiers
are only allowed if the external block itself is marked as `unsafe`.

## Functions

Functions within external blocks are declared in the same way as other Rust
Expand Down
6 changes: 5 additions & 1 deletion src/items/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_BlockExpression_] | `;` )
>
> _FunctionQualifiers_ :\
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> _ItemSafety_<sup>?</sup> (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> _ItemSafety_<sup>?</sup>[^extern-qualifiers] (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
>
> _ItemSafety_ :\
> &nbsp;&nbsp; `safe`[^extern-safe] | `unsafe`
Expand Down Expand Up @@ -45,6 +45,10 @@
> [^extern-safe]: The `safe` function qualifier is only allowed semantically within
> `extern` blocks.
>
> [^extern-qualifiers]: *Relevant to editions earlier than Rust 2024*: Within
> `extern` blocks, the `safe` or `unsafe` function qualifier is only allowed
> when the `extern` is qualified as `unsafe`.
>
> [^fn-param-2015]: Function parameters with only a type are only allowed
> in an associated function of a [trait item] in the 2015 edition.

Expand Down
2 changes: 2 additions & 0 deletions src/unsafe-keyword.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@ Unsafe trait implementations are the logical dual to unsafe traits: where unsafe

The programmer who declares an [external block] must assure that the signatures of the items contained within are correct. Failing to do so may lead to undefined behavior. That this obligation has been met is indicated by writing `unsafe extern`.

**Edition differences**: Prior to edition 2024, `extern` blocks were allowed without being qualified as `unsafe`.

[external block]: items/external-blocks.md

## Unsafe attributes (`#[unsafe(attr)]`)
Expand Down
4 changes: 3 additions & 1 deletion src/unsafety.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,11 @@ Rust:
- Accessing a field of a [`union`], other than to assign to it.
- Calling an unsafe function (including an intrinsic or foreign function).
- Implementing an [unsafe trait].
- Declaring an [`extern`] block.
- Declaring an [`extern`] block[^extern-2024].
- Applying an [unsafe attribute] to an item.

[^extern-2024]: Prior to the 2024 edition, extern blocks were allowed to be declared without `unsafe`.

[`extern`]: items/external-blocks.md
[`union`]: items/unions.md
[mutable]: items/static-items.md#mutable-statics
Expand Down