Skip to content

method has an incompatible type for trait due to lifetimes has too wide a span #33624

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
nagisa opened this issue May 13, 2016 · 7 comments · May be fixed by #140523
Open

method has an incompatible type for trait due to lifetimes has too wide a span #33624

nagisa opened this issue May 13, 2016 · 7 comments · May be fixed by #140523
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@nagisa
Copy link
Member

nagisa commented May 13, 2016

For

trait A<'a> {
    fn a<'b>(self) -> Box<A<'b>>;
}

struct C;

impl<'a> A<'a> for C {
    fn a<'b>(self) -> Box<A<'a>>{}
}

it would be much more useful if we spanned the offending lifetime rather than the whole method: i.e. like this

error: method `a` has an incompatible type for trait:
 expected bound lifetime parameter 'b,
    found concrete lifetime [--explain E0053]
 --> <anon>:8:5
8 |>     fn a<'b>(self) -> Box<A<'a>>{}
  |>                             ^^

instead of

error: method `a` has an incompatible type for trait:
 expected bound lifetime parameter 'b,
    found concrete lifetime [--explain E0053]
 --> <anon>:8:5
8 |>     fn a<'b>(self) -> Box<A<'a>>{}
  |>     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
@nagisa nagisa added the A-diagnostics Area: Messages for errors, warnings, and lints label May 13, 2016
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 25, 2017
@estebank
Copy link
Contributor

Current output:

error[E0195]: lifetime parameters or bounds on method `a` do not match the trait declaration
 --> src/main.rs:8:5
  |
2 |     fn a<'b>(self) -> Box<A<'b>>;
  |     ----------------------------- lifetimes in impl do not match this method in trait
...
8 |     fn a<'b>(self) -> Box<A<'a>> {}
  |     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^ lifetimes do not match method in trait

@estebank
Copy link
Contributor

Current output:

error[E0195]: lifetime parameters or bounds on method `a` do not match the trait declaration
 --> src/main.rs:8:9
  |
2 |     fn a<'b>(self) -> Box<A<'b>>;
  |         ---- lifetimes in impl do not match this method in trait
...
8 |     fn a<'b>(self) -> Box<A<'a>> {}
  |         ^^^^ lifetimes do not match method in trait

It fails to point at the 'a in the second case :-/

@estebank estebank added the T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. label Jan 25, 2020
@estebank
Copy link
Contributor

estebank commented Jun 8, 2022

Triage: no change.

@fmease fmease added D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. A-trait-system Area: Trait system A-lifetimes Area: Lifetimes / regions and removed C-enhancement Category: An issue proposing an enhancement or a PR with one. labels Sep 3, 2024
@fmease fmease added A-trait-system Area: Trait system and removed A-trait-system Area: Trait system labels Dec 21, 2024
@RodBurman
Copy link

The output from:

