Skip to content

Tweak output of missing lifetime on associated type #135602

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion compiler/rustc_resolve/src/late.rs
Original file line number Diff line number Diff line change
Expand Up @@ -360,6 +360,7 @@ enum LifetimeBinderKind {
Function,
Closure,
ImplBlock,
ImplAssocType,
Copy link
Member

@Nadrieril Nadrieril Mar 11, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you add a comment to the Item variant explaining which items it covers? Is it only trait assoc types now? If so better rename it.

}

impl LifetimeBinderKind {
Expand All @@ -370,6 +371,7 @@ impl LifetimeBinderKind {
PolyTrait => "bound",
WhereBound => "bound",
Item | ConstItem => "item",
ImplAssocType => "associated type",
ImplBlock => "impl block",
Function => "function",
Closure => "closure",
Expand Down Expand Up @@ -3395,7 +3397,7 @@ impl<'a, 'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'a, 'ast, 'ra, 'tcx> {
LifetimeRibKind::Generics {
binder: item.id,
span: generics.span,
kind: LifetimeBinderKind::Item,
kind: LifetimeBinderKind::ImplAssocType,
},
|this| {
this.with_lifetime_rib(LifetimeRibKind::AnonymousReportError, |this| {
Expand Down
3 changes: 3 additions & 0 deletions compiler/rustc_resolve/src/late/diagnostics.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2976,6 +2976,9 @@ impl<'ast, 'ra: 'ast, 'tcx> LateResolutionVisitor<'_, 'ast, 'ra, 'tcx> {
{
continue;
}
if let LifetimeBinderKind::ImplAssocType = kind {
continue;
}

if !span.can_be_used_for_suggestions()
&& suggest_note
Expand Down
12 changes: 3 additions & 9 deletions tests/ui/lifetimes/missing-lifetime-in-assoc-type-2.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,11 @@ LL | type Item = &T;
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
|
help: consider introducing lifetime `'a` here
|
LL | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
| ++++
help: consider introducing lifetime `'a` here
|
LL | impl<'a> IntoIterator for &S {
| ++++

error: aborting due to 2 previous errors

Expand Down
7 changes: 5 additions & 2 deletions tests/ui/lifetimes/missing-lifetime-in-assoc-type-3.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,11 @@ LL | type IntoIter = std::collections::btree_map::Values<i32, T>;
|
help: consider introducing a named lifetime parameter
|
LL | type IntoIter<'a> = std::collections::btree_map::Values<'a, i32, T>;
| ++++ +++
LL ~ impl<'a> IntoIterator for &S {
LL | type Item = &T;
LL |
LL ~ type IntoIter = std::collections::btree_map::Values<'a, i32, T>;
|

error: aborting due to 2 previous errors

Expand Down
5 changes: 3 additions & 2 deletions tests/ui/mismatched_types/issue-74918-missing-lifetime.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,9 @@ LL | type Item = IteratorChunk<T, S>;
|
help: consider introducing a named lifetime parameter
|
LL | type Item<'a> = IteratorChunk<'a, T, S>;
| ++++ +++
LL ~ impl<'a, T, S: Iterator<Item = T>> Iterator for ChunkingIterator<T, S> {
LL ~ type Item = IteratorChunk<'a, T, S>;
|

error: aborting due to 1 previous error

Expand Down
11 changes: 2 additions & 9 deletions tests/ui/nll/user-annotations/region-error-ice-109072.stderr
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@ LL | impl Lt<'missing> for () {
error[E0261]: use of undeclared lifetime name `'missing`
--> $DIR/region-error-ice-109072.rs:9:15
|
LL | impl Lt<'missing> for () {
| - help: consider introducing lifetime `'missing` here: `<'missing>`
LL | type T = &'missing ();
| ^^^^^^^^ undeclared lifetime
|
help: consider introducing lifetime `'missing` here
|
LL | type T<'missing> = &'missing ();
| ++++++++++
help: consider introducing lifetime `'missing` here
|
LL | impl<'missing> Lt<'missing> for () {
| ++++++++++

error: aborting due to 2 previous errors

Expand Down