Skip to content

Tweak output of missing lifetime on associated type#145314

Merged
bors merged 6 commits intorust-lang:masterfrom
estebank:issue-135589-all
Nov 5, 2025
Merged

Tweak output of missing lifetime on associated type#145314
bors merged 6 commits intorust-lang:masterfrom
estebank:issue-135589-all

Conversation

@estebank
Copy link
Contributor

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

Loading
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

merged-by-bors This PR was explicitly merged by bors. S-waiting-on-bors Status: Waiting on bors to run and complete tests. Bors will change the label on completion. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants