unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* unused local variables
@ 2007-11-29 12:32 Kazu Yamamoto
  2007-11-29 15:31 ` Thien-Thi Nguyen
                   ` (2 more replies)
  0 siblings, 3 replies; 33+ messages in thread
From: Kazu Yamamoto @ 2007-11-29 12:32 UTC (permalink / raw)
  To: emacs-devel

Hello,

Please tell me how to detect unused local variables of Elisp.

The XEmacs byte compiler tells unused local variables. But I cannot
find the same method in Emacs.

I don't use XEmacs but keep it in my computer only to detect used
local variables of my developing Elisp code. If I can detect them with
Emacs, I want to remove XEmacs from my computer...

--Kazu

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

* Re: unused local variables
  2007-11-29 12:32 unused local variables Kazu Yamamoto
@ 2007-11-29 15:31 ` Thien-Thi Nguyen
  2007-11-30  0:23   ` Kazu Yamamoto
  2007-11-29 16:02 ` Stefan Monnier
  2007-12-04 11:12 ` Katsumi Yamaoka
  2 siblings, 1 reply; 33+ messages in thread
From: Thien-Thi Nguyen @ 2007-11-29 15:31 UTC (permalink / raw)
  To: Kazu Yamamoto; +Cc: emacs-devel

() Kazu Yamamoto (山本和彦) <kazu@iij.ad.jp>
() Thu, 29 Nov 2007 21:32:56 +0900 (JST)

   Please tell me how to detect unused local variables of Elisp.

what does "unused" mean, precisely?  can you give an example?

thi

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

* Re: unused local variables
  2007-11-29 12:32 unused local variables Kazu Yamamoto
  2007-11-29 15:31 ` Thien-Thi Nguyen
@ 2007-11-29 16:02 ` Stefan Monnier
  2007-11-30  0:25   ` Kazu Yamamoto
  2007-12-04 11:12 ` Katsumi Yamaoka
  2 siblings, 1 reply; 33+ messages in thread
From: Stefan Monnier @ 2007-11-29 16:02 UTC (permalink / raw)
  To: Kazu Yamamoto; +Cc: emacs-devel

> Please tell me how to detect unused local variables of Elisp.

> The XEmacs byte compiler tells unused local variables.  But I cannot
> find the same method in Emacs.

I have a local patch that does it.  If there's interest I can clean it
up and install it.


        Stefan

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

* Re: unused local variables
  2007-11-29 15:31 ` Thien-Thi Nguyen
@ 2007-11-30  0:23   ` Kazu Yamamoto
  2007-11-30  9:22     ` Juanma Barranquero
  0 siblings, 1 reply; 33+ messages in thread
From: Kazu Yamamoto @ 2007-11-30  0:23 UTC (permalink / raw)
  To: emacs-devel

> what does "unused" mean, precisely?  can you give an example?

Please look at the following example:

(let ((used-local-variable 0)
      (unused-local-variable 1))
  (setq used-local-variable (1+ used-local-variable)))

--Kazu

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

* Re: unused local variables
  2007-11-29 16:02 ` Stefan Monnier
@ 2007-11-30  0:25   ` Kazu Yamamoto
  0 siblings, 0 replies; 33+ messages in thread
From: Kazu Yamamoto @ 2007-11-30  0:25 UTC (permalink / raw)
  To: emacs-devel

> I have a local patch that does it.  If there's interest I can clean it
> up and install it.

Yes, please do it!
Thanks in advance.

--Kazu

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

* Re: unused local variables
  2007-11-30  0:23   ` Kazu Yamamoto
@ 2007-11-30  9:22     ` Juanma Barranquero
  2007-11-30  9:27       ` Kazu Yamamoto
  0 siblings, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30  9:22 UTC (permalink / raw)
  To: 山本和彦 Kazu Yamamoto; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 396 bytes --]

On Nov 30, 2007 1:23 AM, 山本和彦 Kazu Yamamoto <kazu@iij.ad.jp> wrote:

> Please look at the following example:
>
> (let ((used-local-variable 0)
>       (unused-local-variable 1))
>   (setq used-local-variable (1+ used-local-variable)))

With dynamic binding, that's going to be imposible to detect for most
non-trivial let bodies... Or am I missing something obvious?

             Juanma

