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)