Skip to content

Improve impl implements different signature from trait error message #42706

Closed
@gaurikholkar-zz

Description

@gaurikholkar-zz

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

Activity

gaurikholkar-zz

gaurikholkar-zz commented on Sep 29, 2017

@gaurikholkar-zz
Author

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

added a commit that references this issue on Oct 28, 2019

Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…

05ff8db
added a commit that references this issue on Oct 29, 2019

Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…

1437592
added a commit that references this issue on Oct 30, 2019

Rollup merge of rust-lang#65068 - estebank:trait-impl-lt-mismatch, r=…

ab9b789
added a commit that references this issue on Oct 30, 2019

Auto merge of #65068 - estebank:trait-impl-lt-mismatch, r=nikomatsakis

added
P-lowLow priority
T-compilerRelevant to the compiler team, which will review and decide on the PR/issue.
on Nov 14, 2019
steveklabnik

steveklabnik commented on Apr 29, 2021

@steveklabnik
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

Dylan-DPC commented on Jun 9, 2023

@Dylan-DPC
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

Dylan-DPC commented on Aug 30, 2024

@Dylan-DPC
Member

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    A-diagnosticsArea: Messages for errors, warnings, and lintsC-enhancementCategory: An issue proposing an enhancement or a PR with one.P-lowLow priorityT-compilerRelevant to the compiler team, which will review and decide on the PR/issue.WG-diagnosticsWorking group: Diagnostics

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

      Development

      No branches or pull requests

        Participants

        @steveklabnik@estebank@Mark-Simulacrum@gaurikholkar-zz@jyn514

        Issue actions

          Improve impl implements different signature from trait error message · Issue #42706 · rust-lang/rust