Skip to content

Commit 4a5e190

Browse files
committed
reference: Unsafe section does not fit under Functions
1 parent 219eca1 commit 4a5e190

File tree

1 file changed

+93
-93
lines changed

1 file changed

+93
-93
lines changed

src/doc/reference.md

+93-93
Original file line numberDiff line numberDiff line change
@@ -976,99 +976,6 @@ The type parameters can also be explicitly supplied in a trailing
976976
there is not sufficient context to determine the type parameters. For example,
977977
`mem::size_of::<u32>() == 4`.
978978

979-
#### Unsafety
980-
981-
Unsafe operations are those that potentially violate the memory-safety
982-
guarantees of Rust's static semantics.
983-
984-
The following language level features cannot be used in the safe subset of
985-
Rust:
986-
987-
- Dereferencing a [raw pointer](#pointer-types).
988-
- Reading or writing a [mutable static variable](#mutable-statics).
989-
- Calling an unsafe function (including an intrinsic or foreign function).
990-
991-
##### Unsafe functions
992-
993-
Unsafe functions are functions that are not safe in all contexts and/or for all
994-
possible inputs. Such a function must be prefixed with the keyword `unsafe` and
995-
can only be called from an `unsafe` block or another `unsafe` function.
996-
997-
##### Unsafe blocks
998-
999-
A block of code can be prefixed with the `unsafe` keyword, to permit calling
1000-
`unsafe` functions or dereferencing raw pointers within a safe function.
1001-
1002-
When a programmer has sufficient conviction that a sequence of potentially
1003-
unsafe operations is actually safe, they can encapsulate that sequence (taken
1004-
as a whole) within an `unsafe` block. The compiler will consider uses of such
1005-
code safe, in the surrounding context.
1006-
1007-
Unsafe blocks are used to wrap foreign libraries, make direct use of hardware
1008-
or implement features not directly present in the language. For example, Rust
1009-
provides the language features necessary to implement memory-safe concurrency
1010-
in the language but the implementation of threads and message passing is in the
1011-
standard library.
1012-
1013-
Rust's type system is a conservative approximation of the dynamic safety
1014-
requirements, so in some cases there is a performance cost to using safe code.
1015-
For example, a doubly-linked list is not a tree structure and can only be
1016-
represented with reference-counted pointers in safe code. By using `unsafe`
1017-
blocks to represent the reverse links as raw pointers, it can be implemented
1018-
with only boxes.
1019-
1020-
##### Behavior considered undefined
1021-
1022-
The following is a list of behavior which is forbidden in all Rust code,
1023-
including within `unsafe` blocks and `unsafe` functions. Type checking provides
1024-
the guarantee that these issues are never caused by safe code.
1025-
1026-
* Data races
1027-
* Dereferencing a null/dangling raw pointer
1028-
* Reads of [undef](http://llvm.org/docs/LangRef.html#undefined-values)
1029-
(uninitialized) memory
1030-
* Breaking the [pointer aliasing
1031-
rules](http://llvm.org/docs/LangRef.html#pointer-aliasing-rules)
1032-
with raw pointers (a subset of the rules used by C)
1033-
* `&mut` and `&` follow LLVM’s scoped [noalias] model, except if the `&T`
1034-
contains an `UnsafeCell<U>`. Unsafe code must not violate these aliasing
1035-
guarantees.
1036-
* Mutating non-mutable data (that is, data reached through a shared reference or
1037-
data owned by a `let` binding), unless that data is contained within an `UnsafeCell<U>`.
1038-
* Invoking undefined behavior via compiler intrinsics:
1039-
* Indexing outside of the bounds of an object with `std::ptr::offset`
1040-
(`offset` intrinsic), with
1041-
the exception of one byte past the end which is permitted.
1042-
* Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64`
1043-
intrinsics) on overlapping buffers
1044-
* Invalid values in primitive types, even in private fields/locals:
1045-
* Dangling/null references or boxes
1046-
* A value other than `false` (0) or `true` (1) in a `bool`
1047-
* A discriminant in an `enum` not included in the type definition
1048-
* A value in a `char` which is a surrogate or above `char::MAX`
1049-
* Non-UTF-8 byte sequences in a `str`
1050-
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
1051-
code. Rust's failure system is not compatible with exception handling in
1052-
other languages. Unwinding must be caught and handled at FFI boundaries.
1053-
1054-
[noalias]: http://llvm.org/docs/LangRef.html#noalias
1055-
1056-
##### Behavior not considered unsafe
1057-
1058-
This is a list of behavior not considered *unsafe* in Rust terms, but that may
1059-
be undesired.
1060-
1061-
* Deadlocks
1062-
* Leaks of memory and other resources
1063-
* Exiting without calling destructors
1064-
* Integer overflow
1065-
- Overflow is considered "unexpected" behavior and is always user-error,
1066-
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
1067-
will insert debug checks that panic on overflow, but in optimized builds overflow
1068-
instead results in wrapped values. See [RFC 560] for the rationale and more details.
1069-
1070-
[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md
1071-
1072979
#### Diverging functions
1073980

1074981
A special kind of function can be declared with a `!` character where the
@@ -4042,6 +3949,99 @@ In general, `--crate-type=bin` or `--crate-type=lib` should be sufficient for
40423949
all compilation needs, and the other options are just available if more
40433950
fine-grained control is desired over the output format of a Rust crate.
40443951

3952+
# Unsafety
3953+
3954+
Unsafe operations are those that potentially violate the memory-safety
3955+
guarantees of Rust's static semantics.
3956+
3957+
The following language level features cannot be used in the safe subset of
3958+
Rust:
3959+
3960+
- Dereferencing a [raw pointer](#pointer-types).
3961+
- Reading or writing a [mutable static variable](#mutable-statics).
3962+
- Calling an unsafe function (including an intrinsic or foreign function).
3963+
3964+
## Unsafe functions
3965+
3966+
Unsafe functions are functions that are not safe in all contexts and/or for all
3967+
possible inputs. Such a function must be prefixed with the keyword `unsafe` and
3968+
can only be called from an `unsafe` block or another `unsafe` function.
3969+
3970+
## Unsafe blocks
3971+
3972+
A block of code can be prefixed with the `unsafe` keyword, to permit calling
3973+
`unsafe` functions or dereferencing raw pointers within a safe function.
3974+
3975+
When a programmer has sufficient conviction that a sequence of potentially
3976+
unsafe operations is actually safe, they can encapsulate that sequence (taken
3977+
as a whole) within an `unsafe` block. The compiler will consider uses of such
3978+
code safe, in the surrounding context.
3979+
3980+
Unsafe blocks are used to wrap foreign libraries, make direct use of hardware
3981+
or implement features not directly present in the language. For example, Rust
3982+
provides the language features necessary to implement memory-safe concurrency
3983+
in the language but the implementation of threads and message passing is in the
3984+
standard library.
3985+
3986+
Rust's type system is a conservative approximation of the dynamic safety
3987+
requirements, so in some cases there is a performance cost to using safe code.
3988+
For example, a doubly-linked list is not a tree structure and can only be
3989+
represented with reference-counted pointers in safe code. By using `unsafe`
3990+
blocks to represent the reverse links as raw pointers, it can be implemented
3991+
with only boxes.
3992+
3993+
## Behavior considered undefined
3994+
3995+
The following is a list of behavior which is forbidden in all Rust code,
3996+
including within `unsafe` blocks and `unsafe` functions. Type checking provides
3997+
the guarantee that these issues are never caused by safe code.
3998+
3999+
* Data races
4000+
* Dereferencing a null/dangling raw pointer
4001+
* Reads of [undef](http://llvm.org/docs/LangRef.html#undefined-values)
4002+
(uninitialized) memory
4003+
* Breaking the [pointer aliasing
4004+
rules](http://llvm.org/docs/LangRef.html#pointer-aliasing-rules)
4005+
with raw pointers (a subset of the rules used by C)
4006+
* `&mut` and `&` follow LLVM’s scoped [noalias] model, except if the `&T`
4007+
contains an `UnsafeCell<U>`. Unsafe code must not violate these aliasing
4008+
guarantees.
4009+
* Mutating non-mutable data (that is, data reached through a shared reference or
4010+
data owned by a `let` binding), unless that data is contained within an `UnsafeCell<U>`.
4011+
* Invoking undefined behavior via compiler intrinsics:
4012+
* Indexing outside of the bounds of an object with `std::ptr::offset`
4013+
(`offset` intrinsic), with
4014+
the exception of one byte past the end which is permitted.
4015+
* Using `std::ptr::copy_nonoverlapping_memory` (`memcpy32`/`memcpy64`
4016+
intrinsics) on overlapping buffers
4017+
* Invalid values in primitive types, even in private fields/locals:
4018+
* Dangling/null references or boxes
4019+
* A value other than `false` (0) or `true` (1) in a `bool`
4020+
* A discriminant in an `enum` not included in the type definition
4021+
* A value in a `char` which is a surrogate or above `char::MAX`
4022+
* Non-UTF-8 byte sequences in a `str`
4023+
* Unwinding into Rust from foreign code or unwinding from Rust into foreign
4024+
code. Rust's failure system is not compatible with exception handling in
4025+
other languages. Unwinding must be caught and handled at FFI boundaries.
4026+
4027+
[noalias]: http://llvm.org/docs/LangRef.html#noalias
4028+
4029+
## Behavior not considered unsafe
4030+
4031+
This is a list of behavior not considered *unsafe* in Rust terms, but that may
4032+
be undesired.
4033+
4034+
* Deadlocks
4035+
* Leaks of memory and other resources
4036+
* Exiting without calling destructors
4037+
* Integer overflow
4038+
- Overflow is considered "unexpected" behavior and is always user-error,
4039+
unless the `wrapping` primitives are used. In non-optimized builds, the compiler
4040+
will insert debug checks that panic on overflow, but in optimized builds overflow
4041+
instead results in wrapped values. See [RFC 560] for the rationale and more details.
4042+
4043+
[RFC 560]: https://github.com/rust-lang/rfcs/blob/master/text/0560-integer-overflow.md
4044+
40454045
# Appendix: Influences
40464046

40474047
Rust is not a particularly original language, with design elements coming from

0 commit comments

Comments
 (0)