all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: Alan Mackenzie <acm@muc.de>
Cc: emacs-devel@gnu.org
Subject: Re: Do we need C's extern in Emacs Lisp?
Date: Thu, 31 Mar 2022 17:16:24 -0400	[thread overview]
Message-ID: <jwvo81lygxp.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <YkYWOMYz1AGQIN7r@ACM> (Alan Mackenzie's message of "Thu, 31 Mar 2022 20:59:36 +0000")

> 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	other threads:[~2022-03-31 21:16 UTC|newest]

Thread overview: 5+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2022-03-29 17:45 Do we need C's extern in Emacs Lisp? Alan Mackenzie
2022-03-29 21:09 ` Stefan Monnier
2022-03-31 20:59   ` Alan Mackenzie
2022-03-31 21:16     ` Stefan Monnier [this message]
2022-03-31  4:26 ` Richard Stallman

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=jwvo81lygxp.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=acm@muc.de \
    --cc=emacs-devel@gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.