[-- Attachment #2: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

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

* Re: unused local variables
  2007-11-30  9:22     ` Juanma Barranquero
@ 2007-11-30  9:27       ` Kazu Yamamoto
  2007-11-30  9:37         ` Juanma Barranquero
  0 siblings, 1 reply; 33+ messages in thread
From: Kazu Yamamoto @ 2007-11-30  9:27 UTC (permalink / raw)
  To: emacs-devel

> With dynamic binding, that's going to be imposible to detect for most
> non-trivial let bodies... Or am I missing something obvious?

XEmacs warns unused local variables as if they are statically bound.
Also XEmacs does not warns unused local variables if they are 
also globally defined.

That is good enough to me.

--Kazu

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

* Re: unused local variables
  2007-11-30  9:27       ` Kazu Yamamoto
@ 2007-11-30  9:37         ` Juanma Barranquero
  2007-11-30  9:59           ` David Kastrup
                             ` (2 more replies)
  0 siblings, 3 replies; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30  9:37 UTC (permalink / raw)
  To: 山本和彦 Kazu Yamamoto; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 477 bytes --]

On Nov 30, 2007 10:27 AM, 山本和彦 Kazu Yamamoto <kazu@iij.ad.jp> wrote:

> XEmacs warns unused local variables as if they are statically bound.
> Also XEmacs does not warns unused local variables if they are
> also globally defined.

Are you saying that in

 (defun test ()
   (if my-dynamic-var
       "correct"
     "erroneous"))

 (let ((my-dynamic-var t))
   (test))

you would force my-dynamic-var to be `defvar'ed to avoid a warning in the let?

             Juanma

[-- Attachment #2: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

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

* Re: unused local variables
  2007-11-30  9:37         ` Juanma Barranquero
@ 2007-11-30  9:59           ` David Kastrup
  2007-11-30 10:14             ` Juanma Barranquero
  2007-11-30 10:34           ` Kazu Yamamoto
  2007-11-30 15:10           ` Stefan Monnier
  2 siblings, 1 reply; 33+ messages in thread
From: David Kastrup @ 2007-11-30  9:59 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

"Juanma Barranquero" <lekktu@gmail.com> writes:

> On Nov 30, 2007 10:27 AM, 山本和彦 Kazu Yamamoto <kazu@iij.ad.jp> wrote:
>
>> XEmacs warns unused local variables as if they are statically bound.
>> Also XEmacs does not warns unused local variables if they are
>> also globally defined.
>
> Are you saying that in
>
>  (defun test ()
>    (if my-dynamic-var
>        "correct"
>      "erroneous"))
>
>  (let ((my-dynamic-var t))
>    (test))
>
> you would force my-dynamic-var to be `defvar'ed to avoid a warning in
> the let?

I consider this quite reasonable.  If my-dynamic-var is used for passing
information across function boundaries, it deserves being declared in
the same scope.  There is one obvious exception:

(defun test ()
  (let ((my-variable 3))
    (funcall (lambda nil (1+ my-variable)))))

Namely, lambda functions using upvalues (well, conceptually as long as
we don't have closures).  Those are byte-compiled as well and should not
complain.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: unused local variables
  2007-11-30  9:59           ` David Kastrup
@ 2007-11-30 10:14             ` Juanma Barranquero
  2007-11-30 10:23               ` David Kastrup
  0 siblings, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 10:14 UTC (permalink / raw)
  To: David Kastrup; +Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 10:59 AM, David Kastrup <dak@gnu.org> wrote:

> I consider this quite reasonable.  If my-dynamic-var is used for passing
> information across function boundaries, it deserves being declared in
> the same scope.

I don't know whether it is reasonable, but it is a departure of the current use.

The docstring of `defvar' clearly says:

"Define symbol as a variable, and return symbol.
You are not required to define a variable in order to use it,
but the definition can supply documentation and an initial value
in a way that tags can recognize."

Getting a warning is tantamount to be required to define it...

Now, I'm all for lexically scoped Lisp and (declare (special
mydynamic-var)) and the like, but clearly that's not the route elisp
is following.

             Juanma

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

* Re: unused local variables
  2007-11-30 10:14             ` Juanma Barranquero
@ 2007-11-30 10:23               ` David Kastrup
  2007-11-30 10:26                 ` Juanma Barranquero
  0 siblings, 1 reply; 33+ messages in thread
From: David Kastrup @ 2007-11-30 10:23 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

"Juanma Barranquero" <lekktu@gmail.com> writes:

> On Nov 30, 2007 10:59 AM, David Kastrup <dak@gnu.org> wrote:
>
>> I consider this quite reasonable.  If my-dynamic-var is used for passing
>> information across function boundaries, it deserves being declared in
>> the same scope.
>
> I don't know whether it is reasonable, but it is a departure of the
> current use.
>
> The docstring of `defvar' clearly says:

The docstring is not defining "current use".

> Getting a warning is tantamount to be required to define it...
>
> Now, I'm all for lexically scoped Lisp and (declare (special
> mydynamic-var)) and the like, but clearly that's not the route elisp
> is following.

