Skip to content

Commit b265568

Browse files
Clarify rules around promotablility of consts
1 parent 49e161b commit b265568

File tree

1 file changed

+12
-6
lines changed

1 file changed

+12
-6
lines changed

promotion.md

+12-6
Original file line numberDiff line numberDiff line change
@@ -115,16 +115,22 @@ resources for little benefit.
115115

116116
### Access to a `const` or `static`
117117

118-
Accesses to `const`s are always promotable, regardless of the body of the
119-
`const`. For instance, while the previous example was not legal, the
120-
following would be:
118+
When accessing a `const` in a promotable context, the restrictions on single
119+
assignment and named locals do not apply to the body of the `const`. All other
120+
restrictions, notably that the result of the `const` cannot be `Drop` or mutable
121+
through a reference still apply. For instance, while the previous example was
122+
not legal, the following would be:
121123

122124
```rust
123-
const NOT_WINDOWS: i32 = if cfg!(windows) { 0 } else { 1 };
124-
let x: &'static i32 = &NOT_WINDOWS;
125+
const BOOL: i32 = {
126+
let ret = if cfg!(windows) { 0 } else { 1 };
127+
ret
128+
};
129+
130+
let x: &'static i32 = &BOOL;
125131
```
126132

127-
However, an access to a `static` is only promotable within the initializer of
133+
An access to a `static` is only promotable within the initializer of
128134
another `static`.
129135

130136
### Panics

0 commit comments

Comments
 (0)