Compare commits

..

2 Commits

Author SHA1 Message Date
Laurenz
0bc68df2a9
Hint for label in both document and bibliography (#6457) 2025-06-19 07:29:38 +00:00
Laurenz
f32cd5b3e1
Ensure that label repr is syntactically valid (#6456) 2025-06-19 07:29:35 +00:00
3 changed files with 18 additions and 5 deletions

View File

@ -79,7 +79,12 @@ impl Label {
impl Repr for Label {
fn repr(&self) -> EcoString {
eco_format!("<{}>", self.resolve())
let resolved = self.resolve();
if typst_syntax::is_valid_label_literal_id(&resolved) {
eco_format!("<{resolved}>")
} else {
eco_format!("label({})", resolved.repr())
}
}
}

View File

@ -5,7 +5,7 @@ use crate::diag::{bail, At, Hint, SourceResult};
use crate::engine::Engine;
use crate::foundations::{
cast, elem, Cast, Content, Context, Func, IntoValue, Label, NativeElement, Packed,
Show, Smart, StyleChain, Synthesize,
Repr, Show, Smart, StyleChain, Synthesize,
};
use crate::introspection::{Counter, CounterKey, Locatable};
use crate::math::EquationElem;
@ -229,8 +229,15 @@ impl Show for Packed<RefElem> {
// RefForm::Normal
if BibliographyElem::has(engine, self.target) {
if elem.is_ok() {
bail!(span, "label occurs in the document and its bibliography");
if let Ok(elem) = elem {
bail!(
span,
"label `{}` occurs both in the document and its bibliography",
self.target.repr();
hint: "change either the {}'s label or the \
bibliography key to resolve the ambiguity",
elem.func().name(),
);
}
return Ok(to_citation(self, engine, styles)?.pack().spanned(span));

View File

@ -51,7 +51,8 @@ $ A = 1 $ <eq2>
// Test ambiguous reference.
= Introduction <arrgh>
// Error: 1-7 label occurs in the document and its bibliography
// Error: 1-7 label `<arrgh>` occurs both in the document and its bibliography
// Hint: 1-7 change either the heading's label or the bibliography key to resolve the ambiguity
@arrgh
#bibliography("/assets/bib/works.bib")