taiki-e / pin-project-lite

A lightweight version of pin-project written with declarative macros.
https://docs.rs/pin-project-lite
Apache License 2.0
216 stars 15 forks source link

Support const generics #62

Open taiki-e opened 3 years ago

taiki-e commented 3 years ago

Currently the following code fails to compile:

pin_project_lite::pin_project! {
    struct S<const N: usize> {
        f: [u8; N],
    }
}
error: no rules expected the token `[`
  --> tests/test.rs:16:5
   |
16 | /     pin_project! {
17 | |         struct S<const N: usize> {
18 | |             f: [u8; N],
19 | |         }
20 | |     }
   | |_____^ no rules expected this token in macro call
   |
   = note: this error originates in the macro `pin_project` (in Nightly builds, run with -Z macro-backtrace for more info)

Originally found by @jjl (https://twitter.com/techpractical/status/1415215677225553922).

jjl commented 3 years ago

Oh sorry, I assumed it was a known limitation, or I would have filed a ticket.

I assume the restriction on not being able to implement Drop is desired though?

taiki-e commented 3 years ago

I assume the restriction on not being able to implement Drop is desired though?

I think this issue is purely a bug of the generics parsing.

https://github.com/taiki-e/pin-project-lite/blob/ac4096c3bb574aa6be77f3c06f54509b8f707582/src/lib.rs#L1555-L1561

notgull commented 5 months ago

I took a run at this; it turns out it's a lot harder to parse arbitrary generics using nothing but declarative macros. I'll take another run at it later.

jjl commented 5 months ago

i had a go (in 2021 apparently!) and decided it was probably impossible with macros as they are. if it isn't, i couldn't figure it out anyway.