Skip to content

Commit 7f7dc76

Browse files
authored
Rollup merge of rust-lang#37928 - chriskrycho:document-rfc-1623, r=steveklabnik
Document RFC 1623: static lifetime elision. This should be the last item required for stabilizing RFC 1623 (rust-lang#35897).
2 parents 29dece1 + 4096dd6 commit 7f7dc76

File tree

1 file changed

+60
-9
lines changed

1 file changed

+60
-9
lines changed

src/doc/reference.md

+60-9
Original file line numberDiff line numberDiff line change
@@ -1291,15 +1291,18 @@ guaranteed to refer to the same memory address.
12911291

12921292
Constant values must not have destructors, and otherwise permit most forms of
12931293
data. Constants may refer to the address of other constants, in which case the
1294-
address will have the `static` lifetime. The compiler is, however, still at
1295-
liberty to translate the constant many times, so the address referred to may not
1296-
be stable.
1294+
address will have elided lifetimes where applicable, otherwise – in most cases –
1295+
defaulting to the `static` lifetime. (See below on [static lifetime elision].)
1296+
The compiler is, however, still at liberty to translate the constant many times,
1297+
so the address referred to may not be stable.
1298+
1299+
[static lifetime elision]: #static-lifetime-elision
12971300

12981301
Constants must be explicitly typed. The type may be `bool`, `char`, a number, or
12991302
a type derived from those primitive types. The derived types are references with
13001303
the `static` lifetime, fixed-size arrays, tuples, enum variants, and structs.
13011304

1302-
```
1305+
```rust
13031306
const BIT1: u32 = 1 << 0;
13041307
const BIT2: u32 = 1 << 1;
13051308

@@ -1317,6 +1320,8 @@ const BITS_N_STRINGS: BitsNStrings<'static> = BitsNStrings {
13171320
};
13181321
```
13191322

1323+
1324+
13201325
### Static items
13211326

13221327
A *static item* is similar to a *constant*, except that it represents a precise
@@ -1351,7 +1356,7 @@ running in the same process.
13511356
Mutable statics are still very useful, however. They can be used with C
13521357
libraries and can also be bound from C libraries (in an `extern` block).
13531358

1354-
```
1359+
```rust
13551360
# fn atomic_add(_: &mut u32, _: u32) -> u32 { 2 }
13561361

13571362
static mut LEVELS: u32 = 0;
@@ -1375,6 +1380,53 @@ unsafe fn bump_levels_unsafe2() -> u32 {
13751380
Mutable statics have the same restrictions as normal statics, except that the
13761381
type of the value is not required to ascribe to `Sync`.
13771382

1383+
#### `'static` lifetime elision
1384+
1385+
[Unstable] Both constant and static declarations of reference types have
1386+
*implicit* `'static` lifetimes unless an explicit lifetime is specified. As
1387+
such, the constant declarations involving `'static` above may be written
1388+
without the lifetimes. Returning to our previous example:
1389+
1390+
```rust
1391+
# #![feature(static_in_const)]
1392+
const BIT1: u32 = 1 << 0;
1393+
const BIT2: u32 = 1 << 1;
1394+
1395+
const BITS: [u32; 2] = [BIT1, BIT2];
1396+
const STRING: &str = "bitstring";
1397+
1398+
struct BitsNStrings<'a> {
1399+
mybits: [u32; 2],
1400+
mystring: &'a str,
1401+
}
1402+
1403+
const BITS_N_STRINGS: BitsNStrings = BitsNStrings {
1404+
mybits: BITS,
1405+
mystring: STRING,
1406+
};
1407+
```
1408+
1409+
Note that if the `static` or `const` items include function or closure
1410+
references, which themselves include references, the compiler will first try the
1411+
standard elision rules ([see discussion in the nomicon][elision-nomicon]). If it
1412+
is unable to resolve the lifetimes by its usual rules, it will default to using
1413+
the `'static` lifetime. By way of example:
1414+
1415+
[elision-nomicon]: https://doc.rust-lang.org/nomicon/lifetime-elision.html
1416+
1417+
```rust,ignore
1418+
// Resolved as `fn<'a>(&'a str) -> &'a str`.
1419+
const RESOLVED_SINGLE: fn(&str) -> &str = ..
1420+
1421+
// Resolved as `Fn<'a, 'b, 'c>(&'a Foo, &'b Bar, &'c Baz) -> usize`.
1422+
const RESOLVED_MULTIPLE: Fn(&Foo, &Bar, &Baz) -> usize = ..
1423+
1424+
// There is insufficient information to bound the return reference lifetime
1425+
// relative to the argument lifetimes, so the signature is resolved as
1426+
// `Fn(&'static Foo, &'static Bar) -> &'static Baz`.
1427+
const RESOLVED_STATIC: Fn(&Foo, &Bar) -> &Baz = ..
1428+
```
1429+
13781430
### Traits
13791431

13801432
A _trait_ describes an abstract interface that types can
@@ -2072,7 +2124,9 @@ macro scope.
20722124

20732125
### Miscellaneous attributes
20742126

2075-
- `deprecated` - mark the item as deprecated; the full attribute is `#[deprecated(since = "crate version", note = "...")`, where both arguments are optional.
2127+
- `deprecated` - mark the item as deprecated; the full attribute is
2128+
`#[deprecated(since = "crate version", note = "...")`, where both arguments
2129+
are optional.
20762130
- `export_name` - on statics and functions, this determines the name of the
20772131
exported symbol.
20782132
- `link_section` - on statics and functions, this specifies the section of the
@@ -2489,9 +2543,6 @@ The currently implemented features of the reference compiler are:
24892543
into a Rust program. This capability, especially the signature for the
24902544
annotated function, is subject to change.
24912545

2492-
* `static_in_const` - Enables lifetime elision with a `'static` default for
2493-
`const` and `static` item declarations.
2494-
24952546
* `thread_local` - The usage of the `#[thread_local]` attribute is experimental
24962547
and should be seen as unstable. This attribute is used to
24972548
declare a `static` as being unique per-thread leveraging

0 commit comments

Comments
 (0)