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

Weird HRTB behavior with associated types #99548

Open
ArtBlnd opened this issue Jul 21, 2022 · 1 comment
Open

Weird HRTB behavior with associated types #99548

ArtBlnd opened this issue Jul 21, 2022 · 1 comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions T-types Relevant to the types team, which will review and decide on the PR/issue.

Comments

@ArtBlnd
Copy link

ArtBlnd commented Jul 21, 2022

I've trying compile this code.

// This does not compiles
fn create_baa_in_the_foo1() {
    create_baa(Foo(()));
}

// This compiles
fn create_baa_in_the_foo2() {
    Baa(Foo(()));
}

struct Baa<A>(A);
fn create_baa<A, B>(foo: A) -> Baa<A>
where
    A: for<'a> FooTrait<'a, Output = B>,
{
    Baa(foo)
}

trait FooTrait<'a> {
    type Output;
    fn output(&'a self) -> Self::Output;
}

struct Foo(());
impl<'a> FooTrait<'a> for Foo {
    type Output = &'a ();

    fn output(&'a self) -> Self::Output {
        &self.0
    }
}

Everyone knows the lifetime of the Foo(()) is general enough but it emits error.

error: implementation of `FooTrait` is not general enough
  |
2 |     create_baa(Foo(()));
  |     ^^^^^^^^^ implementation of `FooTrait` is not general enough
  |
  = note: `Foo` must implement `FooTrait<'0>`, for any lifetime `'0`...
  = note: ...but it actually implements `FooTrait<'1>`, for some specific lifetime `'1`

If we remove Output = B and its generic type B it works fine.

struct Baa<A>(A);
fn create_baa<A>(foo: A) -> Baa<A>
where
    A: for<'a> FooTrait<'a>,
{
    Baa(foo)
}

or in other way, moving references associate type to method signature. it also works.

trait FooTrait<'a> {
    type Output;
    fn output(&'a self) -> &'a Self::Output;
}

struct Foo(());
impl<'a> FooTrait<'a> for Foo {
    type Output = ();

    fn output(&'a self) -> &'a Self::Output {
        &self.0
    }
}

I guess at least this code should have same lifetime behavior as first one.
Am I doing wrong with something or this is something the bug related to #70263?

@ArtBlnd
Copy link
Author

ArtBlnd commented Jul 22, 2022

I think its because there is no way to proof (or very hard way using dyn and HRTB) generic type B has HRTB lifetime 'a current rust implementation.

@jyn514 jyn514 added A-lifetimes Area: Lifetimes / regions A-associated-items Area: Associated items (types, constants & functions) T-types Relevant to the types team, which will review and decide on the PR/issue. labels Apr 26, 2023
@fmease fmease changed the title Weird HRTB behavior with associate types. Weird HRTB behavior with associated types Sep 24, 2024
@fmease fmease added the A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) label Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-associated-items Area: Associated items (types, constants & functions) A-higher-ranked Area: Higher-ranked things (e.g., lifetimes, types, trait bounds aka HRTBs) A-lifetimes Area: Lifetimes / regions T-types Relevant to the types team, which will review and decide on the PR/issue.
Projects
None yet
Development

No branches or pull requests

3 participants