Add warnings when labels are ignored or unattached (#4783)

This commit is contained in:
Max 2024-09-02 12:58:33 +00:00 committed by GitHub
parent a2628ac970
commit c644dce81b
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 39 additions and 1 deletions

View File

@ -1,6 +1,8 @@
use crate::diag::{warning, SourceResult}; use crate::diag::{warning, SourceResult};
use crate::eval::{Eval, Vm}; use crate::eval::{Eval, Vm};
use crate::foundations::{Content, Label, NativeElement, Smart, Unlabellable, Value}; use crate::foundations::{
Content, Label, NativeElement, Repr, Smart, Unlabellable, Value,
};
use crate::math::EquationElem; use crate::math::EquationElem;
use crate::model::{ use crate::model::{
EmphElem, EnumItem, HeadingElem, LinkElem, ListItem, ParbreakElem, RefElem, EmphElem, EnumItem, HeadingElem, LinkElem, ListItem, ParbreakElem, RefElem,
@ -52,7 +54,20 @@ fn eval_markup<'a>(
if let Some(elem) = if let Some(elem) =
seq.iter_mut().rev().find(|node| !node.can::<dyn Unlabellable>()) seq.iter_mut().rev().find(|node| !node.can::<dyn Unlabellable>())
{ {
if elem.label().is_some() {
vm.engine.sink.warn(warning!(
elem.span(), "content labelled multiple times";
hint: "only the last label is used, the rest are ignored",
));
}
*elem = std::mem::take(elem).labelled(label); *elem = std::mem::take(elem).labelled(label);
} else {
vm.engine.sink.warn(warning!(
expr.span(),
"label `{}` is not attached to anything",
label.repr()
));
} }
} }
value => seq.push(value.display().spanned(expr.span())), value => seq.push(value.display().spanned(expr.span())),

View File

@ -50,6 +50,7 @@ _Visible_
--- label-in-block --- --- label-in-block ---
// Test that label only works within one content block. // Test that label only works within one content block.
#show <strike>: strike #show <strike>: strike
// Warning: 13-21 label `<strike>` is not attached to anything
*This is* #[<strike>] *protected.* *This is* #[<strike>] *protected.*
*This is not.* <strike> *This is not.* <strike>
@ -74,3 +75,25 @@ _Visible_
// Hint: 7-7 labels can only be applied in markup mode // Hint: 7-7 labels can only be applied in markup mode
// Hint: 7-7 try wrapping your code in a markup block (`[ ]`) // Hint: 7-7 try wrapping your code in a markup block (`[ ]`)
#{ [A] <a> } #{ [A] <a> }
--- label-multiple-ignored-warn ---
// Warning: 1-8 content labelled multiple times
// Hint: 1-8 only the last label is used, the rest are ignored
= Hello <a> <b>
// Warning: 12-19 content labelled multiple times
// Hint: 12-19 only the last label is used, the rest are ignored
#let f = [#block()<c>]
#f<d>
// Warning: 6-13 content labelled multiple times
// Hint: 6-13 only the last label is used, the rest are ignored
#[#[#block()]<e>]<f>
// Error: 1-3 label `<a>` does not exist in the document
@a
--- label-unattached-warn ---
#set heading(numbering: "1.")
// Warning: 1-4 label `<a>` is not attached to anything
<a>