% cargo -V -v
cargo 1.84.0 (66221abde 2024-11-19)
release: 1.84.0
commit-hash: 66221abdeca2002d318fde6efff516aab091df0e
commit-date: 2024-11-19
host: aarch64-apple-darwin
libgit2: 1.8.1 (sys:0.19.0 vendored)
libcurl: 8.7.1 (sys:0.4.74+curl-8.9.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 15.2.0 [64-bit]

is:

% cargo build
   Compiling toowidespan v0.1.0 (/Users/rod/code/rust/triage/toowidespan)
error[E0195]: lifetime parameters or bounds on method `a` do not match the trait declaration
 --> src/lib.rs:8:9
  |
2 |     fn a<'b>(self) -> Box<A<'b>>;
  |         ---- lifetimes in impl do not match this method in trait
...
8 |     fn a<'b>(self) -> Box<A<'a>>{}
  |         ^^^^ lifetimes do not match method in trait

error[E0782]: expected a type, found a trait
 --> src/lib.rs:2:27
  |
2 |     fn a<'b>(self) -> Box<A<'b>>;
  |                           ^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
2 |     fn a<'b>(self) -> Box<dyn A<'b>>;
  |                           +++

error[E0782]: expected a type, found a trait
 --> src/lib.rs:8:27
  |
8 |     fn a<'b>(self) -> Box<A<'a>>{}
  |                           ^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
8 |     fn a<'b>(self) -> Box<dyn A<'a>>{}
  |                           +++

Some errors have detailed explanations: E0195, E0782.
For more information about an error, try `rustc --explain E0195`.
error: could not compile `toowidespan` (lib) due to 3 previous errors

The spans indicated are now much shorter but none seem to indicate the 'a that was originally suggested, thus I am not sure if we can agree to close the issue.

@estebank
Copy link
Contributor

Yeah, the ticket should not be closed as now the shorter span is actively not pointing at where it should be pointing. I'm happy with the output you show above in general compared to what it used to be, but it can be better.

@flusterIO
Copy link

Has anyone resolved this? I'm very new to rust, so maybe I'm missing an obvious solution, but what's the work around for this? I tried expanding the tonic::async_trait macro, but it doesn't resolve the issue.

Before I started learning rust I heard everyone talking about how much the borrow checker is a pain, but so far the only struggle has been these ****ing lifetimes. Any help is greatly appreciated, even if it is only a bandaid for now.

@RodBurman
Copy link

RodBurman commented Apr 23, 2025

Things move on, so with:

% cargo -v -V
cargo 1.86.0 (adf9b6ad1 2025-02-28)
release: 1.86.0
commit-hash: adf9b6ad14cfa10ff680d5806741a144f7163698
commit-date: 2025-02-28
host: aarch64-apple-darwin
libgit2: 1.9.0 (sys:0.20.0 vendored)
libcurl: 8.7.1 (sys:0.4.79+curl-8.12.0 system ssl:(SecureTransport) LibreSSL/3.3.6)
ssl: OpenSSL 1.1.1w  11 Sep 2023
os: Mac OS 15.4.1 [64-bit]

I get:

% cargo build  
   Compiling toowidespan v0.1.0 (/Users/rod/code/rust/triage/2025/01/toowidespan)
error[E0782]: expected a type, found a trait
 --> src/lib.rs:2:27
  |
2 |     fn a<'b>(self) -> Box<A<'b>>;
  |                           ^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
2 |     fn a<'b>(self) -> Box<dyn A<'b>>;
  |                           +++

error[E0782]: expected a type, found a trait
 --> src/lib.rs:8:27
  |
8 |     fn a<'b>(self) -> Box<A<'a>>{}
  |                           ^^^^^
  |
help: you can add the `dyn` keyword if you want a trait object
  |
8 |     fn a<'b>(self) -> Box<dyn A<'a>>{}
  |                           +++

error[E0195]: lifetime parameters or bounds on method `a` do not match the trait declaration
 --> src/lib.rs:8:9
  |
2 |     fn a<'b>(self) -> Box<A<'b>>;
  |         ---- lifetimes in impl do not match this method in trait
...
8 |     fn a<'b>(self) -> Box<A<'a>>{}
  |         ^^^^ lifetimes do not match method in trait

Some errors have detailed explanations: E0195, E0782.
For more information about an error, try `rustc --explain E0195`.
error: could not compile `toowidespan` (lib) due to 3 previous errors

Which is similar to the messages from 1.84 but in a different order with the "2,8" block last rather than first.
To answer the question posed by @flusterIO (though I don't think this is the right forum for such a "code fix" question, this forum being for compiler/documentation fixes); following through iteratively on the compiler's suggested fixes until all errors are removed yields (for me) the following code:

#![allow(warnings)] // suppress warnings
trait A<'a> {
    fn a<'b>(self) -> Box<dyn A<'b>>;
}

struct C;

impl<'a> A<'a> for C {
    fn a<'b>(self) -> Box<dyn A<'b>>{ Box::new(self) }
}

Now since this sample is designed to reproduce a compiler problem with the least code, this "fixed" code may well be doing something entirely at odds with the intent of the original code that highlighted the compiler problem, so the error messages may still not be optimal.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-diagnostics Area: Messages for errors, warnings, and lints A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system D-confusing Diagnostics: Confusing error or lint that should be reworked. D-terse Diagnostics: An error or lint that doesn't give enough information about the problem at hand. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

6 participants