From c38558d9db86dc817e867fc9248ca260fd616d73 Mon Sep 17 00:00:00 2001 From: Said Aroua Date: Sun, 25 May 2025 15:02:34 +0200 Subject: [PATCH] Fail on empty label name (#5776) --- crates/typst-library/src/foundations/label.rs | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/crates/typst-library/src/foundations/label.rs b/crates/typst-library/src/foundations/label.rs index 2f5520b1c..143da2354 100644 --- a/crates/typst-library/src/foundations/label.rs +++ b/crates/typst-library/src/foundations/label.rs @@ -1,7 +1,10 @@ use ecow::{eco_format, EcoString}; use typst_utils::{PicoStr, ResolvedPicoStr}; -use crate::foundations::{func, scope, ty, Repr, Str}; +use crate::{ + diag::StrResult, + foundations::{bail, func, scope, ty, Repr, Str}, +}; /// A label for an element. /// @@ -27,7 +30,8 @@ use crate::foundations::{func, scope, ty, Repr, Str}; /// # Syntax /// This function also has dedicated syntax: You can create a label by enclosing /// its name in angle brackets. This works both in markup and code. A label's -/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. +/// name can contain letters, numbers, `_`, `-`, `:`, and `.`. Empty label names +/// get rejected. /// /// Note that there is a syntactical difference when using the dedicated syntax /// for this function. In the code below, the `[]` terminates the heading and @@ -67,13 +71,17 @@ impl Label { #[scope] impl Label { - /// Creates a label from a string. + /// Creates a label from a string. Fails for empty strings. #[func(constructor)] pub fn construct( /// The name of the label. name: Str, - ) -> Label { - Self(PicoStr::intern(name.as_str())) + ) -> StrResult