GNU bug report logs - #49116
28.0.50; Why `bound-and-true-p' is not working in lexical binding?

Previous Next

Package: emacs;

Reported by: Jean Louis <bugs <at> gnu.support>

Date: Sat, 19 Jun 2021 11:43:02 UTC

Severity: normal

Found in version 28.0.50

Done: Phil Sainty <psainty <at> orcon.net.nz>

Bug is archived. No further changes may be made.

To add a comment to this bug, you must first unarchive it, by sending
a message to control AT debbugs.gnu.org, with unarchive 49116 in the body.
You can then email your comments to 49116 AT debbugs.gnu.org in the normal way.

Toggle the display of automated, internal messages from the tracker.

View this report as an mbox folder, status mbox, maintainer mbox


Report forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Sat, 19 Jun 2021 11:43:02 GMT) Full text and rfc822 format available.

Acknowledgement sent to Jean Louis <bugs <at> gnu.support>:
New bug report received and forwarded. Copy sent to bug-gnu-emacs <at> gnu.org. (Sat, 19 Jun 2021 11:43:02 GMT) Full text and rfc822 format available.

Message #5 received at submit <at> debbugs.gnu.org (full text, mbox):

From: Jean Louis <bugs <at> gnu.support>
To: bug-gnu-emacs <at> gnu.org
Subject: 28.0.50; Why `bound-and-true-p' is not working in lexical binding?
Date: Sat, 19 Jun 2021 14:39:54 +0300
When I evaluate following it is not detecting variable `direction'
under lexical bindings:

(defun verify-direction (timestamp &optional direction)
  (cond ((bound-and-true-p direction) (message "OK"))))

(verify-direction "10:00" t) ⇒ nil

I would like to understand if it is intended to be so? 

Or is this possible bug?



In GNU Emacs 28.0.50 (build 1, x86_64-pc-linux-gnu, GTK+ Version 3.24.29, cairo version 1.17.4)
 of 2021-06-02 built on protected.rcdrun.com
Repository revision: 09081ca119c3debcc72159a46d3bafabfd7519bb
Repository branch: master
Windowing system distributor 'The X.Org Foundation', version 11.0.12011000
System Description: Parabola GNU/Linux-libre

Configured features:
ACL CAIRO DBUS FREETYPE GIF GLIB GMP GNUTLS GPM GSETTINGS HARFBUZZ JPEG
JSON LCMS2 LIBOTF LIBSYSTEMD LIBXML2 M17N_FLT MODULES NOTIFY INOTIFY
PDUMPER PNG RSVG SECCOMP SOUND THREADS TIFF TOOLKIT_SCROLL_BARS X11 XDBE
XIM XPM GTK3 ZLIB

Important settings:
  value of $LC_ALL: en_US.UTF-8
  value of $LANG: de_DE.UTF-8
  value of $XMODIFIERS: @im=exwm-xim
  locale-coding-system: utf-8-unix

Major mode: Lisp Interaction

Minor modes in effect:
  tooltip-mode: t
  global-eldoc-mode: t
  eldoc-mode: t
  electric-indent-mode: t
  mouse-wheel-mode: t
  tool-bar-mode: t
  menu-bar-mode: t
  file-name-shadow-mode: t
  global-font-lock-mode: t
  font-lock-mode: t
  blink-cursor-mode: t
  auto-composition-mode: t
  auto-encryption-mode: t
  auto-compression-mode: t
  line-number-mode: t
  transient-mark-mode: t

Load-path shadows:
None found.

Features:
(shadow sort mail-extr emacsbug message rmc puny dired dired-loaddefs
rfc822 mml mml-sec epa derived epg epg-config gnus-util rmail
rmail-loaddefs auth-source cl-seq eieio eieio-core cl-macs
eieio-loaddefs password-cache json map text-property-search time-date
subr-x seq byte-opt gv bytecomp byte-compile cconv mm-decode mm-bodies
mm-encode mail-parse rfc2231 mailabbrev gmm-utils mailheader cl-loaddefs
cl-lib sendmail rfc2047 rfc2045 ietf-drums mm-util mail-prsvr mail-utils
iso-transl tooltip eldoc electric uniquify ediff-hook vc-hooks
lisp-float-type mwheel term/x-win x-win term/common-win x-dnd tool-bar
dnd fontset image regexp-opt fringe tabulated-list replace newcomment
text-mode elisp-mode lisp-mode prog-mode register page tab-bar menu-bar
rfn-eshadow isearch easymenu timer select scroll-bar mouse jit-lock
font-lock syntax font-core term/tty-colors frame minibuffer cl-generic
cham georgian utf-8-lang misc-lang vietnamese tibetan thai tai-viet lao
korean japanese eucjp-ms cp51932 hebrew greek romanian slovak czech
european ethiopic indian cyrillic chinese composite charscript charprop
case-table epa-hook jka-cmpr-hook help simple abbrev obarray
cl-preloaded nadvice button loaddefs faces cus-face macroexp files
window text-properties overlay sha1 md5 base64 format env code-pages
mule custom widget hashtable-print-readable backquote threads dbusbind
inotify lcms2 dynamic-setting system-font-setting font-render-setting
cairo move-toolbar gtk x-toolkit x multi-tty make-network-process emacs)

Memory information:
((conses 16 53041 4440)
 (symbols 48 7128 3)
 (strings 32 19321 1728)
 (string-bytes 1 630312)
 (vectors 16 13239)
 (vector-slots 8 175206 9539)
 (floats 8 21 34)
 (intervals 56 201 0)
 (buffers 992 11))

-- 
Thanks,
Jean Louis

Take action in Free Software Foundation campaigns:
https://www.fsf.org/campaigns





Reply sent to Phil Sainty <psainty <at> orcon.net.nz>:
You have taken responsibility. (Sat, 19 Jun 2021 12:50:02 GMT) Full text and rfc822 format available.

Notification sent to Jean Louis <bugs <at> gnu.support>:
bug acknowledged by developer. (Sat, 19 Jun 2021 12:50:02 GMT) Full text and rfc822 format available.

Message #10 received at 49116-done <at> debbugs.gnu.org (full text, mbox):

From: Phil Sainty <psainty <at> orcon.net.nz>
To: Jean Louis <bugs <at> gnu.support>
Cc: 49116-done <at> debbugs.gnu.org
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Sun, 20 Jun 2021 00:49:14 +1200
On 2021-06-19 23:39, Jean Louis wrote:
> When I evaluate following it is not detecting variable `direction'
> under lexical bindings:
> 
> (defun verify-direction (timestamp &optional direction)
>   (cond ((bound-and-true-p direction) (message "OK"))))
> 
> (verify-direction "10:00" t) ⇒ nil
> 
> I would like to understand if it is intended to be so?
> 
> Or is this possible bug?

