From 53d13bae369ec3a186e2a7e3a340cb0032c48e1a Mon Sep 17 00:00:00 2001 From: mkorje Date: Wed, 25 Jun 2025 16:03:00 +1000 Subject: [PATCH] Add warning --- crates/typst-eval/src/math.rs | 20 +++++++++++++++--- ...h-attach-followed-by-func-call-warning.png | Bin 0 -> 630 bytes tests/suite/math/attach.typ | 18 ++++++++++++++++ 3 files changed, 35 insertions(+), 3 deletions(-) create mode 100644 tests/ref/math-attach-followed-by-func-call-warning.png diff --git a/crates/typst-eval/src/math.rs b/crates/typst-eval/src/math.rs index c2325a8c5..9712b2b81 100644 --- a/crates/typst-eval/src/math.rs +++ b/crates/typst-eval/src/math.rs @@ -1,5 +1,5 @@ use ecow::eco_format; -use typst_library::diag::{At, SourceResult}; +use typst_library::diag::{warning, At, SourceResult}; use typst_library::foundations::{Content, NativeElement, Symbol, SymbolElem, Value}; use typst_library::math::{ AlignPointElem, AttachElem, FracElem, LrElem, PrimesElem, RootElem, @@ -80,7 +80,14 @@ impl Eval for ast::MathAttach<'_> { let mut elem = AttachElem::new(base); if let Some(expr) = self.top() { - elem.t.set(Some(expr.eval_display(vm)?)); + let top = expr.eval(vm)?; + if let Value::Func(_) = top { + vm.engine.sink.warn(warning!( + expr.span(), "function literal used as superscript"; + hint: "wrap the entire function call in parentheses", + )); + } + elem.t.set(Some(top.display().spanned(self.span()))); } // Always attach primes in scripts style (not limits style), @@ -90,7 +97,14 @@ impl Eval for ast::MathAttach<'_> { } if let Some(expr) = self.bottom() { - elem.b.set(Some(expr.eval_display(vm)?)); + let bottom = expr.eval(vm)?; + if let Value::Func(_) = bottom { + vm.engine.sink.warn(warning!( + expr.span(), "function literal used as subscript"; + hint: "wrap the entire function call in parentheses", + )); + } + elem.b.set(Some(bottom.display().spanned(self.span()))); } Ok(elem.pack()) diff --git a/tests/ref/math-attach-followed-by-func-call-warning.png b/tests/ref/math-attach-followed-by-func-call-warning.png new file mode 100644 index 0000000000000000000000000000000000000000..1b05ef8416c0622a1e27af466852a540ffb28b55 GIT binary patch literal 630 zcmV-+0*U>JP)=GoMiZHmnfv=Z8J zp0&j0be*Nu38(DXIAhw{k9?|a2fWzs-r0rcz4@M(=Yh}7;r%w@Kb3@>2`;J$sk6i2lR=JfXhntueN%9qx!`u z5M)d|cN-);17L+yb7OTTKN)g#VgH4YEU*M*a~Plp2cpsW zQY+vEET$DNUf_wbP)@?pWj`*K6T4;MU*a+;bRUqRNWHczpgoxytig59AE+lirv`E- QM*si-07*qoM6N<$g2*-_xc~qF literal 0 HcmV?d00001 diff --git a/tests/suite/math/attach.typ b/tests/suite/math/attach.typ index bac0417c5..994355652 100644 --- a/tests/suite/math/attach.typ +++ b/tests/suite/math/attach.typ @@ -45,6 +45,10 @@ $ // Test function call after subscripts and superscripts. #set page(width: auto) $a_1(x), a^10(x), a_f(x), a^zeta(x), a_"text"(x), +// Warning: 22-25 function literal used as subscript +// Hint: 22-25 wrap the entire function call in parentheses +// Warning: 32-36 function literal used as superscript +// Hint: 32-36 wrap the entire function call in parentheses a_∂(x), a^cos(x), a_abs(x), a^root(2, x), a_subset.eq(x) \ a_(1(x)), a^(10(x)), a_(f(x)), a^(zeta(x)), a_("text"(x)), a_(∂(x)), a^(cos(x)), a_(abs(x)), a^(root(2, x)), a_(subset.eq(x))$ @@ -52,14 +56,28 @@ $a_1(x), a^10(x), a_f(x), a^zeta(x), a_"text"(x), --- math-attach-followed-by-func-call-complex --- // Test function call with named arguments after scripts. #let cross = $c$ +// Warning: 4-10 function literal used as subscript +// Hint: 4-10 wrap the entire function call in parentheses $a_cancel(x, cross: #true)$ --- math-attach-followed-by-func-call-error --- // Error: 14-19 unknown variable: cross // Hint: 14-19 if you meant to display multiple letters as is, try adding spaces between each letter: `c r o s s` // Hint: 14-19 or if you meant to display this as text, try placing it in quotes: `"cross"` +// Warning: 4-10 function literal used as subscript +// Hint: 4-10 wrap the entire function call in parentheses $a_cancel(x, cross: #true)$ +--- math-attach-followed-by-func-call-warning --- +// Test warning for function literal as subscripts and superscripts. +#let myfunc(arg) = $arg$ +// Warning: 4-10 function literal used as subscript +// Hint: 4-10 wrap the entire function call in parentheses +$a_myfunc(x) a_(myfunc(x))$ +// Warning: 4-10 function literal used as subscript +// Hint: 4-10 wrap the entire function call in parentheses +$a_myfunc a_(myfunc)$ + --- math-attach-nested --- // Test associativity and scaling. $ 1/(V^2^3^4^5),