Skip to content

Commit

Permalink
document guarantee about evaluation of associated consts
Browse files Browse the repository at this point in the history
  • Loading branch information
RalfJung committed May 10, 2024
1 parent e356977 commit 1e2a01a
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 3 deletions.
11 changes: 8 additions & 3 deletions src/expressions/block-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ fn foo<T>() -> usize {
}
```

is equivalent to:
is roughly equivalent to:

```rust
fn foo<T>() -> usize {
Expand All @@ -152,8 +152,13 @@ fn foo<T>() -> usize {
}
```

This also means that const blocks are treated similarly to associated constants.
For example, they are not guaranteed to be evaluated when the enclosing function is unused.
If the function containing the const block is executed at runtime,
then the const block is guaranteed to be evaluated. This is true even if the const block occurs in dead code.
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).
Note that this is only guaranteed for all currently existing syntactic constructs; in the future, a new
kind of block may be introduced such that constants inside that block are *not* unconditionally guaranteed to be evaluated.
If the function is not executed at runtime, const blocks inside of it may or may not be evaluated.

## `unsafe` blocks

Expand Down
3 changes: 3 additions & 0 deletions src/expressions/path-expr.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,13 @@ let push_integer = Vec::<i32>::push;
let slice_reverse = <[i32]>::reverse;
```

Evaluation of associated constants is handled the same way as [`const` blocks].

[_PathInExpression_]: ../paths.md#paths-in-expressions
[_QualifiedPathInExpression_]: ../paths.md#qualified-paths
[place expressions]: ../expressions.md#place-expressions-and-value-expressions
[value expressions]: ../expressions.md#place-expressions-and-value-expressions
[path]: ../paths.md
[`static mut`]: ../items/static-items.md#mutable-statics
[`unsafe` block]: block-expr.md#unsafe-blocks
[`const` blocks]: block-expr.md#const-blocks

0 comments on commit 1e2a01a

Please sign in to comment.