Skip to content

Commit 1b6c4b0

Browse files
authored
Merge pull request #867 from ehuss/const-updates
Some constant/static updates.
2 parents fba96cc + da910b7 commit 1b6c4b0

File tree

4 files changed

+21
-11
lines changed

4 files changed

+21
-11
lines changed

src/const_eval.md

+7-2
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,9 @@ also constant expressions and do not cause any [`Drop::drop`][destructors] calls
2020
to be run.
2121

2222
* [Literals].
23-
* [Paths] to [functions] and constants.
23+
* [Paths] to [functions] and [constants].
2424
Recursively defining constants is not allowed.
25+
* Paths to [statics]. These are only allowed within the initializer of a static.
2526
* [Tuple expressions].
2627
* [Array expressions].
2728
* [Struct] expressions.
@@ -53,7 +54,7 @@ to be run.
5354
A _const context_ is one of the following:
5455

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

80+
Conversely, the following are possible in a const function, but not in a const context:
81+
82+
* Use of generic parameters.
83+
7984
[arithmetic]: expressions/operator-expr.md#arithmetic-and-logical-binary-operators
8085
[array expressions]: expressions/array-expr.md
8186
[array indexing]: expressions/array-expr.md#array-and-slice-indexing-expressions

src/items/constant-items.md

+4-2
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,12 @@
77
A *constant item* is an optionally named _[constant value]_ which is not associated
88
with a specific memory location in the program. Constants are essentially inlined
99
wherever they are used, meaning that they are copied directly into the relevant
10-
context when used. References to the same constant are not necessarily
10+
context when used. This includes usage of constants from external crates, and
11+
non-[`Copy`] types. References to the same constant are not necessarily
1112
guaranteed to refer to the same memory address.
1213

1314
Constants must be explicitly typed. The type must have a `'static` lifetime: any
14-
references it contains must have `'static` lifetimes.
15+
references in the initializer must have `'static` lifetimes.
1516

1617
Constants may refer to the address of other constants, in which case the
1718
address will have elided lifetimes where applicable, otherwise – in most cases
@@ -94,3 +95,4 @@ m!(const _: () = (););
9495
[underscore imports]: use-declarations.md#underscore-imports
9596
[_Type_]: ../types.md#type-expressions
9697
[_Expression_]: ../expressions.md
98+
[`Copy`]: ../special-types-and-traits.md#copy

src/items/static-items.md

+8-6
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,19 @@
88
A *static item* is similar to a [constant], except that it represents a precise
99
memory location in the program. All references to the static refer to the same
1010
memory location. Static items have the `static` lifetime, which outlives all
11-
other lifetimes in a Rust program. Non-`mut` static items that contain a type
12-
that is not [interior mutable] may be placed in read-only memory. Static items
13-
do not call [`drop`] at the end of the program.
11+
other lifetimes in a Rust program. Static items do not call [`drop`] at the
12+
end of the program.
13+
14+
The static initializer is a [constant expression] evaluated at compile time.
15+
Static initializers may refer to other statics.
16+
17+
Non-`mut` static items that contain a type that is not [interior mutable] may
18+
be placed in read-only memory.
1419

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

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

2426
## Mutable statics

src/types/array.md

+2-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
>    `[` [_Type_] `;` [_Expression_] `]`
66
77
An array is a fixed-size sequence of `N` elements of type `T`. The array type
8-
is written as `[T; N]`. The size is an expression that evaluates to a
8+
is written as `[T; N]`. The size is a [constant expression] that evaluates to a
99
[`usize`].
1010

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

0 commit comments

Comments
 (0)