You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
This removes some content for const_refs_to_static which seems to
duplicate content elsewhere. In particular:
* Remove the examples of allowed use of `static` in `const_eval.md`. We
don't show examples for any of the other permitted uses. Additionally,
I believe the other points in the list already cover concerns such as
interior mutability (and possibly more precise way, since the existing
content mentions things like transient places).
* I think a single statement that reads of `extern` statics is
sufficient to follow the pattern of all the other allowed behaviors.
* Removed the examples from the `constant-items.md` chapter about
allowing use of `static`s. Again, we generally don't duplicate what
is allowed unless there is some explicit statement indicating that
it is not. Also, I am reluctant to duplicate what is allowed between
`const_eval.md` and `constant-items.md`.
Copy file name to clipboardExpand all lines: src/const_eval.md
+2-36
Original file line number
Diff line number
Diff line change
@@ -24,6 +24,7 @@ to be run.
24
24
*[Paths] to [functions] and [constants].
25
25
Recursively defining constants is not allowed.
26
26
* Paths to immutable [statics].
27
+
* Reads of [`extern` statics] are not allowed.
27
28
*[Tuple expressions].
28
29
*[Array expressions].
29
30
*[Struct] expressions.
@@ -71,41 +72,6 @@ surrounding generic parameters: such an expression must either be a single bare
71
72
const generic parameter, or an arbitrary expression not making use of any
72
73
generics.
73
74
74
-
## Permitted use of `static`s
75
-
76
-
Within the constant evaluation context, items that ultimately contain no mutable memory or data that is
77
-
capable of interior mutability can be used, borrowed or taken address of.
78
-
Among those are the `static` items as long as they are not actually `mut` items.
79
-
80
-
```rust
81
-
constfnallowed() ->&'staticu32 {
82
-
staticVALUE:u32=0;
83
-
&VALUE
84
-
}
85
-
constA_CONST:&'staticu32=allowed();
86
-
```
87
-
88
-
On the contrary, for example, `static mut` and types embedding an `UnsafeCell` is not allowed.
89
-
90
-
```rust,edition2021,compile_fail,E0080
91
-
const fn not_allowed() -> &'static u32 {
92
-
static mut VALUE: u32 = 0;
93
-
&VALUE
94
-
}
95
-
const WILL_FAIL: &'static u32 = not_allowed();
96
-
```
97
-
98
-
One other instance that the constant evaluation will reject is any attempt to read values behind a `static` item declared in an [`extern`] block, even though the item is not marked as `mut`.
99
-
Computing the address to the `static` item is still permitted. However, as soon as a use or dereferencing of the whole or a part of the `static` item is performed, it constitutes a read access which will fail the constant evaluation.
100
-
101
-
```rust,edition2021,compile_fail,E0080
102
-
extern {
103
-
static S: u32;
104
-
}
105
-
106
-
const C: u32 = unsafe { S };
107
-
```
108
-
109
75
## Const Functions
110
76
111
77
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
@@ -141,7 +107,7 @@ of whether you are building on a `64` bit or a `32` bit system.
0 commit comments