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 09:37:49 -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="9253"; 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 14:38:57 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 1rk2LJ-000299-EZ for geb-bug-gnu-emacs@m.gmane-mx.org; Tue, 12 Mar 2024 14:38:57 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1rk2Kt-0005Ep-8T; Tue, 12 Mar 2024 09:38:31 -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 1rk2Kq-0005DE-Qu for bug-gnu-emacs@gnu.org; Tue, 12 Mar 2024 09:38:28 -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 1rk2Kq-00035P-Iv for bug-gnu-emacs@gnu.org; Tue, 12 Mar 2024 09:38:28 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1rk2LO-00067L-I3 for bug-gnu-emacs@gnu.org; Tue, 12 Mar 2024 09:39: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 13:39: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.171025071723483 (code B ref 69739); Tue, 12 Mar 2024 13:39:02 +0000 Original-Received: (at 69739) by debbugs.gnu.org; 12 Mar 2024 13:38:37 +0000 Original-Received: from localhost ([127.0.0.1]:42093 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rk2Ky-00066g-NR for submit@debbugs.gnu.org; Tue, 12 Mar 2024 09:38:37 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:41107) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1rk2Kv-00066Q-Rn for 69739@debbugs.gnu.org; Tue, 12 Mar 2024 09:38:35 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id CAC3F806EF; Tue, 12 Mar 2024 09:37:52 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1710250671; bh=DB1nZU6IUuQRtjtn5iKC1QfCe2VswDi5xynEVueh52M=; h=From:To:Subject:In-Reply-To:References:Date:From; b=jHtOS0axBWQcNXBvqevinpYK+0UihEH5AnvFRDThtOBBBNhr1aAK3j0nfI2+t0QTS 12rPccGV++4bcKAziEg4MkJWKSJmAyDpjE7yD+ED0gm6oGhcXbMSgb8A3y+ZgglfwL xMDjZiyfMIZrS6sLSeweUR1gFVHxK/kyMIfD96Fu/WGoBxJMd5zBovYoEibRa3fu42 awIsmnuWGfWMNGKDgDqtnAgD5ZYnT5vab0n4v6YMqx8ziVZBOnTO3ujnlNx33CS6vq i5Izm32hjuV4cRmNWj6Wkh7unSOrCHFqZp2YmGo/MMqsqHI+H/O3xo9AwgAzlD+mAo TMo98ec29EZXA== Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 3C191800D0; Tue, 12 Mar 2024 09:37:51 -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 1D160120472; Tue, 12 Mar 2024 09:37:51 -0400 (EDT) In-Reply-To: (Stefan Monnier's message of "Mon, 11 Mar 2024 19:19:40 -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:281514 Archived-At: --=-=-= Content-Type: text/plain To make it more concrete, here's a suggested patch. Comments? Objections? Stefan --=-=-= Content-Type: text/x-diff Content-Disposition: inline; filename=0001-type-of-Return-more-precise-types-bug-69739.patch >From 7b017522a11f8d8e0bafab85b7032d4e9763aeff 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-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 --=-=-=--