GNU bug report logs - #33828
26.1; Unbound defvar across compilation units

Previous Next

Package: emacs;

Reported by: Luís Oliveira <luismbo <at> gmail.com>

Date: Fri, 21 Dec 2018 16:23:02 UTC

Severity: minor

Tags: notabug

Found in version 26.1

Done: Glenn Morris <rgm <at> gnu.org>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 33828 in the body.
You can then email your comments to 33828 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Fri, 21 Dec 2018 16:23:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Luís Oliveira <luismbo <at> gmail.com>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Fri, 21 Dec 2018 16:23:04 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Luís Oliveira <luismbo <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Cc: João Távora <joaotavora <at> gmail.com>
Subject: 26.1; Unbound defvar across compilation units
Date: Fri, 21 Dec 2018 12:02:55 +0000
Hello,

I think I've come across a bug.

    $ cat f1.el
    ;;; f1.el ---                                        -*-
lexical-binding: t; -*-
    ;;; Lexical binding is set to T, but the bug happens regardless.

    (defvar my-special)

    (provide 'f1)


    $ cat f2.el
    ;;; f2.el ---                                        -*-
lexical-binding: t; -*-

    (require 'f1)

    (defun f2-foo ()
      (let ((my-special 123))
        (f2-bar)))

    (defun f2-bar ()
      my-special)

    (print (f2-bar))



    $ emacs -Q -L . -f batch-byte-compile f2.el

    In toplevel form:
    f2.el:5:1:Warning: Unused lexical variable `my-special'

    In f2-bar:
    f2.el:10:3:Warning: reference to free variable `my-special'


Also:

    $ emacs -Q -L . -batch -l f2.el
    Symbol's value as variable is void: my-special


Changing f1.el's defvar to have an initial value makes the problem go
away, but this behaviour was unexpected to me. Moving defvar inside
f2.el also fixes it.

Cheers,
Luís




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Fri, 21 Dec 2018 16:41:01 GMT) Full text and rfc822 format available.

Message #8 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Luís Oliveira <luismbo <at> gmail.com>
To: bug-gnu-emacs <at> gnu.org
Cc: João Távora <joaotavora <at> gmail.com>
Subject: Re: 26.1; Unbound defvar across compilation units
Date: Fri, 21 Dec 2018 13:50:02 +0000
On Fri, Dec 21, 2018 at 12:02 PM Luís Oliveira <luismbo <at> gmail.com> wrote:
> I think I've come across a bug.

But it seems to be documented behaviour:

    Note that specifying a value, even nil, marks the variable as
special permanently. Whereas if value is omitted then the variable is
only marked special locally (i.e. within the current lexical scope, or
file if at the top-level). This can be useful for suppressing byte
compilation warnings, see Compiler Errors.
    (https://www.gnu.org/software/emacs/manual/html_node/elisp/Defining-Variables.html)

Please disregard this bug report. :-)

Luís




Severity set to 'minor' from 'normal' Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 21 Dec 2018 16:54:01 GMT) Full text and rfc822 format available.

Added tag(s) notabug. Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 21 Dec 2018 16:54:01 GMT) Full text and rfc822 format available.

bug closed, send any further explanations to 33828 <at> debbugs.gnu.org and Luís Oliveira <luismbo <at> gmail.com> Request was from Glenn Morris <rgm <at> gnu.org> to control <at> debbugs.gnu.org. (Fri, 21 Dec 2018 16:54:01 GMT) Full text and rfc822 format available.

Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Fri, 21 Dec 2018 20:09:01 GMT) Full text and rfc822 format available.

Message #17 received at 33828 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Luís Oliveira <luismbo <at> gmail.com>
Cc: joaotavora <at> gmail.com, 33828 <at> debbugs.gnu.org
Subject: Re: bug#33828: 26.1; Unbound defvar across compilation units
Date: Fri, 21 Dec 2018 22:02:49 +0200
> From: Luís Oliveira <luismbo <at> gmail.com>
> Date: Fri, 21 Dec 2018 12:02:55 +0000
> Cc: João Távora <joaotavora <at> gmail.com>
> 
> I think I've come across a bug.

Doesn't look like a bug to me.

