Skip to content

Commit 2526483

Browse files
committed
clarify interaction with elisions
1 parent 93b685e commit 2526483

File tree

1 file changed

+39
-9
lines changed

1 file changed

+39
-9
lines changed

text/0000-static.md

Lines changed: 39 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,11 @@ causes substantial rightwards drift, which makes it hard to format the code
1717
to be visually appealing.
1818

1919
For example, having a `'static` default for lifetimes would turn this:
20-
```
20+
```rust
2121
static my_awesome_tables: &'static [&'static HashMap<Cow<'static, str>, u32>] = ..
2222
```
2323
into this:
24-
```
24+
```rust
2525
static my_awesome_table: &[&HashMap<Cow<str>, u32>] = ..
2626
```
2727

@@ -34,13 +34,14 @@ elision for function signatures will work as it does now (see example below).
3434

3535
The same default that RFC #599 sets up for trait object is to be used for
3636
statics and const declarations. In those declarations, the compiler will assume
37-
`'static` when a lifetime is not explicitly given in both refs and generics.
37+
`'static` when a lifetime is not explicitly given in all reference lifetimes,
38+
including reference lifetimes obtained via generic substitution.
3839

3940
Note that this RFC does not forbid writing the lifetimes, it only sets a
4041
default when no is given. Thus the change will not cause any breakage and is
41-
thus backwards-compatible. It's also very unlikely that implementing this RFC
42-
will restrict our design space for `static` and `const` definitions down the
43-
road.
42+
therefore backwards-compatible. It's also very unlikely that implementing this
43+
RFC will restrict our design space for `static` and `const` definitions down
44+
the road.
4445

4546
The `'static` default does *not* override lifetime elision in function
4647
signatures, but work alongside it:
@@ -50,16 +51,40 @@ static foo: fn(&u32) -> &u32 = ...; // for<'a> fn(&'a u32) -> &'a u32
5051
static bar: &Fn(&u32) -> &u32 = ...; // &'static for<'a> Fn(&'a u32) -> &'a u32
5152
```
5253

53-
With generics, it will work as anywhere else. Notably, writing out the lifetime
54+
With generics, it will work as anywhere else, also differentiating between
55+
function lifetimes and reference lifetimes. Notably, writing out the lifetime
5456
is still possible.
5557

56-
```
57-
trait SomeObject<'a> { ... }
58+
```rust
59+
trait SomeObject<'a> { .. }
5860
static foo: &SomeObject = ...; // &'static SomeObject<'static>
5961
static bar: &for<'a> SomeObject<'a> = ...; // &'static for<'a> SomeObject<'a>
6062
static baz: &'static [u8] = ...;
63+
64+
struct SomeStruct<'a, 'b> {
65+
foo: &'a Foo,
66+
bar: &'a Bar,
67+
f: for<'b> Fn(&'b Foo) -> &'b Bar
68+
}
69+
70+
static blub: &SomeStruct = ...; // &'static SomeStruct<'static, 'b> for any 'b
6171
```
6272

73+
It will still be an error to omit lifetimes in function types *not* eligible
74+
for elision, e.g.
75+
76+
```rust
77+
static blobb: FnMut(&Foo, &Bar) -> &Baz = ...; //~ ERROR: missing lifetimes for
78+
//^ &Foo, &Bar, &Baz
79+
```
80+
81+
This ensures that the really hairy cases that need the full type documented
82+
aren't unduly abbreviated.
83+
84+
It should also be noted that since statics and constants have no `self` type,
85+
elision will only work with distinct input lifetimes or one input+output
86+
lifetime.
87+
6388
# Drawbacks
6489
[drawbacks]: #drawbacks
6590

@@ -74,6 +99,11 @@ writing macros if they have many resources.
7499
* Write the aforementioned macro. This is inferior in terms of UX. Depending on
75100
the implementation it may or may not be possible to default lifetimes in
76101
generics.
102+
* Make all non-elided lifetimes `'static`. This has the drawback of creating
103+
hard-to-spot errors (that would also probably occur in the wrong place) and
104+
confusing users.
105+
* Make all non-declared lifetimes `'static`. This would not be backwards
106+
compatible due to interference with lifetime elision.
77107
* Infer types for statics. The absence of types makes it harder to reason about
78108
the code, so even if type inference for statics was to be implemented,
79109
defaulting lifetimes would have the benefit of pulling the cost-benefit

0 commit comments

Comments
 (0)