summaryrefslogtreecommitdiffstats
path: root/Test
diff options
context:
space:
mode:
authorBart Schaefer <schaefer@zsh.org>2025-05-06 10:46:23 -0700
committerBart Schaefer <schaefer@zsh.org>2025-05-06 10:50:53 -0700
commitf555e902db1e4acec7961374153ec19843e2833a (patch)
tree1f0cc5ac9e804601cf41f19abfbc2921b42b39c4 /Test
parent53535: fix builds with interactive manpagers (diff)
downloadzsh-f555e902db1e4acec7961374153ec19843e2833a.tar
zsh-f555e902db1e4acec7961374153ec19843e2833a.tar.gz
zsh-f555e902db1e4acec7961374153ec19843e2833a.tar.bz2
zsh-f555e902db1e4acec7961374153ec19843e2833a.tar.lz
zsh-f555e902db1e4acec7961374153ec19843e2833a.tar.xz
zsh-f555e902db1e4acec7961374153ec19843e2833a.tar.zst
zsh-f555e902db1e4acec7961374153ec19843e2833a.zip
53546,53557 (plus test): Fix scoping of "placeholder" named references
When using placeholders declared several levels earlier than assignment: 1) Searching "up" for "typeset -n -u" could find deeper locals than intended 2) Searching for a subscript reference could skip to the top level 3) Exiting a function scope could incorrectly change the reference level
Diffstat (limited to 'Test')
-rw-r--r--Test/K01nameref.ztst160
1 files changed, 160 insertions, 0 deletions
diff --git a/Test/K01nameref.ztst b/Test/K01nameref.ztst
index 1603ab1b9..cc689613b 100644
--- a/Test/K01nameref.ztst
+++ b/Test/K01nameref.ztst
@@ -7,6 +7,76 @@
: ${ZTST_continue::=1}
+ # The following test allows to assess what different types of named
+ # references refer to during their lifetime depending on where they
+ # were initialized and whether they were defined with or without the
+ # "-u" flag.
+ #
+ # The first parameter determines whether the named references are
+ # defined with or without the flag "-u".
+ #
+ # The second parameter determines where the named references are
+ # initialized. In all cases the named references are defined at the
+ # start of function "g". With value "0" they are initialized at the
+ # same place (in the same statement). With the other values, the
+ # initialization is delayed until later, the greater the value and the
+ # later the initialization.
+
+ function e() {
+ local s=$0 a=($0);
+ f "$@";
+ }
+
+ function f() {
+ local s=$0 a=($0);
+ g "$@";
+ }
+
+ function g() {
+ if (($2)); then local -n $1 rs ra rs1 ra1;
+ else local -n $1 rs=s ra=a rs1="s[1]" ra1="a[1]"; fi;
+ if (($2 == 1)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
+ echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ local s=$0 a=($0);
+ if (($2 == 2)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
+ echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ h "$@";
+ echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ }
+
+ function h() {
+ if (($2 == 3)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
+ echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ local s=$0 a=($0);
+ if (($2 == 4)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
+ echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ i "$@";
+ echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ # Check that re-entering the same scope doesn't undo scope exit
+ k "$@"
+ }
+
+ function i() {
+ if (($2 == 5)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
+ echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ local s=$0 a=($0);
+ if (($2 == 6)); then rs=s; ra=a; rs1="s[1]"; ra1="a[1]"; fi;
+ echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ j "$@";
+ echo "$0:3: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ k "$@";
+ }
+
+ function j() {
+ echo "$0:1: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ local s=$0 a=($0);
+ echo "$0:2: rs=$rs - ra=$ra - rs1=$rs1 - ra1=$ra1";
+ }
+
+ functions -c j k
+
+ local s=T a=(T);
+
%test
typeset -n ptr
@@ -902,6 +972,96 @@ F:previously this could create an infinite recursion and crash
1:create nameref by pattern match not allowed
*?*typeset:1: -m not allowed with -n
+ e -u 0
+0:assignment at different scope than declaration, -u 0
+>g:1: rs=f - ra=f - rs1=f - ra1=f
+>g:2: rs=f - ra=f - rs1=f - ra1=f
+>h:1: rs=f - ra=f - rs1=f - ra1=f
+>h:2: rs=f - ra=f - rs1=f - ra1=f
+>i:1: rs=f - ra=f - rs1=f - ra1=f
+>i:2: rs=f - ra=f - rs1=f - ra1=f
+>j:1: rs=f - ra=f - rs1=f - ra1=f
+>j:2: rs=f - ra=f - rs1=f - ra1=f
+>i:3: rs=f - ra=f - rs1=f - ra1=f
+>k:1: rs=f - ra=f - rs1=f - ra1=f
+>k:2: rs=f - ra=f - rs1=f - ra1=f
+>h:3: rs=f - ra=f - rs1=f - ra1=f
+>k:1: rs=f - ra=f - rs1=f - ra1=f
+>k:2: rs=f - ra=f - rs1=f - ra1=f
+>g:3: rs=f - ra=f - rs1=f - ra1=f
+
+ e '' 0
+0:assignment at different scope than declaration, '' 0
+>g:1: rs=f - ra=f - rs1=f - ra1=f
+>g:2: rs=f - ra=f - rs1=f - ra1=f
+>h:1: rs=f - ra=f - rs1=f - ra1=f
+>h:2: rs=f - ra=f - rs1=f - ra1=f
+>i:1: rs=f - ra=f - rs1=f - ra1=f
+>i:2: rs=f - ra=f - rs1=f - ra1=f
+>j:1: rs=f - ra=f - rs1=f - ra1=f
+>j:2: rs=f - ra=f - rs1=f - ra1=f
+>i:3: rs=f - ra=f - rs1=f - ra1=f
+>k:1: rs=f - ra=f - rs1=f - ra1=f
+>k:2: rs=f - ra=f - rs1=f - ra1=f
+>h:3: rs=f - ra=f - rs1=f - ra1=f
+>k:1: rs=f - ra=f - rs1=f - ra1=f
+>k:2: rs=f - ra=f - rs1=f - ra1=f
+>g:3: rs=f - ra=f - rs1=f - ra1=f
+
+ e -u 2
+0:assignment at different scope than declaration, -u 2
+>g:1: rs= - ra= - rs1= - ra1=
+>g:2: rs=f - ra=f - rs1=f - ra1=f
+>h:1: rs=f - ra=f - rs1=f - ra1=f
+>h:2: rs=f - ra=f - rs1=f - ra1=f
+>i:1: rs=f - ra=f - rs1=f - ra1=f
+>i:2: rs=f - ra=f - rs1=f - ra1=f
+>j:1: rs=f - ra=f - rs1=f - ra1=f
+>j:2: rs=f - ra=f - rs1=f - ra1=f
+>i:3: rs=f - ra=f - rs1=f - ra1=f
+>k:1: rs=f - ra=f - rs1=f - ra1=f
+>k:2: rs=f - ra=f - rs1=f - ra1=f
+>h:3: rs=f - ra=f - rs1=f - ra1=f
+>k:1: rs=f - ra=f - rs1=f - ra1=f
+>k:2: rs=f - ra=f - rs1=f - ra1=f
+>g:3: rs=f - ra=f - rs1=f - ra1=f
+
+ e -u 6
+0:assignment at different scope than declaration, -u 6
+>g:1: rs= - ra= - rs1= - ra1=
+>g:2: rs= - ra= - rs1= - ra1=
+>h:1: rs= - ra= - rs1= - ra1=
+>h:2: rs= - ra= - rs1= - ra1=
+>i:1: rs= - ra= - rs1= - ra1=
+>i:2: rs=h - ra=h - rs1=h - ra1=h
+>j:1: rs=h - ra=h - rs1=h - ra1=h
+>j:2: rs=h - ra=h - rs1=h - ra1=h
+>i:3: rs=h - ra=h - rs1=h - ra1=h
+>k:1: rs=h - ra=h - rs1=h - ra1=h
+>k:2: rs=h - ra=h - rs1=h - ra1=h
+>h:3: rs=g - ra=g - rs1=g - ra1=g
+>k:1: rs=h - ra=h - rs1=h - ra1=h
+>k:2: rs=h - ra=h - rs1=h - ra1=h
+>g:3: rs=f - ra=f - rs1=f - ra1=f
+
+ e '' 6
+0:assignment at different scope than declaration, '' 6
+>g:1: rs= - ra= - rs1= - ra1=
+>g:2: rs= - ra= - rs1= - ra1=
+>h:1: rs= - ra= - rs1= - ra1=
+>h:2: rs= - ra= - rs1= - ra1=
+>i:1: rs= - ra= - rs1= - ra1=
+>i:2: rs=i - ra=i - rs1=i - ra1=i
+>j:1: rs=i - ra=i - rs1=i - ra1=i
+>j:2: rs=i - ra=i - rs1=i - ra1=i
+>i:3: rs=i - ra=i - rs1=i - ra1=i
+>k:1: rs=i - ra=i - rs1=i - ra1=i
+>k:2: rs=i - ra=i - rs1=i - ra1=i
+>h:3: rs=h - ra=h - rs1=h - ra1=h
+>k:1: rs=h - ra=h - rs1=h - ra1=h
+>k:2: rs=h - ra=h - rs1=h - ra1=h
+>g:3: rs=g - ra=g - rs1=g - ra1=g
+
#
# The following tests are run in interactive mode, using PS1 as an
# assignable special with side-effects. This crashed at one time.