Skip to content

Commit 95285c4

Browse files
committed
Auto merge of #28621 - tshepang:move-safety, r=steveklabnik
2 parents b75fe81 + 4a5e190 commit 95285c4

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
@@ -984,99 +984,6 @@ The type parameters can also be explicitly supplied in a trailing
984984
there is not sufficient context to determine the type parameters. For example,
985985
`mem::size_of::<u32>() == 4`.
986986

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

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

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

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

0 commit comments

Comments
 (0)