Skip to content

Commit

Permalink
Merge pull request #1565 from ehuss/unsafe-extern-2024
Browse files Browse the repository at this point in the history
2024: Add updates for unsafe extern blocks
  • Loading branch information
ehuss authored Nov 25, 2024
2 parents 4ad2685 + bbc6785 commit 440c0bd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 3 deletions.
10 changes: 9 additions & 1 deletion src/items/external-blocks.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ r[items.extern]
r[items.extern.syntax]
> **<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 @@ -15,6 +15,8 @@ r[items.extern.syntax]
> &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.
r[items.extern.intro]
External blocks provide _declarations_ of items that are not _defined_ in the
Expand All @@ -31,6 +33,12 @@ Calling functions or accessing statics that are declared in external blocks is o
r[items.extern.namespace]
The external block defines its functions and statics in the [value namespace] of the module or block where it is located.

r[items.extern.unsafe-required]
The `unsafe` keyword is semantically required to appear before the `extern` keyword on external blocks.

r[items.extern.edition2024]
> **Edition differences**: Prior to the 2024 edition, the `unsafe` keyword is optional. The `safe` and `unsafe` item qualifiers are only allowed if the external block itself is marked as `unsafe`.
## Functions

r[items.extern.fn]
Expand Down
6 changes: 5 additions & 1 deletion src/items/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ r[items.fn.syntax]
> &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 @@ -48,6 +48,10 @@ r[items.fn.syntax]
> [^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
3 changes: 3 additions & 0 deletions src/unsafe-keyword.md
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,9 @@ r[unsafe.extern]

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`.

r[unsafe.extern.edition2024]
> **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 @@ -26,11 +26,13 @@ r[safety.unsafe-impl]
- Implementing an [unsafe trait].

r[safety.unsafe-extern]
- Declaring an [`extern`] block.
- Declaring an [`extern`] block[^extern-2024].

r[safety.unsafe-attribute]
- 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

0 comments on commit 440c0bd

Please sign in to comment.