3

This question is motivated by a question about the "void function" error on cl-loop in the init file.

The following test shows that (require 'cl-lib) is not needed for byte-compilation:

  • Assume you have a file ~/tmp/test.el with the following content:

    (defun test ()
      (cl-loop for i from 1 to 2 do
           (message "Looping %i." i)))
    
  • Start Emacs 26.3 with emacs -Q.
  • Run M-: (byte-compile-file "~/tmp/test.el"). Byte compilation works without errors.
  • Run M-x load-file RET ~/tmp/test.el RET.
    The file is loaded and gives the message

    Looping 1.
    Looping 2.
    

But cl-macs is not loaded by default as the "void function" error on evaluating cl-loop shows.

The cl info manual says that cl-lib.el loads cl-loaddefs.el. This is done by a (load "cl-loaddefs" 'noerror 'quiet) in the code.

But in the test code above there is no (require 'cl-lib). How gets cl-macs loaded at byte-compilation?

I already noted the (register-definition-prefixes "cl-macs" '("cl-")) in loaddefs.el.
The help string on register-definition-prefixes just says: Register that FILE uses PREFIXES., which is as much as nothing.

The info manual does also not contain any information about register-definition-prefixes.
Might it be that that command causes the autoloading at byte-compilation?

A look into register-definition-prefixes shows that it registers the prefix in the variable definition-prefixes.

The doc-string of definition-prefixes says:

Hash table mapping prefixes to the files in which they're used.
This can be used to automatically fetch not-yet-loaded definitions.
More specifically, if there is a value of the form (FILES...) for
a string PREFIX it means that the FILES define variables or functions
with names that start with PREFIX.

Note that it does not imply that all definitions starting with PREFIX can
be found in those files.  E.g. if prefix is "gnus-article-" there might
still be definitions of the form "gnus-article-toto-titi" in other files,
which would presumably appear in this table under another prefix such as
"gnus-" or "gnus-article-toto-".

Hm, "This can be used to automatically fetch not-yet-loaded definitions." seems to be a strong indication that register-definition-prefixes could play a role in the autoloading of cl-macs on byte-compiling code that contains cl-loop. But, the doc string does not say anything definite.

Tobias
  • 32,569
  • 1
  • 34
  • 75

2 Answers2

3

byte-compile-file comes from bytecomp.el which (require 'cl-lib), thus cl-lib is always already loaded before any byte compilation.

The info manual does also not contain any information about register-definition-prefixes. Might it be that that command causes the autoloading at byte-compilation?

According to my understanding, that's a feature introduced in Emacs 26.3, you should be able to find more info in the NEWS file, that is, loaddefs.el contains

(register-definition-prefixes "cl-lib" '("cl-"))

then when you type C-h f cl- and TAB for completion, Emacs will automatically load cl-lib.el. I think it is too aggressive for me, thus I disable it:

(setq help-enable-completion-auto-load nil)
xuchunyang
  • 14,302
  • 1
  • 18
  • 39
  • I think something like that should be mentioned in the manual just to be sure that this is a feature one can rely on. I just searched the Elisp info manual for `cl-lib`. It does not mention it in connection with byte-compilation. – Tobias Feb 24 '20 at 07:38
3

This is a bug (will be fixed in 27.1); https://debbugs.gnu.org/30635

  • Hm. Interesting. Why is that classified as a bug? From my perspective the other way out would have been a proper statement in the Elisp manual that `cl-lib` is preloaded at compilation time. – Tobias Feb 24 '20 at 21:02