Skip to content

Commit 82c9863

Browse files
authored
Merge pull request #1970 from ehuss/extern-block-unsafe-fn-call
Clarify that safe extern items do not require unsafe
2 parents 9c6bcbd + 3b88b41 commit 82c9863

File tree

3 files changed

+22
-9
lines changed

3 files changed

+22
-9
lines changed

src/items/external-blocks.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,8 @@ r[items.extern.allowed-kinds]
2828
Two kinds of item _declarations_ are allowed in external blocks: [functions] and
2929
[statics].
3030

31-
r[items.extern.fn-safety]
32-
Calling functions or accessing statics that are declared in external blocks is only allowed in an `unsafe` context.
31+
r[items.extern.safety]
32+
Calling unsafe functions or accessing unsafe statics that are declared in external blocks is only allowed in an [`unsafe` context].
3333

3434
r[items.extern.namespace]
3535
The external block defines its functions and statics in the [value namespace] of the module or block where it is located.
@@ -465,6 +465,7 @@ restrictions as [regular function parameters].
465465
[WebAssembly module]: https://webassembly.github.io/spec/core/syntax/modules.html
466466
[`bundle` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-bundle
467467
[`dylib` versus `raw-dylib`]: #dylib-versus-raw-dylib
468+
[`unsafe` context]: ../unsafe-keyword.md
468469
[`verbatim` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-verbatim
469470
[`whole-archive` documentation for rustc]: ../../rustc/command-line-arguments.html#linking-modifiers-whole-archive
470471
[attributes]: ../attributes.md

src/unsafe-keyword.md

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,15 +2,27 @@ r[unsafe]
22
# The `unsafe` keyword
33

44
r[unsafe.intro]
5-
The `unsafe` keyword can occur in several different contexts:
6-
unsafe functions (`unsafe fn`), unsafe blocks (`unsafe {}`), unsafe traits (`unsafe trait`), unsafe trait implementations (`unsafe impl`), unsafe external blocks (`unsafe extern`), and unsafe attributes (`#[unsafe(attr)]`).
7-
It plays several different roles, depending on where it is used and whether the `unsafe_op_in_unsafe_fn` lint is enabled:
8-
- it is used to mark code that *defines* extra safety conditions (`unsafe fn`, `unsafe trait`)
9-
- 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`, `#[unsafe(attr)]`)
5+
The `unsafe` keyword is used to create or discharge the obligation to prove something safe. Specifically:
6+
7+
- It is used to mark code that *defines* extra safety conditions that must be upheld elsewhere.
8+
- This includes `unsafe fn`, `unsafe static`, and `unsafe trait`.
9+
- It is used to mark code that the programmer *asserts* satisfies safety conditions defined elsewhere.
10+
- This includes `unsafe {}`, `unsafe impl`, `unsafe fn` without [`unsafe_op_in_unsafe_fn`], `unsafe extern`, and `#[unsafe(attr)]`.
1011

1112
The following discusses each of these cases.
1213
See the [keyword documentation][keyword] for some illustrative examples.
1314

15+
r[unsafe.positions]
16+
The `unsafe` keyword can occur in several different contexts:
17+
18+
- unsafe functions (`unsafe fn`)
19+
- unsafe blocks (`unsafe {}`)
20+
- unsafe traits (`unsafe trait`)
21+
- unsafe trait implementations (`unsafe impl`)
22+
- unsafe external blocks (`unsafe extern`)
23+
- unsafe external statics (`unsafe static`)
24+
- unsafe attributes (`#[unsafe(attr)]`)
25+
1426
r[unsafe.fn]
1527
## Unsafe functions (`unsafe fn`)
1628

src/unsafety.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,13 +13,13 @@ r[safety.unsafe-deref]
1313
- Dereferencing a [raw pointer].
1414

1515
r[safety.unsafe-static]
16-
- Reading or writing a [mutable] or [external] static variable.
16+
- Reading or writing a [mutable] or unsafe [external] static variable.
1717

1818
r[safety.unsafe-union-access]
1919
- Accessing a field of a [`union`], other than to assign to it.
2020

2121
r[safety.unsafe-call]
22-
- Calling an unsafe function (including an intrinsic or foreign function).
22+
- Calling an unsafe function.
2323

2424
r[safety.unsafe-target-feature-call]
2525
- Calling a safe function marked with a [`target_feature`][attributes.codegen.target_feature] from a function that does not have a `target_feature` attribute enabling the same features (see [attributes.codegen.target_feature.safety-restrictions]).

0 commit comments

Comments
 (0)