Skip to content
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

Improve impl implements different signature from trait error message #42706

Closed
gaurikholkar-zz opened this issue Jun 16, 2017 · 4 comments
Closed
Labels
A-diagnostics Area: Messages for errors, warnings, and lints C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics

Comments

@gaurikholkar-zz
Copy link

gaurikholkar-zz commented Jun 16, 2017

We need to improve the error message for the second case.

trait Foo {
    fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32;
}

// First is when impl implements same signature as trait:
impl Foo for () {
    fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 {
        if x > y { x } else { y }
    }
}
// Second is when impl implements different signature from trait.
// Here we *could* suggest adding lifetime to `x`.
trait Bar {
    fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32;
}

impl Bar for () {
    fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 {
        // this body `y` would be OK
        if x > y { x } else { y }
    }
}

cc @nikomatsakis

@Mark-Simulacrum Mark-Simulacrum added the A-diagnostics Area: Messages for errors, warnings, and lints label Jun 23, 2017
@Mark-Simulacrum Mark-Simulacrum added the C-enhancement Category: An issue proposing an enhancement or a PR with one. label Jul 26, 2017
@gaurikholkar-zz
Copy link
Author

gaurikholkar-zz commented Sep 29, 2017

@nikomatsakis, maybe this needs mentoring?
Also for E0621, we don't handle Impl Items, should I open up an issue for that?

@estebank estebank added E-needs-mentor WG-diagnostics Working group: Diagnostics labels Nov 30, 2017
JohnTitor added a commit to JohnTitor/rust that referenced this issue Oct 28, 2019
…nikomatsakis

Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses rust-lang#42706, rust-lang#41343, fix rust-lang#40900.
Centril added a commit to Centril/rust that referenced this issue Oct 29, 2019
…nikomatsakis

Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses rust-lang#42706, rust-lang#41343, fix rust-lang#40900.
tmandry added a commit to tmandry/rust that referenced this issue Oct 30, 2019
…nikomatsakis

Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses rust-lang#42706, rust-lang#41343, fix rust-lang#40900.
bors added a commit that referenced this issue Oct 30, 2019
Custom lifetime error for `impl` item doesn't conform to `trait`

Partly addresses #42706, #41343, fix #40900.
@estebank estebank added P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Nov 14, 2019
@steveklabnik
Copy link
Member

Triage: here's the error today:

error[E0623]: lifetime mismatch
  --> src/lib.rs:10:20
   |
8  |     fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 {
   |                   ----                 -------
   |                   |
   |                   this parameter and the return type are declared with different lifetimes...
9  |         // this body `y` would be OK
10 |         if x > y { x } else { y }
   |                    ^ ...but data from `x` is returned here

@Dylan-DPC
Copy link
Member

Current error:

error: lifetime may not live long enough
 --> src/lib.rs:8:20
  |
7 |     fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 {
  |            --     - let's call the lifetime of this reference `'1`
  |            |
  |            lifetime `'a` defined here
8 |         if x > y { x } else { y }
  |                    ^ associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`

@Dylan-DPC
Copy link
Member

Dylan-DPC commented Aug 30, 2024

Current output:

error: lifetime may not live long enough
 --> src/lib.rs:8:20
  |
6 |     fn foo<'a>(x: &i32, y: &'a i32) -> &'a i32 {
  |            --     - let's call the lifetime of this reference `'1`
  |            |
  |            lifetime `'a` defined here
7 |         // this body `y` would be OK
8 |         if x > y { x } else { y }
  |                    ^ associated function was supposed to return data with lifetime `'a` but it is returning data with lifetime `'1`
  |
help: consider reusing a named lifetime parameter and update trait if needed
  |
6 |     fn foo<'a>(x: &'a i32, y: &'a i32) -> &'a i32 {
  |                    ++

This already indicates that the lifetime should be added, so should be good enough to close this issue.

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 C-enhancement Category: An issue proposing an enhancement or a PR with one. P-low Low priority T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. WG-diagnostics Working group: Diagnostics
Projects
None yet
Development

No branches or pull requests

6 participants