unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Basil L. Contovounesios" <contovob@tcd.ie>
To: "João Távora" <joaotavora@gmail.com>
Cc: darthandrus@gmail.com, 40693@debbugs.gnu.org,
	Dmitry Gutov <dgutov@yandex.ru>
Subject: bug#40693: 28.0.50; json-encode-alist changes alist
Date: Sat, 23 May 2020 17:13:57 +0100	[thread overview]
Message-ID: <87wo52ochm.fsf@tcd.ie> (raw)
In-Reply-To: <87eerbybfn.fsf@gmail.com> ("João Távora"'s message of "Fri, 22 May 2020 21:14:20 +0100")

João Távora <joaotavora@gmail.com> writes:

> "Basil L. Contovounesios" <contovob@tcd.ie> writes:
>
>>> Am I missing something or doesn't this work like you want?
>>
>> There's a problem with the conditional require at byte-compile time.  If
>> the version of Emacs doing the byte-compilation has native JSON support,
>> then it will generate invalid byte-code (and complain) for the case
>> where there is no native JSON support.  In other words, there will be a
>> bug when an Emacs without native JSON loads a file that was
>> byte-compiled in an Emacs with native JSON.
>
> This doesn't give any any warning:
>
>     (defvar foo-42 42)
>      
>     (provide 'foo)
>     ;; foo.el ends here
>
> and a bar.el
>
>     (eval-and-compile
>       (defvar bar-have-native-42 t) ;; or nil
>      
>       (unless bar-have-native-42 
>         (require 'foo)))
>      
>     (defalias 'forty-two
>       (if (eval-when-compile bar-have-native-42)
>           (lambda () (message "%s" 42.0))
>         (lambda () (message "%s" foo-42))))
>     ;; bar.el ends here
>
> And it seems to work in both cases:
>
>      ~/tmp ❯❯❯ emacs -Q --batch -L . -f batch-byte-compile bar.el
>      ~/tmp ❯❯❯ emacs -Q --batch -L . -l bar.elc -f forty-two
>      42.0
>      # now change that t to nil
>      ~/tmp ❯❯❯ emacs -Q --batch -L . -f batch-byte-compile bar.el
>      ~/tmp ❯❯❯ emacs -Q --batch -L . -l bar.elc -f forty-two
>      42
>
> But indeed in the recipe I gave you, I had forgotten the second
> eval-when-compile.  If you take that away, it warns again.

This is not a representative example for the following reasons:

0. bar.el does not use lexical-binding.
1. The second lambda in forty-two does not let-bind foo-42.
2. If you byte-compile bar.el with bar-have-native-42 set to t, and then
   load bar.elc in an Emacs that has bar-have-native-42 set to nil, then
   42.0 gets printed, which is wrong.  This is due to the incorrect
   usage of eval-when-compile: we want the check to happen at runtime as
   well.

Try this instead:

;;; foo.el --- foo -*- lexical-binding: t -*-

(eval-and-compile
  (unless (fboundp 'json-parse-buffer)
    (require 'json)))

(defalias 'foo
  (if (eval-when-compile (fboundp 'json-parse-buffer))
      (lambda ()
        (json-parse-buffer :object-type 'plist
                           :null-object nil
                           :false-object :json-false))
    (lambda ()
      (let ((json-object-type 'plist))
        (json-read)))))

;;; foo.el ends here

> No idea how to check if byte-code is "valid" or not: I just check the
> warnings.  Can you tell me?

0. emacs -Q -batch -f batch-byte-compile foo.el
1. emacs -Q
2. (fset 'json-parse-buffer nil) C-j
3. M-x load-file RET foo.elc RET
4. (disassemble 'foo) C-j

This prints:

  byte code for foo:
    args: nil
  0       constant  plist
  1       constant  json-read
  2       call      0
  3       return    

whereas I'd expect:

  byte code for foo:
    args: nil
  0       constant  plist
  1       varbind   json-object-type
  2       constant  json-read
  3       call      0
  4       unbind    1
  5       return    

>> Thanks.  I've therefore gone with the original patch[1], as it exhibits
>> correct behaviour and is the least intrusive, but you should obviously
>> feel free to tweak it as you prefer.
>
> I think I'll let it be, the reason I'm not a huge fan is the foward
> declarations of individual functions and variables, but it's not so bad.

I think the declarations make the intention explicit to both the reader
and the byte-compiler in a simple way, without wrestling the
eval-*-compile machinery or allowing for subtle bugs like the ones
above.

Thanks,

-- 
Basil





  reply	other threads:[~2020-05-23 16:13 UTC|newest]

Thread overview: 40+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-04-18  2:59 bug#40693: 28.0.50; json-encode-alist changes alist Ivan Andrus
2020-04-18 17:29 ` Dmitry Gutov
2020-04-18 21:00   ` Ivan Andrus
2020-04-18 23:13   ` Michael Heerdegen
2020-04-19  0:10     ` Dmitry Gutov
2020-04-19  0:29       ` Michael Heerdegen
2020-04-19  0:33     ` Basil L. Contovounesios
2020-04-19  0:34   ` Basil L. Contovounesios
2020-04-29 10:11     ` Basil L. Contovounesios
2020-04-29 10:30       ` Eli Zaretskii
2020-04-29 12:08         ` Dmitry Gutov
2020-04-29 12:21           ` Eli Zaretskii
2020-04-29 14:28             ` Dmitry Gutov
2020-04-29 14:40               ` Eli Zaretskii
2020-04-29 15:02                 ` Dmitry Gutov
2020-04-29 15:07                   ` Eli Zaretskii
2020-04-29 14:41             ` Basil L. Contovounesios
2020-05-18  1:24               ` Basil L. Contovounesios
2020-05-18 14:27                 ` Eli Zaretskii
2020-05-18 22:50                 ` Dmitry Gutov
2020-05-18 23:50                 ` João Távora
2020-05-21 21:14                   ` Basil L. Contovounesios
2020-05-21 22:16                     ` João Távora
2020-05-22 14:54                       ` Basil L. Contovounesios
2020-05-22 20:14                         ` João Távora
2020-05-23 16:13                           ` Basil L. Contovounesios [this message]
2020-05-23 19:40                             ` João Távora
2020-05-23 22:41                               ` Basil L. Contovounesios
2020-05-23 22:45                                 ` João Távora
2020-04-19 20:35 ` Paul Eggert
2020-04-19 21:01   ` Drew Adams
2020-04-19 22:14     ` Paul Eggert
2020-04-19 22:29       ` Michael Heerdegen
2020-04-19 23:59         ` Paul Eggert
2020-04-20  0:25           ` Michael Heerdegen
2020-04-20  0:32             ` Paul Eggert
2020-04-20  0:57               ` Michael Heerdegen
2020-04-20  2:55                 ` Paul Eggert
2020-04-20 14:56                   ` Eli Zaretskii
2020-04-20  5:45                 ` Drew Adams

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87wo52ochm.fsf@tcd.ie \
    --to=contovob@tcd.ie \
    --cc=40693@debbugs.gnu.org \
    --cc=darthandrus@gmail.com \
    --cc=dgutov@yandex.ru \
    --cc=joaotavora@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
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).