Tweak output of missing lifetime on associated type - #135602
Conversation
|
r? @Nadrieril rustbot has assigned @Nadrieril. Use |
This comment has been minimized.
This comment has been minimized.
bcacdc8 to
7b38d31
Compare
estebank
left a comment
There was a problem hiding this comment.
Attached screenshots as I find that 1) the colors help get a better sense of what the output looks in practice and 2) the lack of spans into the std types makes it harder to get what I was going for.
| 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 { | ||
| | ++ |
| error: missing lifetime in associated type | ||
| --> $DIR/missing-lifetime-in-assoc-type-4.rs:5:17 | ||
| | | ||
| 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: 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; | ||
| | |
| 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 |
|
Feel free to review on a per-commit basis and stop early. I can split-off later commits onto their own PRs. They are all together because they end up affecting the same output and build on each other, but they don't need to land together. |
|
☔ The latest upstream changes (presumably #138151) made this pull request unmergeable. Please resolve the merge conflicts. |
| let Some((rib, span)) = self.lifetime_ribs[..i] | ||
| .iter() | ||
| .rev() | ||
| .skip(1) |
There was a problem hiding this comment.
Why the skip? Could you add a comment explaining it?
There was a problem hiding this comment.
can't recall and removing it made no change :)
|
I appear to have massively procrastinated this. I've reviewed the first 4 commits and I have questions that may be addressed with comments. |
|
If you don't mind splitting off the other commits to a different PR. @rustbot author |
|
☔ The latest upstream changes (presumably #139657) made this pull request unmergeable. Please resolve the merge conflicts. |
This comment has been minimized.
This comment has been minimized.
|
@Nadrieril rebased and left only the first 4 commits. Created #145314 with the rest. |
|
Hi @Nadrieril, it looks like this is ready to review and the author has responded to your previous round of feedback when you have a chance. Thanks! |
|
@bors r+ apologies for the delay! |
|
This PR was rebased onto a different master commit. Here's a range-diff highlighting what actually changed. Rebasing is a normal part of keeping PRs up to date, so no action is needed—this note is just to help reviewers. |
|
|
@bors r=Nadrieril |
| .filter_map(|rib| match rib.kind { | ||
| LifetimeRibKind::Generics { span, kind: LifetimeBinderKind::ImplBlock, .. } => { | ||
| Some((rib, span)) | ||
| } |



Each commit can be reviewed independently. Address parts of #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.On unconstrained lifetime on impl block, suggest using it if there's an implicit borrow in the self type
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_spanfor the item, so we at least provide some context, even if its span is too wide.