-
Notifications
You must be signed in to change notification settings - Fork 12.7k
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
Document RFC 1623: static lifetime elision. #37928
Merged
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
ba60af3
Document RFC 1623: static lifetime elision.
chriskrycho e8cb83a
Add feature flag to reference docs for RFC 1623.
chriskrycho 3f0ca55
Change placement of `[Unstable]` marker in RFC 1623 docs.
chriskrycho 4096dd6
Add more examples, get everything passing at last.
chriskrycho File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -1271,15 +1271,16 @@ guaranteed to refer to the same memory address. | |
|
||
Constant values must not have destructors, and otherwise permit most forms of | ||
data. Constants may refer to the address of other constants, in which case the | ||
address will have the `static` lifetime. The compiler is, however, still at | ||
address will have the `static` lifetime. (See below on [static lifetime | ||
elision](#static-lifetime-elision).) The compiler is, however, still at | ||
liberty to translate the constant many times, so the address referred to may not | ||
be stable. | ||
|
||
Constants must be explicitly typed. The type may be `bool`, `char`, a number, or | ||
a type derived from those primitive types. The derived types are references with | ||
the `static` lifetime, fixed-size arrays, tuples, enum variants, and structs. | ||
|
||
``` | ||
```rust | ||
const BIT1: u32 = 1 << 0; | ||
const BIT2: u32 = 1 << 1; | ||
|
||
|
@@ -1331,7 +1332,7 @@ running in the same process. | |
Mutable statics are still very useful, however. They can be used with C | ||
libraries and can also be bound from C libraries (in an `extern` block). | ||
|
||
``` | ||
```rust | ||
# fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 } | ||
|
||
static mut LEVELS: u32 = 0; | ||
|
@@ -1355,6 +1356,32 @@ unsafe fn bump_levels_unsafe2() -> u32 { | |
Mutable statics have the same restrictions as normal statics, except that the | ||
type of the value is not required to ascribe to `Sync`. | ||
|
||
#### `'static` lifetime elision | ||
|
||
[Unstable] Both constant and static declarations of reference types have | ||
*implicit* `'static` lifetimes unless an explicit lifetime is specified. As | ||
such, the constant declarations involving `'static` above may be written | ||
without the lifetimes. Returning to our previous example: | ||
|
||
```rust | ||
#[feature(static_in_const)] | ||
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. this should be 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. YOU ARE MY HERO |
||
const BIT1: u32 = 1 << 0; | ||
const BIT2: u32 = 1 << 1; | ||
|
||
const BITS: [u32; 2] = [BIT1, BIT2]; | ||
const STRING: &str = "bitstring"; | ||
|
||
struct BitsNStrings<'a> { | ||
mybits: [u32; 2], | ||
mystring: &'a str, | ||
} | ||
|
||
const BITS_N_STRINGS: BitsNStrings = BitsNStrings { | ||
mybits: BITS, | ||
mystring: STRING, | ||
}; | ||
``` | ||
|
||
### Traits | ||
|
||
A _trait_ describes an abstract interface that types can | ||
|
@@ -2458,9 +2485,6 @@ The currently implemented features of the reference compiler are: | |
into a Rust program. This capability, especially the signature for the | ||
annotated function, is subject to change. | ||
|
||
* `static_in_const` - Enables lifetime elision with a `'static` default for | ||
`const` and `static` item declarations. | ||
|
||
* `thread_local` - The usage of the `#[thread_local]` attribute is experimental | ||
and should be seen as unstable. This attribute is used to | ||
declare a `static` as being unique per-thread leveraging | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
How about "will have elided lifetimes where applicable, otherwise – in most cases – defaulting to the
'static
lifetime.