From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Matthew Malcomson Newsgroups: gmane.emacs.bugs Subject: bug#62419: 28.2; Elisp let-bound buffer-local variable and kill-local-variable Date: Fri, 24 Mar 2023 13:37:57 +0000 Message-ID: <19A857D6-D071-44DE-AF89-539A563FD782@gmail.com> Mime-Version: 1.0 (Mac OS X Mail 15.0 \(3693.60.0.1.1\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_0BB9BE86-326B-47CF-A415-8D41F45FD27A" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="11615"; mail-complaints-to="usenet@ciao.gmane.io" To: 62419@debbugs.gnu.org Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Fri Mar 24 16:27:56 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 1pfjKd-0002NA-RN for geb-bug-gnu-emacs@m.gmane-mx.org; Fri, 24 Mar 2023 16:27:55 +0100 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1pfhgD-0001zS-ME; Fri, 24 Mar 2023 09:42:05 -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 1pfhgA-0001ym-Q8 for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2023 09:42: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 1pfhgA-0003ko-Cx for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2023 09:42:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1pfhgA-0001mw-8s for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2023 09:42:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Matthew Malcomson Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Fri, 24 Mar 2023 13:42:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: report 62419 X-GNU-PR-Package: emacs X-Debbugs-Original-To: bug-gnu-emacs@gnu.org Original-Received: via spool by submit@debbugs.gnu.org id=B.16796652936830 (code B ref -1); Fri, 24 Mar 2023 13:42:02 +0000 Original-Received: (at submit) by debbugs.gnu.org; 24 Mar 2023 13:41:33 +0000 Original-Received: from localhost ([127.0.0.1]:40031 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pfhfg-0001m6-I2 for submit@debbugs.gnu.org; Fri, 24 Mar 2023 09:41:32 -0400 Original-Received: from [209.51.188.17] (port=56186 helo=lists.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1pfhfc-0001lm-5a for submit@debbugs.gnu.org; Fri, 24 Mar 2023 09:41:30 -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 1pfhfV-0001wD-UA for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2023 09:41:21 -0400 Original-Received: from mail-wr1-f41.google.com ([209.85.221.41]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1pfhdG-000342-NJ for bug-gnu-emacs@gnu.org; Fri, 24 Mar 2023 09:41:21 -0400 Original-Received: by mail-wr1-f41.google.com with SMTP id h17so1822234wrt.8 for ; Fri, 24 Mar 2023 06:38:10 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20210112; t=1679665078; h=to:date:message-id:subject:mime-version:from:from:to:cc:subject :date:message-id:reply-to; bh=R0pgfQltGADi4VTZQwQMiledUMehW6Bz3jyY7RfNqYU=; b=btsewsi73MraonH4bJDkTR5DKwGDKKLuWECwkiD72RWpNuzxoC+j5zbIpQhqcqhMXa SAW0APbVrZCK6jet81qq4Qn7roDwSlgtDY4pKrm47jIGn/XhDJUhIXF7R3n95CpuYE+p e3RlujuJRJUZz6yYngFEfhVaUNRfkVRys1tOBebrqnWGUgtBQd+s0Zi6Y7rT+0R+vhy/ s3pDpx+T3F/q+xEUEC9fHR3KCQhrCn2mUT31qRrCbH5HvSNSAW/Df5iYGJIIM7kPKXT2 GxZ7Amv+B8xx32T+h88zH3Az+0oYQQLS1TQ3ge/bgSlmkcIUZOfDP8unij5XS+THrh0h fjqg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20210112; t=1679665078; h=to:date:message-id:subject:mime-version:from:x-gm-message-state :from:to:cc:subject:date:message-id:reply-to; bh=R0pgfQltGADi4VTZQwQMiledUMehW6Bz3jyY7RfNqYU=; b=YIZ+T2nubY8WIqaSEQRu9Lfi7GjgroPVrP+vw5duNuQ6tFtGDRG8mEa7WMLoUopVJx ndxQueIdelxBsE0LM2zALSXcbt7U+HTgfZOZkbRFRD4y0py7jhjG9ZxA83n+sRM4z0yF yD5HiC++QE6yFPg52BfeC0QyThT3x4QAR+e2o+sBJj+eQDdgDbKr4T2w3rChuO+bFsz0 U2AM/sCAxYXRhQbitEuLqnWYh797GmecrQJYXcJyMWfBcAUywnorDOgNNF7piZvRZ4tI FHBdH7Amk9v/KIXzFq3+/Lf8u7agPmPxDkBVC2eigrkATgF+soL30sOqxpiVRv7H77Ie OmDQ== X-Gm-Message-State: AAQBX9eqLcfU0YByzPZ1johxEr7Th2baFSLIqGtN4NXp2Bu/s5o5HaZ8 e3v40Usy1IA7EQhnkyvv6ICc2uHjnAM= X-Google-Smtp-Source: AKy350ZkFi1qtzKFks6NMK0DxYAZLNX7q2rZRjWdn/NHqskHVg7UOQV+EMQ4yXUNI1EsJoxUlNvugg== X-Received: by 2002:adf:e6ca:0:b0:2ce:adff:61fc with SMTP id y10-20020adfe6ca000000b002ceadff61fcmr2192722wrm.37.1679665078511; Fri, 24 Mar 2023 06:37:58 -0700 (PDT) Original-Received: from smtpclient.apple ([2a00:23c6:5484:ac01:e12d:76f0:ef75:d129]) by smtp.gmail.com with ESMTPSA id p14-20020a5d48ce000000b002d45575643esm16390512wrs.43.2023.03.24.06.37.57 for (version=TLS1_2 cipher=ECDHE-ECDSA-AES128-GCM-SHA256 bits=128/128); Fri, 24 Mar 2023 06:37:57 -0700 (PDT) X-Mailer: Apple Mail (2.3693.60.0.1.1) Received-SPF: pass client-ip=209.85.221.41; envelope-from=hardenedapple@gmail.com; helo=mail-wr1-f41.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, RCVD_IN_MSPIKE_H2=-0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action 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:258500 Archived-At: --Apple-Mail=_0BB9BE86-326B-47CF-A415-8D41F45FD27A Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=utf-8 Hello, I=E2=80=99m inlining some elisp which has behaviour I find unintuitive. To view the bug I would run each top-level form with C-x C-e in turn in = an elisp buffer. This behaviour may be expected =E2=80=94 the manual mentions something = related =E2=80=94 but I believe this is an unintended edge-case. N.b. the use of `auto-fill-function=E2=80=99 is just for a variable = which turns buffer-local when set, not as anything related to this = particular symbol. FWIW I believe this behaviour to be the root cause of = https://github.com/joaotavora/yasnippet/issues/919 (which was closed due = to not being able to reproduce it). =E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94 ;; Ensure that `auto-fill-function' has a buffer-local version and a = global ;; version. (setq auto-fill-function 'local-symbol) (describe-variable 'auto-fill-function) ;; `auto-fill-function' is let-bound in the buffer scope (let ((auto-fill-function 'temp-symbol)) ;; Now there is no buffer-local variable for `auto-fill-function', but = the ;; `let' unwrapping info is still there. (kill-local-variable 'auto-fill-function) ;; Since the check in the emacs source is ;; a) Is there a buffer-local variable. ;; b) Is there a let-binding shadowing the current variable. ;; Then this `setq' sets the *global* variable. (setq auto-fill-function 'other-symbol)) ;; Exiting the `let' has special handling to avoid resetting a local = variable ;; when the local variable was `let' bound, which means that overall the = `setq' ;; set the global variable and the `let' has been lost. (describe-variable 'auto-fill-function) =E2=80=94=E2=80=94=E2=80=94=E2=80=94=E2=80=94 I think the final state after having done the above should be: 1) Global value has not changed. 2) Local value has not changed. While the observed state after the above is: 1) Global value has been set to `other-symbol'. 2) Local value has been removed. - The `setq` inside the `let` sets the *global* value rather than = creating a buffer-local variable. - The `let` on the buffer-local version of the variable is lost. The manual for `make-variable-buffer-local` =E2=80=94 in `(elisp) = Creating Buffer-Local` =E2=80=94 does mention that if a variable is in a = `let` binding then a `setq` does not create a buffer-local version. That said, I=E2=80=99m guessing the intention of this behaviour is so a = `let` binding on a global variable is modified rather than bypassed by a = `setq`. I=E2=80=99d suggest that is not relevant to the above code since the use = of `kill-local-variable` means the `let` is effectively lost already = (e.g. it does not get =E2=80=9Creset=E2=80=9D on an unwind). I=E2=80=99m attaching the source code hack that I=E2=80=99ve started = using to work around this. I=E2=80=99m not proposing this as a change, just including it for extra = context about the cause. I *believe* that the form of the C code around here looks like the = special case of a forwarded variable not having a buffer-local value but = having a buffer-local `let` binding could easily have been an oversight = rather than intention. (N.b. for this hack I guessed the meanings of the `let` enum values). --Apple-Mail=_0BB9BE86-326B-47CF-A415-8D41F45FD27A Content-Disposition: attachment; filename=emacs-patch.diff Content-Type: application/octet-stream; x-unix-mode=0644; name="emacs-patch.diff" Content-Transfer-Encoding: 7bit diff --git a/src/data.c b/src/data.c index 57205d8..af427ea 100644 --- a/src/data.c +++ b/src/data.c @@ -1607,7 +1607,7 @@ set_internal (Lisp_Object symbol, Lisp_Object newval, Lisp_Object where, if (idx > 0 && bindflag == SET_INTERNAL_SET && !PER_BUFFER_VALUE_P (buf, idx)) { - if (let_shadows_buffer_binding_p (sym)) + if (let_shadows_buffer_binding_p_and_is_global (sym)) set_default_internal (symbol, newval, bindflag); else SET_PER_BUFFER_VALUE_P (buf, idx, 1); diff --git a/src/eval.c b/src/eval.c index d002e81..9200d71 100644 --- a/src/eval.c +++ b/src/eval.c @@ -3491,6 +3491,25 @@ let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol) return 0; } +bool +let_shadows_buffer_binding_p_and_is_global (struct Lisp_Symbol *symbol) +{ + union specbinding *p; + Lisp_Object buf = Fcurrent_buffer (); + + for (p = specpdl_ptr; p > specpdl; ) + if ((--p)->kind == SPECPDL_LET_DEFAULT) + { + struct Lisp_Symbol *let_bound_symbol = XSYMBOL (specpdl_symbol (p)); + eassert (let_bound_symbol->u.s.redirect != SYMBOL_VARALIAS); + if (symbol == let_bound_symbol + && EQ (specpdl_where (p), buf)) + return 1; + } + + return 0; +} + static void do_specbind (struct Lisp_Symbol *sym, union specbinding *bind, Lisp_Object value, enum Set_Internal_Bind bindflag) diff --git a/src/lisp.h b/src/lisp.h index 0cad97c..0a296cd 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -4221,6 +4221,7 @@ extern void mark_specpdl (union specbinding *first, union specbinding *ptr); extern void get_backtrace (Lisp_Object array); Lisp_Object backtrace_top_function (void); extern bool let_shadows_buffer_binding_p (struct Lisp_Symbol *symbol); +extern bool let_shadows_buffer_binding_p_and_is_global (struct Lisp_Symbol *symbol); /* Defined in unexmacosx.c. */ #if defined DARWIN_OS && defined HAVE_UNEXEC --Apple-Mail=_0BB9BE86-326B-47CF-A415-8D41F45FD27A--