changeset 25483:37dbf79c2297

maint: Merge stable to default.
author John W. Eaton <jwe@octave.org>
date Tue, 19 Jun 2018 09:37:03 -0400
parents 920299ced721 (current diff) 0d7a89bec20e (diff)
children b7db401e1a99
files libinterp/parse-tree/pt-eval.cc
diffstat 2 files changed, 30 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/libinterp/parse-tree/pt-eval.cc	Wed Feb 21 23:14:02 2018 +0530
+++ b/libinterp/parse-tree/pt-eval.cc	Tue Jun 19 09:37:03 2018 -0400
@@ -1049,10 +1049,10 @@
 
                 if (local_val_is_defined)
                   {
-                    warning_with_id ("Octave:global-from-local",
+                    warning_with_id ("Octave:global-local-conflict",
                                      "global: '%s' is defined in the current scope",
                                      name.c_str ());
-                    warning_with_id ("Octave:global-from-local",
+                    warning_with_id ("Octave:global-local-conflict",
                                      "global: in a future version, global variables must be declared before use");
 
                     // If the symbol is defined in the local but not the
@@ -1061,10 +1061,15 @@
                     // initializer in the global statement.
                     octave_value global_val = global_scope.varval (name);
 
-                    if (! global_val.is_defined ())
+                    if (global_val.is_defined ())
                       {
-                        warning_with_id ("Octave:global-from-local",
-                                         "global: global value overrides local value");
+                        warning_with_id ("Octave:global-local-conflict",
+                                         "global: global value overrides existing local value");
+                      }
+                    else
+                      {
+                        warning_with_id ("Octave:global-local-conflict",
+                                         "global: existing local value used to initialize global variable");
 
                         global_scope.assign (name, val);
                       }
--- a/test/global.tst	Wed Feb 21 23:14:02 2018 +0530
+++ b/test/global.tst	Tue Jun 19 09:37:03 2018 -0400
@@ -87,16 +87,16 @@
 %!  r = x;
 %!endfunction
 %!test
-%! warning ("off", "Octave:global-from-local", "local");
-%! clear global x
+%! warning ("off", "Octave:global-local-conflict", "local");
+%! clear global x      ## clears global and local value
 %! global x
 %! x = 0;
 %! assert (f (), 0);
 %! global x
 %! assert (x, 0);
 %!test
-%! warning ("off", "Octave:global-from-local", "local");
-%! clear global x
+%! warning ("off", "Octave:global-local-conflict", "local");
+%! clear global x      ## clears global and local value
 %! assert (f (), 1);
 %! global x
 %! assert (x, 1);
@@ -107,16 +107,29 @@
 %!  r = x;
 %!endfunction
 %!test
-%! warning ("off", "Octave:global-from-local", "local");
-%! clear global x
+%! warning ("off", "Octave:global-local-conflict", "local");
+%! clear global x      ## clears global and local value
 %! global x
 %! x = 0;
 %! assert (f (), 0);
 %! global x
 %! assert (x, 0);
 %!test
-%! warning ("off", "Octave:global-from-local", "local");
+%! warning ("off", "Octave:global-local-conflict", "local");
 %! clear global x
 %! assert (f (), 1);
 %! global x
 %! assert (x, 1);
+
+%!test
+%! warning ("off", "Octave:global-local-conflict", "local");
+%! clear global x      ## clears global and local value
+%! x = 42;             ## local value
+%! global x            ## link to undefined global, global gets local value
+%! assert (x, 42);
+%! clear x             ## clears local; global still defined
+%! x = 13;             ## new local value
+%! global x;           ## link to existing global, local gets global value
+%! assert (x, 42);
+%! clear global x      ## clears global and local value
+%! assert (exist ("x"), 0);