Skip to content

Commit 79bd970

Browse files
authored
Merge pull request #1619 from chorman0773/spec-add-identifiers-unsafety
Add spec identifier syntax to `unsafe-keyword.md` and `unsafety.md`
2 parents 0a0c9e7 + 1ee2b2e commit 79bd970

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

src/unsafe-keyword.md

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

3+
r[unsafe]
4+
5+
r[unsafe.intro]
36
The `unsafe` keyword can occur in several different contexts:
47
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)]`).
58
It plays several different roles, depending on where it is used and whether the `unsafe_op_in_unsafe_fn` lint is enabled:
@@ -11,16 +14,25 @@ See the [keyword documentation][keyword] for some illustrative examples.
1114

1215
## Unsafe functions (`unsafe fn`)
1316

17+
r[unsafe.fn]
18+
19+
r[unsafe.fn.intro]
1420
Unsafe functions are functions that are not safe in all contexts and/or for all possible inputs.
1521
We say they have *extra safety conditions*, which are requirements that must be upheld by all callers and that the compiler does not check.
1622
For example, [`get_unchecked`] has the extra safety condition that the index must be in-bounds.
1723
The unsafe function should come with documentation explaining what those extra safety conditions are.
1824

25+
r[unsafe.fn.safety]
1926
Such a function must be prefixed with the keyword `unsafe` and can only be called from inside an `unsafe` block, or inside `unsafe fn` without the [`unsafe_op_in_unsafe_fn`] lint.
2027

2128
## Unsafe blocks (`unsafe {}`)
2229

30+
r[unsafe.block]
31+
32+
r[unsafe.block.intro]
2333
A block of code can be prefixed with the `unsafe` keyword, to permit calling `unsafe` functions or dereferencing raw pointers.
34+
35+
r[unsafe.block.fn-body]
2436
By default, the body of an unsafe function is also considered to be an unsafe block;
2537
this can be changed by enabling the [`unsafe_op_in_unsafe_fn`] lint.
2638

@@ -41,13 +53,19 @@ By using `unsafe` blocks to represent the reverse links as raw pointers, it can
4153

4254
## Unsafe traits (`unsafe trait`)
4355

56+
r[unsafe.trait]
57+
58+
r[unsafe.trait.intro]
4459
An unsafe trait is a trait that comes with extra safety conditions that must be upheld by *implementations* of the trait.
4560
The unsafe trait should come with documentation explaining what those extra safety conditions are.
4661

62+
r[unsafe.trait.safety]
4763
Such a trait must be prefixed with the keyword `unsafe` and can only be implemented by `unsafe impl` blocks.
4864

4965
## Unsafe trait implementations (`unsafe impl`)
5066

67+
r[unsafe.impl]
68+
5169
When implementing an unsafe trait, the implementation needs to be prefixed with the `unsafe` keyword.
5270
By writing `unsafe impl`, the programmer states that they have taken care of satisfying the extra safety conditions required by the trait.
5371

@@ -59,12 +77,16 @@ Unsafe trait implementations are the logical dual to unsafe traits: where unsafe
5977

6078
## Unsafe external blocks (`unsafe extern`)
6179

80+
r[unsafe.extern]
81+
6282
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`.
6383

6484
[external block]: items/external-blocks.md
6585

6686
## Unsafe attributes (`#[unsafe(attr)]`)
6787

88+
r[unsafe.attribute]
89+
6890
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)]`.
6991

7092
[unsafe attribute]: attributes.md

src/unsafety.md

+17
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,34 @@
11
# Unsafety
22

3+
r[safety]
4+
5+
r[safety.intro]
36
Unsafe operations are those that can potentially violate the memory-safety
47
guarantees of Rust's static semantics.
58

9+
r[safety.unsafe-ops]
610
The following language level features cannot be used in the safe subset of
711
Rust:
812

13+
r[safety.unsafe-deref]
914
- Dereferencing a [raw pointer].
15+
16+
r[safety.unsafe-static]
1017
- Reading or writing a [mutable] or [external] static variable.
18+
19+
r[safety.unsafe-union-access]
1120
- Accessing a field of a [`union`], other than to assign to it.
21+
22+
r[safety.unsafe-call]
1223
- Calling an unsafe function (including an intrinsic or foreign function).
24+
25+
r[safety.unsafe-impl]
1326
- Implementing an [unsafe trait].
27+
28+
r[safety.unsafe-extern]
1429
- Declaring an [`extern`] block.
30+
31+
r[safety.unsafe-attribute]
1532
- Applying an [unsafe attribute] to an item.
1633

1734
[`extern`]: items/external-blocks.md

0 commit comments

Comments
 (0)