unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
* bug#12079: 23.3; erroneous byte-compile-file warning
@ 2012-07-28 14:00 Winston
  2012-07-29 23:12 ` Glenn Morris
                   ` (2 more replies)
  0 siblings, 3 replies; 5+ messages in thread
From: Winston @ 2012-07-28 14:00 UTC (permalink / raw)
  To: 12079

GNU Emacs 23.3.1 (amd64-portbld-freebsd9.0, GTK+ Version 2.24.6) of 2011-12-05

It looks like byte-compile-file is issuing a bogus warning for
(set-buffer) inside (save-excursion).

1) The internal description of (save-excursion) says it saves and
   restores point, mark, and the current buffer (just as it's always
   done).  Here's the relevant part of the description (with -->).

    save-excursion is a special form in `C source code'.

    (save-excursion &rest BODY)

    Save point, mark, and current buffer; execute BODY; restore those things.
    Executes BODY just like `progn'.
--> The values of point, mark and the current buffer are restored
    even in case of abnormal exit (throw or error).
    The state of activation of the mark is also restored.

2) This trivial example, run from buffer *scratch*, agrees with that
   description, that the current buffer gets restored:

    (progn
      (save-excursion
        (set-buffer "foo")      ; I'd already created this
        (barf-if-buffer-read-only)
        )
      (current-buffer)
    )
    #<buffer *scratch*>

3) However, when I write that code to foo.el and run byte-compile-file
   on it, I get the following error:

    Compiling file /home/wbe/foo.el at Sat Jul 28 09:07:28 2012
    Entering directory `/home/wbe/'
--> foo.el:1:1:Warning: `save-excursion' defeated by `set-buffer'

It looks to me like the Warning message is untrue.
 -WBE

[The rest is from (report-emacs-bug).  I removed some clearly irrelevant
parts.]


In GNU Emacs 23.3.1 (amd64-portbld-freebsd9.0, GTK+ Version 2.24.6)
 of 2011-12-05 on jail-15
Windowing system distributor `The X.Org Foundation', version 11.0.10707000
configured using `configure  '--localstatedir=/var' '--with-x-toolkit=gtk' '--x-libraries=/usr/local/lib' '--x-includes=/usr/local/include' '--prefix=/usr/local' '--mandir=/usr/local/man' '--infodir=/usr/local/info/' '--build=amd64-portbld-freebsd9.0' 'build_alias=amd64-portbld-freebsd9.0' 'CC=cc' 'CFLAGS=-O2 -pipe -fno-strict-aliasing' 'LDFLAGS= -L/usr/local/lib -rpath=/usr/lib:/usr/local/lib' 'CPPFLAGS=-I/usr/local/include' 'CPP=cpp''

Important settings:
  value of $LC_ALL: nil
  value of $LC_COLLATE: nil
  value of $LC_CTYPE: iso_8859_1
  value of $LC_MESSAGES: nil
  value of $LC_MONETARY: nil
  value of $LC_NUMERIC: nil
  value of $LC_TIME: nil
  value of $LANG: C
  value of $XMODIFIERS: nil
  locale-coding-system: iso-latin-1-unix
  default enable-multibyte-characters: t

Major mode: Message

Minor modes in effect:
  mml-mode: t
  gnus-message-citation-mode: t
  tooltip-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t
  abbrev-mode: t

[...]

Load-path shadows:
/home/wbe/lib/info hides /usr/local/share/emacs/23.3/lisp/info

Features:
(shadow emacsbug info gnus-fun warnings compile comint debug apropos pp
etags ring html2text browse-url newcomment novice rect flow-fill
gnus-cite smiley ansi-color mail-extr gnus-bcklg gnus-async byte-opt
bytecomp byte-compile gnus-ml disp-table parse-time timezone nndraft
nnmh auth-source gnus-agent gnus-srvr gnus-score score-mode nnvirtual
gnus-msg gnus-art mm-uu mml2015 pgg pgg-parse pgg-def epg-config mm-view
smime dig nntp gnus-cache gnus-sum nnoo gnus-group gnus-undo nnmail
mail-source format-spec gnus-start gnus-spec gnus-int gnus-range message
sendmail regexp-opt ecomplete rfc822 mml mml-sec password-cache
mm-decode mm-bodies mm-encode mailcap mail-parse rfc2231 rfc2047 rfc2045
qp ietf-drums mailabbrev gmm-utils mailheader canlock sha1 hex-util
hashcash gnus-win gnus gnus-ems nnheader gnus-util netrc time-date
mail-utils mm-util mail-prsvr sort multi-isearch kmacro two-column
iso-transl help-mode view hexl eldoc help-fns cus-edit easymenu
cus-start cus-load wid-edit tooltip ediff-hook vc-hooks lisp-float-type
mwheel x-win x-dnd font-setting tool-bar dnd fontset image fringe
lisp-mode register page menu-bar rfn-eshadow timer select scroll-bar
mldrag mouse jit-lock font-lock syntax facemenu font-core frame cham
georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese hebrew greek romanian slovak czech european ethiopic
indian cyrillic chinese case-table epa-hook jka-cmpr-hook help simple
abbrev loaddefs button minibuffer faces cus-face files text-properties
overlay md5 base64 format env code-pages mule custom widget
hashtable-print-readable backquote make-network-process dbusbind
system-font-setting font-render-setting gtk x-toolkit x multi-tty emacs)





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

* bug#12079: 23.3; erroneous byte-compile-file warning
  2012-07-28 14:00 bug#12079: 23.3; erroneous byte-compile-file warning Winston
