-
Notifications
You must be signed in to change notification settings - Fork 508
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
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
|
@@ -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] | ||
* The initializer of | ||
* [constants] | ||
* [statics] | ||
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. that's not possible in There was a problem hiding this comment. Choose a reason for hiding this commentThe 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>()
} There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. ah, well, yes. I was thinking of trait bounds on generic parameters. There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Maybe also mention that this holds for |
||
|
||
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 | ||
|
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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.