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
Auto merge of #145314 - estebank:issue-135589-all, r=Nadrieril
Tweak output of missing lifetime on associated type
Follow up to #135602.
Previously we only showed the trait's assoc item if the trait was local, because we were looking for a small span only for the generics, which we don't have for foreign traits. We now use `def_span` for the item, so we at least provide some context, even if its span is too wide.
```
error[E0195]: lifetime parameters or bounds on type `IntoIter` do not match the trait declaration
--> tests/ui/lifetimes/missing-lifetime-in-assoc-type-4.rs:7:18
|
7 | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
| ^^^^ lifetimes do not match type in trait
|
::: /home/gh-estebank/rust/library/core/src/iter/traits/collect.rs:292:5
|
292 | type IntoIter: Iterator<Item = Self::Item>;
| ------------------------------------------ lifetimes in impl do not match this type in trait
```
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;
|
```
Move the previous long message to a note and use a shorter primary message:
```
error: missing lifetime in associated type
--> $DIR/missing-lifetime-in-assoc-type-1.rs:9:17
|
LL | impl<'a> IntoIterator for &S {
| ---- there is a named lifetime specified on the impl block you could use
...
LL | type Item = &T;
| ^ this lifetime must come from the implemented type
|
note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
--> $SRC_DIR/core/src/iter/traits/collect.rs:LL:COL
help: consider using the lifetime from the impl block
|
LL | type Item = &'a T;
| ++
```
r? `@Nadrieril`
Copy file name to clipboardExpand all lines: compiler/rustc_resolve/messages.ftl
+2-2Lines changed: 2 additions & 2 deletions
Original file line number
Diff line number
Diff line change
@@ -11,9 +11,9 @@ resolve_added_macro_use =
11
11
resolve_ancestor_only =
12
12
visibilities can only be restricted to ancestor modules
13
13
14
-
resolve_anonymous_lifetime_non_gat_report_error =
15
-
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
14
+
resolve_anonymous_lifetime_non_gat_report_error = missing lifetime in associated type
16
15
.label = this lifetime must come from the implemented type
16
+
.note = in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
17
17
18
18
resolve_arguments_macro_use_not_allowed = arguments to `macro_use` are not allowed here
Copy file name to clipboardExpand all lines: tests/ui/impl-header-lifetime-elision/assoc-type.rs
+14-1Lines changed: 14 additions & 1 deletion
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 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
1
+
error: missing lifetime in associated 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
+
= note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
8
+
help: add a lifetime to the impl block and use it in the self type and associated type
9
+
|
10
+
LL ~ impl<'a> MyTrait for &'a i32 {
11
+
LL ~ type Output = &'a i32;
12
+
|
8
13
9
14
error[E0637]: `'_` cannot be used here
10
15
--> $DIR/assoc-type.rs:16:20
11
16
|
12
17
LL | type Output = &'_ i32;
13
18
| ^^ `'_` is a reserved lifetime name
14
19
15
-
error: aborting due to 2 previous errors
20
+
error: missing lifetime in associated type
21
+
--> $DIR/assoc-type.rs:21:19
22
+
|
23
+
LL | impl<'a> MyTrait for &f64 {
24
+
| ---- there is a named lifetime specified on the impl block you could use
25
+
LL | type Output = &f64;
26
+
| ^ this lifetime must come from the implemented type
27
+
|
28
+
= note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
29
+
help: consider using the lifetime from the impl block
30
+
|
31
+
LL | type Output = &'a f64;
32
+
| ++
33
+
34
+
error: missing lifetime in associated type
35
+
--> $DIR/assoc-type.rs:29:19
36
+
|
37
+
LL | type Output = &f64;
38
+
| ^ this lifetime must come from the implemented type
39
+
|
40
+
= note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
41
+
help: add a lifetime to the impl block and use it in the trait and associated type
42
+
|
43
+
LL ~ impl<'a> OtherTrait<'a> for f64 {
44
+
LL ~ type Output = &'a f64;
45
+
|
46
+
47
+
error: aborting due to 4 previous errors
16
48
17
49
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-1.stderr
+3-1Lines changed: 3 additions & 1 deletion
Original file line number
Diff line number
Diff line change
@@ -1,4 +1,4 @@
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
| ^ this lifetime must come from the implemented type
9
9
|
10
+
note: in the trait the associated type is declared without lifetime parameters, so using a borrowed type for them requires that lifetime to come from the implemented type
0 commit comments