From 7f10d3282e85b0223efe569d1ee612eb43ab8195 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?S=C3=A9bastien=20d=27Herbais=20de=20Thun?= Date: Mon, 4 Dec 2023 11:44:34 +0100 Subject: [PATCH] Fix defaults on `#[synthesized]` fields (#2825) Fixes #2821 --- crates/typst-macros/src/elem.rs | 19 ++++++++++++++----- tests/typ/bugs/2821-missing-fields.typ | 9 +++++++++ 2 files changed, 23 insertions(+), 5 deletions(-) create mode 100644 tests/typ/bugs/2821-missing-fields.typ diff --git a/crates/typst-macros/src/elem.rs b/crates/typst-macros/src/elem.rs index e531a7edb..b3f861172 100644 --- a/crates/typst-macros/src/elem.rs +++ b/crates/typst-macros/src/elem.rs @@ -723,7 +723,7 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream { quote! { <#elem as #foundations::ElementFields>::Fields::#name => None, } - } else if field.inherent() { + } else if field.inherent() || (field.synthesized && field.default.is_some()) { quote! { <#elem as #foundations::ElementFields>::Fields::#name => Some( #foundations::IntoValue::into_value(self.#field_ident.clone()) @@ -748,7 +748,7 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream { quote! { <#elem as #foundations::ElementFields>::Fields::#name => false, } - } else if field.inherent() { + } else if field.inherent() || (field.synthesized && field.default.is_some()) { quote! { <#elem as #foundations::ElementFields>::Fields::#name => true, } @@ -867,13 +867,22 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream { quote! { ::ecow::EcoString::inline(#name).into() } }; - quote! { - if let Some(value) = &self.#field_ident { + if field.synthesized && field.default.is_some() { + quote! { fields.insert( #field_call, - #foundations::IntoValue::into_value(value.clone()) + #foundations::IntoValue::into_value(self.#field_ident.clone()) ); } + } else { + quote! { + if let Some(value) = &self.#field_ident { + fields.insert( + #field_call, + #foundations::IntoValue::into_value(value.clone()) + ); + } + } } }); diff --git a/tests/typ/bugs/2821-missing-fields.typ b/tests/typ/bugs/2821-missing-fields.typ new file mode 100644 index 000000000..0fec20437 --- /dev/null +++ b/tests/typ/bugs/2821-missing-fields.typ @@ -0,0 +1,9 @@ +// Issue #2821: Setting a figure's supplement to none removes the field +// Ref: false + +--- +#show figure.caption: it => { + assert(it.has("supplement")) + assert(it.supplement == none) +} +#figure([], caption: [], supplement: none)