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

Cannot handle generic associated type with where statement #6274

Closed
ArcticLampyrid opened this issue Aug 10, 2024 · 6 comments · Fixed by #5887
Closed

Cannot handle generic associated type with where statement #6274

ArcticLampyrid opened this issue Aug 10, 2024 · 6 comments · Fixed by #5887

Comments

@ArcticLampyrid
Copy link

ArcticLampyrid commented Aug 10, 2024

For code:

pub trait Foo {
    type Bar
    where
        Self: Sized;
}

impl Foo for u8 {
    // note: there is a space between `u16` and LineFeed
    type Bar = u16 
    where Self: Sized;
}

I got the error:

$ cargo fmt
error[internal]: left behind trailing whitespace
 --> \\?\D:\foo\src\a.rs:9:9:19
  |
9 |     type Bar = u16 
  |                   ^
  |

warning: rustfmt has failed to format. See previous 1 errors.
@johnhuichen
Copy link
Contributor

just wondering, is

type Bar = u16 
    where Self: Sized;

really the right syntax?

@ArcticLampyrid
Copy link
Author

@johnhuichen
I did not check any RFC documents, but I believe it is one type of GATs.
It works on rustc 1.70, with no build error or runtime error.

@johnhuichen
Copy link
Contributor

I am just thinking that where Self:Sized tells trait not to implement Type Bar if it's not sized.

Since you already declared the type, does it still make sense to have where Self:Size in the implementation?

@ytmimi
Copy link
Contributor

ytmimi commented Aug 12, 2024

@ArcticLampyrid this one should be addressed once we merge #5887, which we're planning to do in the near future.

@johnhuichen yes it's the correct syntax and it relates to rust-lang/rust#89122. Also, see the type alias docs of the rust reference.

@johnhuichen
Copy link
Contributor

johnhuichen commented Aug 13, 2024

@ArcticLampyrid this one should be addressed once we merge #5887, which we're planning to do in the near future.

@johnhuichen yes it's the correct syntax and it relates to rust-lang/rust#89122. Also, see the type alias docs of the rust reference.

Thanks! I see what's going on now. I think the where Self: Sized in impl block is allowed but not necessarily needed

@ArcticLampyrid
Copy link
Author

Thanks! I see what's going on now. I think the where Self: Sized in impl block is allowed but not necessarily needed

It is not code in the real world, it's just a test case.

A more practical usage is:

impl<A> Foo<A> for MyType<A> {
    type Bar = u16 
    where
        A: Sized;
}

@ytmimi ytmimi linked a pull request Aug 13, 2024 that will close this issue
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants