Skip to content

Commit aac08bb

Browse files
committed
document guarantee about evaluation of associated consts and const blocks
1 parent e356977 commit aac08bb

File tree

2 files changed

+10
-3
lines changed

2 files changed

+10
-3
lines changed

src/expressions/block-expr.md

+7-3
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ Const blocks allows you to define a constant value without having to define new
129129
It also supports type inference so there is no need to specify the type, unlike [constant items].
130130

131131
Const blocks have the ability to reference generic parameters in scope, unlike [free][free item] constant items.
132-
They are desugared to associated constant items with generic parameters in scope.
132+
They are desugared to constant items with generic parameters in scope (similar to associated constants, but without a trait or type they are associated with).
133133
For example, this code:
134134

135135
```rust
@@ -152,8 +152,12 @@ 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, then the const block is guaranteed to be evaluated.
156+
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 occurring in the function have successfully evaluated (and not panicked).
158+
Note that this is only guaranteed for all currently existing syntactic constructs;
159+
in the future, a new kind of block may be introduced such that constants inside that block are *not* unconditionally guaranteed to be evaluated.
160+
If the function is not executed at runtime, const blocks inside of it may or may not be evaluated.
157161

158162
## `unsafe` blocks
159163

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)