jam1garner / binrw

A Rust crate for helping parse and rebuild binary data using ✨macro magic✨.
https://binrw.rs
MIT License
545 stars 35 forks source link

ExpandLifetimes does not recursively fold type under reference #241

Closed himikof closed 7 months ago

himikof commented 7 months ago

The following code leads to some very confusing compilation errors:

    #[derive(binrw::NamedArgs)]
    struct ExtraArgs<'a> {
        test: PhantomData<&'a ()>,
    }
    #[derive(binrw::NamedArgs)]
    struct FieldArgs<'a, Extra> {
        tag: &'a String,
        extra: Extra,
    }
    #[binrw::binread]
    #[br(import_raw(args: FieldArgs<'_, &'_ ExtraArgs<'_>>))]
    struct Example {}

    #[binrw::binread]
    #[br(import_raw(args: FieldArgs<'_, &ExtraArgs<'_>>))]
    struct Example2 {}
error[E0637]: `'_` cannot be used here
   --> src/test.rs:109:42
    |
109 |     #[br(import_raw(args: FieldArgs<'_, &'_ ExtraArgs<'_>>))]
    |                                          ^^ `'_` is a reserved lifetime name
error[E0637]: `'_` cannot be used here
   --> src/test.rs:109:63
    |
109 |     #[br(import_raw(args: FieldArgs<'_, &'_ ExtraArgs<'_>>))]
    |                                                       ^^ `'_` is a reserved lifetime name
error[E0637]: `'_` cannot be used here
   --> src/test.rs:114:60
    |
114 |     #[br(import_raw(args: FieldArgs<'_, &ExtraArgs<'_>>))]
    |                                                    ^^ `'_` is a reserved lifetime name

I would expect the '_ lifetimes to be recursively replaced in those types. The "'_ cannot be used here" error is very confusing because the successful '_ usage is right next to it. I only understood the problem after reading the output of cargo expand.