all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* How do I remove "reference to free variable" warnings on buffer-local variables?
@ 2009-11-09 15:10 rocky
  2009-11-09 16:45 ` Tassilo Horn
                   ` (3 more replies)
  0 siblings, 4 replies; 7+ messages in thread
From: rocky @ 2009-11-09 15:10 UTC (permalink / raw)
  To: help-gnu-emacs

I have code that uses buffer local variables. I don't want to declare
this variable global. So how can I remove messages of the form
"reference to free variable `...' " when I byte compile a file?

Thanks.


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

* Re: How do I remove "reference to free variable" warnings on buffer-local variables?
  2009-11-09 15:10 How do I remove "reference to free variable" warnings on buffer-local variables? rocky
@ 2009-11-09 16:45 ` Tassilo Horn
  2009-11-09 17:40   ` How do I remove "reference to free variable" warnings onbuffer-local variables? Drew Adams
       [not found] ` <mailman.10351.1257785158.2239.help-gnu-emacs@gnu.org>
                   ` (2 subsequent siblings)
  3 siblings, 1 reply; 7+ messages in thread
From: Tassilo Horn @ 2009-11-09 16:45 UTC (permalink / raw)
  To: help-gnu-emacs

rocky <rocky@gnu.org> writes:

> I have code that uses buffer local variables. I don't want to declare
> this variable global. So how can I remove messages of the form
> "reference to free variable `...' " when I byte compile a file?

This warning indicates, that this variable isn't defvar-ed somewhere in
the code you are compiling.  But you can supress those like it's stated
in the manual:

,----[ (info "(elisp)Compiler Errors") ]
|    You can tell the compiler that a function is defined using
| `declare-function' (*note Declaring Functions::).  Likewise, you can
| tell the compiler that a variable is defined using `defvar' with no
| initial value.
| 
|    You can suppress the compiler warning for a specific use of an
| undefined variable VARIABLE by conditionalizing its use on a `boundp'
| test, like this:
| 
|      (if (boundp 'VARIABLE) ...VARIABLE...)
`----

