Skip to content

Commit 53292b1

Browse files
committed
document implicit lifetime bounds
Add documentation about implicit lifetime bounds that the compiler can infer for functions, trait implementations, and structs. This draws from RFC 2093 (for structs) and RFC 1214 for functions and trait implementations.
1 parent 1c14973 commit 53292b1

File tree

1 file changed

+87
-0
lines changed

1 file changed

+87
-0
lines changed

src/trait-bounds.md

+87
Original file line numberDiff line numberDiff line change
@@ -117,6 +117,93 @@ fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) where 'a: 'b {
117117
`T: 'a` means that all lifetime parameters of `T` outlive `'a`.
118118
For example, if `'a` is an unconstrained lifetime parameter, then `i32: 'static` and `&'static str: 'a` are satisfied, but `Vec<&'a ()>: 'static` is not.
119119

120+
In the following text, the words "inferred" and "implied" refer to similar lifetime bounds that may be omitted.
121+
These different words appear in different contexts in existing documentation, due to implementation details.
122+
From the programmer's perspective, they both refer to a category of lifetime bounds that are not required to be explicitly written because the compiler can derive them from other information.
123+
124+
For functions and trait implementations, some lifetime bounds are implied because inputs to functions and trait implementations are assumed to be well-formed.
125+
For the purposes of determining implied bounds on functions, both the function parameter types and the function return type are considered to be inputs.
126+
127+
For example, for a function parameter `x: &'a T` to be well-formed, `T: 'a` must be satisfied (referents must outlive any references to them), so it is not necessary to explicitly write that lifetime bound on the function definition.
128+
On the other hand, removing the bound `where 'a: 'b` from the above example results in an error, because the constructed reference type exists only in the function body and is not among the inputs to `f`:
129+
130+
```rust,compile_fail
131+
fn f<'a, 'b>(x: &'a i32, mut y: &'b i32) {
132+
// This is NOT well formed, because the bound `'a: 'b` is missing;
133+
// therefore, `&'b &'a i32` could possibly outlive `&'a i32`.
134+
let r: &'b &'a i32 = &&0; // error
135+
}
136+
```
137+
138+
For the purpose of determining implied bounds on trait implementations, the implementing type (the `T` in `impl Trait for T`) is considered to be an input, as are generic parameters on the trait definition.
139+
Parameters of the trait implementation, which come directly after the `impl` keyword, are not otherwise considered to be inputs.
140+
For example, for the `Vec<T>` implementation below, `'a` and `Vec<T>` are inputs to the trait, but `T` and `&'a T` are not:
141+
142+
```rust
143+
trait MakeRef<'a> { type Type; }
144+
145+
impl<'a, T> MakeRef<'a> for Vec<T>
146+
where T: 'a // Required because `&'a T` is not an input to the trait
147+
{
148+
type Type = &'a T;
149+
}
150+
151+
// `T: 'a` is implied: `&'a T` (implementing type) is an input to the trait
152+
impl<'a, T> MakeRef<'a> for &'a T {
153+
type Type = &'a T;
154+
}
155+
```
156+
157+
For struct definitions, certain lifetime bounds are inferred due to requirements for the types of struct fields to be well-formed.
158+
These inferred lifetime bounds do not have to be explicitly written on the struct definition.
159+
160+
For example, a struct containing a field with a reference such as `&'a T` must satisfy `T: 'a` to be well-formed:
161+
162+
```rust
163+
struct RefToSlice1<'a, T>(&'a [T]); // inferred
164+
struct RefToSlice2<'a, T>(&'a [T]) where T: 'a; // explicit
165+
// References to references have additional requirements that can be inferred.
166+
struct DoubleRef1<'a, 'b, T>(&'a &'b T); // inferred
167+
struct DoubleRef2<'a, 'b, T>(&'a &'b T) where 'b: 'a, T: 'b; // explicit
168+
```
169+
170+
A reference to a struct such as `Bar<'a, T>` can also result in inferred lifetime bounds derived from the definition of `Bar`.
171+
The explicit `T: 'a` bound in the `where` clause on `Bar` causes the bound `U: 'b` to be inferred on `Foo1`:
172+
173+
```rust
174+
struct Foo1<'b, U>(Bar<'b, U>); // inferred
175+
struct Foo2<'b, U>(Bar<'b, U>) where U: 'b; // explicit
176+
177+
struct Bar<'a, T>(&'a (), T) where T: 'a;
178+
```
179+
180+
For associated type references such as `<T as MakeRef<'a>>::Type`, only `T` itself is checked to be well-formed, and no lifetime bounds are inferred based on the lifetime requirements of the associated type `MakeRef<'a>::Type`.
181+
An explicit bound is still required to ensure that any lifetime requirements of the associated type are met:
182+
183+
```rust
184+
trait MakeRef<'a> { type Type; }
185+
186+
impl<'a, T> MakeRef<'a> for Vec<T>
187+
where T: 'a
188+
{
189+
type Type = &'a T;
190+
}
191+
192+
struct UsesMakeRef<'a, T>
193+
where T: 'a // Not inferred: only `Vec<T>` is checked to be well-formed
194+
{
195+
foo: <Vec<T> as MakeRef<'a>>::Type
196+
}
197+
```
198+
199+
In contrast, `<T as Iterator>::Item: 'a` is inferred below, because `'a` is part of the type of the reference, not part of the associated type `<T as Iterator>::Item`:
200+
201+
```rust
202+
struct RefAssocType1<'a, T: Iterator>(&'a T::Item); // inferred
203+
struct RefAssocType2<'a, T: Iterator>(&'a T::Item)
204+
where <T as Iterator>::Item: 'a; // explicit
205+
```
206+
120207
## Higher-ranked trait bounds
121208

122209
> _ForLifetimes_ :\

0 commit comments

Comments
 (0)