Skip to content

Support parsing #![feature(default_field_values)] #1851

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

Closed
wants to merge 1 commit into from

Conversation

estebank
Copy link

@estebank estebank commented Mar 6, 2025

#[derive(Default)]
struct Pet {
    name: Option<String>,
    age: i128 = 42,
    //        ^^^^
}

Fix #1774.

@estebank estebank force-pushed the default-field-values branch 2 times, most recently from 3939194 to 6671f48 Compare March 6, 2025 19:24
- RFC: rust-lang/rfcs#3681
- Tracking issue: rust-lang/rust#132162
- Feature gate: `#![feature(default_field_values)]`

```rust
struct Pet {
    name: Option<String>,
    age: i128 = 42,
    //        ^^^^
}
```

Fix dtolnay#1774.
@estebank estebank force-pushed the default-field-values branch from 6671f48 to 8303beb Compare March 6, 2025 19:26
Copy link
Owner

@dtolnay dtolnay left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks!

/// Default value: `field_name: i32 = 1`
///
/// `#![feature(default_field_values)]`
pub default: Option<(Token![=], Expr)>,
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This field cannot be added until 3.x, and even then, not unless the default_field_values feature is closer to stabilization.

For now please send a different PR that parses structs containing field default values as syn::Item::Verbatim instead. I'll close this PR to keep this version of the implementation for 3.x.

@madsmtm
Copy link

madsmtm commented May 19, 2025

In the spirit of not blocking this language feature on v3.x, I wonder: Maybe it'd be possible to hackily smuggle it as an #[__syn_internal_default = $expr] attribute? Something like:

// Parse
let eq_token;
let value;

let field = Field {
    attrs: vec![
        Attribute {
            pound_token: Token![#] { span: fake_span!() },
            style: AttrStyle::Outer,
            bracket_token: Bracket { span: fake_span!() },
            meta: Meta::NameValue(MetaNameValue {
                path: fake_path!("__syn_internal_default"),
                eq_token,
                value,
            }),
        },
    ],
    vis,
    mutability,
    ident,
    colon_token,
    ty,
}

I get that that nobody likes this solution, and that it has issues with the serialization and deserialization logic becoming more complex and people possibly accidentally using the fake attribute in unintended ways. But it'd be somewhat more workable than Verbatim.

Another option: Add ItemStruct2, Fields2, FieldsNamed2, FieldsUnnamed2, Field2 etc.

WDYT?

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 this pull request may close these issues.

Parse default values in fields
3 participants