Subject: bug#64232: 28.2; Incorrect(?) byte compilation warning about ‘ansi-color-apply-on-region’

From: Eli Zaretskii <[email-masked]>
Date: Sat, 24 Jun 2023 14:49:43 +0300

> Date: Thu, 22 Jun 2023 15:56:18 -0400
> From: Zack Weinberg <[email-masked]>
> 
> 
> With Emacs 28.2, this test .el file
> 
> ```
> ;; -*- lexical-binding: t; coding: utf-8 -*-
> 
> (declare-function ansi-color-apply-on-region "ansi-color"
>                   (begin end &optional preserve-sequences))
> (autoload 'ansi-color-apply-on-region "ansi-color")
> 
> (eval-when-compile
>   (check-declare-file byte-compile-current-file))
> 
> (defun zw-colorize-compilation-buffer ()
>   "Interpret ANSI color escapes in a compilation-mode buffer, which
> gets them out of the way of the error detector.  Intended for use via
> compilation-filter-hook."
>   (interactive)
>   (let ((inhibit-read-only t))
>     (ansi-color-apply-on-region (point-min) (point-max))))
> ```
> 
> provokes a byte compilation warning:
> 
> ```
> $ emacs -Q --batch -f batch-byte-compile test.el
> uncompressing ansi-color.el.gz...
> uncompressing ansi-color.el.gz...done
> 
> In end of data:
> test.el:16:6: Warning: the function ‘ansi-color-apply-on-region’ might not be
>     defined at runtime.
> ```
> 
> The combination of `declare-function` and `autoload` is intended to
> ensure both that the byte compiler understands what’s going on, and
> that `ansi-color-apply-on-region` really is guaranteed to be available
> at runtime.  Also, I triple-checked that ansi-color.el really does
> define this function.  Why is the compiler still complaining?

The problem is this part:

  (eval-when-compile
    (check-declare-file byte-compile-current-file))

Remove it, and the problem will go away.  So there's actually no
problem here with 'autoload' and 'declare-function'.

I don't understand why you are calling check-declare-file, and
moreover don't understand the argument you pass check-declare-file.
check-declare-file is a command supposed to be used interactively to
verify the 'declare' calls are valid in a given file; it shows the
results, if any, as delayed-warnings.  Can you explain why you do this
in the eval-when-compile form?