Skip to content

Commit a2dde9c

Browse files
add section on treatment on extern statics
1 parent aa13d47 commit a2dde9c

File tree

1 file changed

+12
-0
lines changed

1 file changed

+12
-0
lines changed

src/const_eval.md

+12
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ const fn not_allowed() -> &'static u32 {
9191
const WILL_FAIL: &'static u32 = not_allowed();
9292
```
9393

94+
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`.
95+
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 constitutes a read access which will fail the constant evaluation.
96+
97+
```rust,edition2021,compile_fail,E0080
98+
extern {
99+
static S: u32;
100+
}
101+
102+
const C: u32 = unsafe { S };
103+
```
104+
94105
## Const Functions
95106

96107
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
@@ -126,6 +137,7 @@ of whether you are building on a `64` bit or a `32` bit system.
126137
[enum discriminants]: items/enumerations.md#discriminants
127138
[expression statements]: statements.md#expression-statements
128139
[expressions]: expressions.md
140+
[`extern`]: items/external-blocks.md#statics
129141
[field]: expressions/field-expr.md
130142
[functions]: items/functions.md
131143
[grouped]: expressions/grouped-expr.md

0 commit comments

Comments
 (0)