Skip to content

Commit 2c7160b

Browse files
committed
Do not suggest introducing lifetime in impl assoc type
``` error[E0261]: use of undeclared lifetime name `'a` --> $DIR/missing-lifetime-in-assoc-type-2.rs:7:57 | LL | impl IntoIterator for &S { | - help: consider introducing lifetime `'a` here: `<'a>` ... LL | type IntoIter = std::collections::btree_map::Values<'a, i32, T>; | ^^ undeclared lifetime ``` ``` error[E0106]: missing lifetime specifier --> $DIR/issue-74918-missing-lifetime.rs:9:30 | LL | type Item = IteratorChunk<T, S>; | ^ expected named lifetime parameter | help: consider introducing a named lifetime parameter | LL ~ impl<'a, T, S: Iterator<Item = T>> Iterator for ChunkingIterator<T, S> { LL ~ type Item = IteratorChunk<'a, T, S>; | ```
1 parent a6cb0b5 commit 2c7160b

File tree

6 files changed

+19
-23
lines changed

6 files changed

+19
-23
lines changed

compiler/rustc_resolve/src/late.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -360,6 +360,7 @@ enum LifetimeBinderKind {
360360
Function,
361361
Closure,
362362
ImplBlock,
363+
ImplAssocType,
363364
}
364365

365366
impl LifetimeBinderKind {
@@ -370,6 +371,7 @@ impl LifetimeBinderKind {
370371
PolyTrait => "bound",
371372
WhereBound => "bound",
372373
Item | ConstItem => "item",
374+
ImplAssocType => "associated type",
373375
ImplBlock => "impl block",
374376
Function => "function",
375377
Closure => "closure",
@@ -3395,7 +3397,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
33953397
LifetimeRibKind::Generics {
33963398
binder: item.id,
33973399
span: generics.span,
3398-
kind: LifetimeBinderKind::Item,
3400+
kind: LifetimeBinderKind::ImplAssocType,
33993401
},
34003402
|this| {
34013403
this.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {

compiler/rustc_resolve/src/late/diagnostics.rs

+3
Original file line numberDiff line numberDiff line change
@@ -2976,6 +2976,9 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
29762976
{
29772977
continue;
29782978
}
2979+
if let LifetimeBinderKind::ImplAssocType = kind {
2980+
continue;
2981+
}
29792982

29802983
if !span.can_be_used_for_suggestions()
29812984
&& suggest_note

tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr

+3-9
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,11 @@ LL | type Item = &T;
99
error[E0261]: use of undeclared lifetime name `'a`
1010
--> $DIR/missing-lifetime-in-assoc-type-2.rs:7:57
1111
|
12+
LL | impl IntoIterator for &S {
13+
| - help: consider introducing lifetime `'a` here: `<'a>`
14+
...
1215
LL | type IntoIter = std::collections::btree_map::Values<'a, i32, T>;
1316
| ^^ undeclared lifetime
14-
|
15-
help: consider introducing lifetime `'a` here
16-
|
17-
LL | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
18-
| ++++
19-
help: consider introducing lifetime `'a` here
20-
|
21-
LL | impl<'a> IntoIterator for &S {
22-
| ++++
2317

2418
error: aborting due to 2 previous errors
2519

tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr

+5-2
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,11 @@ LL | type IntoIter = std::collections::btree_map::Values<i32, T>;
1414
|
1515
help: consider introducing a named lifetime parameter
1616
|
17-
LL | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
18-
| ++++ +++
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+
|
1922

2023
error: aborting due to 2 previous errors
2124

tests/ui/mismatched_types/issue-74918-missing-lifetime.stderr

+3-2
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,9 @@ LL | type Item = IteratorChunk<T, S>;
66
|
77
help: consider introducing a named lifetime parameter
88
|
9-
LL | type Item<'a> = IteratorChunk<'a, T, S>;
10-
| ++++ +++
9+
LL ~ impl<'a, T, S: Iterator<Item = T>> Iterator for ChunkingIterator<T, S> {
10+
LL ~ type Item = IteratorChunk<'a, T, S>;
11+
|
1112

1213
error: aborting due to 1 previous error
1314

tests/ui/nll/user-annotations/region-error-ice-109072.stderr

+2-9
Original file line numberDiff line numberDiff line change
@@ -9,17 +9,10 @@ LL | impl Lt<'missing> for () {
99
error[E0261]: use of undeclared lifetime name `'missing`
1010
--> $DIR/region-error-ice-109072.rs:9:15
1111
|
12+
LL | impl Lt<'missing> for () {
13+
| - help: consider introducing lifetime `'missing` here: `<'missing>`
1214
LL | type T = &'missing ();
1315
| ^^^^^^^^ undeclared lifetime
14-
|
15-
help: consider introducing lifetime `'missing` here
16-
|
17-
LL | type T<'missing> = &'missing ();
18-
| ++++++++++
19-
help: consider introducing lifetime `'missing` here
20-
|
21-
LL | impl<'missing> Lt<'missing> for () {
22-
| ++++++++++
2316

2417
error: aborting due to 2 previous errors
2518

0 commit comments

Comments
 (0)