Skip to content

Commit

Permalink
Merge pull request #867 from ehuss/const-updates
Browse files Browse the repository at this point in the history
Some constant/static updates.
  • Loading branch information
Havvy committed Aug 19, 2020
2 parents fba96cc + da910b7 commit 1b6c4b0
Show file tree
Hide file tree
Showing 4 changed files with 21 additions and 11 deletions.
9 changes: 7 additions & 2 deletions src/const_eval.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,9 @@ also constant expressions and do not cause any [`Drop::drop`][destructors] calls
to be run.

* [Literals].
* [Paths] to [functions] and constants.
* [Paths] to [functions] and [constants].
Recursively defining constants is not allowed.
* Paths to [statics]. These are only allowed within the initializer of a static.
* [Tuple expressions].
* [Array expressions].
* [Struct] expressions.
Expand Down Expand Up @@ -53,7 +54,7 @@ to be run.
A _const context_ is one of the following:

* [Array type length expressions]
* Repeat expression length expressions
* [Array repeat length expressions][array expressions]
* The initializer of
* [constants]
* [statics]
Expand All @@ -76,6 +77,10 @@ Notable features that const contexts have, but const fn haven't are:
* union field access
* [`transmute`] invocations.

Conversely, the following are possible in a const function, but not in a const context:

* Use of generic parameters.

[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
[array expressions]: expressions/array-expr.md
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions
Expand Down
6 changes: 4 additions & 2 deletions src/items/constant-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
A *constant item* is an optionally named _[constant value]_ which is not associated
with a specific memory location in the program. Constants are essentially inlined
wherever they are used, meaning that they are copied directly into the relevant
context when used. References to the same constant are not necessarily
context when used. This includes usage of constants from external crates, and
non-[`Copy`] types. References to the same constant are not necessarily
guaranteed to refer to the same memory address.

Constants must be explicitly typed. The type must have a `'static` lifetime: any
references it contains must have `'static` lifetimes.
references in the initializer must have `'static` lifetimes.

Constants may refer to the address of other constants, in which case the
address will have elided lifetimes where applicable, otherwise – in most cases
Expand Down Expand Up @@ -94,3 +95,4 @@ m!(const _: () = (););
[underscore imports]: use-declarations.md#underscore-imports
[_Type_]: ../types.md#type-expressions
[_Expression_]: ../expressions.md
[`Copy`]: ../special-types-and-traits.md#copy
14 changes: 8 additions & 6 deletions src/items/static-items.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,17 +8,19 @@
A *static item* is similar to a [constant], except that it represents a precise
memory location in the program. All references to the static refer to the same
memory location. Static items have the `static` lifetime, which outlives all
other lifetimes in a Rust program. Non-`mut` static items that contain a type
that is not [interior mutable] may be placed in read-only memory. Static items
do not call [`drop`] at the end of the program.
other lifetimes in a Rust program. Static items do not call [`drop`] at the
end of the program.

The static initializer is a [constant expression] evaluated at compile time.
Static initializers may refer to other statics.

Non-`mut` static items that contain a type that is not [interior mutable] may
be placed in read-only memory.

All access to a static is safe, but there are a number of restrictions on
statics:

* The type must have the `Sync` trait bound to allow thread-safe access.
* Statics allow using paths to statics in the [constant expression] used to
initialize them, but statics may not refer to other statics by value, only
through a reference.
* Constants cannot refer to statics.

## Mutable statics
Expand Down
3 changes: 2 additions & 1 deletion src/types/array.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
>    `[` [_Type_] `;` [_Expression_] `]`
An array is a fixed-size sequence of `N` elements of type `T`. The array type
is written as `[T; N]`. The size is an expression that evaluates to a
is written as `[T; N]`. The size is a [constant expression] that evaluates to a
[`usize`].

Examples:
Expand All @@ -28,3 +28,4 @@ always bounds-checked in safe methods and operators.
[_Type_]: ../types.md#type-expressions
[`Vec<T>`]: ../../std/vec/struct.Vec.html
[`usize`]: numeric.md#machine-dependent-integer-types
[constant expression]: ../const_eval.md#constant-expressions

0 comments on commit 1b6c4b0

Please sign in to comment.