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

Rustc fails to normalize trait functions that have late-bound lifetimes #109476

Open
zakarumych opened this issue Mar 22, 2023 · 1 comment
Open
Labels
A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.

Comments

@zakarumych
Copy link
Contributor

I tried this code:

trait Foo {
    type Item<'a>: 'a;
    
    fn foo<'a>() -> Self::Item<'a>;
}

struct Bar;

impl Foo for Bar {
    type Item<'a> = ();

    fn foo<'a>() -> () { () }

    // this works
    // fn foo<'a>() -> Self::Item<'a> { () }

    // this works too
    // fn foo<'a>() -> () where (): 'a { () }
}

Playground link

I expected to see this happen: Successful compilation

Instead, this happened: error[[E0195]](https://doc.rust-lang.org/stable/error_codes/E0195.html): lifetime parameters or bounds on method foo do not match the trait declaration

A workaround to this issue is using associated generic type in impl, add trivial where clause (): 'a or
add dummy argument that uses the lifetime

Meta

rustc --version --verbose:

rustc 1.68.0 (2c8cc3432 2023-03-06)
rustc 1.70.0-nightly (22f247c6f 2023-03-13)
@zakarumych zakarumych added the C-bug Category: This is a bug. label Mar 22, 2023
@lukas-code
Copy link
Member

lukas-code commented Mar 22, 2023

Looks like unused lifetimes are late-bound, even though they probably shouldn't be: (playground)

fn late<'a>() {}
fn early<'a: 'a>() {}

fn check<'a>() {
    early::<'a>(); // OK
    late::<'a>(); // ERROR: cannot specify lifetime arguments explicitly if late bound lifetime parameters are present
}

Edit: Nevermind early vs late is correct for this example.


Better minimized example: link

trait Foo {
    fn foo<'a: 'a>(); // early
}

impl Foo for () {
    fn foo<'a>() {} // late, but should be early
}

@jyn514 jyn514 changed the title Rustc doesn't accept valid code Rustc fails to normalize trait functions that have late-bound lifetimes Mar 25, 2023
@jyn514 jyn514 added A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system T-compiler Relevant to the compiler team, which will review and decide on the PR/issue. labels Mar 25, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-lifetimes Area: Lifetimes / regions A-trait-system Area: Trait system C-bug Category: This is a bug. T-compiler Relevant to the compiler team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants