Skip to content

Commit 97abd04

Browse files
committed
Simplify const_refs_to_static
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`.
1 parent 9af6601 commit 97abd04

File tree

2 files changed

+2
-63
lines changed

2 files changed

+2
-63
lines changed

Diff for: src/const_eval.md

+2-36
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ to be run.
2424
* [Paths] to [functions] and [constants].
2525
Recursively defining constants is not allowed.
2626
* Paths to immutable [statics].
27+
* Reads of [`extern` statics] are not allowed.
2728
* [Tuple expressions].
2829
* [Array expressions].
2930
* [Struct] expressions.
@@ -71,41 +72,6 @@ surrounding generic parameters: such an expression must either be a single bare
7172
const generic parameter, or an arbitrary expression not making use of any
7273
generics.
7374

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-
const fn allowed() -> &'static u32 {
82-
static VALUE: u32 = 0;
83-
&VALUE
84-
}
85-
const A_CONST: &'static u32 = 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-
10975
## Const Functions
11076

11177
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.
141107
[enum discriminants]: items/enumerations.md#discriminants
142108
[expression statements]: statements.md#expression-statements
143109
[expressions]: expressions.md
144-
[`extern`]: items/external-blocks.md#statics
110+
[`extern` statics]: items/external-blocks.md#statics
145111
[field]: expressions/field-expr.md
146112
[functions]: items/functions.md
147113
[grouped]: expressions/grouped-expr.md

Diff for: src/items/constant-items.md

-27
Original file line numberDiff line numberDiff line change
@@ -90,32 +90,6 @@ m!(const _: () = (););
9090
// const _: () = ();
9191
```
9292

93-
## Use and reference to `static` items
94-
95-
When a constant item or constant block is defined, [`static` items] can be used, borrowed or taken address of.
96-
By extension, you are allowed to call methods that immutably borrows the `static` items as receivers.
97-
98-
```rust
99-
static A: u32 = 32;
100-
const ANOTHER_A: u32 = A;
101-
const BORROW_A: &'static u32 = &A;
102-
const POINTER_TO_A: *const u32 = &A as _;
103-
104-
struct MyStruct {
105-
inner: u32,
106-
}
107-
impl MyStruct {
108-
const fn get(&self) -> u32 {
109-
self.inner + 1
110-
}
111-
}
112-
static MYSTRUCT: MyStruct = MyStruct {
113-
inner: 0
114-
};
115-
const BORROW_STATIC_INNER: &'static u32 = &MYSTRUCT.inner;
116-
const CALL_CONST_STATIC_ASSOCIATED_METHOD: u32 = MYSTRUCT.get();
117-
```
118-
11993
## Evaluation
12094

12195
[Free][free] constants are always [evaluated][const_eval] at compile-time to surface
@@ -136,7 +110,6 @@ fn unused_generic_function<T>() {
136110
[constant value]: ../const_eval.md#constant-expressions
137111
[free]: ../glossary.md#free-item
138112
[static lifetime elision]: ../lifetime-elision.md#static-lifetime-elision
139-
[`static` items]: ./static-items.md
140113
[trait definition]: traits.md
141114
[IDENTIFIER]: ../identifiers.md
142115
[underscore imports]: use-declarations.md#underscore-imports

0 commit comments

Comments
 (0)