@@ -17,11 +17,11 @@ causes substantial rightwards drift, which makes it hard to format the code
1717to be visually appealing.
1818
1919For example, having a ` 'static `  default for lifetimes would turn this:
20- ``` 
20+ ``` rust 
2121static  my_awesome_tables :  & 'static  [& 'static  HashMap <Cow <'static , str >, u32 >] =  .. 
2222``` 
2323into this:
24- ``` 
24+ ``` rust 
2525static  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
3535The same default that RFC #599   sets up for trait object is to be used for 
3636statics 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
3940Note that this RFC does not forbid writing the lifetimes, it only sets a 
4041default 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
4546The ` 'static `  default does * not*  override lifetime elision in function 
4647signatures, but work alongside it:
@@ -50,16 +51,40 @@ static foo: fn(&u32) -> &u32 = ...;  // for<'a> fn(&'a u32) -> &'a u32
5051static  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
5456is still possible.
5557
56- ``` 
57- trait SomeObject<'a> { ...  } 
58+ ``` rust 
59+ trait  SomeObject <'a > { ..  }
5860static  foo :  & SomeObject  =  ... ; //  &'static SomeObject<'static>
5961static  bar :  & for <'a > SomeObject <'a > =  ... ; //  &'static for<'a> SomeObject<'a>
6062static  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
75100the implementation it may or may not be possible to default lifetimes in
76101generics.
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
78108the code, so even if type inference for statics was to be implemented, 
79109defaulting lifetimes would have the benefit of pulling the cost-benefit 
0 commit comments