Skip to content

Commit 6671f48

Browse files
committed
Support parsing #![feature(default_field_values)]
- 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.
1 parent aed58d1 commit 6671f48

File tree

12 files changed

+167
-1
lines changed

12 files changed

+167
-1
lines changed

src/data.rs

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -196,6 +196,11 @@ ast_struct! {
196196
pub colon_token: Option<Token![:]>,
197197

198198
pub ty: Type,
199+
200+
/// Default value: `field_name: i32 = 1`
201+
///
202+
/// `#![feature(default_field_values)]`
203+
pub default: Option<(Token![=], Expr)>,
199204
}
200205
}
201206

@@ -345,6 +350,11 @@ pub(crate) mod parsing {
345350
} else {
346351
input.parse()?
347352
};
353+
let mut default: Option<(Token![=], Expr)> = None;
354+
if input.peek(Token![=]) {
355+
let eq_token: Token![=] = input.parse()?;
356+
default = Some((eq_token, input.parse()?));
357+
}
348358

349359
Ok(Field {
350360
attrs,
@@ -353,6 +363,7 @@ pub(crate) mod parsing {
353363
ident: Some(ident),
354364
colon_token: Some(colon_token),
355365
ty,
366+
default,
356367
})
357368
}
358369

@@ -366,6 +377,7 @@ pub(crate) mod parsing {
366377
ident: None,
367378
colon_token: None,
368379
ty: input.parse()?,
380+
default: None,
369381
})
370382
}
371383
}

src/gen/clone.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen/debug.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen/eq.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen/fold.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen/hash.rs

Lines changed: 1 addition & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen/visit.rs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/gen/visit_mut.rs

Lines changed: 4 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/parse_quote.rs

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,7 @@ impl<T: Parse> ParseQuote for T {
149149

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

@@ -194,13 +194,20 @@ impl ParseQuote for Field {
194194

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

197+
let mut default: Option<(Token![=], Expr)> = None;
198+
if is_named && input.peek(Token![=]) {
199+
let eq_token: Token![=] = input.parse()?;
200+
default = Some((eq_token, input.parse()?));
201+
}
202+
197203
Ok(Field {
198204
attrs,
199205
vis,
200206
mutability: FieldMutability::None,
201207
ident,
202208
colon_token,
203209
ty,
210+
default,
204211
})
205212
}
206213
}

syn.json

Lines changed: 12 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)