Subject: Re: bug#62419: 28.2; Elisp let-bound buffer-local variable and kill-local-variable

From: Matthew Malcomson <[email-masked]>
Date: Sun, 26 Mar 2023 16:01:22 +0100

> On 26 Mar 2023, at 15:01, Stefan Monnier <[email-masked]> wrote:
> 
>> (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.
> 
> AFAIK the behavior is "as intended": the `let` only affects *one*
> binding, either the global one or the buffer-local one.
> 

Not going to push much on this since your suggested change to `newline` would fix everything to me.
But the part I think is strange is `setq` not creating a buffer-local binding in this environment.
(i.e. not to do with what `let` is affecting).

The “special behaviour” that a `setq` may act on a global binding of an automatic buffer-local variable when inside a let binding seems to me like the intention is to avoid “bypassing” a let on a global binding.

I.e. currently the behaviour of `setq` on automatic	buffer-local variables is:
	- Outside `let`, always affect buffer-local (creating if necessary)
	- In `let` of global binding, affect global binding.
	- In `let` of buffer-local binding, affect buffer-local
	- In `let` of buffer-local binding but where buffer-local value has been killed, affect global value.

I believe that last condition is strange and the behaviour of `setq` would be more understandable without it.
Especially since when viewed from the top-level (i.e. evaluate some lisp which contains a `setq` of an automatic buffer-local variable and may or may not have a `let`) the options are:
	- If `setq` is outside any `let`, it will affect a buffer-local binding.
	- If `setq` is inside a `let` then it won’t be visible once exited the `let` (i.e. it affects the binding that the `let` acted on)
	- *Unless* the `let` was of a buffer-local binding and that buffer-local binding was killed, in which case the `setq` will affect the global binding.

Either way, thanks for looking into this!
Matthew