@ 2012-07-29 23:12 ` Glenn Morris
  2012-07-30  4:42 ` Winston
  2012-07-30 13:40 ` Winston
  2 siblings, 0 replies; 5+ messages in thread
From: Glenn Morris @ 2012-07-29 23:12 UTC (permalink / raw)
  To: Winston; +Cc: 12079

Winston wrote:

>     (progn
>       (save-excursion
>         (set-buffer "foo")      ; I'd already created this
>         (barf-if-buffer-read-only)
>         )
>       (current-buffer)
>     )
>     #<buffer *scratch*>
>
> 3) However, when I write that code to foo.el and run byte-compile-file
>    on it, I get the following error:
>
>     Compiling file /home/wbe/foo.el at Sat Jul 28 09:07:28 2012
>     Entering directory `/home/wbe/'
> --> foo.el:1:1:Warning: `save-excursion' defeated by `set-buffer'
>
> It looks to me like the Warning message is untrue.

In an example such as you show, you should use with-current-buffer
instead of save-excursion+set-buffer. I think this has been discussed
several times on this list and/or emacs-devel.

The compilation warning is because save-excursion is pointless in the
code as shown. Eg it doesn't preserve point in buffer foo (unless foo
happened to be current beforehand).





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

* bug#12079: 23.3; erroneous byte-compile-file warning
  2012-07-28 14:00 bug#12079: 23.3; erroneous byte-compile-file warning Winston
  2012-07-29 23:12 ` Glenn Morris
@ 2012-07-30  4:42 ` Winston
  2012-07-30  7:06   ` Alp Aker
  2012-07-30 13:40 ` Winston
  2 siblings, 1 reply; 5+ messages in thread
From: Winston @ 2012-07-30  4:42 UTC (permalink / raw)
  To: Glenn Morris; +Cc: 12079

I originally submitted (in part):
>>     (progn
>>       (save-excursion
>>         (set-buffer "foo")      ; I'd already created this
>>         (barf-if-buffer-read-only)
>>         )
>>       (current-buffer)
>>     )
>>     #<buffer *scratch*>
>>
>> 3) However, when I write that code to foo.el and run byte-compile-file
>>    on it, I get the following error:
>>
>>     Compiling file /home/wbe/foo.el at Sat Jul 28 09:07:28 2012
>>     Entering directory `/home/wbe/'
>> --> foo.el:1:1:Warning: `save-excursion' defeated by `set-buffer'
>>
>> It looks to me like the Warning message is untrue.

to which Glenn replied:
> In an example such as you show, you should use with-current-buffer
> instead of save-excursion+set-buffer.

OK.  Back when the code I was compiling was written, with-current-buffer
didn't exist.  After reading the description of with-current-buffer,
yes, it looks like it would be as good or better than save-excursion if
I feel like rewriting the code.

> I think this has been discussed several times on this list and/or
> emacs-devel.

I wouldn't know.  I'm not on that list.

> The compilation warning is because save-excursion is pointless in the
> code as shown.

I disagree.  When the body finishes, save-excursion changes back to the
buffer that was current when it was called.  Perhaps it's useless in a
trivial example like the one above, but "return to whatever the current
buffer is after making changes in some other buffer" can be useful in
subroutines.  Restoring the buffer is certainly not "pointless".

I think what you're trying to say is that using save-excursion instead
of with-current-buffer when the body begins with set-buffer is
"pointless" because save-excursion does nothing more than what
with-current-buffer would do.  That's a fair point, but that's NOT what
the warning message says.  The warning claims that save-excursion won't
work.

> Eg it doesn't preserve point in buffer foo (unless foo happened to be
> current beforehand).

Irrelevant.  You've agreed that save-excursion works exactly as the
documentation claims, restoring on exit the buffer, point, and mark as
of the time it was called, so the clear English meaning of the warning
is false -- no part of save-excursion's functionality was "defeated" by
calling set-buffer.

I recommend the current misleading warning be replaced with a suggestion
that using with-current-buffer would be more efficient, which is
apparently what was intended.
 -WBE





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

* bug#12079: 23.3; erroneous byte-compile-file warning
  2012-07-30  4:42 ` Winston
@ 2012-07-30  7:06   ` Alp Aker
  0 siblings, 0 replies; 5+ messages in thread
From: Alp Aker @ 2012-07-30  7:06 UTC (permalink / raw)
  To: Winston; +Cc: 12079

On Mon, Jul 30, 2012 at 12:42 AM, Winston <wbe@psr.com> wrote:
> I recommend the current misleading warning be replaced with a suggestion
> that using with-current-buffer would be more efficient, which is
> apparently what was intended.

The warning message was changed in just that way over a year ago.  If
you upgrade to v24.1 the warning you'll see is "Use
`with-current-buffer' rather than save-excursion+set-buffer".





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

* bug#12079: 23.3; erroneous byte-compile-file warning
  2012-07-28 14:00 bug#12079: 23.3; erroneous byte-compile-file warning Winston
  2012-07-29 23:12 ` Glenn Morris
  2012-07-30  4:42 ` Winston
@ 2012-07-30 13:40 ` Winston
  2 siblings, 0 replies; 5+ messages in thread
From: Winston @ 2012-07-30 13:40 UTC (permalink / raw)
  To: Alp Aker; +Cc: 12079

> The warning message was changed in just that way over a year ago.  If
> you upgrade to v24.1 the warning you'll see is "Use
> `with-current-buffer' rather than save-excursion+set-buffer".

Sounds good.  bug#12079 is resolved, then.
 -WBE





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

end of thread, other threads:[~2012-07-30 13:40 UTC | newest]

Thread overview: 5+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2012-07-28 14:00 bug#12079: 23.3; erroneous byte-compile-file warning Winston
2012-07-29 23:12 ` Glenn Morris
2012-07-30  4:42 ` Winston
2012-07-30  7:06   ` Alp Aker
2012-07-30 13:40 ` Winston

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