>     (defun f2-foo ()
>       (let ((my-special 123))
>         (f2-bar)))
> 
>     (defun f2-bar ()
>       my-special)
> 
>     (print (f2-bar))
> 
> 
> 
>     $ emacs -Q -L . -f batch-byte-compile f2.el
> 
>     In toplevel form:
>     f2.el:5:1:Warning: Unused lexical variable `my-special'

That's because you do nothing with the value of f2-bar.

>     In f2-bar:
>     f2.el:10:3:Warning: reference to free variable `my-special'
> 
> 
> Also:
> 
>     $ emacs -Q -L . -batch -l f2.el
>     Symbol's value as variable is void: my-special

You need to use defvar in the same file.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Sat, 22 Dec 2018 02:46:02 GMT) Full text and rfc822 format available.

Message #20 received at 33828 <at> debbugs.gnu.org (full text, mbox):

From: João Távora <joaotavora <at> gmail.com>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Luís Oliveira <luismbo <at> gmail.com>, 33828 <at> debbugs.gnu.org
Subject: Re: bug#33828: 26.1; Unbound defvar across compilation units
Date: Sat, 22 Dec 2018 02:45:32 +0000
Eli Zaretskii <eliz <at> gnu.org> writes:

>> From: Luís Oliveira <luismbo <at> gmail.com>
>> Date: Fri, 21 Dec 2018 12:02:55 +0000
>> Cc: João Távora <joaotavora <at> gmail.com>
>> 
>> I think I've come across a bug.
>
> Doesn't look like a bug to me.

Indeed, it's the behaviour described in the Emacs manual, but the
motivation is questionable: it says it's useful for shooshing the
byte-compiler's warnings.  Aren't there better alternatives to do this
instead of imposing this seeming inconsistency?

>>     (defun f2-foo ()
>>       (let ((my-special 123))
>>         (f2-bar)))
>> 
>>     (defun f2-bar ()
>>       my-special)
>> 
>>     (print (f2-bar))
>> 
>> 
>> 
>>     $ emacs -Q -L . -f batch-byte-compile f2.el
>> 
>>     In toplevel form:
>>     f2.el:5:1:Warning: Unused lexical variable `my-special'
>
> That's because you do nothing with the value of f2-bar.

f2-bar is a function, and it is called.  Did you mean 'my-special'?  It
is used as well, in f2-bar.

>> Also:
>> 
>>     $ emacs -Q -L . -batch -l f2.el
>>     Symbol's value as variable is void: my-special
>
> You need to use defvar in the same file.

Or alternatively, use the variable in a different file but ensure the
defvar was declared with an initial value.  When you do that, it produces
the expected behaviour for someone who is accustomed to other Lisps
lexical and dynamic binding, such as Common Lisp.  In other words, it's
the edge case defvar-in-different-file-with-no-initial-binding that
we're questioning here.  The presence of the initial binding seems to
matter, even though its value is useless.

João






Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Sat, 22 Dec 2018 07:45:02 GMT) Full text and rfc822 format available.

Message #23 received at 33828 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: João Távora <joaotavora <at> gmail.com>
Cc: luismbo <at> gmail.com, 33828 <at> debbugs.gnu.org
Subject: Re: bug#33828: 26.1; Unbound defvar across compilation units
Date: Sat, 22 Dec 2018 09:43:59 +0200
> From: João Távora <joaotavora <at> gmail.com>
> Cc: Luís Oliveira <luismbo <at> gmail.com>,
>   33828 <at> debbugs.gnu.org
> Date: Sat, 22 Dec 2018 02:45:32 +0000
> 
> >>     (defun f2-foo ()
> >>       (let ((my-special 123))
> >>         (f2-bar)))
> >> 
> >>     (defun f2-bar ()
> >>       my-special)
> >> 
> >>     (print (f2-bar))
> >> 
> >> 
> >> 
> >>     $ emacs -Q -L . -f batch-byte-compile f2.el
> >> 
> >>     In toplevel form:
> >>     f2.el:5:1:Warning: Unused lexical variable `my-special'
> >
> > That's because you do nothing with the value of f2-bar.
> 
> f2-bar is a function, and it is called.  Did you mean 'my-special'?  It
> is used as well, in f2-bar.

