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

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

    Issue actions