It is pretty much the current practice in Emacs code.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: unused local variables
  2007-11-30 10:23               ` David Kastrup
@ 2007-11-30 10:26                 ` Juanma Barranquero
  2007-11-30 10:36                   ` David Kastrup
  0 siblings, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 10:26 UTC (permalink / raw)
  To: David Kastrup; +Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 11:23 AM, David Kastrup <dak@gnu.org> wrote:

> > Now, I'm all for lexically scoped Lisp and (declare (special
> > mydynamic-var)) and the like, but clearly that's not the route elisp
> > is following.
>
> It is pretty much the current practice in Emacs code.

Lexical scoping? Or are you purposefully misinterpreting the phrase you quoted?

             Juanma

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

* Re: unused local variables
  2007-11-30  9:37         ` Juanma Barranquero
  2007-11-30  9:59           ` David Kastrup
@ 2007-11-30 10:34           ` Kazu Yamamoto
  2007-11-30 10:45             ` Juanma Barranquero
  2007-11-30 15:10           ` Stefan Monnier
  2 siblings, 1 reply; 33+ messages in thread
From: Kazu Yamamoto @ 2007-11-30 10:34 UTC (permalink / raw)
  To: emacs-devel

> Are you saying that in
> 
>  (defun test ()
>    (if my-dynamic-var
>        "correct"
>      "erroneous"))
> 
>  (let ((my-dynamic-var t))
>    (test))
> 
> you would force my-dynamic-var to be `defvar'ed to avoid a 
> warning in the let?

I'm not sure the point of this example.

What I want to say is that case-fold-search in the following example
is not warned with XEmacs because it is defined with 'defvar'.

(let ((case-fold-search t))
     ;; code which does not directory refer to case-fold-search.
     )

--Kazu

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

* Re: unused local variables
  2007-11-30 10:26                 ` Juanma Barranquero
@ 2007-11-30 10:36                   ` David Kastrup
  2007-11-30 10:49                     ` Juanma Barranquero
  0 siblings, 1 reply; 33+ messages in thread
From: David Kastrup @ 2007-11-30 10:36 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

"Juanma Barranquero" <lekktu@gmail.com> writes:

> On Nov 30, 2007 11:23 AM, David Kastrup <dak@gnu.org> wrote:
>
>> > Now, I'm all for lexically scoped Lisp and (declare (special
>> > mydynamic-var)) and the like, but clearly that's not the route elisp
>> > is following.
>>
>> It is pretty much the current practice in Emacs code.
>
> Lexical scoping?

defvar-ing variables used for inter-function communication.

> Or are you purposefully misinterpreting the phrase you quoted?

I think we have enough potential for misunderstandings to make it
unnecessary to do them on purpose.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: unused local variables
  2007-11-30 10:34           ` Kazu Yamamoto
@ 2007-11-30 10:45             ` Juanma Barranquero
  2007-11-30 11:26               ` David Kastrup
  2007-12-03  2:30               ` Kazu Yamamoto
  0 siblings, 2 replies; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 10:45 UTC (permalink / raw)
  To: 山本和彦 Kazu Yamamoto; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 596 bytes --]

On Nov 30, 2007 11:34 AM, 山本和彦 Kazu Yamamoto <kazu@iij.ad.jp> wrote:

> I'm not sure the point of this example.

