Skip to content

Commit 440c0bd

Browse files
authored
Merge pull request #1565 from ehuss/unsafe-extern-2024
2024: Add updates for unsafe extern blocks
2 parents 4ad2685 + bbc6785 commit 440c0bd

File tree

4 files changed

+20
-3
lines changed

4 files changed

+20
-3
lines changed

src/items/external-blocks.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ r[items.extern]
55
r[items.extern.syntax]
66
> **<sup>Syntax</sup>**\
77
> _ExternBlock_ :\
8-
> &nbsp;&nbsp; `unsafe`<sup>?</sup> `extern` [_Abi_]<sup>?</sup> `{`\
8+
> &nbsp;&nbsp; `unsafe`<sup>?</sup>[^unsafe-2024] `extern` [_Abi_]<sup>?</sup> `{`\
99
> &nbsp;&nbsp; &nbsp;&nbsp; [_InnerAttribute_]<sup>\*</sup>\
1010
> &nbsp;&nbsp; &nbsp;&nbsp; _ExternalItem_<sup>\*</sup>\
1111
> &nbsp;&nbsp; `}`
@@ -15,6 +15,8 @@ r[items.extern.syntax]
1515
> &nbsp;&nbsp; &nbsp;&nbsp; &nbsp;&nbsp; [_MacroInvocationSemi_]\
1616
> &nbsp;&nbsp; &nbsp;&nbsp; | ( [_Visibility_]<sup>?</sup> ( [_StaticItem_] | [_Function_] ) )\
1717
> &nbsp;&nbsp; )
18+
>
19+
> [^unsafe-2024]: Starting with the 2024 Edition, the `unsafe` keyword is required semantically.
1820
1921
r[items.extern.intro]
2022
External blocks provide _declarations_ of items that are not _defined_ in the
@@ -31,6 +33,12 @@ Calling functions or accessing statics that are declared in external blocks is o
3133
r[items.extern.namespace]
3234
The external block defines its functions and statics in the [value namespace] of the module or block where it is located.
3335

36+
r[items.extern.unsafe-required]
37+
The `unsafe` keyword is semantically required to appear before the `extern` keyword on external blocks.
38+
39+
r[items.extern.edition2024]
40+
> **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`.
41+
3442
## Functions
3543

3644
r[items.extern.fn]

src/items/functions.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ r[items.fn.syntax]
1111
> &nbsp;&nbsp; &nbsp;&nbsp; ( [_BlockExpression_] | `;` )
1212
>
1313
> _FunctionQualifiers_ :\
14-
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> _ItemSafety_<sup>?</sup> (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
14+
> &nbsp;&nbsp; `const`<sup>?</sup> `async`[^async-edition]<sup>?</sup> _ItemSafety_<sup>?</sup>[^extern-qualifiers] (`extern` _Abi_<sup>?</sup>)<sup>?</sup>
1515
>
1616
> _ItemSafety_ :\
1717
> &nbsp;&nbsp; `safe`[^extern-safe] | `unsafe`
@@ -48,6 +48,10 @@ r[items.fn.syntax]
4848
> [^extern-safe]: The `safe` function qualifier is only allowed semantically within
4949
> `extern` blocks.
5050
>
51+
> [^extern-qualifiers]: *Relevant to editions earlier than Rust 2024*: Within
52+
> `extern` blocks, the `safe` or `unsafe` function qualifier is only allowed
53+
> when the `extern` is qualified as `unsafe`.
54+
>
5155
> [^fn-param-2015]: Function parameters with only a type are only allowed
5256
> in an associated function of a [trait item] in the 2015 edition.
5357

src/unsafe-keyword.md

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,9 @@ r[unsafe.extern]
8181

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

84+
r[unsafe.extern.edition2024]
85+
> **Edition differences**: Prior to edition 2024, `extern` blocks were allowed without being qualified as `unsafe`.
86+
8487
[external block]: items/external-blocks.md
8588

8689
## Unsafe attributes (`#[unsafe(attr)]`)

src/unsafety.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,11 +26,13 @@ r[safety.unsafe-impl]
2626
- Implementing an [unsafe trait].
2727

2828
r[safety.unsafe-extern]
29-
- Declaring an [`extern`] block.
29+
- Declaring an [`extern`] block[^extern-2024].
3030

3131
r[safety.unsafe-attribute]
3232
- Applying an [unsafe attribute] to an item.
3333

34+
[^extern-2024]: Prior to the 2024 edition, extern blocks were allowed to be declared without `unsafe`.
35+
3436
[`extern`]: items/external-blocks.md
3537
[`union`]: items/unions.md
3638
[mutable]: items/static-items.md#mutable-statics

0 commit comments

Comments
 (0)