Subject: 28.2; Byte-compiler warning for free variable

From: Drew Adams <[email-masked]>
Date: Sun, 25 Dec 2022 17:07:55 +0000

You might not consider this a bug, or you might consider it not worth
trying to fix.  That's fine by me.  I think it's worth reporting, at
least.  Maybe something can be done for it, or maybe you can let me know
a good way to work around it.

Unlike `defvar', `defconst', alters the value each time it's evaluated.
E.g., if you load a file with (defconst foo bar "...") more than once
then the value of `foo' gets set each time to whatever is the current
value of `bar'.  That's normal and desirable (and consistent with other
Lisps, such as CL).

How to ensure that a defconst sets the value only once, when the file
it's in is loaded?  A defvar does that, but a defconst does not.

Maybe there's a better way, but I've been using this so far:

  (unless (boundp 'foo) (defconst foo bar "..."))

But that results in this byte-compiler warning:

 In blah-fun:
 toto.el:236:34:Warning: reference to free variable
     `foo'

An alternative is to use a defvar and just say in its doc string that
it's intended as a constant (don't set it etc.).  Maybe that's less
confusing to users than seeing the warning?  Dunno.

In GNU Emacs 28.2 (build 2, x86_64-w64-mingw32)
 of 2022-09-13 built on AVALON
Windowing system distributor 'Microsoft Corp.', version 10.0.19044
System Description: Microsoft Windows 10 Pro (v10.0.2009.19044.2364)

Configured using:
 'configure --with-modules --without-dbus --with-native-compilation
 --without-compress-install CFLAGS=-O2'

Configured features:
ACL GIF GMP GNUTLS HARFBUZZ JPEG JSON LCMS2 LIBXML2 MODULES NATIVE_COMP
NOTIFY W32NOTIFY PDUMPER PNG RSVG SOUND THREADS TIFF TOOLKIT_SCROLL_BARS
XPM ZLIB

(NATIVE_COMP present but libgccjit not available)