Skip to content

Commit

Permalink
const expression can borrow static items
Browse files Browse the repository at this point in the history
  • Loading branch information
dingxiangfei2009 committed Nov 1, 2024
1 parent 604e682 commit dec67c2
Show file tree
Hide file tree
Showing 3 changed files with 9 additions and 6 deletions.
6 changes: 5 additions & 1 deletion src/const_eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,10 @@ r[const-eval.const-expr.path-item]
Recursively defining constants is not allowed.

r[const-eval.const-expr.path-static]
* Paths to [statics]. These are only allowed within the initializer of a static.
* Paths to [statics] with these restrictions and observations.
* In particular, reads and writes to any `static`, `static mut` or [`extern` statics] is not allowed.
* Immutable borrows and pointers into immutable part of a `static` are allowed and observes the same restriction
on all other forms of [borrow]s as mentioned below.

r[const-eval.const-expr.tuple]
* [Tuple expressions].
Expand Down Expand Up @@ -177,6 +180,7 @@ of whether you are building on a `64` bit or a `32` bit system.
[enum discriminants]: items/enumerations.md#discriminants
[expression statements]: statements.md#expression-statements
[expressions]: expressions.md
[`extern` statics]: items/external-blocks.md#statics
[field]: expressions/field-expr.md
[functions]: items/functions.md
[grouped]: expressions/grouped-expr.md
Expand Down
2 changes: 2 additions & 0 deletions src/items/constant-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
};
```

The final value of a `const` item cannot contain references to anything mutable.

r[items.const.expr-omission]
The constant expression may only be omitted in a [trait definition].

Expand Down
7 changes: 2 additions & 5 deletions src/items/static-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,10 +37,7 @@ All access to a static is safe, but there are a number of restrictions on
statics:

r[items.static.sync]
* The type must have the `Sync` trait bound to allow thread-safe access.

r[items.static.const]
* Constants cannot refer to statics.
* The type must have the [`Sync`](std::marker::Sync) trait bound to allow thread-safe access.

r[items.static.init.omission]
The initializer expression must be omitted in an [external block], and must be
Expand Down Expand Up @@ -159,7 +156,7 @@ It can be confusing whether or not you should use a constant item or a static
item. Constants should, in general, be preferred over statics unless one of the
following are true:

* Large amounts of data are being stored
* Large amounts of data are being stored.
* The single-address property of statics is required.
* Interior mutability is required.

Expand Down

0 comments on commit dec67c2

Please sign in to comment.