Subject: Re: bug#10604: 23.2; variable lookup: make-local-variable interaction with with-temp-buffer

From: Stefan Monnier <[email-masked]>
Date: Wed, 25 Jan 2012 21:11:54 -0500

tags 10604 wontfix
thanks

> Switch to *scratch*, and do the following:
> (defun func (my-var)
>   (with-temp-buffer
>     (message my-var)))
> C-j

> (func "z")
> C-j
> ; no problem, works as expected
> (make-local-variable 'my-var)
> C-j
> (func "z")
> C-j
> ; ERROR with the stack trace

> Debugger entered--Lisp error: (void-variable my-var)

Yes, that's how dynamic-scoping and buffer-local bindings interact.
Another way to trigger the problem:

   (progn
    (make-local-variable 'my-var)
    (let ((my-var 1))
      (with-temp-buffer
       (message my-var)))

of course, it's the same because `let' works the same as the binding
done for function arguments.

Maybe there is a way to define the interaction of buffer-local bindings
and dynamically scoped vars in a way that doesn't suffer from this
problem, but it's probably going to suffer from other problems instead,
and it's highly unlikely to be compatible with existing code, so
you're better learning to live with it (the way to do that is to
usually avoid such interactions like the plague: either let-bind or
buffer-local but not both, tho sometimes you can't avoid combining
them).

        Stefan