[latex3-commits] [git/LaTeX3-latex3-latex2e] gh818: Complain if a rule is added to an used one-time hook (4f9b0024)

PhelypeOleinik phelype.oleinik at latex-project.org
Thu May 19 05:01:22 CEST 2022


Repository : https://github.com/latex3/latex2e
On branch  : gh818
Link       : https://github.com/latex3/latex2e/commit/4f9b00245f87ec3880754b420f5cd91781256cd2

>---------------------------------------------------------------

commit 4f9b00245f87ec3880754b420f5cd91781256cd2
Author: PhelypeOleinik <phelype.oleinik at latex-project.org>
Date:   Thu May 19 00:01:22 2022 -0300

    Complain if a rule is added to an used one-time hook


>---------------------------------------------------------------

4f9b00245f87ec3880754b420f5cd91781256cd2
 base/doc/ltnews35.tex                  |  6 +++---
 base/lthooks.dtx                       | 39 +++++++++++++++++++++++++++-------
 base/testfiles-lthooks/github-0606.tlg |  9 ++++++++
 base/testfiles-lthooks/lthooks-031.tlg | 10 +++++++++
 4 files changed, 53 insertions(+), 11 deletions(-)

diff --git a/base/doc/ltnews35.tex b/base/doc/ltnews35.tex
index 33ce5f6e..8f468a6a 100644
--- a/base/doc/ltnews35.tex
+++ b/base/doc/ltnews35.tex
@@ -795,9 +795,9 @@ contains the declaration of a hook ordering rule for that one hook (such
 that the sorting mechanism kicks in while running the hook code), a bug
 existed which would cause the spurious typesetting of the code labels
 and the hook name.  This was caused because the sorting mechanism was
-incorrectly called for a one-time hook.  The bug is now fixed and the
-sorting mechanism first checks if the hook is not one-time before trying
-to sort it.
+incorrectly called for a one-time hook.  The bug is now fixed and an
+error is now raised when a new sorting rule is added to an already used
+one-time hook.
 %
 \githubissue{818}
 
