bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#69739: 30.0.50; `type-of` is not precise enough


From: Stefan Monnier
Subject: bug#69739: 30.0.50; `type-of` is not precise enough
Date: Tue, 12 Mar 2024 10:40:52 -0400
User-agent: Gnus/5.13 (Gnus v5.13)

> To make it more concrete, here's a suggested patch.

Hmm... got a bit carried away on the simplification of
cl-generic, sorry.
Here's a better patch that does work.


        Stefan
>From dcea90a70e96d238650f771b2a26348d73b1fa05 Mon Sep 17 00:00:00 2001
From: Stefan Monnier <monnier@iro.umontreal.ca>
Date: Tue, 12 Mar 2024 09:26:24 -0400
Subject: [PATCH] (type-of): Return more precise types (bug#69739)

* src/data.c (Ftype_of): Give more precise types for nil, t, and the
three subcases of `subr`.
(syms_of_data): Define the corresponding new symbols.

* lisp/emacs-lisp/cl-preloaded.el (subr): Demote it to `atom`.
(subr-native-elisp, subr-primitive): Add `compiled-function` as
parent instead.
(special-form): New type.
(finalizer): New (previously missing) type.

* lisp/emacs-lisp/seq.el (seq-remove-at-position): Adjust to `type-of`
returning `null` for nil.

* lisp/obsolete/eieio-core.el (cl--generic-struct-tag):
* lisp/emacs-lisp/cl-generic.el (cl--generic-typeof-generalizer): Simplify.

* test/lisp/emacs-lisp/ert-tests.el (ert-test-plist-difference-explanation)
(ert-test-explain-equal, ert-test-explain-equal-string-properties):
Adjust expected answers.

* doc/lispref/objects.texi (Type Predicates): Update.
---
 doc/lispref/objects.texi          | 13 +++++++------
 etc/NEWS                          |  5 +++++
 lisp/emacs-lisp/cl-generic.el     |  3 +--
 lisp/emacs-lisp/cl-preloaded.el   | 11 +++++++----
 lisp/emacs-lisp/eieio-core.el     |  2 +-
 lisp/emacs-lisp/seq.el            |  2 +-
 src/data.c                        | 14 +++++++++++---
 test/lisp/emacs-lisp/ert-tests.el |  8 ++++----
 8 files changed, 37 insertions(+), 21 deletions(-)

diff --git a/doc/lispref/objects.texi b/doc/lispref/objects.texi
index 279f449a994..00581814825 100644
--- a/doc/lispref/objects.texi
+++ b/doc/lispref/objects.texi
@@ -1485,8 +1485,8 @@ Type Descriptors
 @subsection Type Descriptors
 
   A @dfn{type descriptor} is a @code{record} which holds information
-about a type.  Slot 1 in the record must be a symbol naming the type, and
-@code{type-of} relies on this to return the type of @code{record}
+about a type.  The first slot in the record must be a symbol naming the type,
+and @code{type-of} relies on this to return the type of @code{record}
 objects.  No other type descriptor slot is used by Emacs; they are
 free for use by Lisp extensions.
 
@@ -2186,8 +2186,9 @@ Type Predicates
 @code{float}, @code{font-entity}, @code{font-object},
 @code{font-spec}, @code{frame}, @code{hash-table}, @code{integer},
 @code{marker}, @code{mutex}, @code{obarray}, @code{overlay}, @code{process},
-@code{string}, @code{subr}, @code{symbol}, @code{thread},
-@code{vector}, @code{window}, or @code{window-configuration}.
+@code{string}, @code{subr-primitive}, @code{subr-native-elisp},
+@code{special-form}, @code{symbol}, @code{null}, @code{boolean},
+@code{thread}, @code{vector}, @code{window}, or @code{window-configuration}.
 However, if @var{object} is a record, the type specified by its first
 slot is returned; @ref{Records}.
 
@@ -2196,9 +2197,9 @@ Type Predicates
      @result{} integer
 @group
 (type-of 'nil)
-     @result{} symbol
+     @result{} null
 (type-of '())    ; @r{@code{()} is @code{nil}.}
-     @result{} symbol
+     @result{} null
 (type-of '(x))
      @result{} cons
 (type-of (record 'foo))
diff --git a/etc/NEWS b/etc/NEWS
index 19cd170e5c7..d06b3f9c06d 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -62,6 +62,11 @@ more details.
 
 * Incompatible Changes in Emacs 30.1
 
+** 'type-of' sometimes returns more precise types.
+More specifically, for nil and t it returns 'null' and 'boolean'
+instead of just 'symbol' and for "subrs", it now returns one of
+'special-form', 'subr-primitive', or 'subr-native-elisp'.
+
 ** Tree-Sitter modes are now declared as submodes of the non-TS modes.
 In order to help the use of those Tree-Sitter modes, they are now
 declared to have the corresponding non-Tree-Sitter mode as an
diff --git a/lisp/emacs-lisp/cl-generic.el b/lisp/emacs-lisp/cl-generic.el
index 84eb800ec24..06f70146808 100644
--- a/lisp/emacs-lisp/cl-generic.el
+++ b/lisp/emacs-lisp/cl-generic.el
@@ -1339,8 +1339,7 @@ cl--generic-type-specializers
            (cl--class-allparents class)))))
 
 (cl-generic-define-generalizer cl--generic-typeof-generalizer
-  ;; FIXME: We could also change `type-of' to return `null' for nil.
-  10 (lambda (name &rest _) `(if ,name (type-of ,name) 'null))
+  10 (lambda (name &rest _) `(type-of ,name))
   #'cl--generic-type-specializers)
 
 (cl-defmethod cl-generic-generalizers :extra "typeof" (type)
diff --git a/lisp/emacs-lisp/cl-preloaded.el b/lisp/emacs-lisp/cl-preloaded.el
index 5743684fa89..7ab39fed222 100644
--- a/lisp/emacs-lisp/cl-preloaded.el
+++ b/lisp/emacs-lisp/cl-preloaded.el
@@ -355,6 +355,7 @@ frame
 (cl--define-built-in-type buffer atom)
 (cl--define-built-in-type window atom)
 (cl--define-built-in-type process atom)
+(cl--define-built-in-type finalizer atom)
 (cl--define-built-in-type window-configuration atom)
 (cl--define-built-in-type overlay atom)
 (cl--define-built-in-type number-or-marker atom
@@ -415,15 +416,17 @@ compiled-function
   "Abstract type of functions that have been compiled.")
 (cl--define-built-in-type byte-code-function (compiled-function)
   "Type of functions that have been byte-compiled.")
-(cl--define-built-in-type subr (compiled-function)
+(cl--define-built-in-type subr (atom)
   "Abstract type of functions compiled to machine code.")
 (cl--define-built-in-type module-function (function)
   "Type of functions provided via the module API.")
 (cl--define-built-in-type interpreted-function (function)
   "Type of functions that have not been compiled.")
-(cl--define-built-in-type subr-native-elisp (subr)
-  "Type of function that have been compiled by the native compiler.")
-(cl--define-built-in-type subr-primitive (subr)
+(cl--define-built-in-type special-form (subr)
+  "Type of the core syntactic elements of the Emacs Lisp language.")
+(cl--define-built-in-type subr-native-elisp (subr compiled-function)
+  "Type of functions that have been compiled by the native compiler.")
+(cl--define-built-in-type subr-primitive (subr compiled-function)
   "Type of functions hand written in C.")
 
 (unless (cl--class-parents (cl--find-class 'cl-structure-object))
diff --git a/lisp/emacs-lisp/eieio-core.el b/lisp/emacs-lisp/eieio-core.el
index a2f7c4172a3..8221625d885 100644
--- a/lisp/emacs-lisp/eieio-core.el
+++ b/lisp/emacs-lisp/eieio-core.el
@@ -1046,7 +1046,7 @@ 'inconsistent-class-hierarchy
 
 (defun cl--generic-struct-tag (name &rest _)
   ;; Use exactly the same code as for `typeof'.
-  `(if ,name (type-of ,name) 'null))
+  `(type-of ,name))
 
 (cl-generic-define-generalizer eieio--generic-generalizer
   ;; Use the exact same tagcode as for cl-struct, so that methods
diff --git a/lisp/emacs-lisp/seq.el b/lisp/emacs-lisp/seq.el
index 20077db9e60..cd6f26abdb3 100644
--- a/lisp/emacs-lisp/seq.el
+++ b/lisp/emacs-lisp/seq.el
@@ -363,7 +363,7 @@ seq-remove-at-position
 The result is a sequence of the same type as SEQUENCE."
   (seq-concatenate
    (let ((type (type-of sequence)))
-     (if (eq type 'cons) 'list type))
+     (if (memq type '(null cons)) 'list type))
    (seq-subseq sequence 0 n)
    (seq-subseq sequence (1+ n))))
 
diff --git a/src/data.c b/src/data.c
index 35f4c82c68f..35bd3b19e45 100644
--- a/src/data.c
+++ b/src/data.c
@@ -202,7 +202,9 @@ DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
       return Qinteger;
 
     case Lisp_Symbol:
-      return Qsymbol;
+      return NILP (object) ? Qnull
+             : EQ (object, Qt) ? Qboolean
+             : Qsymbol;
 
     case Lisp_String:
       return Qstring;
@@ -224,7 +226,10 @@ DEFUN ("type-of", Ftype_of, Stype_of, 1, 1, 0,
         case PVEC_WINDOW_CONFIGURATION: return Qwindow_configuration;
         case PVEC_PROCESS: return Qprocess;
         case PVEC_WINDOW: return Qwindow;
-        case PVEC_SUBR: return Qsubr;
+        case PVEC_SUBR:
+          return XSUBR (object)->max_args == UNEVALLED ? Qspecial_form
+                 : SUBR_NATIVE_COMPILEDP (object) ? Qsubr_native_elisp
+                 : Qsubr_primitive;
         case PVEC_COMPILED: return Qcompiled_function;
         case PVEC_BUFFER: return Qbuffer;
         case PVEC_CHAR_TABLE: return Qchar_table;
@@ -4202,6 +4207,7 @@ #define PUT_ERROR(sym, tail, msg)                 \
             "Variable binding depth exceeds max-specpdl-size");
 
   /* Types that type-of returns.  */
+  DEFSYM (Qboolean, "boolean");
   DEFSYM (Qinteger, "integer");
   DEFSYM (Qsymbol, "symbol");
   DEFSYM (Qstring, "string");
@@ -4217,7 +4223,9 @@ #define PUT_ERROR(sym, tail, msg)                 \
   DEFSYM (Qwindow_configuration, "window-configuration");
   DEFSYM (Qprocess, "process");
   DEFSYM (Qwindow, "window");
-  DEFSYM (Qsubr, "subr");
+  DEFSYM (Qspecial_form, "special-form");
+  DEFSYM (Qsubr_primitive, "subr-primitive");
+  DEFSYM (Qsubr_native_elisp, "subr-native-elisp");
   DEFSYM (Qcompiled_function, "compiled-function");
   DEFSYM (Qbuffer, "buffer");
   DEFSYM (Qframe, "frame");
diff --git a/test/lisp/emacs-lisp/ert-tests.el 
b/test/lisp/emacs-lisp/ert-tests.el
index 1aff73d66f6..289069362b3 100644
--- a/test/lisp/emacs-lisp/ert-tests.el
+++ b/test/lisp/emacs-lisp/ert-tests.el
@@ -674,7 +674,7 @@ ert-test-string-first-line
 
 (ert-deftest ert-test-explain-equal ()
   (should (equal (ert--explain-equal nil 'foo)
-                 '(different-atoms nil foo)))
+                 '(different-types nil foo)))
   (should (equal (ert--explain-equal '(a a) '(a b))
                  '(list-elt 1 (different-atoms a b))))
   (should (equal (ert--explain-equal '(1 48) '(1 49))
@@ -732,10 +732,10 @@ ert-test-plist-difference-explanation
                  nil))
   (should (equal (ert--plist-difference-explanation
                   '(a b c t) '(a b))
-                 '(different-properties-for-key c (different-atoms t nil))))
+                 '(different-properties-for-key c (different-types t nil))))
   (should (equal (ert--plist-difference-explanation
                   '(a b c t) '(c nil a b))
-                 '(different-properties-for-key c (different-atoms t nil))))
+                 '(different-properties-for-key c (different-types t nil))))
   (should (equal (ert--plist-difference-explanation
                   '(a b c (foo . bar)) '(c (foo . baz) a b))
                  '(different-properties-for-key c
@@ -778,7 +778,7 @@ ert-test-explain-equal-string-properties
                   #("foo" 0 1 (a b))
                   "foo")
                  '(char 0 "f"
-                        (different-properties-for-key a (different-atoms b 
nil))
+                        (different-properties-for-key a (different-types b 
nil))
                         context-before ""
                         context-after "oo")))
   (should (equal (ert--explain-equal-including-properties-rec
-- 
2.43.0


reply via email to

[Prev in Thread] Current Thread [Next in Thread]