From aae9ae26c0d7a85e3ca6e4f278d0bb4b4bcdc510 Mon Sep 17 00:00:00 2001 From: Eric Huss Date: Fri, 9 Aug 2024 15:41:03 -0700 Subject: [PATCH] Revert "Remove 2024 unsafe extern block notes" This reverts commit 875b905a389455c5329ae088600c0b5f7222104d. --- src/items/external-blocks.md | 9 ++++++++- src/items/functions.md | 6 +++++- src/unsafe-keyword.md | 2 ++ src/unsafety.md | 4 +++- 4 files changed, 18 insertions(+), 3 deletions(-) diff --git a/src/items/external-blocks.md b/src/items/external-blocks.md index bb59af898..3af2a67f0 100644 --- a/src/items/external-blocks.md +++ b/src/items/external-blocks.md @@ -2,7 +2,7 @@ > **Syntax**\ > _ExternBlock_ :\ ->    `unsafe`? `extern` [_Abi_]? `{`\ +>    `unsafe`?[^unsafe-2024] `extern` [_Abi_]? `{`\ >       [_InnerAttribute_]\*\ >       _ExternalItem_\*\ >    `}` @@ -12,6 +12,8 @@ >          [_MacroInvocationSemi_]\ >       | ( [_Visibility_]? ( [_StaticItem_] | [_Function_] ) )\ >    ) +> +> [^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 @@ -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 diff --git a/src/items/functions.md b/src/items/functions.md index 2a12708ac..224988485 100644 --- a/src/items/functions.md +++ b/src/items/functions.md @@ -8,7 +8,7 @@ >       ( [_BlockExpression_] | `;` ) > > _FunctionQualifiers_ :\ ->    `const`? `async`[^async-edition]? _ItemSafety_? (`extern` _Abi_?)? +>    `const`? `async`[^async-edition]? _ItemSafety_?[^extern-qualifiers] (`extern` _Abi_?)? > > _ItemSafety_ :\ >    `safe`[^extern-safe] | `unsafe` @@ -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. diff --git a/src/unsafe-keyword.md b/src/unsafe-keyword.md index 0206d8792..3ac2eca4d 100644 --- a/src/unsafe-keyword.md +++ b/src/unsafe-keyword.md @@ -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)]`) diff --git a/src/unsafety.md b/src/unsafety.md index 9383eba9d..c7d2b7dcc 100644 --- a/src/unsafety.md +++ b/src/unsafety.md @@ -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