Fix defaults on #[synthesized] fields (#2825)

Fixes #2821
This commit is contained in:
Sébastien d'Herbais de Thun 2023-12-04 11:44:34 +01:00 committed by GitHub
parent 9926a594e7
commit 7f10d3282e
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 23 additions and 5 deletions

View File

@ -723,7 +723,7 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream {
quote! { quote! {
<#elem as #foundations::ElementFields>::Fields::#name => None, <#elem as #foundations::ElementFields>::Fields::#name => None,
} }
} else if field.inherent() { } else if field.inherent() || (field.synthesized && field.default.is_some()) {
quote! { quote! {
<#elem as #foundations::ElementFields>::Fields::#name => Some( <#elem as #foundations::ElementFields>::Fields::#name => Some(
#foundations::IntoValue::into_value(self.#field_ident.clone()) #foundations::IntoValue::into_value(self.#field_ident.clone())
@ -748,7 +748,7 @@ fn create_native_elem_impl(element: &Elem) -> TokenStream {
quote! { quote! {
<#elem as #foundations::ElementFields>::Fields::#name => false, <#elem as #foundations::ElementFields>::Fields::#name => false,
} }
} else if field.inherent() { } else if field.inherent() || (field.synthesized && field.default.is_some()) {
quote! { quote! {
<#elem as #foundations::ElementFields>::Fields::#name => true, <#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! { ::ecow::EcoString::inline(#name).into() }
}; };
quote! { if field.synthesized && field.default.is_some() {
if let Some(value) = &self.#field_ident { quote! {
fields.insert( fields.insert(
#field_call, #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())
);
}
}
} }
}); });

View File

@ -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)