From 55296a9196ad441c0898787915cd63558ed3c3c5 Mon Sep 17 00:00:00 2001 From: Laurenz Date: Mon, 30 Oct 2023 22:59:24 +0100 Subject: [PATCH] Say which label occurs never or multiple times in error message --- Cargo.lock | 1 + crates/typst/src/model/introspect.rs | 8 +++++--- tests/typ/meta/bibliography.typ | 16 +++++++++------- tests/typ/meta/link.typ | 4 ++-- tests/typ/meta/ref.typ | 4 ++-- 5 files changed, 19 insertions(+), 14 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index e271201c1..b751a1611 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2888,6 +2888,7 @@ dependencies = [ "comemo", "ecow", "if_chain", + "log", "serde", "typst", "unscanny", diff --git a/crates/typst/src/model/introspect.rs b/crates/typst/src/model/introspect.rs index f8a3358ef..bd79e5ed2 100644 --- a/crates/typst/src/model/introspect.rs +++ b/crates/typst/src/model/introspect.rs @@ -5,7 +5,7 @@ use std::hash::Hash; use std::num::NonZeroUsize; use comemo::{Prehashed, Track, Tracked, Validate}; -use ecow::{EcoString, EcoVec}; +use ecow::{eco_format, EcoString, EcoVec}; use indexmap::IndexMap; use super::{Content, Selector}; @@ -388,11 +388,13 @@ impl Introspector { let mut found = None; for elem in self.all().filter(|elem| elem.label() == Some(label)) { if found.is_some() { - bail!("label occurs multiple times in the document"); + bail!("label `{}` occurs multiple times in the document", label.repr()); } found = Some(elem.clone()); } - found.ok_or_else(|| "label does not exist in the document".into()) + found.ok_or_else(|| { + eco_format!("label `{}` does not exist in the document", label.repr()) + }) } /// The total number pages. diff --git a/tests/typ/meta/bibliography.typ b/tests/typ/meta/bibliography.typ index 07c17dc61..e3d746e67 100644 --- a/tests/typ/meta/bibliography.typ +++ b/tests/typ/meta/bibliography.typ @@ -1,12 +1,5 @@ // Test citations and bibliographies. ---- -// Test ambiguous reference. -= Introduction -// Error: 1-7 label occurs in the document and its bibliography -@arrgh -#bibliography("/files/works.bib") - --- #set page(width: 200pt) = Details @@ -41,3 +34,12 @@ And quark! @quark = Multiple Bibs Now we have multiple bibliographies containing #cite("glacier-melt", "keshav2007read") #bibliography(("/files/works.bib", "/files/works_too.bib")) + +--- +// Test ambiguous reference. += Introduction + +// Error: 1-7 label occurs in the document and its bibliography +@arrgh +#bibliography("/files/works.bib") + diff --git a/tests/typ/meta/link.typ b/tests/typ/meta/link.typ index 5dfe37e7c..f93189cbc 100644 --- a/tests/typ/meta/link.typ +++ b/tests/typ/meta/link.typ @@ -67,11 +67,11 @@ Text #link()[Go to text.] --- -// Error: 2-20 label does not exist in the document +// Error: 2-20 label `` does not exist in the document #link()[Nope.] --- Text Text -// Error: 2-20 label occurs multiple times in the document +// Error: 2-20 label `` occurs multiple times in the document #link()[Nope.] diff --git a/tests/typ/meta/ref.typ b/tests/typ/meta/ref.typ index 630bad614..e01bc941b 100644 --- a/tests/typ/meta/ref.typ +++ b/tests/typ/meta/ref.typ @@ -10,14 +10,14 @@ See @setup. As seen in @intro, we proceed. --- -// Error: 1-5 label does not exist in the document +// Error: 1-5 label `` does not exist in the document @foo --- = First = Second -// Error: 1-5 label occurs multiple times in the document +// Error: 1-5 label `` occurs multiple times in the document @foo ---