Zsh Mailing List Archive
Messages sorted by: Reverse Date, Date, Thread, Author

Re: Hiding specials, and namerefs



On Wed, Jul 22, 2026 at 6:24 AM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
Answering some of my own questions, asking new ones ...

On Tue, Jul 21, 2026 at 4:51 PM Bart Schaefer <schaefer@xxxxxxxxxxxxxxxx> wrote:
>
> B02typeset.ztst
> (eval):typeset:3: SRANDOM: can't change type of autoloaded parameter
> Was testing: Global -h variable doesn't hide autoload variable
>
> I think this one exemplifies the change, the use of -h shouldn't be a
> silent no-op.  Thoughts?

Clarification here.  The restriction on -h only applies when
attempting to change the attribute ("type" in the error message is
wrong, I've corrected) on an existing parameter.   That applies to
this test because it uses -gh.  If the -g is removed, a new local is
being created and that is definitely allowed to hide the special from
the surrounding scope.  I think I have that working.

New question:  Should there be just a warning in this case, or an
actual error?  An error would change the behavior.

I assume that -h is a rather rarely used flag. It's probably even more rarely used in cases where it fails to hide the targeted parameter. So, while an error changes the behavior, there are probably extremely few existing scripts where this would make a difference. On the other hand, while you develop a new script, an error is somewhat more likely to grab your attention.
 
> Why does it ever make sense to apply -h to a global?  What's the point
> of this test?

I still have this question.

A plain "local SRANDOM" doesn't hide the special SRANDOM but when -h is added, it does. The tests with -gh are to ensure that whatever logic triggers this hiding doesn't kick in when -g is present (or when running at the top-level).

Does it make sense to apply -h to a global? Probably not but it should still not cause unexpected results.

I noticed that one of the tests is missing a -g and two cases are missing. The following patch fixes both issues.

Fix and complete hiding tests
 
Philippe

diff --git a/Test/B02typeset.ztst b/Test/B02typeset.ztst
index 32e79ced2..fe9eb55eb 100644
--- a/Test/B02typeset.ztst
+++ b/Test/B02typeset.ztst
@@ -1123,12 +1123,36 @@
 
  zmodload -u zsh/random
  echo v=${SRANDOM/<->/integer}
- typeset SRANDOM
+ typeset -g SRANDOM
  echo v=${SRANDOM/<->/integer}
 0:Global non -h variable doesn't hide special variable
 >v=integer
 >v=integer
 
+ zmodload -u zsh/random
+ echo v=${SRANDOM/<->/integer}
+ typeset -gh SRANDOM
+ echo v=${SRANDOM/<->/integer}
+0:Global -h variable doesn't hide special variable
+>v=integer
+>v=integer
+
+ zmodload -u zsh/random
+ echo v=${SRANDOM/<->/integer}
+ typeset SRANDOM
+ echo v=${SRANDOM/<->/integer}
+0:Local non -h variable doesn't hide special variable
+>v=integer
+>v=integer
+
+ zmodload -u zsh/random
+ echo v=${SRANDOM/<->/integer}
+ typeset -h SRANDOM
+ echo v=${SRANDOM/<->/integer}
+0:Local -h variable hides special variable
+>v=integer
+>v=
+
  zmodload -u zsh/random
  echo z=${(M)${(f)${ zmodload -ap}}:#*SRANDOM*}
  typeset -g SRANDOM
@@ -1147,14 +1171,6 @@
 0:Global -h variable doesn't hide autoload variable
 >z=SRANDOM (zsh/random)
 >z=SRANDOM (zsh/random)
->v=integer
-
- zmodload -u zsh/random
- echo v=${SRANDOM/<->/integer}
- typeset SRANDOM
- echo v=${SRANDOM/<->/integer}
-0:Local non -h variable doesn't hide special variable
->v=integer
 >v=integer
 
  zmodload -u zsh/random


Messages sorted by: Reverse Date, Date, Thread, Author