bug#81564: Possible bug when binding dynamically with an existing lexical binding

Eli Zaretskii <[email protected]> Thu, 06 Aug 2026 08:43:45 +0300
Newsgroups gmane.emacs.bugs
Message-ID <[email protected]>
> From: Thuna <[email protected]>
> Date: Thu, 06 Aug 2026 02:23:40 +0200
> 
> The code
> 
>   (let ((x 2)) (defvar x) (let ((x 10)) x))
> 
> returns 2, which seems extremely unintuitive to me, if not an outright
> bug.
> 
> The unintuitive thing is that the `defvar' effects not the first binding
> but the second binding, but if that is simply how it works then I don't
> have anything to say about it.
> 
> The possible bug is that the `x' that returns is referring to the
> lexical `x' bound in the outer `let', instead of referring to the
> dynamic `x' bound in the inner `let'.
> 
> I think what is happening is that when `x' reaches `eval_sub', it looks
> for the lexical value first, assuming that it wouldn't have a lexical
> value if it was marked dynamic, not taking into account the situation
> where it was marked dynamic only locally with a lexical binding around.
> 
> I am unsure if this is a bug or simply an illegal way to use `defvar'.

Stefan, any comments?