summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien d'Herbais de Thun <sebastien.d.herbais@gmail.com>2023-12-04 11:44:34 +0100
committerGitHub <noreply@github.com>2023-12-04 11:44:34 +0100
commit7f10d3282e85b0223efe569d1ee612eb43ab8195 (patch)
tree4aac92ee2b5e2e905b3359bf3da24e11f5da0233
parent9926a594e7f462103b47930270a00c1b9ce3cbf3 (diff)
Fix defaults on `#[synthesized]` fields (#2825)
Fixes #2821
-rw-r--r--crates/typst-macros/src/elem.rs19
-rw-r--r--tests/typ/bugs/2821-missing-fields.typ9
2 files changed, 23 insertions, 5 deletions
diff --git a/crates/typst-macros/src/elem.rs b/crates/typst-macros/src/elem.rs
index e531a7ed..b3f86117 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 00000000..0fec2043
--- /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)