So in your case you could add something like

  (or (boundp 'some-variable) (defvar some-variable))

at the top of your file.

HTH,
Tassilo





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

* Re: How do I remove "reference to free variable" warnings on  buffer-local variables?
       [not found] ` <mailman.10351.1257785158.2239.help-gnu-emacs@gnu.org>
@ 2009-11-09 17:28   ` rocky
  0 siblings, 0 replies; 7+ messages in thread
From: rocky @ 2009-11-09 17:28 UTC (permalink / raw)
  To: help-gnu-emacs

On Nov 9, 11:45 am, Tassilo Horn <tass...@member.fsf.org> wrote:
> rocky <ro...@gnu.org> writes:
> > I have code that uses buffer local variables. I don't want to declare
> > this variable global. So how can I remove messages of the form
> > "reference to free variable `...' " when I byte compile a file?
>
> This warning indicates, that this variable isn't defvar-ed somewhere in
> the code you are compiling.  But you can supress those like it's stated
> in the manual:
>
> ,----[ (info "(elisp)Compiler Errors") ]
> |    You can tell the compiler that a function is defined using
> | `declare-function' (*note Declaring Functions::).  Likewise, you can
> | tell the compiler that a variable is defined using `defvar' with no
> | initial value.
> |
> |    You can suppress the compiler warning for a specific use of an
> | undefined variable VARIABLE by conditionalizing its use on a `boundp'
> | test, like this:
> |
> |      (if (boundp 'VARIABLE) ...VARIABLE...)
> `----
>
> So in your case you could add something like
>
>   (or (boundp 'some-variable) (defvar some-variable))

And since this is just a compile error, I suppose I can wrap this in
an (eval-when-compile).

>
> at the top of your file.
>
> HTH,
> Tassilo

But doesn't that define the variable globally? And that's not what I
want to do.

In some respects the warning is correct - I am accessing a free
variable. But I want to access a free variable, which I also want to
ensure is only buffer local. I suppose I just want to tell the
compiler that I expect this free variable to be there so don't give me
warnings about it.



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

* RE: How do I remove "reference to free variable" warnings onbuffer-local variables?
  2009-11-09 16:45 ` Tassilo Horn
@ 2009-11-09 17:40   ` Drew Adams
  2009-11-09 18:58     ` Tassilo Horn
  0 siblings, 1 reply; 7+ messages in thread
From: Drew Adams @ 2009-11-09 17:40 UTC (permalink / raw)
  To: 'Tassilo Horn', help-gnu-emacs

> ,----[ (info "(elisp)Compiler Errors") ]
> |    You can tell the compiler that a function is defined using
> | `declare-function' (*note Declaring Functions::).  Likewise, you can
> | tell the compiler that a variable is defined using `defvar' with no
> | initial value.
> | 
> |    You can suppress the compiler warning for a specific use of an
> | undefined variable VARIABLE by conditionalizing its use on 
> | a `boundp' test, like this:
> | 
> |      (if (boundp 'VARIABLE) ...VARIABLE...)
> `----
> 
> So in your case you could add something like
>   (or (boundp 'some-variable) (defvar some-variable))
> at the top of your file.

I don't think so - but I'm no expert on this.

`(defvar some-variable)' does not assign a value to the variable. It is used
(AFAIK) only to suppress a compiler warning. As such, there is no need and no
reason to put it behind `(boundp 'some-variable)'.

Since it does not assign a value, there is typically also no need to take
special measures for local values vs default value. Just use `(defvar
some-variable)' at the top level of a file, if you want to suppress the compiler
warning generally for that variable.

What that passage from the manual is saying, I think, is that you can use
`boundp' to suppress the warning only locally if you want, "for a specific use
of an undefined variable". In the example you gave, `defvar' doesn't really
constitute a "use" of the variable - or if it does, it is not a use that would
cause a warning.

An example of using `boundp' to suppress a warning would be `(if (boundp 'foo)
foo bar)'. Here, if `foo' is not bound, no warning is issued. If `bar' is
unbound, you will get the warning for `bar', since it is not protected by
`boundp'.

IOW, as an alternative to using `(defvar foo)', which suppresses all warnings
about foo being unbound, you can use `(boundp 'foo)' to suppress the unbound
warning for only a specific use of the variable. This warning suppression via
`boundp' was new with Emacs 22.

Again, I'm no expert on this. Others will no doubt correct me. But I'm pretty
sure that the example you gave is not useful: the `boundp' test is not needed,
and it doesn't do anything, since the "use" of the variable for which the
warning would be suppressed here is simply `(defvar some-variable)', which
doesn't issue a warning anyway.





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

* Re: How do I remove "reference to free variable" warnings on buffer-local variables?
  2009-11-09 15:10 How do I remove "reference to free variable" warnings on buffer-local variables? rocky
  2009-11-09 16:45 ` Tassilo Horn
       [not found] ` <mailman.10351.1257785158.2239.help-gnu-emacs@gnu.org>
@ 2009-11-09 17:40 ` Pascal J. Bourguignon
  2009-11-11  5:09 ` Stefan Monnier
  3 siblings, 0 replies; 7+ messages in thread
From: Pascal J. Bourguignon @ 2009-11-09 17:40 UTC (permalink / raw)
  To: help-gnu-emacs

rocky <rocky@gnu.org> writes:

> I have code that uses buffer local variables. I don't want to declare
> this variable global. So how can I remove messages of the form
> "reference to free variable `...' " when I byte compile a file?

A free variable is a variable that is used inside a function that is
not defined locally.  It must be considered a global variable.

As indicated in the other answers, if no such global variable is
declared, then you get this warning.

The point is that most often, you don't want a global variable
(otherwise you would have used defvar to define it), but you want a
local variable, and you used setq or setf instead of let.

Use let (or let*) to define local variables.  Instead of writing:

(defun equa2 (a b c)
   (setq delta (- (* b b) (* 4 a c)))
   (cond ((< delta 0)   '())
         ((= delta 0)   (list (/ (- b) 2 a)))
         (t             (list (/ (+ (- b) (sqrt delta)) 2 a)
                              (/ (- (- b) (sqrt delta)) 2 a)))))

write:

(defun equa2 (a b c)
   (let ((delta (- (* b b) (* 4 a c))))
       (cond ((< delta 0)   '())
             ((= delta 0)   (list (/ (- b) 2 a)))
             (t             (list (/ (+ (- b) (sqrt delta)) 2 a)
                                  (/ (- (- b) (sqrt delta)) 2 a))))))


In general, try to avoid setq or setf, and rather use the functional
programming style.


-- 
__Pascal Bourguignon__


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

* Re: How do I remove "reference to free variable" warnings onbuffer-local variables?
  2009-11-09 17:40   ` How do I remove "reference to free variable" warnings onbuffer-local variables? Drew Adams
@ 2009-11-09 18:58     ` Tassilo Horn
  0 siblings, 0 replies; 7+ messages in thread
From: Tassilo Horn @ 2009-11-09 18:58 UTC (permalink / raw)
  To: Drew Adams; +Cc: help-gnu-emacs

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

Hi Drew,

> Again, I'm no expert on this. Others will no doubt correct me. But I'm
> pretty sure that the example you gave is not useful: the `boundp' test
> is not needed, and it doesn't do anything, since the "use" of the
> variable for which the warning would be suppressed here is simply
> `(defvar some-variable)', which doesn't issue a warning anyway.

Yes, you are right.  And even if it was bound an additional defvar (even
with an init value) would do no harm.  So simply put a

  (defvar foo)

to suppress the compiler warnings after double-checking that the
warnings are only caused by the fact that the variable is undefined only
at compile time and not at runtime.

Bye,
Tassilo




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

* Re: How do I remove "reference to free variable" warnings on buffer-local variables?
  2009-11-09 15:10 How do I remove "reference to free variable" warnings on buffer-local variables? rocky
                   ` (2 preceding siblings ...)
  2009-11-09 17:40 ` Pascal J. Bourguignon
@ 2009-11-11  5:09 ` Stefan Monnier
  3 siblings, 0 replies; 7+ messages in thread
From: Stefan Monnier @ 2009-11-11  5:09 UTC (permalink / raw)
  To: help-gnu-emacs

> I have code that uses buffer local variables. I don't want to declare
> this variable global. So how can I remove messages of the form
> "reference to free variable `...' " when I byte compile a file?

There is a special use of defvar designed specifically for that
situation:

  (defvar <my-var-here>)

this will have no effect other than silence the byte-compiler.


        Stefan


PS: If you really really want to, you can put it inside
`eval-when-compile', in which case it may currently work by accident,
but it logically shouldn't work (and may not work in the future) since
(defvar <foo>) does nothing when evaluated, and `eval-when-compile' asks
the byte-compiler to eval its argument instead of byte-compiling it.


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

end of thread, other threads:[~2009-11-11  5:09 UTC | newest]

Thread overview: 7+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2009-11-09 15:10 How do I remove "reference to free variable" warnings on buffer-local variables? rocky
2009-11-09 16:45 ` Tassilo Horn
2009-11-09 17:40   ` How do I remove "reference to free variable" warnings onbuffer-local variables? Drew Adams
2009-11-09 18:58     ` Tassilo Horn
     [not found] ` <mailman.10351.1257785158.2239.help-gnu-emacs@gnu.org>
2009-11-09 17:28   ` How do I remove "reference to free variable" warnings on buffer-local variables? rocky
2009-11-09 17:40 ` Pascal J. Bourguignon
2009-11-11  5:09 ` Stefan Monnier

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.