unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Science to suppress compiler warnings
@ 2009-06-02 23:08 Xavier Maillard
  2009-06-02 23:36 ` Davis Herring
                   ` (2 more replies)
  0 siblings, 3 replies; 14+ messages in thread
From: Xavier Maillard @ 2009-06-02 23:08 UTC (permalink / raw)
  To: emacs-devel

Hi,

Elisp manual at "Compiler Errors" section (16.6) says we should
conditionalize variable use with a boundp test (same thing for
undefined function) but I find it very unpractical.

I thought (probably was wrong) that:

(eval-when-compile (defvar foo nil))

would do the trick. Is it the correct way to avoid warnings ?

Also while at it, here is what I got when compiling an old
package:

records.el:931:49:Warning: reference to free variable
`records-link-menu-map'

In records-mode:
records.el:1066:23:Warning: assignment to free variable
    `records-link-menu-map'

What is the difference between these two warnings exactly ?
Adding a defvar at the right place fixed that but to feed my
curiosity.

Regards,

Xavier




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-02 23:08 Science to suppress compiler warnings Xavier Maillard
@ 2009-06-02 23:36 ` Davis Herring
  2009-06-03  8:58   ` Xavier Maillard
  2009-06-02 23:57 ` Drew Adams
  2009-06-03  4:43 ` Stephen J. Turnbull
  2 siblings, 1 reply; 14+ messages in thread
From: Davis Herring @ 2009-06-02 23:36 UTC (permalink / raw)
  To: Xavier Maillard; +Cc: emacs-devel

> Elisp manual at "Compiler Errors" section (16.6) says we should
> conditionalize variable use with a boundp test (same thing for
> undefined function) but I find it very unpractical.
>
> I thought (probably was wrong) that:
>
> (eval-when-compile (defvar foo nil))
>
> would do the trick. Is it the correct way to avoid warnings ?

That should work if you have confidence that it will be defined (and
non-void) at runtime.  The [f]boundp, of course, will let you react to it
not existing then, if there's a real possibility (as with an old Emacs
version) of it not being there.

> Also while at it, here is what I got when compiling an old
> package:
>
> records.el:931:49:Warning: reference to free variable
> `records-link-menu-map'
>
> In records-mode:
> records.el:1066:23:Warning: assignment to free variable
>     `records-link-menu-map'
>
> What is the difference between these two warnings exactly ?
> Adding a defvar at the right place fixed that but to feed my
> curiosity.

The first is for reading, the second for writing; that's all.

Davis

-- 
This product is sold by volume, not by mass.  If it appears too dense or
too sparse, it is because mass-energy conversion has occurred during
shipping.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: Science to suppress compiler warnings
  2009-06-02 23:08 Science to suppress compiler warnings Xavier Maillard
  2009-06-02 23:36 ` Davis Herring
@ 2009-06-02 23:57 ` Drew Adams
  2009-06-03  0:51   ` Lennart Borgman
                     ` (3 more replies)
  2009-06-03  4:43 ` Stephen J. Turnbull
  2 siblings, 4 replies; 14+ messages in thread
From: Drew Adams @ 2009-06-02 23:57 UTC (permalink / raw)
  To: 'Xavier Maillard', emacs-devel

> (eval-when-compile (defvar foo nil))

(eval-when-compile (defvar foo))

You don't give it a value; you just declare it (to the compiler).

> Warning: reference to free variable `records-link-menu-map'
> Warning: assignment to free variable `records-link-menu-map'
> 
> What is the difference between these two warnings exactly ?

The first means the variable is used (referenced) somehow.
The second means that it is assigned to.

E.g., (setq foo bar) refers to variable `bar', and if it's free, then you get
the first warning. (setq bar 5) assigns `bar' a value, which will give you the
second warning (if `bar' is free).





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-02 23:57 ` Drew Adams
@ 2009-06-03  0:51   ` Lennart Borgman
  2009-06-03  5:35   ` Stefan Monnier
                     ` (2 subsequent siblings)
  3 siblings, 0 replies; 14+ messages in thread
