From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Thuna Newsgroups: gmane.emacs.devel Subject: Recognizing declares in lambdas Date: Tue, 30 Jul 2024 23:29:51 +0200 Message-ID: <87ikwm1sc0.fsf@gmail.com> 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="39834"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Wed Jul 31 04:22:07 2024 Return-path: Envelope-to: ged-emacs-devel@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 1sYyyd-000AF8-7B for ged-emacs-devel@m.gmane-mx.org; Wed, 31 Jul 2024 04:22:07 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sYyy5-00013Q-K4; Tue, 30 Jul 2024 22:21:33 -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 1sYuPu-0002BE-Fp for emacs-devel@gnu.org; Tue, 30 Jul 2024 17:29:58 -0400 Original-Received: from mail-ed1-x536.google.com ([2a00:1450:4864:20::536]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1sYuPs-0005da-GY for emacs-devel@gnu.org; Tue, 30 Jul 2024 17:29:58 -0400 Original-Received: by mail-ed1-x536.google.com with SMTP id 4fb4d7f45d1cf-5af326eddb2so484324a12.1 for ; Tue, 30 Jul 2024 14:29:55 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1722374994; x=1722979794; darn=gnu.org; h=mime-version:message-id:date:subject:to:from:from:to:cc:subject :date:message-id:reply-to; bh=m4RxaIsyQ4wIRT7G8W3WcYdMIW6ft5Gstm0CQOvUVLs=; b=KC+mU1/FO0Gou08OCppWDtJXX4b4XYOJqsPmcJ9C3fTe40aEfL2Fk/Adze1n5h9U/B hhXivMNgrcWTCY3sBA5Zd5oT2AFq1ofIngDidu57Knf6HHPTxLjz4tW2LSTPE2fLSRyq FqjrovQrPoV45nGHuONw9LgEwHs/2TJmUgi4pzN3jf/15LkLUwhkM7bs8W0AMvcdF0u+ LTsgIntN1+IlTB6yrKZ7+VyMIex/01JUDP8g3WJ7p/7km4cIVwgt6LLpP3f9T324b4+Z oDCSYdHbVyPXHKgx8Su+Q5a3nJp6I6iJvZTfVH5MspdPz2IYegAiItZw78wSyIKqfp+W NmVA== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1722374994; x=1722979794; h=mime-version:message-id:date:subject:to:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=m4RxaIsyQ4wIRT7G8W3WcYdMIW6ft5Gstm0CQOvUVLs=; b=GhtiG60nqjK7GAWSREkamU4cZmPznXjANV4k34b+a90h4AaFSSewI8noYKDdfaKZnO /Vw48h/6vLARlEHV+MeoU1reH3FnItP3f7ekY0SGclosClxK+G6Ql5EzwXlxvaXCxIPj LdFn6uqJDYqD18O6mbSSCEgcwyxQYINVP+rXEcn3sdgk29yMk1fIPLf/s44eMRFh8zrc d3pa8TMzyqFWS/n5Z9eO7Bbz+SliGM0GT+WY4CSdc5LXJEP0oliIA5+MHoOtwCF3N71J ttxv1NRWAYJtH+8GxZPU0wajZ3y3N47lB56RCAjEnLYENOQtRl2CLj4pJAmIsYe8VqSN 2mJg== X-Gm-Message-State: AOJu0YyAOcaIVYl6XusHh049+RTFoh5HC6Oxq9VhKP+RgLwejGHpbrBi 1XmByBu5XSqMqzkR7omsROnElE2/bJIzDaevtLLdjQelcQ9KmzpLNUp3sw== X-Google-Smtp-Source: AGHT+IHMiNyUlR5izX/CWHb5bGlxtUEbraICBwIk9w5jJH6dAwMr9lX7Z/rl+t9GDdAMNnjKCXupCQ== X-Received: by 2002:a17:906:6a16:b0:a7a:afe8:1013 with SMTP id a640c23a62f3a-a7d858b380amr327191566b.1.1722374993461; Tue, 30 Jul 2024 14:29:53 -0700 (PDT) Original-Received: from thuna-lis3 ([85.106.105.81]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-a7acad90486sm689222266b.150.2024.07.30.14.29.52 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Tue, 30 Jul 2024 14:29:52 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::536; envelope-from=thuna.cing@gmail.com; helo=mail-ed1-x536.google.com X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 30 Jul 2024 22:21:32 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:322225 Archived-At: --=-=-= Content-Type: text/plain Right now `declare' is essentially a hack; it only works because `def*' forms recognize and remove it from the lambda which is then passed to defalias. And due to the unfortunate order of `declare' and `interactive' forms in defun it is not possible to simply put the `declare' into the lambda, as that would break any following `interactive' forms. This alone is a simple fix - I have attached a PoC patch - but the question of what to actually do with those declarations is one that is very wide-reaching and I cannot answer that, much less implement it, on my own. One possibility which I am thinking of would be to add another slot to closures which holds these declare forms and to make function-get and function-put refer to these slots instead, and while a lot of declarations would work out of the box with this setup, stuff like gv's declarations will not. A solution might be to have `def*' apply and remove the declarations they recognize in their `*-declarations-alist' and send the rest over to the `lambda', which *would* conceptually work, but as I have not actually implemented it might contain pitfalls that I cannot predict at the moment. I would like to get some opinions on this. I think that at the very least lambdas should recognize declares even if they don't do anything with it. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment; filename=0001-Recognize-declares-in-lambdas.patch Content-Description: Patch which makes lambdas recognize declare forms >From d22e1d7c03df30208556904ec7cbf3c1210fa502 Mon Sep 17 00:00:00 2001 From: Thuna Date: Tue, 30 Jul 2024 02:33:49 +0200 Subject: [PATCH] Recognize declares in lambdas --- lisp/subr.el | 14 ++------------ src/eval.c | 19 ++++++++++++++++++- 2 files changed, 20 insertions(+), 13 deletions(-) diff --git a/lisp/subr.el b/lisp/subr.el index fc847049332..481beeeecd8 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -378,7 +378,7 @@ dotimes ;; FIXME: This let often leads to "unused var" warnings. `((let ((,var ,counter)) ,@(cddr spec))))))) -(defmacro declare (&rest specs) +(defalias 'declare #'ignore "Do not evaluate any arguments, and return nil. If a `declare' form appears as the first form in the body of a `defun' or `defmacro' form, SPECS specifies various additional @@ -388,17 +388,7 @@ declare The possible values of SPECS are specified by `defun-declarations-alist' and `macro-declarations-alist'. -For more information, see info node `(elisp)Declare Form'." - ;; `declare' is handled directly by `defun/defmacro' rather than here. - ;; If we get here, it's because there's a `declare' somewhere not attached - ;; to a `defun/defmacro', i.e. a `declare' which doesn't do what it's - ;; intended to do. - (let ((form `(declare . ,specs))) ;; FIXME: WIBNI we had &whole? - (macroexp-warn-and-return - (format-message "Stray `declare' form: %S" form) - ;; Make a "unique" harmless form to circumvent - ;; the cache in `macroexp-warn-and-return'. - `(progn ',form nil) nil 'compile-only))) +For more information, see info node `(elisp)Declare Form'.") (defmacro ignore-errors (&rest body) "Execute BODY; if an error occurs, return nil. diff --git a/src/eval.c b/src/eval.c index 2161ab1e1ea..3ca3ce2328a 100644 --- a/src/eval.c +++ b/src/eval.c @@ -574,7 +574,7 @@ DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0, Lisp_Object cdr = XCDR (quoted); Lisp_Object args = Fcar (cdr); cdr = Fcdr (cdr); - Lisp_Object docstring = Qnil, iform = Qnil; + Lisp_Object docstring = Qnil, decls = Qnil, iform = Qnil; if (CONSP (cdr)) { docstring = XCAR (cdr); @@ -596,6 +596,18 @@ DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0, else docstring = Qnil; /* Not a docstring after all. */ } + if (CONSP (cdr)) + { + decls = XCAR (cdr); + if (CONSP (decls) + && EQ (Qdeclare, XCAR (decls))) + { + decls = XCDR (decls); + cdr = XCDR (cdr); + } + else + decls = Qnil; /* Not a declare-form after all. */ + } if (CONSP (cdr)) { iform = XCAR (cdr); @@ -605,6 +617,10 @@ DEFUN ("function", Ffunction, Sfunction, 1, UNEVALLED, 0, else iform = Qnil; /* Not an interactive-form after all. */ } + /* FIXME: Temporary patch since we're not handling declare forms + in lambdas yet. */ + if (!NILP (decls)) /* FIXME: What about an empty declare? */ + cdr = Fcons (Fcons (Qdeclare, decls), cdr); if (NILP (cdr)) cdr = Fcons (Qnil, Qnil); /* Make sure the body is never empty! */ @@ -4334,6 +4350,7 @@ syms_of_eval (void) DEFSYM (Qexit, "exit"); DEFSYM (Qinteractive, "interactive"); + DEFSYM (Qdeclare, "declare"); DEFSYM (Qcommandp, "commandp"); DEFSYM (Qand_rest, "&rest"); DEFSYM (Qand_optional, "&optional"); -- 2.44.2 --=-=-=--