This is expected / not a bug (and so I'm closing it).

(bound-and-true-p VAR) is syntactic sugar for (and (boundp 'VAR) VAR)
which means that it works only for dynamic variables (i.e. variables
stored in the value slot of a symbol).

Under lexical binding, function arguments are lexical variables, which
means they are not stored in the symbol of that name, and hence cannot
be detected with `boundp'.

In your case there's no need to be checking whether `direction' is
"bound" -- whether it's lexical or dynamic, you already *know* that
it exists because it's an argument for the function in which you're
checking it.  All you want is the "true-p" part, which is implicit
in every value (either nil or non-nil).

Instead of "(bound-and-true-p direction)" just use "direction".


-Phil





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Sat, 19 Jun 2021 12:55:02 GMT) Full text and rfc822 format available.

Message #13 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Eli Zaretskii <eliz <at> gnu.org>
To: Phil Sainty <psainty <at> orcon.net.nz>
Cc: 49116 <at> debbugs.gnu.org, psainty <at> orcon.net.nz, bugs <at> gnu.support
Subject: Re: bug#49116: 28.0.50;
 Why `bound-and-true-p' is not working in lexical binding?
Date: Sat, 19 Jun 2021 15:54:59 +0300
> Date: Sun, 20 Jun 2021 00:49:14 +1200
> From: Phil Sainty <psainty <at> orcon.net.nz>
> Cc: 49116-done <at> debbugs.gnu.org
> 
> (bound-and-true-p VAR) is syntactic sugar for (and (boundp 'VAR) VAR)
> which means that it works only for dynamic variables (i.e. variables
> stored in the value slot of a symbol).
> 
> Under lexical binding, function arguments are lexical variables, which
> means they are not stored in the symbol of that name, and hence cannot
> be detected with `boundp'.

Should this be mentioned in the doc string of bound-and-true-p?




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Sat, 19 Jun 2021 13:03:02 GMT) Full text and rfc822 format available.

Message #16 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: Phil Sainty <psainty <at> orcon.net.nz>, 49116 <at> debbugs.gnu.org, bugs <at> gnu.support
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Sat, 19 Jun 2021 15:01:56 +0200
Eli Zaretskii <eliz <at> gnu.org> writes:

> Should this be mentioned in the doc string of bound-and-true-p?

I think so.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Sat, 19 Jun 2021 13:14:02 GMT) Full text and rfc822 format available.

Message #19 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Phil Sainty <psainty <at> orcon.net.nz>
To: Eli Zaretskii <eliz <at> gnu.org>
Cc: 49116 <at> debbugs.gnu.org, bugs <at> gnu.support
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Sun, 20 Jun 2021 01:13:22 +1200
On 2021-06-20 00:54, Eli Zaretskii wrote:
> Should this be mentioned in the doc string of bound-and-true-p?

It wouldn't hurt, and I see that `boundp' has such a note:

"Note that if `lexical-binding' is in effect, this refers to the
global value outside of any lexical scope."

That same text could be used.

