From a517a08d2fc50706654bb649db496b21b47f421b Mon Sep 17 00:00:00 2001 From: T0mstone Date: Sat, 22 Mar 2025 22:31:51 +0100 Subject: [PATCH 1/6] Warn on last show/set rule in block --- crates/typst-eval/src/code.rs | 13 +++++++++++++ tests/suite/styling/in-block.typ | 12 ++++++++++++ 2 files changed, 25 insertions(+) create mode 100644 tests/suite/styling/in-block.typ diff --git a/crates/typst-eval/src/code.rs b/crates/typst-eval/src/code.rs index 9078418e4..56c96a071 100644 --- a/crates/typst-eval/src/code.rs +++ b/crates/typst-eval/src/code.rs @@ -285,6 +285,19 @@ impl Eval for ast::CodeBlock<'_> { fn eval(self, vm: &mut Vm) -> SourceResult { vm.scopes.enter(); + if let Some((span, kind)) = + self.body().exprs().next_back().and_then(|x| match x { + ast::Expr::ShowRule(_) => Some((x.span(), "show")), + ast::Expr::SetRule(_) => Some((x.span(), "set")), + _ => None, + }) + { + vm.engine.sink.warn(typst_library::diag::warning!( + span, + "{kind} rule has no effect"; + hint: "See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules" + )); + } let output = self.body().eval(vm)?; vm.scopes.exit(); Ok(output) diff --git a/tests/suite/styling/in-block.typ b/tests/suite/styling/in-block.typ new file mode 100644 index 000000000..e13a2d3a8 --- /dev/null +++ b/tests/suite/styling/in-block.typ @@ -0,0 +1,12 @@ +--- warn-show-set-last-in-block --- +#{ +// Warning: 1-14 show rule has no effect +// Hint: 1-14 See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules +show "a": "b" +} + +#{ +// Warning: 1-15 set rule has no effect +// Hint: 1-15 See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules +set text(blue) +} From 72c219d24ad0e9c42660bb4123ec2968c1bf9735 Mon Sep 17 00:00:00 2001 From: T0mstone Date: Wed, 11 Jun 2025 14:25:27 +0200 Subject: [PATCH 2/6] Suppress warning in tests --- tests/ref/context-delayed-warning.png | Bin 0 -> 800 bytes .../issue-4769-document-context-conditional.png | Bin 0 -> 703 bytes tests/suite/foundations/context.typ | 1 + tests/suite/model/document.typ | 1 + 4 files changed, 2 insertions(+) create mode 100644 tests/ref/context-delayed-warning.png create mode 100644 tests/ref/issue-4769-document-context-conditional.png diff --git a/tests/ref/context-delayed-warning.png b/tests/ref/context-delayed-warning.png new file mode 100644 index 0000000000000000000000000000000000000000..a4e53e159b109c15e7165f3329ce9d8fae0360c8 GIT binary patch literal 800 zcmV+*1K<3KP)98^E6WU}n+``2X>>~mQLW3Oh=@f|c7UOk zm@|(^ls0GH_jKxaZtnEia21L;AyR+n`~2ne!{>*`Z?E?lMyW9pEWr}|Kfw;XXM2XH z#|+5s4K7)Xx3Z7oJ);ay0s~sx+VNy4xF+95cdf3C<{~F`VnID+#Q>%T^c@;AbmlZ+ ztp+W%ru+u9B>xPB3+47VL6zR&tKo-2+BL$Of;*vmSD{$4 zf5w*hd1rz2o%n3tJ+o8vKPdb9EfNGaEpz|w3cdt1&N>h!BYnOy@H+y9LCTO1_$*W8UM<}>36-t)`>QbpV5TV<14x%3l*QiNYjlZEh6t8ea2)x*?Sx?p2h=_<>MFwfds9fNI1-O#T z?WKx_U+^bxrl1W2;?)>sOa$Em1pHdCO<@WESsNpB7#g-svT)?08@7%&VWdAi*@5C= zZYDR5=mW7$=nJ1$QX?avln1d0tba-N5)W*+Mfn^z{A2Op@Fclh-jcQ`F4W|PQO)1A zLhKKZ-w+#UX`CIuFx?d@j~v$*ZVTA9Mn-0ELU08Fr9lZnc^;VXnAJ)*Y?*d$FxXk| z&P9j52fGnyuY`6R77>>Luy!DH>zrWKx_}Bx6Gl5`6V6ruyUx)J7Oevs!LIG@eSsc* z7qs+XuKxSw5=Lt>tE3|#!T%0cJsJu4)sAZ-DeAbV`3cE3>XrLzKQ0Q(8Gg7XN|&?` zUscg`d2#e@N5I>xK=JTN)9}L-UrAhzubr58+*5w>Q>mslpSi~H!#VS^Ui89SGq|7h zX0BST?ivyJX^fb>@()D#SL!woxveqsPn^8f9wAvT%{7rYnt2veZ&yg61L2 zP*Slmk4v5BQy!LW9@0(U*+n-+E-L2D?|uC|yso|(_66VymztKvhoW+?ao59EHWN zri1Ddb5)UDKwt>GLS4g^dP@oZv>P#tcx!~^|eLWa3q zJ?Vdj^eViT#)8OG;r$uji)fD-JPiA{;Cpg9f^h>t?NYh*&^m0{g(d3KfyM4HeELyc zVw_+Nj=YHkw1I?luOBz45KZREO-_9ueYyV#&uN%-_*xRM?lfgMf~c*@&!Wu59$=)& zcoO%yf#78;@bl>G) zys|^EF3dLYTDf#!IleAE>$RknVO9RB;@}ILz^&S@2$G~Yj0}qL(IjN{gkuxfm|t{^ z^eWt#^_zFrYG!Q(->#kX=4J)X=JBIA&M@C6+FaG!0B*ZcdA1`~suMSqg^x5`$X_sr lmh{Sm0TEyU7GMGX(Kjv~0ygh)w}=1$002ovPDHLkV1l-3PBj1k literal 0 HcmV?d00001 diff --git a/tests/suite/foundations/context.typ b/tests/suite/foundations/context.typ index e506d50db..7c71b0f95 100644 --- a/tests/suite/foundations/context.typ +++ b/tests/suite/foundations/context.typ @@ -48,4 +48,5 @@ let n = query().len() let fonts = ("nope", "Roboto") set text(font: fonts.at(n)) + lorem(5) } diff --git a/tests/suite/model/document.typ b/tests/suite/model/document.typ index 2ed199fee..beaff0538 100644 --- a/tests/suite/model/document.typ +++ b/tests/suite/model/document.typ @@ -51,4 +51,5 @@ Hello #context { set document(author: "Changed") if "Normal" in document.author set document(title: "Changed") if document.title == "Normal" + lorem(5) } From d799a4207bc70e838bda0358d95d7f53540deeba Mon Sep 17 00:00:00 2001 From: T0mstone Date: Wed, 11 Jun 2025 14:36:09 +0200 Subject: [PATCH 3/6] Change test fix to empty content --- tests/ref/context-delayed-warning.png | Bin 800 -> 0 bytes .../issue-4769-document-context-conditional.png | Bin 703 -> 0 bytes tests/suite/foundations/context.typ | 2 +- tests/suite/model/document.typ | 2 +- 4 files changed, 2 insertions(+), 2 deletions(-) delete mode 100644 tests/ref/context-delayed-warning.png delete mode 100644 tests/ref/issue-4769-document-context-conditional.png diff --git a/tests/ref/context-delayed-warning.png b/tests/ref/context-delayed-warning.png deleted file mode 100644 index a4e53e159b109c15e7165f3329ce9d8fae0360c8..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 800 zcmV+*1K<3KP)98^E6WU}n+``2X>>~mQLW3Oh=@f|c7UOk zm@|(^ls0GH_jKxaZtnEia21L;AyR+n`~2ne!{>*`Z?E?lMyW9pEWr}|Kfw;XXM2XH z#|+5s4K7)Xx3Z7oJ);ay0s~sx+VNy4xF+95cdf3C<{~F`VnID+#Q>%T^c@;AbmlZ+ ztp+W%ru+u9B>xPB3+47VL6zR&tKo-2+BL$Of;*vmSD{$4 zf5w*hd1rz2o%n3tJ+o8vKPdb9EfNGaEpz|w3cdt1&N>h!BYnOy@H+y9LCTO1_$*W8UM<}>36-t)`>QbpV5TV<14x%3l*QiNYjlZEh6t8ea2)x*?Sx?p2h=_<>MFwfds9fNI1-O#T z?WKx_U+^bxrl1W2;?)>sOa$Em1pHdCO<@WESsNpB7#g-svT)?08@7%&VWdAi*@5C= zZYDR5=mW7$=nJ1$QX?avln1d0tba-N5)W*+Mfn^z{A2Op@Fclh-jcQ`F4W|PQO)1A zLhKKZ-w+#UX`CIuFx?d@j~v$*ZVTA9Mn-0ELU08Fr9lZnc^;VXnAJ)*Y?*d$FxXk| z&P9j52fGnyuY`6R77>>Luy!DH>zrWKx_}Bx6Gl5`6V6ruyUx)J7Oevs!LIG@eSsc* z7qs+XuKxSw5=Lt>tE3|#!T%0cJsJu4)sAZ-DeAbV`3cE3>XrLzKQ0Q(8Gg7XN|&?` zUscg`d2#e@N5I>xK=JTN)9}L-UrAhzubr58+*5w>Q>mslpSi~H!#VS^Ui89SGq|7h zX0BST?ivyJX^fb>@()D#SL!woxveqsPn^8f9wAvT%{7rYnt2veZ&yg61L2 zP*Slmk4v5BQy!LW9@0(U*+n-+E-L2D?|uC|yso|(_66VymztKvhoW+?ao59EHWN zri1Ddb5)UDKwt>GLS4g^dP@oZv>P#tcx!~^|eLWa3q zJ?Vdj^eViT#)8OG;r$uji)fD-JPiA{;Cpg9f^h>t?NYh*&^m0{g(d3KfyM4HeELyc zVw_+Nj=YHkw1I?luOBz45KZREO-_9ueYyV#&uN%-_*xRM?lfgMf~c*@&!Wu59$=)& zcoO%yf#78;@bl>G) zys|^EF3dLYTDf#!IleAE>$RknVO9RB;@}ILz^&S@2$G~Yj0}qL(IjN{gkuxfm|t{^ z^eWt#^_zFrYG!Q(->#kX=4J)X=JBIA&M@C6+FaG!0B*ZcdA1`~suMSqg^x5`$X_sr lmh{Sm0TEyU7GMGX(Kjv~0ygh)w}=1$002ovPDHLkV1l-3PBj1k diff --git a/tests/suite/foundations/context.typ b/tests/suite/foundations/context.typ index 7c71b0f95..74905beeb 100644 --- a/tests/suite/foundations/context.typ +++ b/tests/suite/foundations/context.typ @@ -48,5 +48,5 @@ let n = query().len() let fonts = ("nope", "Roboto") set text(font: fonts.at(n)) - lorem(5) + [] } diff --git a/tests/suite/model/document.typ b/tests/suite/model/document.typ index beaff0538..b8e9222ae 100644 --- a/tests/suite/model/document.typ +++ b/tests/suite/model/document.typ @@ -51,5 +51,5 @@ Hello #context { set document(author: "Changed") if "Normal" in document.author set document(title: "Changed") if document.title == "Normal" - lorem(5) + [] } From 801b689dae169ceeec9ae94389e93d976bb46014 Mon Sep 17 00:00:00 2001 From: T0mstone Date: Wed, 11 Jun 2025 16:33:26 +0200 Subject: [PATCH 4/6] Apply suggestions Co-authored-by: Laurenz --- crates/typst-eval/src/code.rs | 36 +++++++++++++++++++------------- tests/suite/styling/in-block.typ | 12 +++++------ 2 files changed, 28 insertions(+), 20 deletions(-) diff --git a/crates/typst-eval/src/code.rs b/crates/typst-eval/src/code.rs index 56c96a071..f42c9d730 100644 --- a/crates/typst-eval/src/code.rs +++ b/crates/typst-eval/src/code.rs @@ -7,6 +7,7 @@ use typst_library::foundations::{ }; use typst_library::introspection::{Counter, State}; use typst_syntax::ast::{self, AstNode}; +use typst_syntax::Span; use typst_utils::singleton; use crate::{CapturesVisitor, Eval, FlowEvent, Vm}; @@ -284,21 +285,19 @@ impl Eval for ast::CodeBlock<'_> { type Output = Value; fn eval(self, vm: &mut Vm) -> SourceResult { - vm.scopes.enter(); - if let Some((span, kind)) = - self.body().exprs().next_back().and_then(|x| match x { - ast::Expr::ShowRule(_) => Some((x.span(), "show")), - ast::Expr::SetRule(_) => Some((x.span(), "set")), - _ => None, - }) - { - vm.engine.sink.warn(typst_library::diag::warning!( - span, - "{kind} rule has no effect"; - hint: "See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules" - )); + let body = self.body(); + match body.exprs().next_back() { + Some(expr @ ast::Expr::ShowRule(_)) => { + warn_for_useless_rule(&mut vm.engine, expr.span(), "show") + } + Some(expr @ ast::Expr::SetRule(_)) => { + warn_for_useless_rule(&mut vm.engine, expr.span(), "set") + } + _ => {} } - let output = self.body().eval(vm)?; + + vm.scopes.enter(); + let output = body.eval(vm)?; vm.scopes.exit(); Ok(output) } @@ -406,3 +405,12 @@ fn warn_for_discarded_content(engine: &mut Engine, event: &FlowEvent, joined: &V engine.sink.warn(warning); } + +/// Emits a warning when a set or show rule has no effect. +fn warn_for_useless_rule(engine: &mut Engine, span: Span, kind: &str) { + engine.sink.warn(warning!( + span, + "{kind} rule has no effect"; + hint: "a {kind} rule is only in effect until the end of the surrounding code block" + )); +} diff --git a/tests/suite/styling/in-block.typ b/tests/suite/styling/in-block.typ index e13a2d3a8..7645f83ca 100644 --- a/tests/suite/styling/in-block.typ +++ b/tests/suite/styling/in-block.typ @@ -1,12 +1,12 @@ --- warn-show-set-last-in-block --- #{ -// Warning: 1-14 show rule has no effect -// Hint: 1-14 See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules -show "a": "b" + // Warning: 1-14 show rule has no effect + // Hint: 1-14 a show rule is only in effect until the end of the surrounding code block + show "a": "b" } #{ -// Warning: 1-15 set rule has no effect -// Hint: 1-15 See https://typst.app/docs/tutorial/making-a-template/#set-and-show-rules -set text(blue) + // Warning: 1-15 set rule has no effect + // Hint: 1-15 a set rule is only in effect until the end of the surrounding code block + set text(blue) } From fb0b01d45c1a3d417c4197287b81bb12b2872514 Mon Sep 17 00:00:00 2001 From: T0mstone Date: Wed, 11 Jun 2025 16:37:03 +0200 Subject: [PATCH 5/6] Fix test spans --- tests/suite/styling/in-block.typ | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/suite/styling/in-block.typ b/tests/suite/styling/in-block.typ index 7645f83ca..92c8f2051 100644 --- a/tests/suite/styling/in-block.typ +++ b/tests/suite/styling/in-block.typ @@ -1,12 +1,12 @@ --- warn-show-set-last-in-block --- #{ // Warning: 1-14 show rule has no effect - // Hint: 1-14 a show rule is only in effect until the end of the surrounding code block + // Hint: 3-16 a show rule is only in effect until the end of the surrounding code block show "a": "b" } #{ // Warning: 1-15 set rule has no effect - // Hint: 1-15 a set rule is only in effect until the end of the surrounding code block + // Hint: 3-17 a set rule is only in effect until the end of the surrounding code block set text(blue) } From 01f73c35ed279d26678fe9c991ef55a0e44a1b76 Mon Sep 17 00:00:00 2001 From: T0mstone Date: Wed, 11 Jun 2025 16:42:00 +0200 Subject: [PATCH 6/6] Fix test spans for real Haste is the enemy of correctness --- tests/suite/styling/in-block.typ | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/suite/styling/in-block.typ b/tests/suite/styling/in-block.typ index 92c8f2051..5ac8bde1c 100644 --- a/tests/suite/styling/in-block.typ +++ b/tests/suite/styling/in-block.typ @@ -1,12 +1,12 @@ --- warn-show-set-last-in-block --- #{ - // Warning: 1-14 show rule has no effect + // Warning: 3-16 show rule has no effect // Hint: 3-16 a show rule is only in effect until the end of the surrounding code block show "a": "b" } #{ - // Warning: 1-15 set rule has no effect + // Warning: 3-17 set rule has no effect // Hint: 3-17 a set rule is only in effect until the end of the surrounding code block set text(blue) }