From: Lennart Borgman @ 2009-06-03  0:51 UTC (permalink / raw)
  To: Drew Adams; +Cc: Xavier Maillard, emacs-devel

On Wed, Jun 3, 2009 at 1:57 AM, Drew Adams <drew.adams@oracle.com> wrote:
>> (eval-when-compile (defvar foo nil))
>
> (eval-when-compile (defvar foo))
>
> You don't give it a value; you just declare it (to the compiler).

If you check later for a library (which is probably what you normally
want to do as Drew said) you could also do:

  (eval-when-compile (require 'THELIB))

Then you probably have things in your the library you compile like

(defun my-defun ()
   (require 'THELIB nil t)
   (when (featurep 'THELIB) ...)




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Science to suppress compiler warnings
  2009-06-02 23:08 Science to suppress compiler warnings Xavier Maillard
  2009-06-02 23:36 ` Davis Herring
  2009-06-02 23:57 ` Drew Adams
@ 2009-06-03  4:43 ` Stephen J. Turnbull
  2009-06-03  9:58   ` Lennart Borgman
  2 siblings, 1 reply; 14+ messages in thread
From: Stephen J. Turnbull @ 2009-06-03  4:43 UTC (permalink / raw)
  To: Xavier Maillard; +Cc: emacs-devel

Xavier Maillard writes:

 > Elisp manual at "Compiler Errors" section (16.6) says we should
 > conditionalize variable use with a boundp test (same thing for
 > undefined function) but I find it very unpractical.

If this is related to your work on supporting old versions of Emacs,
my advice is "live with it".  Either use the runtime test if it's an
Emacs-defined variable, or use an appropriate `defvar' or `defconst'
to initialize the variable.

Suppressing compiler warnings is very likely to lead to runtime
errors, often intermittent ones.  Eg, there may be an unusual code
path that leads to reference to a void variable very early in an Emacs
session.  Boom!  But unless you type that exact sequence of commands
immediately after starting Emacs, something requires the relevant
library, and no problem can be found.

Such bugs are quite hard to diagnose, even to localize, if the user is
not an Emacs expert.  A boundp check with a good error message, or a
proper initialization with `defvar', will prevent or at least help
diagnose a lot of problems.

 > I thought (probably was wrong) that:
 > 
 > (eval-when-compile (defvar foo nil))
 > 
 > would do the trick. Is it the correct way to avoid warnings ?

If you don't understand what this does, I have to recommend avoiding
this trick.  It's a dangerous optimization, not a fix.

This kind of warning suppression is a *white lie* to Lisp.  If you
know what you're doing, and you're desperate for space savings, it may
be useful.  Otherwise, you're just asking for trouble.  Just defvar at
loadtime (costless) or require the defining library (usually at a
fairly small space cost).




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-02 23:57 ` Drew Adams
  2009-06-03  0:51   ` Lennart Borgman
@ 2009-06-03  5:35   ` Stefan Monnier
  2009-06-03  9:04     ` Xavier Maillard
  2009-06-03 14:11     ` Drew Adams
  2009-06-03  8:40   ` Andreas Schwab
  2009-06-03  8:59   ` Xavier Maillard
  3 siblings, 2 replies; 14+ messages in thread
From: Stefan Monnier @ 2009-06-03  5:35 UTC (permalink / raw)
  To: Drew Adams; +Cc: 'Xavier Maillard', emacs-devel

>> (eval-when-compile (defvar foo nil))
> (eval-when-compile (defvar foo))

Aka just

  (defvar foo)


-- Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-02 23:57 ` Drew Adams
  2009-06-03  0:51   ` Lennart Borgman
  2009-06-03  5:35   ` Stefan Monnier
@ 2009-06-03  8:40   ` Andreas Schwab
  2009-06-03 12:26     ` Xavier Maillard
  2009-06-03  8:59   ` Xavier Maillard
  3 siblings, 1 reply; 14+ messages in thread
From: Andreas Schwab @ 2009-06-03  8:40 UTC (permalink / raw)
  To: Drew Adams; +Cc: 'Xavier Maillard', emacs-devel

"Drew Adams" <drew.adams@oracle.com> writes:

>> (eval-when-compile (defvar foo nil))
>
> (eval-when-compile (defvar foo))
>
> You don't give it a value; you just declare it (to the compiler).

You don't even need eval-when-compile, since toplevel defvars are always
seen by the compiler, and a defvar without a value never sets the
variable.

Andreas.

-- 
Andreas Schwab, schwab@linux-m68k.org
GPG Key fingerprint = 58CA 54C7 6D53 942B 1756  01D3 44D5 214B 8276 4ED5
"And now for something completely different."




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-02 23:36 ` Davis Herring
@ 2009-06-03  8:58   ` Xavier Maillard
  0 siblings, 0 replies; 14+ messages in thread
From: Xavier Maillard @ 2009-06-03  8:58 UTC (permalink / raw)
  To: herring; +Cc: Xavier Maillard, emacs-devel

At Tue, 2 Jun 2009 16:36:22 -0700 (PDT),
Davis Herring wrote:

> > records.el:931:49:Warning: reference to free variable
> > `records-link-menu-map'
> >
> > In records-mode:
> > records.el:1066:23:Warning: assignment to free variable
> >     `records-link-menu-map'
> >
> > What is the difference between these two warnings exactly ?
> > Adding a defvar at the right place fixed that but to feed my
> > curiosity.
> 
> The first is for reading, the second for writing; that's all.

Yup, I posted this one too quickly (I should have read it mor
carefully before posting).




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-02 23:57 ` Drew Adams
                     ` (2 preceding siblings ...)
  2009-06-03  8:40   ` Andreas Schwab
@ 2009-06-03  8:59   ` Xavier Maillard
  3 siblings, 0 replies; 14+ messages in thread
From: Xavier Maillard @ 2009-06-03  8:59 UTC (permalink / raw)
  To: Drew Adams; +Cc: 'Xavier Maillard', emacs-devel

At Tue, 2 Jun 2009 16:57:10 -0700,
Drew Adams wrote:
> 
> > (eval-when-compile (defvar foo nil))
> 
> (eval-when-compile (defvar foo))
> 
> You don't give it a value; you just declare it (to the compiler).

Good catch, thank you.




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-03  5:35   ` Stefan Monnier
@ 2009-06-03  9:04     ` Xavier Maillard
  2009-06-03 17:33       ` Stefan Monnier
  2009-06-03 14:11     ` Drew Adams
  1 sibling, 1 reply; 14+ messages in thread
From: Xavier Maillard @ 2009-06-03  9:04 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: 'Xavier Maillard', Drew Adams, emacs-devel

At Wed, 03 Jun 2009 01:35:11 -0400,
Stefan Monnier wrote:
> 
> >> (eval-when-compile (defvar foo nil))
> > (eval-when-compile (defvar foo))
> 
> Aka just
> 
>   (defvar foo)

Could you explain to me why ? When does one want to use
(eval-when-compile ...) ? I know at least one for sure, macro
compilation, but why not for variables or even functions ?

Is the byte-compilation process and/or eval-* functions well
covered somewhere in the emacs documentation ? I read a lot,
googled a lot but nothing really helpful (even the elisp manual
is pretty poor on that).

Thank you,

Xavier




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-03  4:43 ` Stephen J. Turnbull
@ 2009-06-03  9:58   ` Lennart Borgman
  0 siblings, 0 replies; 14+ messages in thread
From: Lennart Borgman @ 2009-06-03  9:58 UTC (permalink / raw)
  To: Stephen J. Turnbull; +Cc: Xavier Maillard, emacs-devel

On Wed, Jun 3, 2009 at 6:43 AM, Stephen J. Turnbull <stephen@xemacs.org> wrote:
> Xavier Maillard writes:
>
>  > Elisp manual at "Compiler Errors" section (16.6) says we should
>  > conditionalize variable use with a boundp test (same thing for
>  > undefined function) but I find it very unpractical.
>
> If this is related to your work on supporting old versions of Emacs,
> my advice is "live with it".  Either use the runtime test if it's an
> Emacs-defined variable, or use an appropriate `defvar' or `defconst'
> to initialize the variable.
>
> Suppressing compiler warnings is very likely to lead to runtime
> errors, often intermittent ones.  Eg, there may be an unusual code
> path that leads to reference to a void variable very early in an Emacs
> session.  Boom!  But unless you type that exact sequence of commands
> immediately after starting Emacs, something requires the relevant
> library, and no problem can be found.
>
> Such bugs are quite hard to diagnose, even to localize, if the user is
> not an Emacs expert.  A boundp check with a good error message, or a
> proper initialization with `defvar', will prevent or at least help
> diagnose a lot of problems.


But if you combine (defvar foo) with tests everywhere foo is used you
should be safe ...




^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-03  8:40   ` Andreas Schwab
@ 2009-06-03 12:26     ` Xavier Maillard
  0 siblings, 0 replies; 14+ messages in thread
From: Xavier Maillard @ 2009-06-03 12:26 UTC (permalink / raw)
  To: Andreas Schwab; +Cc: 'Xavier Maillard', Drew Adams, emacs-devel

At Wed, 03 Jun 2009 10:40:20 +0200,
Andreas Schwab wrote:
> 
> "Drew Adams" <drew.adams@oracle.com> writes:
> 
> >> (eval-when-compile (defvar foo nil))
> >
> > (eval-when-compile (defvar foo))
> >
> > You don't give it a value; you just declare it (to the compiler).
> 
> You don't even need eval-when-compile, since toplevel defvars are always
> seen by the compiler, and a defvar without a value never sets the
> variable.

Thank you that answers my other question to Stefan.

Xavier




^ permalink raw reply	[flat|nested] 14+ messages in thread

* RE: Science to suppress compiler warnings
  2009-06-03  5:35   ` Stefan Monnier
  2009-06-03  9:04     ` Xavier Maillard
@ 2009-06-03 14:11     ` Drew Adams
  1 sibling, 0 replies; 14+ messages in thread
From: Drew Adams @ 2009-06-03 14:11 UTC (permalink / raw)
  To: 'Stefan Monnier'; +Cc: 'Xavier Maillard', emacs-devel

> >> (eval-when-compile (defvar foo nil))
> > (eval-when-compile (defvar foo))
> 
> Aka just (defvar foo)

Oops, yes, thanks.

> You don't even need eval-when-compile, since toplevel defvars 
> are always seen by the compiler, and a defvar without a value
> never sets the variable.





^ permalink raw reply	[flat|nested] 14+ messages in thread

* Re: Science to suppress compiler warnings
  2009-06-03  9:04     ` Xavier Maillard
@ 2009-06-03 17:33       ` Stefan Monnier
  0 siblings, 0 replies; 14+ messages in thread
From: Stefan Monnier @ 2009-06-03 17:33 UTC (permalink / raw)
  To: Xavier Maillard; +Cc: Drew Adams, emacs-devel

>> >> (eval-when-compile (defvar foo nil))
>> > (eval-when-compile (defvar foo))
>> Aka just
>> (defvar foo)
> Could you explain to me why ?

Because (defvar FOO) is defined to do exactly that: tell the compiler to
shut up, while not affecting the runtime at all.


        Stefan




^ permalink raw reply	[flat|nested] 14+ messages in thread

end of thread, other threads:[~2009-06-03 17:33 UTC | newest]

Thread overview: 14+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-06-02 23:08 Science to suppress compiler warnings Xavier Maillard
2009-06-02 23:36 ` Davis Herring
2009-06-03  8:58   ` Xavier Maillard
2009-06-02 23:57 ` Drew Adams
2009-06-03  0:51   ` Lennart Borgman
2009-06-03  5:35   ` Stefan Monnier
2009-06-03  9:04     ` Xavier Maillard
2009-06-03 17:33       ` Stefan Monnier
2009-06-03 14:11     ` Drew Adams
2009-06-03  8:40   ` Andreas Schwab
2009-06-03 12:26     ` Xavier Maillard
2009-06-03  8:59   ` Xavier Maillard
2009-06-03  4:43 ` Stephen J. Turnbull
2009-06-03  9:58   ` Lennart Borgman

Code repositories for project(s) associated with this public inbox

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

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).