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

PATCH: ::= didn't respect parameter flags



The documentation says
    ${name::=word}
          In  the first form, if name is unset then set it to word; in
          the second form, if name is unset or null then set it to word;
          and in the third form, unconditionally  set name to word.  In
          all forms, the value of the parameter is then substituted.

Particularly that "the value of the parameter is substituted", not "the
word is substituted".

This is what happens before the fix, and similarly for array flags like "unique":

% typeset -E3 fl; hi=5; echo ${fl::=hi}
hi
% echo $fl
5.00e+00
---

> So to clarify my original confusion, := does nothing on a parameter
> declared with typeset -Z3 because it expands to be padded before
> checking if it's "empty", despite actually being unset, and ::= shows
> the assigned value ignoring the parameter flags (but the flags aren't
> actually cleared, they just aren't used for the particular expansion
> that used ::=).
>
> Would we consider either or both of these to be bugs?

We agreed that at least the latter is definitely disagreeing with what you would
expect and the documentation.

 Src/subst.c            | 16 +++++++++++++---
 Test/D04parameter.ztst | 13 +++++++++++++
 2 files changed, 26 insertions(+), 3 deletions(-)

diff --git a/Src/subst.c b/Src/subst.c
index 16a3cb055c..f72a6baf27 100644
--- a/Src/subst.c
+++ b/Src/subst.c
@@ -3300,13 +3300,23 @@ paramsubst(LinkList l, LinkNode n, char **str, int qt, int pf_flags,
 			Param pm = sethparam(idbeg, a);
 			if (pm)
 			    aval = paramvalarr(pm->gsu.h->getfn(pm), hkeys|hvals);
-		    } else
-			setaparam(idbeg, a);
+		    } else {
+			Param pm = setaparam(idbeg, a);
+			if (pm)
+			    aval = pm->gsu.a->getfn(pm);
+		    }
 		    isarr = 1;
 		    arrasg = 0;
 		} else {
 		    untokenize(val);
-		    setsparam(idbeg, ztrdup(val));
+		    Param pm = setsparam(idbeg, ztrdup(val));
+		    if (pm) {
+			struct value vbuf = { 0 };
+			vbuf.pm = pm;
+			vbuf.end = -1;
+			vbuf.valflags = VALFLAG_SUBST;
+			val = getstrvalue(&vbuf);
+		    }
 		}
 		*idend = sav;
 		copied = 1;
diff --git a/Test/D04parameter.ztst b/Test/D04parameter.ztst
index 6a039d4830..21b1b9dcea 100644
--- a/Test/D04parameter.ztst
+++ b/Test/D04parameter.ztst
@@ -82,6 +82,19 @@
 >wasnull2d
 >wasnull2d
 
+  typeset -Z3 zerothree
+  print ${zerothree::=15}
+  typeset -E3 ethree
+  five=5
+  print ${ethree::=five}
+  typeset -a -U uniquearr
+  typeset -a duplicates=(1 2 1 1 2 3)
+  print ${(A)uniquearr::=$duplicates}
+0:::= respects expansion flags
+>015
+>5.00e+00
+>1 2 3
+
   unset array
   print ${#${(A)=array=word}}
 0:${#${(A)=array=word}} counts array elements
-- 
2.38.1





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