1
- - Feature Name: (fill me in with a unique ident, my_awesome_feature)
2
- - Start Date: (fill me in with today's date, YYYY-MM-DD)
1
+ - Feature Name: ` infer_outlives `
2
+ - Start Date: 2017-08-02
3
3
- RFC PR: https://github.com/rust-lang/rfcs/pull/2093
4
4
- Rust Issue: https://github.com/rust-lang/rust/issues/44493
5
5
8
8
9
9
Remove the need for explicit ` T: 'x ` annotations on structs. We will
10
10
infer their presence based on the fields of the struct. In short, if
11
- the struct contains a reference ( directly or indirectly) to ` T ` with
11
+ the struct contains a reference, directly or indirectly, to ` T ` with
12
12
lifetime ` 'x ` , then we will infer that ` T: 'x ` is a requirement:
13
13
14
14
``` rust
@@ -24,7 +24,7 @@ lifetime defaults, and simply for backwards compatibility.
24
24
# Motivation
25
25
[ motivation ] : #motivation
26
26
27
- Today, when you writes generic struct definitions that contain
27
+ Today, when you write generic struct definitions that contain
28
28
references, those structs require where-clauses of the form `T:
29
29
'a`:
30
30
@@ -73,7 +73,7 @@ fixed-point inference similar to [variance inference].
73
73
74
74
[ variance inference ] : https://github.com/rust-lang/rfcs/blob/master/text/0738-variance.md
75
75
76
- There is one other ( relatively obscure) place where explicit lifetime
76
+ There is one other, relatively obscure, place where explicit lifetime
77
77
annotations are used today: trait object lifetime defaults
78
78
([ RFC 599] [ ] ). The interaction there is discussed in the Guide-Level
79
79
Explanation below.
@@ -97,7 +97,7 @@ has no references at all. (The "outlives" rules were later tweaked by
97
97
In practice, this means that in Rust, when you define a struct that
98
98
contains references to a generic type, or references to other
99
99
references, you need to add various where clauses for that struct type
100
- to be considered valid. For example, consider this (currently invalid)
100
+ to be considered valid. For example, consider the following (currently invalid)
101
101
struct ` SharedRef ` :
102
102
103
103
``` rust
@@ -123,7 +123,7 @@ struct SharedRef<'a, T>
123
123
In principle, similar where clauses would be required on generic
124
124
functions or impl to ensure that their parameters or inputs are
125
125
well-formed. However, as you may have noticed, this is not the
126
- case. For example, this function is valid as written:
126
+ case. For example, the following function is valid as written:
127
127
128
128
``` rust
129
129
fn foo <'a , T >(x : & 'a T ) {
@@ -190,7 +190,7 @@ impl<'a, T> MakeRef<'a> for Vec<T>
190
190
In this case, the impl has two inputs -- the lifetime ` 'a ` and the
191
191
type ` Vec<T> ` (note that ` 'a ` and ` T ` are the impl parameters; the
192
192
inputs come from the parameters of the trait that is being
193
- implemented). Neither of these inputs requires that ` T: 'a ` , so when
193
+ implemented). Neither of these inputs requires that ` T: 'a ` . So, when
194
194
we try to specify the value of the associated type as ` &'a T ` , we
195
195
still require a where clause to infer that ` T: 'a ` must hold.
196
196
@@ -247,7 +247,7 @@ The type `Ref<'x, Debug>` defaults to `Ref<'x, Debug + 'x>` and not
247
247
` Ref<'x, Debug + 'static> ` . Effectively the ` where T: 'a ` declaration
248
248
acts as a kind of signal that ` Ref ` acts as a "reference to ` T ` ".
249
249
250
- This RFC does not change these defaulting rules: in particular, these
250
+ This RFC does not change these defaulting rules. In particular, these
251
251
defaults are applied ** before** where-clause inference takes place,
252
252
and hence are not affected by the results. Trait object defaulting
253
253
therefore requires an explicit ` where T: 'a ` declaration on the
@@ -336,7 +336,7 @@ annotation is still potentially a breaking change (even if it would be
336
336
inferred), due to the trait object rules; but also, adding or removing
337
337
a field of type ` &'a T ` could affect the results of inference, and
338
338
hence may be a breaking change. As an example, consider a struct like
339
- this :
339
+ the following :
340
340
341
341
``` rust
342
342
struct Iter <'a , T > {
@@ -407,7 +407,7 @@ This set must meet the constraints derived by the following algorithm.
407
407
408
408
First, if the struct contains a where-clause ` C ` matching the above
409
409
forms, then we add the constraint that ` C in A[S] ` . So, for example,
410
- in this struct:
410
+ in the following struct:
411
411
412
412
``` rust
413
413
struct Foo <'a , T > where T : 'a { .. }
@@ -428,7 +428,7 @@ the outlines of the algorithm:
428
428
not necessarily a type parameter; for example, ` &'a &'b i32 ` would
429
429
lead to the outlives requirement that ` 'b: 'a ` ).
430
430
- A reference to a struct like ` Foo<'a, T> ` may also require outlives
431
- requirements; this is determined by checking the (current) value of
431
+ requirements. This is determined by checking the (current) value of
432
432
` A[Foo] ` , after substituting its parameters.
433
433
- For an associated type reference like ` <T as BarTrait<'a>>::Type ` , we do
434
434
not attempt normalization, but rather just check that ` T ` is well-formed.
@@ -483,7 +483,7 @@ Here, the requirement will be that `<T as Iterator>::Item: 'a`.
483
483
### Example 3: Explicit where-clauses
484
484
485
485
In some cases, we may have constraints that arise from explicit where-clauses
486
- and not from field types, as in this example:
486
+ and not from field types, as in the following example:
487
487
488
488
``` rust
489
489
struct Foo <'b , U > {
0 commit comments