From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Newsgroups: gmane.emacs.bugs Subject: bug#69739: 30.0.50; `type-of` is not precise enough Date: Tue, 12 Mar 2024 10:40:52 -0400 Message-ID: References: Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="38444"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) To: 69739@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Tue Mar 12 15:44:28 2024 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 1rk3Mg-0009lt-Kn for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 12 Mar 2024 15:44:27 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rk3K6-0006GT-Pq; Tue, 12 Mar 2024 10:41:47 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1rk3Jo-00060d-JD for bug-gnu-emacs@gnu.org; Tue, 12 Mar 2024 10:41:31 -0400 Original-Received: from debbugs.gnu.org ([2001:470:142:5::43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1rk3Jo-0008AU-AC for bug-gnu-emacs@gnu.org; Tue, 12 Mar 2024 10:41:28 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rk3KM-0002XT-GL for bug-gnu-emacs@gnu.org; Tue, 12 Mar 2024 10:42:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Tue, 12 Mar 2024 14:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 69739 X-GNU-PR-Package: emacs Original-Received: via spool by 69739-submit@debbugs.gnu.org id=B69739.17102545179746 (code B ref 69739); Tue, 12 Mar 2024 14:42:02 +0000 Original-Received: (at 69739) by debbugs.gnu.org; 12 Mar 2024 14:41:57 +0000 Original-Received: from localhost ([127.0.0.1]:43346 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rk3KG-0002X5-Jg for submit@debbugs.gnu.org; Tue, 12 Mar 2024 10:41:57 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:16047) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rk3KC-0002Wg-VY for 69739@debbugs.gnu.org; Tue, 12 Mar 2024 10:41:54 -0400 Original-Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id BE2891001CE; Tue, 12 Mar 2024 10:40:55 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1710254454; bh=lzA7Ps08mChwEs206/5KJBfUhZihl7hU3IcI7bfdlBc=; h=From:To:Subject:In-Reply-To:References:Date:From; b=V874WcC8OeBkwzIx96nnamoQfcjXqPqivXdGT4D/77ekUYDMFV5gcaHFZkNEVEPM2 bTK6KDSSmmKfI9WbnQFGYZbk5Z+l9r+7516tc8+r3ZyAi1mrs/ew4d7PNwPGaWIgrC ZclJwuZwnV5H42KWBOGGtFpGCwmChQFhG9yDmw4OPPqchLgKru1n3RzQ+GKJGXsRBp hQwlRBs9QL6yOn9rgxVKChXBlv6d3hqKLraJi0NX74eb2uPRkMLP0UMoCLFBFkeRUa wjHGSb7y3VW0qZGU/IqOeABCi/9ru+E7LU8Rqc8Z300MGFkPJ+h26l8Y9NHNSnFLQc g6ed4f7+kWinA== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 3E7C3100051; Tue, 12 Mar 2024 10:40:54 -0400 (EDT) Original-Received: from pastel (69-165-147-56.dsl.teksavvy.com [69.165.147.56]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 21075120024; Tue, 12 Mar 2024 10:40:54 -0400 (EDT) In-Reply-To: (Stefan Monnier's message of "Tue, 12 Mar 2024 09:37:49 -0400") 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-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.bugs:281520 Archived-At: --=-=-= Content-Type: text/plain > 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 --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-type-of-Return-more-precise-types-bug-69739.patch >From dcea90a70e96d238650f771b2a26348d73b1fa05 Mon Sep 17 00:00:00 2001 From: Stefan Monnier 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 --=-=-=--