emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Do we need C's extern in Emacs Lisp?


From: Stefan Monnier
Subject: Re: Do we need C's extern in Emacs Lisp?
Date: Thu, 31 Mar 2022 17:16:24 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/29.0.50 (gnu/linux)

> I was wrong about the "subsequent use ... generate an unknown variable
> warning.".  Sorry.  The problem happened during a more unusual
> compilation situation best explained by the following comment from
> cc-fonts.el:
>
>     ;; Need to declare these local symbols during compilation since
>     ;; they're referenced from lambdas in `byte-compile' calls that are
>     ;; executed at compile time.  They don't need to have the proper
>     ;; definitions, though, since the generated functions aren't called
>     ;; during compilation.
>
> The "need to declare" means "need to give the value/function cell of the
> symbol a binding", because the byte compiler seems to use boundp and
> fboundp to check whether to emit "not known" warnings.

Explicit calls to `byte-compile` are rather unusual, indeed, and since
declarations like (defvar FOO) are supposed to affect the code only in
the current lexical scope they can't be applied to this use case since
calls to `byte-compile` are dynamic and thus not easily associated with
a particular lexical scope.

Arguably we should be able to provide some kind of "environment"
argument to `byte-compile`, but for your use case, maybe the better
option is to wrap the code that you pass to `byte-compile` along the
lines of

    (defun my-byte-compile (form vars funs)
      (eval
       (byte-compile
        `(progn
          ,@(mapcar (lambda (v) `(defvar ,v)) vars)
          ,@(mapcar (lambda (f) `(declare-function ,f nil)) funs)
          ,form))))

> "This variable/function is defined somewhere" is a natural thing to want
> to say.  The (defvar foo) and (declare-function foo nil) don't say this,
> exactly.

Those declarations don't just say "defined somewhere" but something
closer to "will be defined somehow by the time we get here" (tho
admittedly, it's a lot more murky).  This is usually more useful since
it justifies removal of the warning by promising that the corresponding
runtime error won't happen.

It's less clear what we can do with just "this variable/function is
defined somewhere" since it doesn't do us much good if it's defined in
a file that never gets loaded.


        Stefan




reply via email to

[Prev in Thread] Current Thread [Next in Thread]