Skip to content

Commit 1e2a01a

Browse files
committed
document guarantee about evaluation of associated consts
1 parent e356977 commit 1e2a01a

File tree

2 files changed

+11
-3
lines changed

2 files changed

+11
-3
lines changed

src/expressions/block-expr.md

+8-3
Original file line numberDiff line numberDiff line change
@@ -138,7 +138,7 @@ fn foo<T>() -> usize {
138138
}
139139
```
140140

141-
is equivalent to:
141+
is roughly equivalent to:
142142

143143
```rust
144144
fn foo<T>() -> usize {
@@ -152,8 +152,13 @@ fn foo<T>() -> usize {
152152
}
153153
```
154154

155-
This also means that const blocks are treated similarly to associated constants.
156-
For example, they are not guaranteed to be evaluated when the enclosing function is unused.
155+
If the function containing the const block is executed at runtime,
156+
then the const block is guaranteed to be evaluated. This is true even if the const block occurs in dead code.
157+
In particular, unsafe code inside a function can rely on the fact that all const blocks syntactically
158+
occurring in the function have successfully evaluated (and not panicked).
159+
Note that this is only guaranteed for all currently existing syntactic constructs; in the future, a new
160+
kind of block may be introduced such that constants inside that block are *not* unconditionally guaranteed to be evaluated.
161+
If the function is not executed at runtime, const blocks inside of it may or may not be evaluated.
157162

158163
## `unsafe` blocks
159164

src/expressions/path-expr.md

+3
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,13 @@ let push_integer = Vec::<i32>::push;
2323
let slice_reverse = <[i32]>::reverse;
2424
```
2525

26+
Evaluation of associated constants is handled the same way as [`const` blocks].
27+
2628
[_PathInExpression_]: ../paths.md#paths-in-expressions
2729
[_QualifiedPathInExpression_]: ../paths.md#qualified-paths
2830
[place expressions]: ../expressions.md#place-expressions-and-value-expressions
2931
[value expressions]: ../expressions.md#place-expressions-and-value-expressions
3032
[path]: ../paths.md
3133
[`static mut`]: ../items/static-items.md#mutable-statics
3234
[`unsafe` block]: block-expr.md#unsafe-blocks
35+
[`const` blocks]: block-expr.md#const-blocks

0 commit comments

Comments
 (0)