I meant the value returned by f2-bar in f2-foo.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Sat, 22 Dec 2018 22:42:02 GMT) Full text and rfc822 format available.

Message #26 received at 33828 <at> debbugs.gnu.org (full text, mbox):

From: Philipp Stephani <p.stephani2 <at> gmail.com>
To: João Távora <joaotavora <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>, luismbo <at> gmail.com, 33828 <at> debbugs.gnu.org
Subject: Re: bug#33828: 26.1; Unbound defvar across compilation units
Date: Sat, 22 Dec 2018 23:40:50 +0100
Am Sa., 22. Dez. 2018 um 03:46 Uhr schrieb João Távora <joaotavora <at> gmail.com>:
>
> Eli Zaretskii <eliz <at> gnu.org> writes:
>
> >> From: Luís Oliveira <luismbo <at> gmail.com>
> >> Date: Fri, 21 Dec 2018 12:02:55 +0000
> >> Cc: João Távora <joaotavora <at> gmail.com>
> >>
> >> I think I've come across a bug.
> >
> > Doesn't look like a bug to me.
>
> Indeed, it's the behaviour described in the Emacs manual, but the
> motivation is questionable: it says it's useful for shooshing the
> byte-compiler's warnings.  Aren't there better alternatives to do this
> instead of imposing this seeming inconsistency?

The better alternative is to explicitly `require' the library that
defines the variable.
I see the one-argument `defvar' as the variable equivalent to
`declare-function' - it announces that a variable exists without
defining it. (Maybe there should be a `declare-variable' macro to
mirror `declare-function'.) As such, its use should be exceptional;
most libraries should make sure to not have cyclic dependencies and
use plain `require'.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#33828; Package emacs. (Sun, 23 Dec 2018 01:17:01 GMT) Full text and rfc822 format available.

Message #29 received at 33828 <at> debbugs.gnu.org (full text, mbox):

From: João Távora <joaotavora <at> gmail.com>
To: Philipp Stephani <p.stephani2 <at> gmail.com>
Cc: Eli Zaretskii <eliz <at> gnu.org>,
 Luís Borges de Oliveira <luismbo <at> gmail.com>,
 33828 <at> debbugs.gnu.org
Subject: Re: bug#33828: 26.1; Unbound defvar across compilation units
Date: Sun, 23 Dec 2018 01:16:38 +0000
[Message part 1 (text/plain, inline)]
On Sat, Dec 22, 2018 at 10:41 PM Philipp Stephani <p.stephani2 <at> gmail.com>
wrote:

> Am Sa., 22. Dez. 2018 um 03:46 Uhr schrieb João Távora <
> joaotavora <at> gmail.com>:
> >
> > Eli Zaretskii <eliz <at> gnu.org> writes:
> >
> > >> From: Luís Oliveira <luismbo <at> gmail.com>
> > >> Date: Fri, 21 Dec 2018 12:02:55 +0000
> > >> Cc: João Távora <joaotavora <at> gmail.com>
> > >>
> > >> I think I've come across a bug.
> > >
> > > Doesn't look like a bug to me.
> >
> > Indeed, it's the behaviour described in the Emacs manual, but the
> > motivation is questionable: it says it's useful for shooshing the
> > byte-compiler's warnings.  Aren't there better alternatives to do this
> > instead of imposing this seeming inconsistency?
>
> The better alternative is to explicitly `require' the library that
> defines the variable.
> I see the one-argument `defvar' as the variable equivalent to
> `declare-function' - it announces that a variable exists without
> defining it. (Maybe there should be a `declare-variable' macro to
> mirror `declare-function'.) As such, its use should be exceptional;
> most libraries should make sure to not have cyclic dependencies and
> use plain `require'.
>

I think in Luís original report there is a `require' of the library where
the variable is declared before using it. Unfortunately it only works
when it is declared with an initial value.

João
[Message part 2 (text/html, inline)]

bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Sun, 20 Jan 2019 12:24:05 GMT) Full text and rfc822 format available.

This bug report was last modified 6 years and 152 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.