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#47552: 27.1; cl-defstruct field names matching read-only variables -> bad code Date: Sun, 18 Jun 2023 15:03:25 -0400 Message-ID: References: <87wntlyhn9.fsf@rfc20.org> <87y2dxerd8.fsf@gnus.org> <87pmz0236v.fsf@gnus.org> Reply-To: Stefan Monnier Mime-Version: 1.0 Content-Type: text/plain Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="13780"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Cc: Matt Armstrong , 47552@debbugs.gnu.org To: Lars Ingebrigtsen Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jun 18 21:04:21 2023 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 1qAxhF-0003Ki-Fs for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 18 Jun 2023 21:04:21 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1qAxgx-0003IB-Lt; Sun, 18 Jun 2023 15:04:03 -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 1qAxgw-0003Hy-Jv for bug-gnu-emacs@gnu.org; Sun, 18 Jun 2023 15:04:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1qAxgw-0000Bl-B8 for bug-gnu-emacs@gnu.org; Sun, 18 Jun 2023 15:04:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1qAxgv-0004YU-Ts for bug-gnu-emacs@gnu.org; Sun, 18 Jun 2023 15:04:01 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Stefan Monnier Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 18 Jun 2023 19:04:01 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 47552 X-GNU-PR-Package: emacs Original-Received: via spool by 47552-submit@debbugs.gnu.org id=B47552.168711501517473 (code B ref 47552); Sun, 18 Jun 2023 19:04:01 +0000 Original-Received: (at 47552) by debbugs.gnu.org; 18 Jun 2023 19:03:35 +0000 Original-Received: from localhost ([127.0.0.1]:54951 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qAxgV-0004Xl-AQ for submit@debbugs.gnu.org; Sun, 18 Jun 2023 15:03:35 -0400 Original-Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:17440) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1qAxgT-0004XX-81 for 47552@debbugs.gnu.org; Sun, 18 Jun 2023 15:03:34 -0400 Original-Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id A4EC7806F0; Sun, 18 Jun 2023 15:03:27 -0400 (EDT) Original-Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 529F3803BD; Sun, 18 Jun 2023 15:03:26 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1687115006; bh=Fzp9xarFd+JZflZauSuNp1kEBuUpxUVAhv8PA5Yc7y4=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=VZ8ZzxvVLpLqlUGPy5cFCX73yKAeluJme4c3jtOS+70r0zw9NsR5DrJFIt0uFlHmJ 1QPlh37vjB2ulEo4geU/aBtKbWU8h2+mcioGxgwiV/PSBNPjoWuoF2fPEWLpqjI6uv n5gQMCCxKOjX3rSNmgxPo3yS3EQiCL3IsYH29huRkZyzCqrl9bNj1FbgQE8Pkt4NFP zMU+DLtAjqt0pm23cLh+ljh5OerQA/v8mqBjENPd2DXXAoCmhc4QyswT/o5hkRY6pe KU011tdaCzQSok6uo+zjudvuGOLQ2C0gCEQTXsPFHkUaGMwyCKQiXsZAqVAuWDdw37 MLiW12oXub4Vw== Original-Received: from pastel (unknown [45.72.207.87]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 2D42D12019B; Sun, 18 Jun 2023 15:03:26 -0400 (EDT) In-Reply-To: <87pmz0236v.fsf@gnus.org> (Lars Ingebrigtsen's message of "Sun, 11 Apr 2021 18:31:20 +0200") 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:263653 Archived-At: > But... Indeed doing this "doesn't work": > > (cl-defsubst foo4 (&key gcs-done) > gcs-done) > > (foo4 :foo 1) > -> Debugger entered--Lisp error: (wrong-type-argument numberp nil) I think the problem is that ELisp function arguments are defined as being always-statically-scoped, but the macroexpansion of (cl-defun foo4 (&key gcs-done) gcs-done) uses `let` rather than `lambda` to bind `gcs-done`, so it ends up being dynamically-scoped. Maybe we should introduce something like (defmacro slet* (bindings &rest body) (named-let expand ((bindings bindings)) (pcase-exhaustive bindings ('() (macroexp-progn body)) (`((,var ,exp) . ,bindings) (let ((rest (expand bindings))) (if (macroexp--dynamic-variable-p var) `(funcall (lambda (,var) ,rest) ,exp) (macroexp-let* `((,var ,exp)) rest))))))) Except I see that `macroexpand-all` will incorrectly turn the funcall+lambda into a `let`. Some wise ass knew about it but did it anyway. They even wrote a comment about it: ;; In lexical-binding mode, let and functions don't bind vars in the same way ;; (let obey special-variable-p, but functions don't). But luckily, this ;; doesn't matter here, because function's behavior is underspecified so it ;; can safely be turned into a `let', even though the reverse is not true. so we need to either fix that `macroexp--unfold-lambda` or circumvent it by obfuscating the code, e.g.: (defmacro slet* (bindings &rest body) (named-let expand ((bindings bindings)) (pcase-exhaustive bindings ('() (macroexp-progn body)) (`((,var ,exp) . ,bindings) (let ((rest (expand bindings))) (if (macroexp--dynamic-variable-p var) `(funcall (identity (lambda (,var) ,rest)) ,exp) (macroexp-let* `((,var ,exp)) rest))))))) Another way to look at it is that maybe we should introduce an `un-defvar`, such that we can use (un-defvar gcs-done) (cl-defun foo4 (&key gcs-done) gcs-done) and have `gcs-done` bound statically. Stefan