From 819e19eb66b0538086209a3768f0467e97dfabb9 Mon Sep 17 00:00:00 2001
From: Piotr Grzybowski <merlin@narsil.org.pl>
Date: Mon, 2 May 2016 19:41:02 +0200
Subject: [PATCH] Masking references with local variables fix; existing
 references take precedence in make_local_variable

---
 variables.c | 14 ++++++++++++--
 1 file changed, 12 insertions(+), 2 deletions(-)

diff --git a/variables.c b/variables.c
index 69ed170..256662e 100644
--- a/variables.c
+++ b/variables.c
@@ -2297,16 +2297,26 @@ SHELL_VAR *
 make_local_variable (name)
      const char *name;
 {
-  SHELL_VAR *new_var, *old_var;
+  SHELL_VAR *new_var, *old_var, *old_ref;
   VAR_CONTEXT *vc;
   int was_tmpvar;
   char *tmp_value;
 
+  old_ref = find_variable_noref (name);
   /* local foo; local foo;  is a no-op. */
   old_var = find_variable (name);
-  if (old_var && local_p (old_var) && old_var->context == variable_context)
+  if (!old_ref && old_var && local_p (old_var) && old_var->context == variable_context)
     return (old_var);
 
+  /* declare -n foo; declare -n foo;  is a no-op. */
+  if (old_ref && local_p (old_ref) && old_ref->context == variable_context)
+    return (old_ref);
+
+  if (old_ref)
+    {
+      old_var=old_ref;
+    }
+
   was_tmpvar = old_var && tempvar_p (old_var);
   /* If we're making a local variable in a shell function, the temporary env
      has already been merged into the function's variable context stack.  We
-- 
2.7.0

