[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 09:37:49 -0400 |
User-agent: |
Gnus/5.13 (Gnus v5.13) |
To make it more concrete, here's a suggested patch.
Comments? Objections?
Stefan
>From 7b017522a11f8d8e0bafab85b7032d4e9763aeff 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-compat.el (eieio--generic-static-object-generalizer):
* lisp/emacs-lisp/cl-generic.el (cl--generic-typeof-generalizer): Simplify.
* doc/lispref/objects.texi (Type Predicates): Update accordingly.
---
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/seq.el | 2 +-
lisp/obsolete/eieio-compat.el | 2 +-
src/data.c | 14 +++++++++++---
7 files changed, 33 insertions(+), 17 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..cc7773ad4a2 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 #'type-of
#'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/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/lisp/obsolete/eieio-compat.el b/lisp/obsolete/eieio-compat.el
index 8fdcebbd1c4..c50524d359e 100644
--- a/lisp/obsolete/eieio-compat.el
+++ b/lisp/obsolete/eieio-compat.el
@@ -146,7 +146,7 @@ eieio--generic-static-symbol-generalizer
(cl-generic-define-generalizer eieio--generic-static-object-generalizer
;; Give it a slightly higher priority than `class' so that the
;; interleaved list comes before the class's non-interleaved list.
- 51 #'cl--generic-struct-tag
+ 51 #'type-of
(lambda (tag &rest _)
(and (symbolp tag) (setq tag (cl--find-class tag))
(eieio--class-p tag)
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");
--
2.43.0
- bug#69739: 30.0.50; `type-of` is not precise enough, Stefan Monnier, 2024/03/11
- bug#69739: 30.0.50; `type-of` is not precise enough,
Stefan Monnier <=
- bug#69739: 30.0.50; `type-of` is not precise enough, Eli Zaretskii, 2024/03/12
- bug#69739: 30.0.50; `type-of` is not precise enough, Stefan Monnier, 2024/03/12
- bug#69739: 30.0.50; `type-of` is not precise enough, Eli Zaretskii, 2024/03/12
- bug#69739: 30.0.50; `type-of` is not precise enough, Stefan Monnier, 2024/03/12
- bug#69739: 30.0.50; `type-of` is not precise enough, Eli Zaretskii, 2024/03/13
- bug#69739: 30.0.50; `type-of` is not precise enough, Stefan Monnier, 2024/03/14
- bug#69739: 30.0.50; `type-of` is not precise enough, Andrea Corallo, 2024/03/15