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

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

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ ast_struct! {
pub colon_token: Option<Token![:]>,

pub ty: Type,

/// 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.

}
}

Expand Down Expand Up @@ -345,6 +350,11 @@ pub(crate) mod parsing {
} else {
input.parse()?
};
let mut default: Option<(Token![=], Expr)> = None;
if input.peek(Token![=]) {
let eq_token: Token![=] = input.parse()?;
default = Some((eq_token, input.parse()?));
}

Ok(Field {
attrs,
Expand All @@ -353,6 +363,7 @@ pub(crate) mod parsing {
ident: Some(ident),
colon_token: Some(colon_token),
ty,
default,
})
}

Expand All @@ -366,6 +377,7 @@ pub(crate) mod parsing {
ident: None,
colon_token: None,
ty: input.parse()?,
default: None,
})
}
}
Expand Down
1 change: 1 addition & 0 deletions src/gen/clone.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/gen/debug.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/gen/eq.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/gen/fold.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions src/gen/hash.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/gen/visit.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions src/gen/visit_mut.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 8 additions & 1 deletion src/parse_quote.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl<T: Parse> ParseQuote for T {

use crate::punctuated::Punctuated;
#[cfg(any(feature = "full", feature = "derive"))]
use crate::{attr, Attribute, Field, FieldMutability, Ident, Type, Visibility};
use crate::{attr, Attribute, Expr, Field, FieldMutability, Ident, Type, Visibility};
#[cfg(feature = "full")]
use crate::{Arm, Block, Pat, Stmt};

Expand Down Expand Up @@ -194,13 +194,20 @@ impl ParseQuote for Field {

let ty: Type = input.parse()?;

let mut default: Option<(Token![=], Expr)> = None;
if is_named && input.peek(Token![=]) {
let eq_token: Token![=] = input.parse()?;
default = Some((eq_token, input.parse()?));
}

Ok(Field {
attrs,
vis,
mutability: FieldMutability::None,
ident,
colon_token,
ty,
default,
})
}
}
Expand Down
12 changes: 12 additions & 0 deletions syn.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 14 additions & 0 deletions tests/debug/gen.rs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

92 changes: 92 additions & 0 deletions tests/test_item.rs
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,98 @@ fn test_impl_visibility() {
snapshot!(tokens as Item, @"Item::Verbatim(`pub default unsafe impl union { }`)");
}

#[test]
fn test_struct_default_field_values() {
let tokens = quote! {
struct Foo {
field: i32 = const { 42 },
}
};
snapshot!(tokens as Item, @r#"
Item::Struct {
vis: Visibility::Inherited,
ident: "Foo",
generics: Generics,
fields: Fields::Named {
named: [
Field {
vis: Visibility::Inherited,
ident: Some("field"),
colon_token: Some,
ty: Type::Path {
path: Path {
segments: [
PathSegment {
ident: "i32",
},
],
},
},
default: Some(Expr::Const {
block: Block {
stmts: [
Stmt::Expr(
Expr::Lit {
lit: 42,
},
None,
),
],
},
}),
},
Token![,],
],
},
}
"#);
}

#[test]
fn test_enum_default_field_values() {
let tokens = quote! {
enum Foo {
Bar {
field: i32 = 42,
}
}
};
snapshot!(tokens as Item, @r#"
Item::Enum {
vis: Visibility::Inherited,
ident: "Foo",
generics: Generics,
variants: [
Variant {
ident: "Bar",
fields: Fields::Named {
named: [
Field {
vis: Visibility::Inherited,
ident: Some("field"),
colon_token: Some,
ty: Type::Path {
path: Path {
segments: [
PathSegment {
ident: "i32",
},
],
},
},
default: Some(Expr::Lit {
lit: 42,
}),
},
Token![,],
],
},
},
],
}
"#);
}

#[test]
fn test_impl_type_parameter_defaults() {
#[cfg(any())]
Expand Down