diff --git a/crates/typst-eval/src/markup.rs b/crates/typst-eval/src/markup.rs index 5beefa912..9118ded56 100644 --- a/crates/typst-eval/src/markup.rs +++ b/crates/typst-eval/src/markup.rs @@ -205,7 +205,9 @@ impl Eval for ast::Label<'_> { type Output = Value; fn eval(self, _: &mut Vm) -> SourceResult { - Ok(Value::Label(Label::new(PicoStr::intern(self.get())))) + Ok(Value::Label( + Label::new(PicoStr::intern(self.get())).expect("unexpected empty label"), + )) } } @@ -213,7 +215,8 @@ impl Eval for ast::Ref<'_> { type Output = Content; fn eval(self, vm: &mut Vm) -> SourceResult { - let target = Label::new(PicoStr::intern(self.target())); + let target = Label::new(PicoStr::intern(self.target())) + .expect("unexpected empty reference"); let mut elem = RefElem::new(target); if let Some(supplement) = self.supplement() { elem.push_supplement(Smart::Custom(Some(Supplement::Content( diff --git a/crates/typst-library/src/foundations/label.rs b/crates/typst-library/src/foundations/label.rs index 403d50758..b8a08ac8d 100644 --- a/crates/typst-library/src/foundations/label.rs +++ b/crates/typst-library/src/foundations/label.rs @@ -53,9 +53,11 @@ pub struct Label(PicoStr); impl Label { /// Creates a label from an interned string. /// Callers need to ensure the given string is not empty. - pub fn new(name: PicoStr) -> Self { - debug_assert!(name != PicoStr::EMPTY); - Self(name) + pub fn new(name: PicoStr) -> Option { + match name { + PicoStr::EMPTY => None, + _ => Some(Self(name)), + } } /// Resolves the label to a string. diff --git a/crates/typst-library/src/model/bibliography.rs b/crates/typst-library/src/model/bibliography.rs index c9b9a4c7c..ef8d6b1f8 100644 --- a/crates/typst-library/src/model/bibliography.rs +++ b/crates/typst-library/src/model/bibliography.rs @@ -318,7 +318,12 @@ impl Bibliography { bail!("bibliography key must not be empty"); } - match map.entry(Label::new(PicoStr::intern(key))) { + let label = match Label::new(PicoStr::intern(key)) { + Some(lbl) => lbl, + None => bail!("unexpected error while creating a label"), + }; + + match map.entry(label) { indexmap::map::Entry::Vacant(vacant) => { vacant.insert(entry); }