From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: "Basil L. Contovounesios" Newsgroups: gmane.emacs.bugs Subject: bug#40693: 28.0.50; json-encode-alist changes alist Date: Sat, 23 May 2020 17:13:57 +0100 Message-ID: <87wo52ochm.fsf@tcd.ie> References: <87a738jotd.fsf@tcd.ie> <87mu6utxd1.fsf@tcd.ie> <83r1w6ziql.fsf@gnu.org> <82572c3f-fb24-0b01-06ec-47067d9219ca@yandex.ru> <83blnazdly.fsf@gnu.org> <87wo5yqrqo.fsf@tcd.ie> <87ftbyavbq.fsf@tcd.ie> <87zha43ira.fsf@gmail.com> <87367t9ej7.fsf@tcd.ie> <87imgp2atd.fsf@gmail.com> <87sgfs81g6.fsf@tcd.ie> <87eerbybfn.fsf@gmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="5461"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux) Cc: darthandrus@gmail.com, 40693@debbugs.gnu.org, Dmitry Gutov To: =?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?= Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sat May 23 18:15:10 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1jcWnm-0001Ig-Ik for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 23 May 2020 18:15:10 +0200 Original-Received: from localhost ([::1]:37702 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1jcWnl-0000Cm-F3 for geb-bug-gnu-emacs@m.gmane-mx.org; Sat, 23 May 2020 12:15:09 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:42016) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1jcWne-0000CT-P2 for bug-gnu-emacs@gnu.org; Sat, 23 May 2020 12:15:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:52101) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1jcWne-00044U-GA for bug-gnu-emacs@gnu.org; Sat, 23 May 2020 12:15:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1jcWne-0000RP-CB for bug-gnu-emacs@gnu.org; Sat, 23 May 2020 12:15:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: "Basil L. Contovounesios" Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sat, 23 May 2020 16:15:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 40693 X-GNU-PR-Package: emacs X-GNU-PR-Keywords: fixed patch Original-Received: via spool by 40693-submit@debbugs.gnu.org id=B40693.15902504471603 (code B ref 40693); Sat, 23 May 2020 16:15:02 +0000 Original-Received: (at 40693) by debbugs.gnu.org; 23 May 2020 16:14:07 +0000 Original-Received: from localhost ([127.0.0.1]:35407 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jcWml-0000Pn-42 for submit@debbugs.gnu.org; Sat, 23 May 2020 12:14:07 -0400 Original-Received: from mail-wm1-f46.google.com ([209.85.128.46]:37522) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1jcWmj-0000Oz-Q0 for 40693@debbugs.gnu.org; Sat, 23 May 2020 12:14:06 -0400 Original-Received: by mail-wm1-f46.google.com with SMTP id f5so4693123wmh.2 for <40693@debbugs.gnu.org>; Sat, 23 May 2020 09:14:05 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=tcd-ie.20150623.gappssmtp.com; s=20150623; h=from:to:cc:subject:references:date:in-reply-to:message-id :user-agent:mime-version:content-transfer-encoding; bh=driYifvTaE7ysAELyuMQYJYNHs++p0NEzH/TeAkmhY0=; b=ytl+NQeR4sGCVBlmyrzxGzrHBC2ilcH3lZKdzqWA6zn2dTNWM4aPaCK2m/hUaJPlxj fJRDXjN7csx/HWu6MYf5U/7ML4BLi9R4IvjsPbYzJ9JMGnLgEiMRdWhL5y3kuy/pIYBD R5pQwwnmWzlP3BkoDhMsjTdbWL5YFNeZ+HiEzusvHNCo7x/8eTBaIaXPdvQY/nF+zCFw weNxUWNVf0l98zqcbskkQYmmr8pTm1DGiR1XkMggwOeNAsjUF5nfk7dYxtzxk/W44Lr/ zZ+mwIT+YOO5ZwQhczGlbg5V9UHXVg2wj2Vux464QCx4+8amXN975qvc8dB5E+mG0QeY YCCg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:cc:subject:references:date:in-reply-to :message-id:user-agent:mime-version:content-transfer-encoding; bh=driYifvTaE7ysAELyuMQYJYNHs++p0NEzH/TeAkmhY0=; b=syleSPcNExFBPTI0TKoEOQpMH/dDg2ZVp62z4203OEreTn5kkh9lYQgnbT57uEdjc3 y0mHbnf8fyFi5ppQZQJdasJTCSNwxbe99JuCY0gmTYNJsWYF7tqZZcdsJbPll+sgAO8a WQWXreKOIZXmh9GlOWDFN105jYhZQZYUDtbnqZBETI/Pk4XizMmS50LSHoGhNdnCUYlg KWhKKC11bwpVWXh9zFKpLfHhKvGlTH1inCm6S1tyfX1TRun92d4hNyiWCd/GUJEA74Ni bmIdv2S6YO7SXtKQjigc60xNFcU1Kaa9yrH0bvf/SVICqCYO/RCCDvj7DWjku6B2pvZ9 qwGw== X-Gm-Message-State: AOAM533PVVjmhiSjDYBdb8X4kH8oBUzbwhO5MIWAhRfroQqs0Sl8ex2v NMAFYwwgheNqKX2hK6uOkmqmtA== X-Google-Smtp-Source: ABdhPJyGQxdMgstn2Ke0fNwnbkSCt14m8L60J57QJqJ7vhWKxudMSyUXlt46/w4CdDvpbLB9HhG2FA== X-Received: by 2002:a1c:1b96:: with SMTP id b144mr17732112wmb.6.1590250439774; Sat, 23 May 2020 09:13:59 -0700 (PDT) Original-Received: from localhost ([2a02:8084:20e2:c380:1f68:7ff5:120d:64e]) by smtp.gmail.com with ESMTPSA id n9sm12136916wrv.43.2020.05.23.09.13.58 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Sat, 23 May 2020 09:13:58 -0700 (PDT) In-Reply-To: <87eerbybfn.fsf@gmail.com> ("=?UTF-8?Q?Jo=C3=A3o_?= =?UTF-8?Q?T=C3=A1vora?="'s message of "Fri, 22 May 2020 21:14:20 +0100") X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:180822 Archived-At: Jo=C3=A3o T=C3=A1vora writes: > "Basil L. Contovounesios" 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) >=20=20=20=20=20=20 > (provide 'foo) > ;; foo.el ends here > > and a bar.el > > (eval-and-compile > (defvar bar-have-native-42 t) ;; or nil >=20=20=20=20=20=20 > (unless bar-have-native-42=20 > (require 'foo))) >=20=20=20=20=20=20 > (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 =E2=9D=AF=E2=9D=AF=E2=9D=AF emacs -Q --batch -L . -f batch-byt= e-compile bar.el > ~/tmp =E2=9D=AF=E2=9D=AF=E2=9D=AF emacs -Q --batch -L . -l bar.elc -= f forty-two > 42.0 > # now change that t to nil > ~/tmp =E2=9D=AF=E2=9D=AF=E2=9D=AF emacs -Q --batch -L . -f batch-byt= e-compile bar.el > ~/tmp =E2=9D=AF=E2=9D=AF=E2=9D=AF 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=20=20=20=20 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=20=20=20=20 >> 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, --=20 Basil