Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Some constant/static updates. #867

Merged
merged 2 commits into from
Aug 19, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 expressions][array expressions]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Specifically only the length field of the repeat expression

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, sorry! I probably shouldn't open PRs late at night.

* 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.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

that's not possible in const fn yet either.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hm, maybe I'm a bit confused. The following seems to work for me:

const fn cfunc<T>(x: &T) -> usize {
    std::mem::size_of::<T>()
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ah, well, yes. I was thinking of trait bounds on generic parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the restriction on trait bounds is already mentioned above. Let me know if you think this should be worded any differently.


[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
7 changes: 4 additions & 3 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
guaranteed to refer to the same memory address.
context when used. This includes usage of constants from external crates.
References to the same constant are not necessarily guaranteed to refer to the
same memory address.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe also mention that this holds for !Copy types. So you can have a const FOO: String = String::new() and copy that around as many times as you want.


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
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 @@
> &nbsp;&nbsp; `[` [_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