If the confusion is over whether function arguments are dynamic
or lexical then the doc change might not explain things, but it
would probably hint at the source of the confusion.


-Phil





Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Sat, 19 Jun 2021 13:22:02 GMT) Full text and rfc822 format available.

Message #22 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Phil Sainty <psainty <at> orcon.net.nz>
Cc: 49116 <at> debbugs.gnu.org, Eli Zaretskii <eliz <at> gnu.org>, bugs <at> gnu.support
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Sat, 19 Jun 2021 15:21:47 +0200
Phil Sainty <psainty <at> orcon.net.nz> writes:

> It wouldn't hurt, and I see that `boundp' has such a note:
>
> "Note that if `lexical-binding' is in effect, this refers to the
> global value outside of any lexical scope."
>
> That same text could be used.

I've now copied over that text to the doc string of bound-and-true-p.

> If the confusion is over whether function arguments are dynamic
> or lexical then the doc change might not explain things, but it
> would probably hint at the source of the confusion.

Yup.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Mon, 21 Jun 2021 17:02:01 GMT) Full text and rfc822 format available.

Message #25 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Lars Ingebrigtsen <larsi <at> gnus.org>
Cc: Phil Sainty <psainty <at> orcon.net.nz>, 49116 <at> debbugs.gnu.org,
 Eli Zaretskii <eliz <at> gnu.org>, bugs <at> gnu.support
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Mon, 21 Jun 2021 19:00:26 +0200
Lars Ingebrigtsen <larsi <at> gnus.org> writes:

> I've now copied over that text to the doc string of bound-and-true-p.

You now changed the text to this:

> Return the value of symbol VAR if it is bound, else nil.
> Note that if `lexical-binding' is in effect, this function isn't
> meaningful if it refers to a lexically bound variable.

If you want feedback: I found this new version confusing, although it's
totally correct.  Because in my mental model, `bound-and-true-p' just
receives a variable (i.e. internally quotes), so how _can_ it even
"refer" to the lexical variable at all?

But it is true:

#+begin_src emacs-lisp
(let* ((a 1)
       (get-a (lambda () (list a (bound-and-true-p a)))))
  (dlet ((a 2))
    (funcall get-a))) ;; -> (1 1), not (1 2) as one might expect
#+end_src

Everything became clear when I looked at the implementation ;-)

Maybe we can further improve the text to something like "not meaningful
in the scope of a lexical binding of the variable" or something like
that?


Thanks,

Michael.




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Tue, 22 Jun 2021 13:01:02 GMT) Full text and rfc822 format available.

Message #28 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Lars Ingebrigtsen <larsi <at> gnus.org>
To: Michael Heerdegen <michael_heerdegen <at> web.de>
Cc: Phil Sainty <psainty <at> orcon.net.nz>, 49116 <at> debbugs.gnu.org,
 Eli Zaretskii <eliz <at> gnu.org>, bugs <at> gnu.support
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Tue, 22 Jun 2021 14:59:50 +0200
Michael Heerdegen <michael_heerdegen <at> web.de> writes:

> Maybe we can further improve the text to something like "not meaningful
> in the scope of a lexical binding of the variable" or something like
> that?

But I don't think that's correct, because the function takes a symbol
that refers to a variable -- and not a variable.  :-)

Explaining why something doesn't make sense is often more complicate
than explaining how something works.

-- 
(domestic pets only, the antidote for overdose, milk.)
   bloggy blog: http://lars.ingebrigtsen.no




Information forwarded to bug-gnu-emacs <at> gnu.org:
bug#49116; Package emacs. (Tue, 22 Jun 2021 23:01:01 GMT) Full text and rfc822 format available.

Message #31 received at 49116 <at> debbugs.gnu.org (full text, mbox):

From: Michael Heerdegen <michael_heerdegen <at> web.de>
To: Jean Louis <bugs <at> gnu.support>
Cc: 49116 <at> debbugs.gnu.org
Subject: Re: bug#49116: 28.0.50; Why `bound-and-true-p' is not working in
 lexical binding?
Date: Wed, 23 Jun 2021 01:00:11 +0200
Jean Louis <bugs <at> gnu.support> writes:

> (defun verify-direction (timestamp &optional direction)
>   (cond ((bound-and-true-p direction) (message "OK"))))
>
> (verify-direction "10:00" t) ⇒ nil

BTW, if your original intention was to check whether a function argument
has been specified in a function call (including nil) -- optional
arguments in `cl-defun' support such a feature.

Michael.




bug archived. Request was from Debbugs Internal Request <help-debbugs <at> gnu.org> to internal_control <at> debbugs.gnu.org. (Wed, 21 Jul 2021 11:24:06 GMT) Full text and rfc822 format available.

This bug report was last modified 2 years and 277 days ago.

Previous Next


GNU bug tracking system
Copyright (C) 1999 Darren O. Benham, 1997,2003 nCipher Corporation Ltd, 1994-97 Ian Jackson.