Skip to content

Commit 9af6601

Browse files
dingxiangfei2009ehuss
authored andcommitted
add section on treatment on extern statics
1 parent d8a53df commit 9af6601

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
@@ -95,6 +95,17 @@ const fn not_allowed() -> &'static u32 {
9595
const WILL_FAIL: &'static u32 = not_allowed();
9696
```
9797

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+
98109
## Const Functions
99110

100111
A _const fn_ is a function that one is permitted to call from a const context. Declaring a function
@@ -130,6 +141,7 @@ of whether you are building on a `64` bit or a `32` bit system.
130141
[enum discriminants]: items/enumerations.md#discriminants
131142
[expression statements]: statements.md#expression-statements
132143
[expressions]: expressions.md
144+
[`extern`]: items/external-blocks.md#statics
133145
[field]: expressions/field-expr.md
134146
[functions]: items/functions.md
135147
[grouped]: expressions/grouped-expr.md

0 commit comments

Comments
 (0)