From 48ffaab4f43b4f6a2d550f08722dd9558adbf5e1 Mon Sep 17 00:00:00 2001 From: Kaz Kylheku Date: Wed, 28 Apr 2021 20:41:22 -0700 Subject: macros: lexical-var-p: don't report t for specials. The lexical-var-p function wrongly reports true for locally rebound special variables, which are not lexical. * eval.c (special_var_p): Static function moved to avoid forward declaration. (lexical_var_p): Bail if sym satisfies special_var_p. (old_lexical_var_p): New function, copy of old lexical_var_p before this bugfix. (eval_init): Conditionally register lexical-var-p as either lexical_var_p or old_lexical_var_p depending on the compat value. * txr.1: Update documentation for lexical-var-p to clarify that it doesn't report true for specials. Also make note that it doesn't report true for global lexicals, and likewise that lexical-fun-p doesn't report global functions. Added compat note. --- txr.1 | 56 ++++++++++++++++++++++++++++++++++++++++++++++++++++---- 1 file changed, 52 insertions(+), 4 deletions(-) (limited to 'txr.1') diff --git a/txr.1 b/txr.1 index c3524bfa..ddf1f105 100644 --- a/txr.1 +++ b/txr.1 @@ -36785,11 +36785,45 @@ parameter. Using these functions, a macro can enquire whether a given .meta form is a symbol which has a variable binding or a function binding -in the lexical environment. +in the local lexical environment. This information is known during macro expansion. The macro expander recognizes lexical function and variable bindings, because these bindings can shadow macros. +Special variables are not lexical. The function +.code lexical-var-p +returns +.code nil +if +.meta form +satisfies +.code special-var-p +function, indicating that it is the name of a special variable. + +The +.code lexical-var-p +function also returns +.code nil +for global lexical variables. If +.meta form +is a symbol for which only a global lexical variable binding is apparent, +.code lexical-var-p +returns +.codn nil . +Testing for the existence for a global variable can be done using +.codn boundp ; +if a symbol is +.code boundp +but not +.codn special-var-p , +then it is a global lexical variable. + +Similarly, +.code lexical-fun-p +returns +.code nil +for global functions. + .TP* Example: .verb @@ -36806,14 +36840,20 @@ bindings can shadow macros. (t :not-lex-fun-var))) ;; - ;; This returns: - ;; - ;; (:lexical-var :not-lex-fun-var :lexical-fun) + ;; Use classify macro above to report classification + ;; of the x, y and f symbols in the given scope ;; (let ((x 1) (y 2)) (symacrolet ((y x)) (flet ((f () (+ 2 2))) (list (classify x) (classify y) (classify f))))) + --> (:lexical-var :not-lex-fun-var :lexical-fun) + + ;; Locally bound specials are not lexical + + (let ((*stdout* *stdnull*)) + (classify *stdout*)) + --> :not-lex-fun-var .brev .TP* Note: @@ -79273,6 +79313,14 @@ A compatibility value of 248 or lower restores the above old behaviors of .code @ and .codn hash-revget . +.IP 257 +Until \*(TX 257, the function +.code lexical-var-p +returned +.code t +for not only lexical variables, but also for locally bound special variables, +which are not lexical. The behavior is restored if 257 or older compatibility +is selected. .IP 244 Until \*(TX 244, the .code env-hash -- cgit v1.2.3