[Top][All Lists]

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

Re: Byte compiler and eval-when-compile

From: Stefan Monnier
Subject: Re: Byte compiler and eval-when-compile
Date: Mon, 15 Oct 2012 14:41:26 -0400
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.2.50 (gnu/linux)

> Somehow the byte compiler got smart during the last days (I think it's
> the change in rev. 110510).

Actually, it just recovered the smartness I added many years ago and
which got broken years ago as well.

> I now get a lot more 'function X might not be defined at runtime'
> warnings than before.

That's expected.

> In CEDET, we often use `require' statements in function bodies, like
> this:

> (defun test()
>   (require 'eldoc)
>   (message "%s" (eldoc-function-argstring '("foo" "bar"))))

> The reason is simply to only do the require when it is actually needed,
> so that startup time is reduced.
> Still, if you byte-compile the above, you'll get a 'might not be
> defined at runtime' warning for `eldoc-function-argstring'.

Yes, that's an annoying case.

> I used to circumvent that problem by simply doing
> (eval-when-compile
>   (require 'eldoc))
> Before you scream at me: I *know* this is not what `eval-when-compile'
> is for, but it has worked until a few days ago.  It seems the
> byte-compiler now sees that I'm actually using a function from eldoc,
> but he still doesn't see that I'm requiring the package it in the
> function body.

If you both do the (eval-when-compile (require 'eldoc)) and the
`require', the byte-compiler could be smarter indeed: when it sees the
inner `require' call, it could check load-history and mark all functions
defined therein as being fine for the current scope.

> Do I now really have to use `declare-function' for all those cases?

Currently, yes (or use an fboundp test, which you probably won't like
any better).

I think a good solution to such cases would be to add a `lazy-require':
when interpreted, it works like `require', but the byte-compiler will
turn it into a bunch of autoloads.

A simpler solution might be to provide a new (funcall-require PACKAGE
FUNCTION &rest ARGS), so you'd do

   (funcall-require 'eldoc #'eldoc-function-argstring '("foo" "bar"))

but the compiler could still be taught to check that
eldoc-function-argstring indeed exists in eldoc and accepts being called
with a single argument.

-- Stefan

reply via email to

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