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

From: Zack Weinberg <[email-masked]>
Date: Thu, 22 Jun 2023 15:56:18 -0400

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?

Analogous constructs work as expected for other functions that might
not yet be loaded when their caller is evaluated, e.g. this very
similar test file

```
;; -*- lexical-binding: t; coding: utf-8 -*-

(declare-function dired-get-marked-files "dired"
                  (&optional localp arg filter distinguish-one-marked error))
(autoload 'dired-get-marked-files "dired")

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

(defun zw-dired-find-marked-files (&optional arg)
  "Open each of the marked files, or the file under the point, or
with prefix arg, the next N files."
  (interactive "P")
  (mapc 'find-file (dired-get-marked-files nil arg)))
```

byte compiles without complaint.

zw

---
In GNU Emacs 28.2 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.37, cairo version 1.16.0)
Windowing system distributor 'The X.Org Foundation', version 11.0.12101007
System Description: Debian GNU/Linux 12 (bookworm)

Configured using:
 'configure
 CONFIG_SHELL=/gnu/store/rib9g2ig1xf3kclyl076w28parmncg4k-bash-minimal-5.1.16/bin/bash
 SHELL=/gnu/store/rib9g2ig1xf3kclyl076w28parmncg4k-bash-minimal-5.1.16/bin/bash
 --prefix=/gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2
 --enable-fast-install --with-modules --with-cairo
 --with-native-compilation --disable-build-details'

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ
JPEG JSON LCMS2 LIBOTF LIBSELINUX LIBSYSTEMD LIBXML2 M17N_FLT MODULES
NATIVE_COMP NOTIFY INOTIFY PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF
TOOLKIT_SCROLL_BARS X11 XDBE XIM XPM GTK3 ZLIB

Important settings:
  value of $EMACSLOADPATH: /home/zack/.guix-profile/share/emacs/site-lisp:/gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp
  value of $LC_COLLATE: C
  value of $LANG: en_US.UTF-8
  locale-coding-system: utf-8-unix

Major mode: Fundamental

Minor modes in effect:
  envrc-global-mode: t
  envrc-mode: t
  doom-modeline-mode: t
  selectrum-prescient-mode: t
  prescient-persist-mode: t
  editorconfig-mode: t
  override-global-mode: t
  typo-global-mode: t
  global-whitespace-mode: t
  global-auto-revert-mode: t
  selectrum-mode: t
  tooltip-mode: t
  global-eldoc-mode: t
  show-paren-mode: t
  mouse-wheel-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
/home/zack/.config/emacs/elpa/transient-20230602.2121/transient hides /gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp/transient
/home/zack/.config/emacs/elpa/jsonrpc-1.0.17/jsonrpc hides /gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp/jsonrpc
/home/zack/.config/emacs/elpa/xref-1.6.3/xref hides /gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp/progmodes/xref
/home/zack/.config/emacs/elpa/project-0.9.8/project hides /gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp/progmodes/project
/home/zack/.config/emacs/elpa/flim-20230205.1423/sasl hides /gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp/net/sasl
/home/zack/.config/emacs/elpa/eldoc-1.14.0/eldoc hides /gnu/store/gvbp7lp6yl3gsmz89kwajds2mh5179r5-emacs-28.2/share/emacs/28.2/lisp/emacs-lisp/eldoc

Features:
(shadow flyspell ispell modb-legacy modb-standard elmo-passwd gnutls
network-stream nsm utf-7 elmo-imap4 time-stamp zw-wl-customization
wl-fldmgr wl-template wl-demo wl-thread wl-action wl wl-draft
wl-folder elmo-nntp elmo-filter wl-summary wl-refile wl-message
wl-mime mime-play filename wl-e21 wl-highlight elmo-mime mmelmo-buffer
mmelmo-imap mmimap mmbuffer mmgeneric wl-address wl-util pp wl-vars
wl-version elmo-net elmo-cache elmo-map elmo-dop modb-generic
elmo-flag elmo-localdir elmo elmo-signal elmo-msgdb modb modb-entity
elmo-date elmo-util elmo-vars elmo-version mime-edit mime-image
mime-view mime-conf calist invisible inv-23 mime-setup mail-mime-setup
semi-setup advice semi-def mime-parse mime luna eword-encode
eword-decode mel path-util pces pces-e20 pces-20 mime-def alist
mcs-e20 mcs-20 wid-edit mcharset std11 pccl pccl-20 broken static
apel-ver product timezone emacsbug message rmc puny dired
dired-loaddefs rfc822 mml mml-sec epa derived epg rfc6068 epg-config
gnus-util rmail rmail-loaddefs time-date mm-decode mm-bodies mm-encode
mail-parse rfc2231 mailabbrev gmm-utils mailheader sendmail rfc2047
rfc2045 ietf-drums mm-util mail-prsvr mail-utils face-remap pulse
envrc inheritenv ansi-color server all-the-icons all-the-icons-faces
data-material data-weathericons data-octicons data-fileicons
data-faicons data-alltheicons doom-modeline doom-modeline-segments
doom-modeline-env doom-modeline-core shrink-path f f-shortdoc shortdoc
text-property-search s dash nerd-icons nerd-icons-faces
nerd-icons-data nerd-icons-data-mdicon nerd-icons-data-flicon
nerd-icons-data-codicon nerd-icons-data-devicon
nerd-icons-data-sucicon nerd-icons-data-wicon nerd-icons-data-faicon
nerd-icons-data-powerline nerd-icons-data-octicon
nerd-icons-data-pomicon nerd-icons-data-ipsicon compat compat-29
edmacro kmacro selectrum-prescient selectrum minibuf-eldef crm
prescient char-fold comp comp-cstr warnings editorconfig
editorconfig-core editorconfig-core-handle editorconfig-fnmatch pcase
solarized-light-theme solarized-palettes solarized solarized-faces
color zw-functions use-package use-package-ensure use-package-delight
use-package-diminish use-package-bind-key bind-key easy-mmode
use-package-core package browse-url url url-proxy url-privacy
url-expand url-methods url-history url-cookie url-domsuf url-util
mailcap url-handlers url-parse auth-source cl-seq eieio eieio-core
cl-macs eieio-loaddefs password-cache json subr-x map url-vars
cl-extra help-mode typo whitespace autorevert filenotify cl-loaddefs
cl-lib cus-load guix-emacs seq byte-opt gv bytecomp byte-compile cconv
info rx iso-transl tooltip eldoc paren electric uniquify ediff-hook
vc-hooks lisp-float-type elisp-mode mwheel term/x-win x-win
term/common-win x-dnd tool-bar dnd fontset image regexp-opt fringe
tabulated-list replace newcomment text-mode lisp-mode prog-mode
register page tab-bar menu-bar rfn-eshadow isearch easymenu timer
select scroll-bar mouse jit-lock font-lock syntax font-core
term/tty-colors frame minibuffer cl-generic cham georgian utf-8-lang
misc-lang vietnamese tibetan thai tai-viet lao korean japanese
eucjp-ms cp51932 hebrew greek romanian slovak czech european ethiopic
indian cyrillic chinese composite emoji-zwj charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
cairo move-toolbar gtk x-toolkit x multi-tty make-network-process
native-compile emacs)

Memory information:
((conses 16 326012 10065)
 (symbols 48 22124 7)
 (strings 32 100305 5994)
 (string-bytes 1 2514246)
 (vectors 16 38478)
 (vector-slots 8 876336 24689)
 (floats 8 741 311)
 (intervals 56 365 0)
 (buffers 992 10))