You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Look at the current impl before suggesting adding a lifetime
Given an associated item that needs a named lifetime, look at the enclosing `impl` item for one. If there is none, look at the self type and the implemented trait to see if either of those has an anonimous lifetime. If so, suggest adding a named lifetime.
```
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
--> $DIR/missing-lifetime-in-assoc-type-2.rs:5:17
|
LL | type Item = &T;
| ^ this lifetime must come from the implemented type
|
help: add a lifetime to the impl block and use it in the self type and associated type
|
LL ~ impl<'a> IntoIterator for &'a S {
LL ~ type Item = &'a T;
|
```
Copy file name to clipboardExpand all lines: tests/ui/impl-header-lifetime-elision/assoc-type.rs
+14-1
Original file line number
Diff line number
Diff line change
@@ -9,14 +9,27 @@ trait MyTrait {
9
9
10
10
implMyTraitfor&i32{
11
11
typeOutput = &i32;
12
-
//~^ ERROR 11:19: 11:20: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
12
+
//~^ ERROR in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
13
13
}
14
14
15
15
implMyTraitfor&u32{
16
16
typeOutput = &'_i32;
17
17
//~^ ERROR `'_` cannot be used here
18
18
}
19
19
20
+
impl<'a>MyTraitfor&f64{
21
+
typeOutput = &f64;
22
+
//~^ ERROR in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
23
+
}
24
+
25
+
traitOtherTrait<'a>{
26
+
typeOutput;
27
+
}
28
+
implOtherTrait<'_>forf64{
29
+
typeOutput = &f64;
30
+
//~^ ERROR in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
2
2
--> $DIR/assoc-type.rs:11:19
3
3
|
4
-
LL | impl MyTrait for &i32 {
5
-
| - you could add a lifetime on the impl block, if the trait or the self type can have one
6
4
LL | type Output = &i32;
7
5
| ^ this lifetime must come from the implemented type
6
+
|
7
+
help: add a lifetime to the impl block and use it in the self type and associated type
8
+
|
9
+
LL ~ impl<'a> MyTrait for &'a i32 {
10
+
LL ~ type Output = &'a i32;
11
+
|
8
12
9
13
error[E0637]: `'_` cannot be used here
10
14
--> $DIR/assoc-type.rs:16:20
11
15
|
12
16
LL | type Output = &'_ i32;
13
17
| ^^ `'_` is a reserved lifetime name
14
18
15
-
error: aborting due to 2 previous errors
19
+
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
20
+
--> $DIR/assoc-type.rs:21:19
21
+
|
22
+
LL | impl<'a> MyTrait for &f64 {
23
+
| ---- there is a named lifetime specified on the impl block you could use
24
+
LL | type Output = &f64;
25
+
| ^ this lifetime must come from the implemented type
26
+
|
27
+
help: consider using the lifetime from the impl block
28
+
|
29
+
LL | type Output = &'a f64;
30
+
| ++
31
+
32
+
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
33
+
--> $DIR/assoc-type.rs:29:19
34
+
|
35
+
LL | type Output = &f64;
36
+
| ^ this lifetime must come from the implemented type
37
+
|
38
+
help: add a lifetime to the impl block and use it in the trait and associated type
39
+
|
40
+
LL ~ impl<'a> OtherTrait<'a> for f64 {
41
+
LL ~ type Output = &'a f64;
42
+
|
43
+
44
+
error: aborting due to 4 previous errors
16
45
17
46
For more information about this error, try `rustc --explain E0637`.
Copy file name to clipboardExpand all lines: tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr
+6-2
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,14 @@
1
1
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
2
2
--> $DIR/missing-lifetime-in-assoc-type-2.rs:5:17
3
3
|
4
-
LL | impl IntoIterator for &S {
5
-
| - you could add a lifetime on the impl block, if the trait or the self type can have one
6
4
LL | type Item = &T;
7
5
| ^ this lifetime must come from the implemented type
6
+
|
7
+
help: add a lifetime to the impl block and use it in the self type and associated type
8
+
|
9
+
LL ~ impl<'a> IntoIterator for &'a S {
10
+
LL ~ type Item = &'a T;
11
+
|
8
12
9
13
error[E0261]: use of undeclared lifetime name `'a`
Copy file name to clipboardExpand all lines: tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr
+6-2
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,14 @@
1
1
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
2
2
--> $DIR/missing-lifetime-in-assoc-type-3.rs:5:17
3
3
|
4
-
LL | impl IntoIterator for &S {
5
-
| - you could add a lifetime on the impl block, if the trait or the self type can have one
6
4
LL | type Item = &T;
7
5
| ^ this lifetime must come from the implemented type
6
+
|
7
+
help: add a lifetime to the impl block and use it in the self type and associated type
Copy file name to clipboardExpand all lines: tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.stderr
+6-2
Original file line number
Diff line number
Diff line change
@@ -1,10 +1,14 @@
1
1
error: in the trait associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
2
2
--> $DIR/missing-lifetime-in-assoc-type-4.rs:5:17
3
3
|
4
-
LL | impl IntoIterator for &S {
5
-
| - you could add a lifetime on the impl block, if the trait or the self type can have one
6
4
LL | type Item = &T;
7
5
| ^ this lifetime must come from the implemented type
6
+
|
7
+
help: add a lifetime to the impl block and use it in the self type and associated type
8
+
|
9
+
LL ~ impl<'a> IntoIterator for &'a S {
10
+
LL ~ type Item = &'a T;
11
+
|
8
12
9
13
error[E0195]: lifetime parameters or bounds on type `IntoIter` do not match the trait declaration
0 commit comments