TedDriggs / darling

A Rust proc-macro attribute parser
MIT License
1.02k stars 66 forks source link

`FromField` doesn't raise UnknownField errors #303

Closed andrewbaxter closed 2 months ago

andrewbaxter commented 2 months ago

The following produces an error as expected:

use darling::{
    FromDeriveInput,
};
use syn::{
    parse_quote,
};

#[derive(Default, FromDeriveInput)]
#[darling(attributes(demo))]
pub struct Receiver {
    example1: Option<String>,
}

fn main() {
    Receiver::from_derive_input(&parse_quote!{
        #[demo(example2 = "hi")]
        struct Example;
    }).unwrap();
}
called `Result::unwrap()` on an `Err` value: Error { kind: UnknownField(ErrorUnknownField { name: "example2", did_you_mean: Some((0.95, "example1")) }), locations: [], span: Some(Span) }

However this runs with no error:

use darling::{
    FromField,
};
use syn::{
    parse_quote,
    ItemStruct,
};

#[derive(Default, FromField)]
pub struct Receiver {
    example1: Option<String>,
}

fn main() {
    let s: ItemStruct = parse_quote!{
        struct Example {
            #[demo(example2 = "hi")]
            x: u32,
        }
    };
    Receiver::from_field(&s.fields.into_iter().next().unwrap()).unwrap();
}

This happens both on the latest commit c330c3440bfecf3d556ffa1f03c96c7b7eac416b and in the latest release.

Edit: default is unnecessary, happens when non-default too.

andrewbaxter commented 2 months ago

Ack, sorry. I thought attributes(x) was only for from_derive_input, but it's required if you want an enclosing attribute anywhere.