Skip to content

Commit 2f5e96b

Browse files
traviscrossspastorino
authored andcommitted
Describe unsafe extern blocks in unsafety chapter
Let's add a description of unsafe extern blocks to the chapter about unsafety.
1 parent d1a7564 commit 2f5e96b

File tree

2 files changed

+14
-2
lines changed

2 files changed

+14
-2
lines changed

src/unsafe-keyword.md

+10-2
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
# The `unsafe` keyword
22

33
The `unsafe` keyword can occur in several different contexts:
4-
unsafe functions (`unsafe fn`), unsafe blocks (`unsafe {}`), unsafe traits (`unsafe trait`), and unsafe trait implementations (`unsafe impl`).
4+
unsafe functions (`unsafe fn`), unsafe blocks (`unsafe {}`), unsafe traits (`unsafe trait`), unsafe trait implementations (`unsafe impl`), and unsafe external blocks (`unsafe extern`).
55
It plays several different roles, depending on where it is used and whether the `unsafe_op_in_unsafe_fn` lint is enabled:
66
- it is used to mark code that *defines* extra safety conditions (`unsafe fn`, `unsafe trait`)
7-
- it is used to mark code that needs to *satisfy* extra safety conditions (`unsafe {}`, `unsafe impl`, `unsafe fn` without [`unsafe_op_in_unsafe_fn`])
7+
- it is used to mark code that needs to *satisfy* extra safety conditions (`unsafe {}`, `unsafe impl`, `unsafe fn` without [`unsafe_op_in_unsafe_fn`], `unsafe extern`)
88

99
The following discusses each of these cases.
1010
See the [keyword documentation][keyword] for some illustrative examples.
@@ -56,3 +56,11 @@ Unsafe trait implementations are the logical dual to unsafe traits: where unsafe
5656
[keyword]: ../std/keyword.unsafe.html
5757
[`get_unchecked`]: ../std/primitive.slice.html#method.get_unchecked
5858
[`unsafe_op_in_unsafe_fn`]: ../rustc/lints/listing/allowed-by-default.html#unsafe-op-in-unsafe-fn
59+
60+
## Unsafe external blocks (`unsafe extern`)
61+
62+
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`.
63+
64+
**Edition differences**: Prior to edition 2024, `extern` blocks were allowed without being qualified as `unsafe`.
65+
66+
[external block]: items/external-blocks.md

src/unsafety.md

+4
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,11 @@ Rust:
1111
- Accessing a field of a [`union`], other than to assign to it.
1212
- Calling an unsafe function (including an intrinsic or foreign function).
1313
- Implementing an [unsafe trait].
14+
- Declaring an [`extern`] block[^extern-2024].
1415

16+
[^extern-2024]: Prior to the 2024 edition, extern blocks were allowed to be declared without `unsafe`.
17+
18+
[`extern`]: items/external-blocks.md
1519
[`union`]: items/unions.md
1620
[mutable]: items/static-items.md#mutable-statics
1721
[external]: items/external-blocks.md

0 commit comments

Comments
 (0)