From 21e608e6e9bfe7c7b1111152c0f5647cfe1e9e4e Mon Sep 17 00:00:00 2001 From: Joshua Gawley <16921823+joshuagawley@users.noreply.github.com> Date: Wed, 18 Dec 2024 16:52:37 +0000 Subject: [PATCH] Change error when accessing non-existant label (#5571) --- crates/typst-library/src/foundations/content.rs | 7 ++++--- tests/suite/foundations/label.typ | 4 ++++ 2 files changed, 8 insertions(+), 3 deletions(-) diff --git a/crates/typst-library/src/foundations/content.rs b/crates/typst-library/src/foundations/content.rs index bfafbc486..ab2f68ac2 100644 --- a/crates/typst-library/src/foundations/content.rs +++ b/crates/typst-library/src/foundations/content.rs @@ -211,9 +211,10 @@ impl Content { /// instead. pub fn get_by_name(&self, name: &str) -> Result { if name == "label" { - if let Some(label) = self.label() { - return Ok(label.into_value()); - } + return self + .label() + .map(|label| label.into_value()) + .ok_or(FieldAccessError::Unknown); } let id = self.elem().field_id(name).ok_or(FieldAccessError::Unknown)?; self.get(id, None) diff --git a/tests/suite/foundations/label.typ b/tests/suite/foundations/label.typ index af6d23803..3b84c2d70 100644 --- a/tests/suite/foundations/label.typ +++ b/tests/suite/foundations/label.typ @@ -88,3 +88,7 @@ _Visible_ #set heading(numbering: "1.") // Warning: 1-4 label `` is not attached to anything + +--- label-non-existent-error --- +// Error: 5-10 sequence does not have field "label" +#[].label