Subject: Re: bug#72511: 30.0.50; prefix-completions is always nil in help--symbol-completion-table

From: Stefan Monnier <[email-masked]>
Date: Wed, 14 Aug 2024 11:50:47 -0400

> (when help-enable-completion-autoload
>   (let ((prefixes (radix-tree-prefixes (help-definition-prefixes) string)))
>     (help--load-prefixes prefixes)))
> (let ((prefix-completions
>        (and help-enable-completion-autoload
>             (mapcar #'intern (all-completions string definition-prefixes)))))
>
> By default, `help-enable-completion-autoload' is t, so this code should
> normally run. The perceived problem here is that the definition of
> `help-definition-prefixes' function is as follows:
>
> (defun help-definition-prefixes ()
>   "Return the up-to-date radix-tree form of `definition-prefixes'."
>   (when (> (hash-table-count definition-prefixes) 0)
>     (maphash (lambda (prefix files)
>                (let ((old (radix-tree-lookup help-definition-prefixes prefix)))
>                  (setq help-definition-prefixes
>                        (radix-tree-insert help-definition-prefixes
>                                           prefix (append old files)))))
>              definition-prefixes)
>     (clrhash definition-prefixes))
>   help-definition-prefixes)
>
> Because of the `clrhash', `definition-prefixes' will always be empty
> after the function call to `help-definition-prefixes'.

Duh, indeed.
IIRC it's a leftover from some older version of the code.
I think the patch below is in order.

> Also, was clearing out the entirety of `definition-prefixes' on completion
> really the intended behavior?

Originally, yes (after which we'd just use the `help-definition-prefixes`
radix-tree instead), but then other places appeared where using the
radix-tree was not convenient.

        Stefan

diff --git a/lisp/help-fns.el b/lisp/help-fns.el
index 0a469a1fd6d..e3dc23036db 100644
--- a/lisp/help-fns.el
+++ b/lisp/help-fns.el
@@ -86,14 +86,14 @@ help-definition-prefixes
 
 (defun help-definition-prefixes ()
   "Return the up-to-date radix-tree form of `definition-prefixes'."
-  (when (> (hash-table-count definition-prefixes) 0)
+  (when (and (null help-definition-prefixes)
+             (> (hash-table-count definition-prefixes) 0))
     (maphash (lambda (prefix files)
                (let ((old (radix-tree-lookup help-definition-prefixes prefix)))
                  (setq help-definition-prefixes
                        (radix-tree-insert help-definition-prefixes
                                           prefix (append old files)))))
-             definition-prefixes)
-    (clrhash definition-prefixes))
+             definition-prefixes))
   help-definition-prefixes)
 
 (defun help--loaded-p (file)