The point of the example is (and I'm sure you know it) that dynamic
variables can be used in ways that are not evident to the
byte-compiler just by looking at the lexical scope where the dynamic
variable is used...

> What I want to say is that case-fold-search in the following example
> is not warned with XEmacs because it is defined with 'defvar'.

So, yes: what you propose is forcing `defvar' for dynamic variables
used across function boundaries.

             Juanma

[-- Attachment #2: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

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

* Re: unused local variables
  2007-11-30 10:36                   ` David Kastrup
@ 2007-11-30 10:49                     ` Juanma Barranquero
  0 siblings, 0 replies; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 10:49 UTC (permalink / raw)
  To: David Kastrup; +Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 11:36 AM, David Kastrup <dak@gnu.org> wrote:

> defvar-ing variables used for inter-function communication.

Well, you were then answering to something I didn't say.

As for `defvar'ing the variables, I think it's more usual now, yes,
because of byte-compiler warnings.

> I think we have enough potential for misunderstandings to make it
> unnecessary to do them on purpose.

Agreed.

             Juanma

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

* Re: unused local variables
  2007-11-30 10:45             ` Juanma Barranquero
@ 2007-11-30 11:26               ` David Kastrup
  2007-11-30 11:39                 ` Juanma Barranquero
  2007-12-03  2:30               ` Kazu Yamamoto
  1 sibling, 1 reply; 33+ messages in thread
From: David Kastrup @ 2007-11-30 11:26 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

"Juanma Barranquero" <lekktu@gmail.com> writes:

> On Nov 30, 2007 11:34 AM, 山本和彦 Kazu Yamamoto <kazu@iij.ad.jp> wrote:
>
>> I'm not sure the point of this example.
>
> The point of the example is (and I'm sure you know it) that dynamic
> variables can be used in ways that are not evident to the
> byte-compiler just by looking at the lexical scope where the dynamic
> variable is used...
>
>> What I want to say is that case-fold-search in the following example
>> is not warned with XEmacs because it is defined with 'defvar'.
>
> So, yes: what you propose is forcing `defvar' for dynamic variables
> used across function boundaries.

I am actually surprised that this is supposedly not already the case:
the (defvar xxx) construct has the sole purpose of silencing the byte
compiler as far as I remember.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: unused local variables
  2007-11-30 11:26               ` David Kastrup
@ 2007-11-30 11:39                 ` Juanma Barranquero
  0 siblings, 0 replies; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 11:39 UTC (permalink / raw)
  To: David Kastrup; +Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 12:26 PM, David Kastrup <dak@gnu.org> wrote:

> I am actually surprised that this is supposedly not already the case:

It is, already. You're right and I'm sleepy.

But in a case like

 (defun test ()
   (if (bound-and-true-p my-dynamic-var)
       "right"
     "wrong"))

 (let ((my-dynamic-var t))
   (test))

the byte-compiler does not issue any warning now. It would in Yamamoto's case.

             Juanma

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

* Re: unused local variables
  2007-11-30  9:37         ` Juanma Barranquero
  2007-11-30  9:59           ` David Kastrup
  2007-11-30 10:34           ` Kazu Yamamoto
@ 2007-11-30 15:10           ` Stefan Monnier
  2007-11-30 15:31             ` Juanma Barranquero
  2007-11-30 15:38             ` Juanma Barranquero
  2 siblings, 2 replies; 33+ messages in thread
From: Stefan Monnier @ 2007-11-30 15:10 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

> Are you saying that in

>  (defun test ()
>    (if my-dynamic-var
>        "correct"
>      "erroneous"))

>  (let ((my-dynamic-var t))
>    (test))

> you would force my-dynamic-var to be `defvar'ed to avoid a warning in the let?

Yes.  And that's good.  E.g. if you want to compile this file with the
lexical-scoping version of Emacs that Miles has been working on, this
is indispensable.

BTW, in the above example, the byte-compiler already complains in the
`test' function, so it's not like it's going to make that big
a difference.

Also dynamic scoping is sufficiently odd and rarely used that it deserve
special mention in the code.  It's pretty common to mark it with
a comment.  And code is always better than a comment since it tends to
bitrot a bit less quickly.


        Stefan


PS: Another "unused var" warning which we should add (I don't have it
    in my lock hack yet, tho) is the one that notices when a `defvar'
    declares a variable which is not used in the file.

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

* Re: unused local variables
  2007-11-30 15:10           ` Stefan Monnier
@ 2007-11-30 15:31             ` Juanma Barranquero
  2007-11-30 15:58               ` Stefan Monnier
  2007-11-30 15:38             ` Juanma Barranquero
  1 sibling, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 15:31 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 4:10 PM, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> Yes.  And that's good.

As I've said, I'm all for a lexically-scoped elisp. But I'm a bit
worried by introducing warnings for things that are not "wrong" (or,
at least, weren't until now).

> E.g. if you want to compile this file with the
> lexical-scoping version of Emacs that Miles has been working on, this
> is indispensable.

Any hope of that branch merged with the trunk sometime?

> BTW, in the above example, the byte-compiler already complains in the
> `test' function, so it's not like it's going to make that big
> a difference.

As shown in a previous message, there's no warning if the function
tests whether my-dynamic-variable is bound.

> Also dynamic scoping is sufficiently odd and rarely used that it deserve
> special mention in the code.  It's pretty common to mark it with
> a comment.  And code is always better than a comment since it tends to
> bitrot a bit less quickly.

I agree that code is better than a comment. I'm not sure I agree that
dynamic scoping is "odd and rarely used" in elisp packages.

> PS: Another "unused var" warning which we should add (I don't have it
>     in my lock hack yet, tho) is the one that notices when a `defvar'
>     declares a variable which is not used in the file.

That one seems useful, yes. byte-compiler-silencing `defvar's (and now
`define-function's), like `require's (and C #include's), tend to
accumulate even when they cease to be necessary.

             Juanma

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

* Re: unused local variables
  2007-11-30 15:10           ` Stefan Monnier
  2007-11-30 15:31             ` Juanma Barranquero
@ 2007-11-30 15:38             ` Juanma Barranquero
  2007-11-30 15:59               ` Stefan Monnier
  1 sibling, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 15:38 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 4:10 PM, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> BTW, in the above example, the byte-compiler already complains in the
> `test' function, so it's not like it's going to make that big
> a difference.

Of course, there's no warning now if the function is in a different
file, perhaps with a (defvar my-dynamic-var).

             Juanma

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

* Re: unused local variables
  2007-11-30 15:31             ` Juanma Barranquero
@ 2007-11-30 15:58               ` Stefan Monnier
  2007-11-30 16:40                 ` David Kastrup
  2007-11-30 18:07                 ` Juanma Barranquero
  0 siblings, 2 replies; 33+ messages in thread
From: Stefan Monnier @ 2007-11-30 15:58 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

>> Yes.  And that's good.
> As I've said, I'm all for a lexically-scoped elisp. But I'm a bit
> worried by introducing warnings for things that are not "wrong" (or,
> at least, weren't until now).

Those warnings do catch left-over unused variables as well.
And they can easily be silenced by adding a `defvar'.

>> E.g. if you want to compile this file with the
>> lexical-scoping version of Emacs that Miles has been working on, this
>> is indispensable.
> Any hope of that branch merged with the trunk sometime?

No idea.  But I hope so.

>> BTW, in the above example, the byte-compiler already complains in the
>> `test' function, so it's not like it's going to make that big
>> a difference.
> As shown in a previous message, there's no warning if the function
> tests whether my-dynamic-variable is bound.

Sure.  Now, find me an example of a use of `boundp' on a variable for
which there exist no defvar.

>> Also dynamic scoping is sufficiently odd and rarely used that it deserve
>> special mention in the code.  It's pretty common to mark it with
>> a comment.  And code is always better than a comment since it tends to
>> bitrot a bit less quickly.

> I agree that code is better than a comment. I'm not sure I agree that
> dynamic scoping is "odd and rarely used" in elisp packages.

Dynamic scoping is often used in Emacs to temporarily change the value
of a "global" variable (variables defined via a defvar somewhere).
It is rarely used for pure argument passing.  It is used that way, but
rarely.  And often those uses come with comments warning about it,
because programmers tend to assume lexical scoping (even when writing
Elisp).  At least that's my experience based on more than 10 years of
browsing emacs/lisp/**/*.el packages.

>> PS: Another "unused var" warning which we should add (I don't have it
>> in my lock hack yet, tho) is the one that notices when a `defvar'
>> declares a variable which is not used in the file.

> That one seems useful, yes. byte-compiler-silencing `defvar's (and now
> `define-function's), like `require's (and C #include's), tend to
> accumulate even when they cease to be necessary.

It will also give false positives.  Take a look at lisp/pcvs-defs.el for
a striking example where most of the defvars are not used in that file.
I guess a first step could be to limit its use to
"byte-compiler-silencing defvars".


        Stefan

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

* Re: unused local variables
  2007-11-30 15:38             ` Juanma Barranquero
@ 2007-11-30 15:59               ` Stefan Monnier
  2007-11-30 18:11                 ` Juanma Barranquero
  0 siblings, 1 reply; 33+ messages in thread
From: Stefan Monnier @ 2007-11-30 15:59 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

>> BTW, in the above example, the byte-compiler already complains in the
>> `test' function, so it's not like it's going to make that big
>> a difference.

> Of course, there's no warning now if the function is in a different
> file, perhaps with a (defvar my-dynamic-var).

Right.  So you're OK with adding (defvar my-dynamic-var) in one of the
files but not in both?  Why is that?


        Stefan

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

* Re: unused local variables
  2007-11-30 15:58               ` Stefan Monnier
@ 2007-11-30 16:40                 ` David Kastrup
  2007-11-30 17:58                   ` tomas
  2007-11-30 18:07                 ` Juanma Barranquero
  1 sibling, 1 reply; 33+ messages in thread
From: David Kastrup @ 2007-11-30 16:40 UTC (permalink / raw)
  To: Stefan Monnier; +Cc: Juanma Barranquero, Kazu Yamamoto, emacs-devel

Stefan Monnier <monnier@iro.umontreal.ca> writes:

>>> E.g. if you want to compile this file with the
>>> lexical-scoping version of Emacs that Miles has been working on, this
>>> is indispensable.
>> Any hope of that branch merged with the trunk sometime?
>
> No idea.  But I hope so.

"sometime".  On our agenda is releasing Emacs 23 with unicode-2.  It is
not merged yet.  Then there is some unspecified fate/timeline for bidi
support.

Pushing out lexical scope will be a somewhat harsh move with regard to
code compatibility and stuff.  So it better happen _early_ in a release
cycle, in order to let third-party stuff catch up.

Personally, I would love to see code like

`(lambda (x) (1+ ',y))

go away rather sooner than later (it is not byte-compilable for one
thing), and

(lexical-let ((y y)) (lambda (x) (1+ y)))

is not really something I fancy.  And anyway, when the binding goes, so
does the value.

-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

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

* Re: unused local variables
  2007-11-30 16:40                 ` David Kastrup
@ 2007-11-30 17:58                   ` tomas
  0 siblings, 0 replies; 33+ messages in thread
From: tomas @ 2007-11-30 17:58 UTC (permalink / raw)
  To: David Kastrup
  Cc: Juanma Barranquero, Kazu Yamamoto, Stefan Monnier, emacs-devel

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

On Fri, Nov 30, 2007 at 05:40:08PM +0100, David Kastrup wrote:

[...]
> Personally, I would love to see code like
> 
> `(lambda (x) (1+ ',y))
> 
> go away rather sooner than later [...]

+1

Since we all seem to expect that this won't be a painless transition:

 * is there any intention to go that path eventually?

 * should new code be written explicitly to be "lexical friendly"?
   (I'd expect most code to be , lexical friendly, and Miles seems to
   have tackled the titanic task with the core code anyway).

I'd imagine that the first step might be style recommendations, the next
would be some kind of checker pointing people to potential problems, and
after a while just jump...

regards

- -- tomás
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.1 (GNU/Linux)

iD8DBQFHUE9YBcgs9XrR2kYRAkQ/AJ9tixfqH0U0353JaQ1DcAleHcMn9ACfWJ1g
m9jxRK5cUFUbrsnrOt+qicA=
=5EVl
-----END PGP SIGNATURE-----

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

* Re: unused local variables
  2007-11-30 15:58               ` Stefan Monnier
  2007-11-30 16:40                 ` David Kastrup
@ 2007-11-30 18:07                 ` Juanma Barranquero
  1 sibling, 0 replies; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 18:07 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 4:58 PM, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> Those warnings do catch left-over unused variables as well.
> And they can easily be silenced by adding a `defvar'.

True.

> Sure.  Now, find me an example of a use of `boundp' on a variable for
> which there exist no defvar.

Lots.

Oh, you mean no defvar and no defcustom... :)

I don't know. I suppose they're rare indeed.

> It will also give false positives.

I'm quite sure of it...

             Juanma

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

* Re: unused local variables
  2007-11-30 15:59               ` Stefan Monnier
@ 2007-11-30 18:11                 ` Juanma Barranquero
  2007-12-01  3:17                   ` Stefan Monnier
  0 siblings, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-11-30 18:11 UTC (permalink / raw)
  To: Stefan Monnier
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

On Nov 30, 2007 4:59 PM, Stefan Monnier <monnier@iro.umontreal.ca> wrote:

> Right.  So you're OK with adding (defvar my-dynamic-var) in one of the
> files but not in both?  Why is that?

I'm not really opposing. I don't like warnings, nor programming errors
caused by subtle interactions (or unexpected ones). Once again: I like
lexically scoped languages (Scheme excluded). I'm just not sure
littering packages with (defvar var) or (declare-function function) is
better. It certainly doesn't look clean.

             Juanma

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

* Re: unused local variables
  2007-11-30 18:11                 ` Juanma Barranquero
@ 2007-12-01  3:17                   ` Stefan Monnier
  0 siblings, 0 replies; 33+ messages in thread
From: Stefan Monnier @ 2007-12-01  3:17 UTC (permalink / raw)
  To: Juanma Barranquero
  Cc: 山本和彦 Kazu Yamamoto, emacs-devel

>> Right.  So you're OK with adding (defvar my-dynamic-var) in one of the
>> files but not in both?  Why is that?

> I'm not really opposing. I don't like warnings, nor programming errors
> caused by subtle interactions (or unexpected ones). Once again: I like
> lexically scoped languages (Scheme excluded).  I'm just not sure
> littering packages with (defvar var) or (declare-function function) is
> better.  It certainly doesn't look clean.

I can assure you that warning about unused vars will only require very
few extra defvars.


        Stefan

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

* Re: unused local variables
  2007-11-30 10:45             ` Juanma Barranquero
  2007-11-30 11:26               ` David Kastrup
@ 2007-12-03  2:30               ` Kazu Yamamoto
  2007-12-03  9:50                 ` Juanma Barranquero
  1 sibling, 1 reply; 33+ messages in thread
From: Kazu Yamamoto @ 2007-12-03  2:30 UTC (permalink / raw)
  To: emacs-devel

Hello,

> What I want to say is that case-fold-search in the following example
> > is not warned with XEmacs because it is defined with 'defvar'.
> 
> So, yes: what you propose is forcing `defvar' for dynamic variables
> used across function boundaries.

Probaby you misunderstood. I did not use the word "force".

Let me explain the whole story:

----
(defvar my-global-var nil)

(defun my-func1 ()
  (setq my-local-inherit-var (1+ my-local-inherit-var)))

(defun my-func()
  (let ((my-global-var t)         ;; <1>
	(my-local-var 1)          ;; <2>
	(my-local-inherit-var 2)) ;; <3>
    ;; code which does not refer these three variabls above
    (my-func1)
    ))
----

My purpose is to detect <2>.

XEmacs warns both <2> and <3>. I said this is good enough because
I can find <2>.

To suppress warnings of <3>, there would be two options:

(1) Provide a option to suppress unused local variables (both <2> and
    <3>). It means that this option does not change Emacs's current
    behavior.

(2) Prepare a *temporary* defvar for <3> when byte-compiling only for
   detection purpose. After removing <2>, you can byte-compile without
   the temporary defvar (and ignoring a warning) and install the elc
   file.

--Kazu

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

* Re: unused local variables
  2007-12-03  2:30               ` Kazu Yamamoto
@ 2007-12-03  9:50                 ` Juanma Barranquero
  2007-12-03 10:03                   ` Kazu Yamamoto
  0 siblings, 1 reply; 33+ messages in thread
From: Juanma Barranquero @ 2007-12-03  9:50 UTC (permalink / raw)
  To: 山本和彦 Kazu Yamamoto; +Cc: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 648 bytes --]

On Dec 3, 2007 3:30 AM, 山本和彦 Kazu Yamamoto <kazu@iij.ad.jp> wrote:

> To suppress warnings of <3>, there would be two options:
>
> (1) Provide a option to suppress unused local variables (both <2> and
>     <3>). It means that this option does not change Emacs's current
>     behavior.
>
> (2) Prepare a *temporary* defvar for <3> when byte-compiling only for
>    detection purpose. After removing <2>, you can byte-compile without
>    the temporary defvar (and ignoring a warning) and install the elc
>    file.

How will you distinguish between <2> and <3> when my-func1 could be in
another elisp file altogether?

             Juanma

[-- Attachment #2: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

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

* Re: unused local variables
  2007-12-03  9:50                 ` Juanma Barranquero
@ 2007-12-03 10:03                   ` Kazu Yamamoto
  0 siblings, 0 replies; 33+ messages in thread
From: Kazu Yamamoto @ 2007-12-03 10:03 UTC (permalink / raw)
  To: emacs-devel

> How will you distinguish between <2> and <3> when my-func1 could be in
> another elisp file altogether?

No good way. But again, that is good enough to me.

Note XEmacs have helped me very much for over 10 years.

--Kazu

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

* Re: unused local variables
  2007-11-29 12:32 unused local variables Kazu Yamamoto
  2007-11-29 15:31 ` Thien-Thi Nguyen
  2007-11-29 16:02 ` Stefan Monnier
@ 2007-12-04 11:12 ` Katsumi Yamaoka
  2007-12-04 15:28   ` Dan Nicolaescu
  2 siblings, 1 reply; 33+ messages in thread
From: Katsumi Yamaoka @ 2007-12-04 11:12 UTC (permalink / raw)
  To: emacs-devel

>>>>> Kazu Yamamoto wrote:

> The XEmacs byte compiler tells unused local variables.

FYI: last week XEmacs in the CVS trunk (i.e. XEmacs 21.5-b28)
came to byte compile the default value section of defcustom
forms and report on undefined vars and functions found there.
For instance:

(defcustom foo (if (and (fboundp 'string-as-multibyte)
			default-enable-multibyte-characters)
		   (string-as-multibyte "foo")
		 "foo")
  "doc string")

While compiling toplevel forms:
  ** reference to free variable default-enable-multibyte-characters
While compiling the end of the data:
  ** the function string-as-multibyte is not known to be defined.

(Emacs and XEmacs 21.4 don't do that.)

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

* Re: unused local variables
  2007-12-04 11:12 ` Katsumi Yamaoka
@ 2007-12-04 15:28   ` Dan Nicolaescu
  0 siblings, 0 replies; 33+ messages in thread
From: Dan Nicolaescu @ 2007-12-04 15:28 UTC (permalink / raw)
  To: Katsumi Yamaoka; +Cc: emacs-devel

Katsumi Yamaoka <yamaoka@jpl.org> writes:

  > >>>>> Kazu Yamamoto wrote:
  > 
  > > The XEmacs byte compiler tells unused local variables.
  > 
  > FYI: last week XEmacs in the CVS trunk (i.e. XEmacs 21.5-b28)
  > came to byte compile the default value section of defcustom
  > forms and report on undefined vars and functions found there.
  > For instance:
  > 
  > (defcustom foo (if (and (fboundp 'string-as-multibyte)
  > 			default-enable-multibyte-characters)
  > 		   (string-as-multibyte "foo")
  > 		 "foo")
  >   "doc string")
  > 
  > While compiling toplevel forms:
  >   ** reference to free variable default-enable-multibyte-characters
  > While compiling the end of the data:
  >   ** the function string-as-multibyte is not known to be defined.
  > 
  > (Emacs and XEmacs 21.4 don't do that.)

I proposed the same thing here recently. Stefan implemented it, but it
was rejected. You should be able to find the patch in the archives...

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

end of thread, other threads:[~2007-12-04 15:28 UTC | newest]

Thread overview: 33+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2007-11-29 12:32 unused local variables Kazu Yamamoto
2007-11-29 15:31 ` Thien-Thi Nguyen
2007-11-30  0:23   ` Kazu Yamamoto
2007-11-30  9:22     ` Juanma Barranquero
2007-11-30  9:27       ` Kazu Yamamoto
2007-11-30  9:37         ` Juanma Barranquero
2007-11-30  9:59           ` David Kastrup
2007-11-30 10:14             ` Juanma Barranquero
2007-11-30 10:23               ` David Kastrup
2007-11-30 10:26                 ` Juanma Barranquero
2007-11-30 10:36                   ` David Kastrup
2007-11-30 10:49                     ` Juanma Barranquero
2007-11-30 10:34           ` Kazu Yamamoto
2007-11-30 10:45             ` Juanma Barranquero
2007-11-30 11:26               ` David Kastrup
2007-11-30 11:39                 ` Juanma Barranquero
2007-12-03  2:30               ` Kazu Yamamoto
2007-12-03  9:50                 ` Juanma Barranquero
2007-12-03 10:03                   ` Kazu Yamamoto
2007-11-30 15:10           ` Stefan Monnier
2007-11-30 15:31             ` Juanma Barranquero
2007-11-30 15:58               ` Stefan Monnier
2007-11-30 16:40                 ` David Kastrup
2007-11-30 17:58                   ` tomas
2007-11-30 18:07                 ` Juanma Barranquero
2007-11-30 15:38             ` Juanma Barranquero
2007-11-30 15:59               ` Stefan Monnier
2007-11-30 18:11                 ` Juanma Barranquero
2007-12-01  3:17                   ` Stefan Monnier
2007-11-29 16:02 ` Stefan Monnier
2007-11-30  0:25   ` Kazu Yamamoto
2007-12-04 11:12 ` Katsumi Yamaoka
2007-12-04 15:28   ` Dan Nicolaescu

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).