diff --git a/base/lthooks.dtx b/base/lthooks.dtx
index 946a2ead..bd8c106e 100644
--- a/base/lthooks.dtx
+++ b/base/lthooks.dtx
@@ -3634,7 +3634,13 @@
         \@@_deprecated_generic_warn:n {#1}
         \@@_do_deprecated_generic:Nn \@@_gset_rule:nnnn {#1}
           {#2} {#3} {#4}
-        \exp_after:wN \use_none:nnnnnnnnn \use_none:n
+        \@@_clean_to_scan:w
+      }
+    \@@_if_execute_immediately:nT {#1}
+      {
+        \msg_error:nnnnnn { hooks } { rule-too-late }
+          {#1} {#2} {#3} {#4}
+        \@@_clean_to_scan:w
       }
 %    \end{macrocode}
 %    First we ensure the basic data structure of the hook exists:
@@ -3653,8 +3659,11 @@
           {#1} {#2} {#4}
         \@@_update_hook_code:n {#1}
       }
-      { \msg_error:nnnnnn { hooks } { unknown-rule }
-                          {#1} {#2} {#3} {#4}        }
+      {
+        \msg_error:nnnnnn { hooks } { unknown-rule }
+          {#1} {#2} {#3} {#4}
+      }
+    \s_@@_mark
   }
 %    \end{macrocode}
 % \end{macro}
@@ -3868,9 +3877,7 @@
 %    on processing time if we do that up front.
 % \changes{v1.0u}{2022/05/17}{Refuse sorting one-time hooks (gh/818).}
 %    \begin{macrocode}
-    \bool_lazy_and:nnT
-        { \@@_if_usable_p:n {#1} }
-        { ! \@@_if_execute_immediately_p:n {#1} }
+    \@@_if_usable:nT {#1}
       {
         \prop_if_empty:cTF { g_@@_#1_code_prop }
           {
@@ -4919,7 +4926,7 @@
 %   empty.
 % \changes{v1.0r}{2021/09/06}{Macro added (gh/606)}
 %    \begin{macrocode}
-\prg_new_conditional:Npnn \@@_if_execute_immediately:n #1 { p, F, TF }
+\prg_new_conditional:Npnn \@@_if_execute_immediately:n #1 { T, F, TF }
   {
     \@@_if_usable:nTF {#1}
       {
@@ -5160,7 +5167,8 @@
 %
 %    \begin{macrocode}
 \msg_new:nnnn { hooks } { unknown-rule }
-  { Unknown~ relationship~ '#3'~
+  {
+    Unknown~ relationship~ '#3'~
     between~ labels~ '#2'~ and~ '#4'~
     \str_if_eq:nnF {#1} {??} { ~in~hook~'#1' }. ~
     Perhaps~ a~ misspelling?
@@ -5177,6 +5185,21 @@
 %    \end{macrocode}
 %
 %    \begin{macrocode}
+\msg_new:nnnn { hooks } { rule-too-late }
+  {
+    Cannot~set~a~sorting~rule~for~the~already~used~hook~'#1'.\\
+    Try~setting~this~rule~earlier.
+  }
+  {
+    You~tried~to~set~the~ordering~of~hook~'#1'~using\\
+    \ \ \iow_char:N\\DeclareHookRule{#1}{#2}{#3}{#4}\\
+    but~hook~'#1'~was~already~used~as~a~one-time~hook,~
+    thus~sorting~is~no~longer~possible.~Try~using~this~
+    command~before~the~hook~is~used.
+  }
+%    \end{macrocode}
+%
+%    \begin{macrocode}
 \msg_new:nnnn { hooks } { misused-top-level }
   {
     Illegal~use~of~\iow_char:N \\AddToHook{#1}[top-level]{...}.\\
diff --git a/base/testfiles-lthooks/github-0606.tlg b/base/testfiles-lthooks/github-0606.tlg
index 82f9eced..3ef2fb16 100644
--- a/base/testfiles-lthooks/github-0606.tlg
+++ b/base/testfiles-lthooks/github-0606.tlg
@@ -10,6 +10,15 @@ l. ...\NewHook{A}
 There already exists a hook declaration with this name.
 Please use a different name for your hook.
 B
+! LaTeX hooks Error: Cannot set a sorting rule for the already used hook 'A'.
+(hooks)              Try setting this rule earlier.
+For immediate help type H <return>.
+ ...                                              
+l. ...\DeclareHookRule{A}{b}{after}{c}
+You tried to set the ordering of hook 'A' using
+  \DeclareHookRule{A}{b}{after}{c}
+but hook 'A' was already used as a one-time hook, thus sorting is no longer
+possible. Try using this command before the hook is used.
 -> The hook 'A':
 > Code chunks:
 >     ---
diff --git a/base/testfiles-lthooks/lthooks-031.tlg b/base/testfiles-lthooks/lthooks-031.tlg
index bfbef442..faefacf4 100644
--- a/base/testfiles-lthooks/lthooks-031.tlg
+++ b/base/testfiles-lthooks/lthooks-031.tlg
@@ -1,2 +1,12 @@
 This is a generated file for the l3build validation system.
 Don't change this file in any respect.
+! LaTeX hooks Error: Cannot set a sorting rule for the already used hook
+(hooks)              'aaaa'.
+(hooks)              Try setting this rule earlier.
+For immediate help type H <return>.
+ ...                                              
+l. ...\hook_use_once:n {aaaa}
+You tried to set the ordering of hook 'aaaa' using
+  \DeclareHookRule{aaaa}{A}{before}{B}
+but hook 'aaaa' was already used as a one-time hook, thus sorting is no longer
+possible. Try using this command before the hook is used.





More information about the latex3-commits mailing list.