Skip to content

Commit 98503e6

Browse files
committed
Add to unsafety section for unsafe attributes
Let's describe unsafe attributes in the chapter on unsafety.
1 parent 4b1f18c commit 98503e6

File tree

2 files changed

+10
-2
lines changed

2 files changed

+10
-2
lines changed

src/unsafe-keyword.md

Lines changed: 8 additions & 2 deletions
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 attributes (`#[unsafe(attr)]`).
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(attr)]`)
88

99
The following discusses each of these cases.
1010
See the [keyword documentation][keyword] for some illustrative examples.
@@ -56,3 +56,9 @@ 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 attributes (`#[unsafe(attr)]`)
61+
62+
An [unsafe attribute] is one that has extra safety conditions that must be upheld when using the attribute. The compiler cannot check whether these conditions have been upheld. To assert that they have been, these attributes must be wrapped in `unsafe(..)`, e.g. `#[unsafe(no_mangle)]`.
63+
64+
[unsafe attribute]: attributes.md

src/unsafety.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,9 +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+
- Applying an [unsafe attribute] to an item.
1415

1516
[`union`]: items/unions.md
1617
[mutable]: items/static-items.md#mutable-statics
1718
[external]: items/external-blocks.md
1819
[raw pointer]: types/pointer.md
1920
[unsafe trait]: items/traits.md#unsafe-traits
21+
[unsafe attribute]: attributes.md

0 commit comments

Comments
 (0)