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
Rollup merge of rust-lang#135602 - estebank:issue-135589, r=Nadrieril
Tweak output of missing lifetime on associated type
Each commit can be reviewed independently. Address parts of rust-lang#135589.
---
When an associated type is missing a lifetime, point at its enclosing `impl`, whether it has or doesn't have lifetimes defined. If it does have a lifetime, suggest using it.
```
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-1.rs:8: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
|
help: consider using the lifetime from the impl block
|
LL | type Item = &'a T;
| ++
```
```
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 | impl IntoIterator for &S {
| - you could add a lifetime on the impl block, if the trait or the self type can have one
LL | type Item = &T;
| ^ this lifetime must come from the implemented type
```
---
On unconstrained lifetime on impl block, suggest using it if there's an implicit borrow in the self type
```
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
--> $DIR/missing-lifetime-in-assoc-type-1.rs:4:6
|
LL | impl<'a> IntoIterator for &S {
| ^^ unconstrained lifetime parameter
|
help: consider using the named lifetime here instead of an implict lifetime
|
LL | impl<'a> IntoIterator for &'a S {
| ++
```
---
Do not suggest introducing lifetime in impl assoc type
---
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
```
Copy file name to clipboardExpand all lines: tests/ui/impl-header-lifetime-elision/assoc-type.stderr
+2Lines changed: 2 additions & 0 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,8 @@
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/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
4
6
LL | type Output = &i32;
5
7
| ^ this lifetime must 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
+
--> $DIR/missing-lifetime-in-assoc-type-1.rs:9:17
3
+
|
4
+
LL | impl<'a> IntoIterator for &S {
5
+
| ---- there is a named lifetime specified on the impl block you could use
6
+
...
7
+
LL | type Item = &T;
8
+
| ^ this lifetime must come from the implemented type
9
+
|
10
+
help: consider using the lifetime from the impl block
11
+
|
12
+
LL | type Item = &'a T;
13
+
| ++
14
+
15
+
error[E0207]: the lifetime parameter `'a` is not constrained by the impl trait, self type, or predicates
16
+
--> $DIR/missing-lifetime-in-assoc-type-1.rs:4:6
17
+
|
18
+
LL | impl<'a> IntoIterator for &S {
19
+
| ^^ unconstrained lifetime parameter
20
+
|
21
+
help: consider using the named lifetime here instead of an implict lifetime
22
+
|
23
+
LL | impl<'a> IntoIterator for &'a S {
24
+
| ++
25
+
26
+
error: aborting due to 2 previous errors
27
+
28
+
For more information about this error, try `rustc --explain E0207`.
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
+
--> $DIR/missing-lifetime-in-assoc-type-2.rs:5:17
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
+
LL | type Item = &T;
7
+
| ^ this lifetime must come from the implemented type
8
+
9
+
error[E0261]: use of undeclared lifetime name `'a`
10
+
--> $DIR/missing-lifetime-in-assoc-type-2.rs:7:57
11
+
|
12
+
LL | type IntoIter = std::collections::btree_map::Values<'a, i32, T>;
13
+
| ^^ undeclared lifetime
14
+
|
15
+
help: consider introducing lifetime `'a` here
16
+
|
17
+
LL | impl<'a> IntoIterator for &S {
18
+
| ++++
19
+
20
+
error: aborting due to 2 previous errors
21
+
22
+
For more information about this error, try `rustc --explain E0261`.
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
+
--> $DIR/missing-lifetime-in-assoc-type-3.rs:5:17
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
+
LL | type Item = &T;
7
+
| ^ this lifetime must come from the implemented type
8
+
9
+
error[E0106]: missing lifetime specifier
10
+
--> $DIR/missing-lifetime-in-assoc-type-3.rs:7:56
11
+
|
12
+
LL | type IntoIter = std::collections::btree_map::Values<i32, T>;
13
+
| ^ expected named lifetime parameter
14
+
|
15
+
help: consider introducing a named lifetime parameter
16
+
|
17
+
LL ~ impl<'a> IntoIterator for &S {
18
+
LL | type Item = &T;
19
+
LL |
20
+
LL ~ type IntoIter = std::collections::btree_map::Values<'a, i32, T>;
21
+
|
22
+
23
+
error: aborting due to 2 previous errors
24
+
25
+
For more information about this error, try `rustc --explain E0106`.
0 commit comments