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

Re: LOCAL_VARS option ?



Bart Schaefer wrote on Wed, Jan 25, 2017 at 13:50:06 -0800:
> On Wed, 25 Jan 2017, Daniel Shahaf wrote:
> 
> > It does warn if the inner function assigns «var[3]=(three)».  I suppose
> > it shouldn't, for the same reason as the above case?
> 
> It warns in that instance because inserting a slice into an array goes
> through the same code branch as assigning to the array as a whole (IIRC).
> It is probably not possible to selectively suppress this warning.

Patch attached.

Cheers,

Daniel
From a97270dbee2e2169ddf99aeb7fc8ac902263a944 Mon Sep 17 00:00:00 2001
From: Daniel Shahaf <d.s@xxxxxxxxxxxxxxxxxx>
Date: Sun, 29 Jan 2017 21:17:48 +0000
Subject: [PATCH] WARN_NESTED_VAR: Don't warn when assigning to a slice of an
 existing array

---
 Src/params.c         | 19 +++++++++++++------
 Test/E01options.ztst |  4 ++++
 2 files changed, 17 insertions(+), 6 deletions(-)

diff --git a/Src/params.c b/Src/params.c
index a8d4f50..20abe6a 100644
--- a/Src/params.c
+++ b/Src/params.c
@@ -2866,10 +2866,14 @@ gethkparam(char *s)
 
 /**/
 static void
-check_warn_pm(Param pm, const char *pmtype, int created)
+check_warn_pm(Param pm, const char *pmtype, int created,
+	      int may_warn_about_nested_vars)
 {
     Funcstack i;
 
+    if (!may_warn_about_nested_vars && !created)
+	return;
+
     if (created && isset(WARNCREATEGLOBAL)) {
 	if (locallevel <= forklevel || pm->level != 0)
 	    return;
@@ -2956,7 +2960,7 @@ assignsparam(char *s, char *val, int flags)
 	return NULL;
     }
     if (flags & ASSPM_WARN)
-	check_warn_pm(v->pm, "scalar", created);
+	check_warn_pm(v->pm, "scalar", created, 1);
     if (flags & ASSPM_AUGMENT) {
 	if (v->start == 0 && v->end == -1) {
 	    switch (PM_TYPE(v->pm->node.flags)) {
@@ -3049,6 +3053,7 @@ assignaparam(char *s, char **val, int flags)
     char *t = s;
     char *ss;
     int created = 0;
+    int may_warn_about_nested_vars = 1;
 
     if (!isident(s)) {
 	zerr("not an identifier: %s", s);
@@ -3062,6 +3067,8 @@ assignaparam(char *s, char **val, int flags)
 	if (!(v = getvalue(&vbuf, &s, 1))) {
 	    createparam(t, PM_ARRAY);
 	    created = 1;
+	} else {
+	    may_warn_about_nested_vars = 0;
 	}
 	*ss = '[';
 	if (v && PM_TYPE(v->pm->node.flags) == PM_HASHED) {
@@ -3105,7 +3112,7 @@ assignaparam(char *s, char **val, int flags)
 	}
 
     if (flags & ASSPM_WARN)
-	check_warn_pm(v->pm, "array", created);
+	check_warn_pm(v->pm, "array", created, may_warn_about_nested_vars);
     if (flags & ASSPM_AUGMENT) {
     	if (v->start == 0 && v->end == -1) {
 	    if (PM_TYPE(v->pm->node.flags) & PM_ARRAY) {
@@ -3176,7 +3183,7 @@ sethparam(char *s, char **val)
 	    /* errflag |= ERRFLAG_ERROR; */
 	    return NULL;
 	}
-    check_warn_pm(v->pm, "associative array", checkcreate);
+    check_warn_pm(v->pm, "associative array", checkcreate, 1);
     setarrvalue(v, val);
     unqueue_signals();
     return v->pm;
@@ -3241,9 +3248,9 @@ setnparam(char *s, mnumber val)
 	    unqueue_signals();
 	    return NULL;
 	}
-	check_warn_pm(v->pm, "numeric", !was_unset);
+	check_warn_pm(v->pm, "numeric", !was_unset, 1);
     } else {
-	check_warn_pm(v->pm, "numeric", 0);
+	check_warn_pm(v->pm, "numeric", 0, 1);
     }
     setnumvalue(v, val);
     unqueue_signals();
diff --git a/Test/E01options.ztst b/Test/E01options.ztst
index 343d5a9..2bd4fdb 100644
--- a/Test/E01options.ztst
+++ b/Test/E01options.ztst
@@ -1137,6 +1137,8 @@
   (
     foo1=global1 foo2=global2 foo3=global3 foo4=global4
     integer foo5=5
+    # skip foo6, defined in fn_wnv
+    foo7=(one two)
     fn_wnv() {
        # warns
        foo1=bar1
@@ -1161,6 +1163,8 @@
        integer foo6=9
        # doesn't warn
        (( foo6=10 ))
+       foo7[3]=three
+       foo7[4]=(four)
     }
     print option off >&2
     fn_wnv


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