unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Paul Eggert <eggert@cs.ucla.edu>
Cc: monnier@iro.umontreal.ca, 34781@debbugs.gnu.org
Subject: bug#34781: 27.0.50; integer in pcase sometimes compared by eq
Date: Thu, 28 Mar 2019 22:51:39 +0100	[thread overview]
Message-ID: <3a02a3378a526c8706b33f36b85c003a82aa2aa1.camel@acm.org> (raw)
In-Reply-To: <95f744d5-426d-1ce1-15fa-9c49aaf6f2f0@cs.ucla.edu>

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

tor 2019-03-28 klockan 13:30 -0700 skrev Paul Eggert:

> If we have the bounds, then portable-fixnum-p is merely a
> convenience, no?

Yes. We can drop it if you prefer it to be open-coded in pcase and
elsewhere, but I thought the predicate would make sense.

> I'd prefer the names most-negative-portable-fixnum and
> most-positive-portable-fixnum. Their documentation should make it
> clear
> what the portability test is for. Presumably the test applies just to
> this version of Emacs, since future versions might change the
> portable
> fixnum bounds.

Here is a patch for that, and an updated pcase patch.


[-- Attachment #2: 0001-Add-bounds-for-portable-fixnums-and-portable-fixnum-.patch --]
[-- Type: text/x-patch, Size: 4717 bytes --]

From f5fe5d987090e69b0f7438435f69c6aab4215e5c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Thu, 28 Mar 2019 22:12:37 +0100
Subject: [PATCH 1/2] Add bounds for portable fixnums, and portable-fixnum-p

These are useful for macros that need to detect whether a number is a
fixnum on any machine, so that the bytecode becomes portable (Bug#34781).

* src/lisp.h (LEAST_EMACS_INT_MAX, MOST_POSITIVE_PORTABLE_FIXNUM,
MOST_NEGATIVE_PORTABLE_FIXNUM):
* src/data.c (most-positive-portable-fixnum, most-negative-portable-fixnum):
* lisp/subr.el (portable-fixnum-p):
New.
* etc/NEWS (Lisp Changes): Mention portable-fixnum-p.
---
 etc/NEWS     |  4 ++++
 lisp/subr.el |  6 ++++++
 src/data.c   | 18 ++++++++++++++++++
 src/lisp.h   | 12 ++++++++++++
 4 files changed, 40 insertions(+)

diff --git a/etc/NEWS b/etc/NEWS
index 7486d6bcfe..a2933fbbd3 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -1424,6 +1424,10 @@ like 'file-attributes' that compute file sizes and other attributes,
 functions like 'process-id' that compute process IDs, and functions like
 'user-uid' and 'group-gid' that compute user and group IDs.
 
+Since the size of fixnums varies between platforms, the new predicate
+'portable-fixnum-p' can be used to determine whether a number is
+a fixnum on any machine running the current Emacs version.
+
 +++
 ** Although the default timestamp format is still (HI LO US PS),
 it is planned to change in a future Emacs version, to exploit bignums.
diff --git a/lisp/subr.el b/lisp/subr.el
index f1a1dddd81..950a0b58e3 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -376,6 +376,12 @@ bignump
   "Return t if OBJECT is a bignum."
   (and (integerp object) (not (fixnump object))))
 
+(defun portable-fixnum-p (object)
+  "Return t if OBJECT is a fixnum on any machine running the current
+Emacs version."
+  (and (integerp object)
+       (<= most-negative-portable-fixnum object most-positive-portable-fixnum)))
+
 (defun lsh (value count)
   "Return VALUE with its bits shifted left by COUNT.
 If COUNT is negative, shifting is actually to the right.
diff --git a/src/data.c b/src/data.c
index 15b6106cfe..2969f2df82 100644
--- a/src/data.c
+++ b/src/data.c
@@ -4110,6 +4110,24 @@ This variable cannot be set; trying to do so will signal an error.  */);
   Vmost_negative_fixnum = make_fixnum (MOST_NEGATIVE_FIXNUM);
   make_symbol_constant (intern_c_string ("most-negative-fixnum"));
 
+  DEFVAR_LISP ("most-positive-portable-fixnum",
+               Vmost_positive_portable_fixnum,
+               doc: /* The greatest integer that is represented efficiently
+on any machine running this version of Emacs.
+This variable can be used to ensure portability of bytecode that works
+with fixnums.  It cannot be set; trying to do so will signal an error.  */);
+  Vmost_positive_portable_fixnum = make_fixnum(MOST_POSITIVE_PORTABLE_FIXNUM);
+  make_symbol_constant (intern_c_string ("most-positive-portable-fixnum"));
+
+  DEFVAR_LISP ("most-negative-portable-fixnum",
+               Vmost_negative_portable_fixnum,
+               doc: /* The least integer that is represented efficiently
+on any machine running this version of Emacs.
+This variable can be used to ensure portability of bytecode that works
+with fixnums.  It cannot be set; trying to do so will signal an error.  */);
+  Vmost_negative_portable_fixnum = make_fixnum(MOST_NEGATIVE_PORTABLE_FIXNUM);
+  make_symbol_constant (intern_c_string ("most-negative-portable-fixnum"));
+
   DEFSYM (Qwatchers, "watchers");
   DEFSYM (Qmakunbound, "makunbound");
   DEFSYM (Qunlet, "unlet");
diff --git a/src/lisp.h b/src/lisp.h
index 178eebed2a..bf1f0a0bf5 100644
--- a/src/lisp.h
+++ b/src/lisp.h
@@ -112,6 +112,13 @@ enum { EMACS_INT_WIDTH = LLONG_WIDTH, EMACS_UINT_WIDTH = ULLONG_WIDTH };
 # endif
 #endif
 
+/* The smallest portable value of EMACS_INT_MAX.  */
+#define LEAST_EMACS_INT_MAX 2147483647   /* 2**31 - 1 */
+
+#if EMACS_INT_MAX < LEAST_EMACS_INT_MAX
+# error "EMACS_INT_MAX less than LEAST_EMACS_INT_MAX"
+#endif
+
 /* Number of bits to put in each character in the internal representation
    of bool vectors.  This should not vary across implementations.  */
 enum {  BOOL_VECTOR_BITS_PER_CHAR =
@@ -1146,6 +1153,11 @@ enum More_Lisp_Bits
 #define MOST_POSITIVE_FIXNUM (EMACS_INT_MAX >> INTTYPEBITS)
 #define MOST_NEGATIVE_FIXNUM (-1 - MOST_POSITIVE_FIXNUM)
 
+/* Largest and smallest values that are guaranteed to be representable
+   as fixnums on any machine.  These are the C values.  */
+#define MOST_POSITIVE_PORTABLE_FIXNUM (LEAST_EMACS_INT_MAX >> INTTYPEBITS)
+#define MOST_NEGATIVE_PORTABLE_FIXNUM (-1 - MOST_POSITIVE_PORTABLE_FIXNUM)
+
 #if USE_LSB_TAG
 
 INLINE Lisp_Object
-- 
2.20.1


[-- Attachment #3: 0002-Don-t-match-integers-with-memq-in-pcase.patch --]
[-- Type: text/x-patch, Size: 2017 bytes --]

From 5054ac21b6bdb522437d97db2a514a53d8ce7773 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Tue, 12 Mar 2019 13:19:35 +0100
Subject: [PATCH 2/2] Don't match integers with `memq' in `pcase'

* lisp/emacs-lisp/pcase.el (pcase--u1):
Use portable-fixnum-p instead of integerp as criterion for memq (Bug#34781).
* test/lisp/emacs-lisp/pcase-tests.el (pcase-tests-member): Test the above.
---
 lisp/emacs-lisp/pcase.el            | 2 +-
 test/lisp/emacs-lisp/pcase-tests.el | 6 ++++++
 2 files changed, 7 insertions(+), 1 deletion(-)

diff --git a/lisp/emacs-lisp/pcase.el b/lisp/emacs-lisp/pcase.el
index 9de2401549..a13694ed33 100644
--- a/lisp/emacs-lisp/pcase.el
+++ b/lisp/emacs-lisp/pcase.el
@@ -792,7 +792,7 @@ pcase--u1
                    (let ((upat (cddr alt)))
                      (eq (car-safe upat) 'quote)))
               (let ((val (cadr (cddr alt))))
-                (unless (or (integerp val) (symbolp val))
+                (unless (or (portable-fixnum-p val) (symbolp val))
                   (setq memq-ok nil))
                 (push (cadr (cddr alt)) simples))
             (push alt others))))
diff --git a/test/lisp/emacs-lisp/pcase-tests.el b/test/lisp/emacs-lisp/pcase-tests.el
index 1e9d37fbfa..29f02cbaa9 100644
--- a/test/lisp/emacs-lisp/pcase-tests.el
+++ b/test/lisp/emacs-lisp/pcase-tests.el
@@ -56,6 +56,12 @@ pcase-tests-grep
            'member (macroexpand-all '(pcase x ((or "a" 2 3) body)))))
   (should-not (pcase-tests-grep
                'memq (macroexpand-all '(pcase x ((or "a" 2 3) body)))))
+  (should (pcase-tests-grep
+           'member (macroexpand-all '(pcase x ((or #x100000000 2 3)
+                                               body)))))
+  (should-not (pcase-tests-grep
+               'memq (macroexpand-all '(pcase x ((or #x100000000 2 3)
+                                                 body)))))
   (let ((exp (macroexpand-all
                       '(pcase x
                          ("a" body1)
-- 
2.20.1


  reply	other threads:[~2019-03-28 21:51 UTC|newest]

Thread overview: 19+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-03-07 15:13 bug#34781: 27.0.50; integer in pcase sometimes compared by eq Mattias Engdegård
     [not found] ` <handler.34781.B.15519717565134.ack@debbugs.gnu.org>
2019-03-12 12:24   ` bug#34781: Acknowledgement (27.0.50; integer in pcase sometimes compared by eq) Mattias Engdegård
2019-03-16 19:09     ` bug#34781: 27.0.50; integer in pcase sometimes compared by eq Mattias Engdegård
2019-03-28 18:25 ` Paul Eggert
2019-03-28 19:47   ` Michael Heerdegen
2019-03-28 20:33     ` Paul Eggert
2019-03-28 21:30       ` Michael Heerdegen
2019-03-28 19:51   ` Mattias Engdegård
2019-03-28 20:30     ` Paul Eggert
2019-03-28 21:51       ` Mattias Engdegård [this message]
2019-03-28 22:10         ` Paul Eggert
2019-03-28 22:11         ` Stefan Monnier
2019-03-28 22:20           ` Mattias Engdegård
2019-03-28 22:38             ` Paul Eggert
2019-03-28 23:03               ` Mattias Engdegård
2019-03-29  8:48         ` Eli Zaretskii
2019-03-29  9:52           ` Mattias Engdegård
2019-03-29 12:33             ` Eli Zaretskii
2019-03-28 19:43 ` Michael Heerdegen

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=3a02a3378a526c8706b33f36b85c003a82aa2aa1.camel@acm.org \
    --to=mattiase@acm.org \
    --cc=34781@debbugs.gnu.org \
    --cc=eggert@cs.ucla.edu \
    --cc=monnier@iro.umontreal.ca \
    /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).