Skip to content

Commit 2056cef

Browse files
committed
generate: correctly handle directly optional fields (x:X?).
1 parent cbed0ec commit 2056cef

File tree

2 files changed

+12
-2
lines changed

2 files changed

+12
-2
lines changed

macros/tests/basic.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -199,6 +199,8 @@ testcases![
199199
Bar:{ "a" field:"b"? } |
200200
Baz:{ baz:"ab" };
201201
}:
202+
A("a") => "\
203+
1:1-1:2 => A::Bar",
202204
A("ab") => "\
203205
1:1-1:3 => A::Foo(
204206
1:1-1:3,

src/generate/rust.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -230,8 +230,16 @@ impl<Pat: RustInputPat> RuleWithFieldsMethods<Pat> for RuleWithFields {
230230
let children = match &cx[self.fields] {
231231
Fields::Leaf(None) => return quote!(_),
232232
Fields::Leaf(Some(field)) => {
233-
let (i, _, _) = rust_fields.get_full(&field.name).unwrap();
234-
return quote!(#i);
233+
let (i, _, rust_field) = rust_fields.get_full(&field.name).unwrap();
234+
235+
// HACK(eddyb) account for the fact that `x:X?` is `x:{X?}`.
236+
let shape = quote!(#i);
237+
if let Rule::Opt(_) = cx[self.rule] {
238+
if rust_field.refutable {
239+
return quote!([#shape]);
240+
}
241+
}
242+
return shape;
235243
}
236244
Fields::Aggregate(children) => children,
237245
};

0 commit comments

Comments
 (0)