changeset 53211:003c062e16ea

8211694: JShell: Redeclared variable should be reset Reviewed-by: sundar
author rfield
date Wed, 24 Oct 2018 21:17:30 -0700
parents b646c9ea2394
children ef0fed0a3953
files src/jdk.jshell/share/classes/jdk/jshell/Eval.java src/jdk.jshell/share/classes/jdk/jshell/Snippet.java test/langtools/jdk/jshell/SnippetTest.java test/langtools/jdk/jshell/ToolBasicTest.java test/langtools/jdk/jshell/VariablesTest.java
diffstat 5 files changed, 92 insertions(+), 28 deletions(-) [+]
line wrap: on
line diff
--- a/src/jdk.jshell/share/classes/jdk/jshell/Eval.java	Thu Oct 25 11:05:24 2018 +0800
+++ b/src/jdk.jshell/share/classes/jdk/jshell/Eval.java	Wed Oct 24 21:17:30 2018 -0700
@@ -361,6 +361,33 @@
                 winit = winit == null ? Wrap.rangeWrap(compileSource, rinit) : winit;
                 nameMax = rinit.begin - 1;
             } else {
+                String sinit;
+                switch (typeName) {
+                    case "byte":
+                    case "short":
+                    case "int":
+                        sinit = "0";
+                        break;
+                    case "long":
+                        sinit = "0L";
+                        break;
+                    case "float":
+                        sinit = "0.0f";
+                        break;
+                    case "double":
+                        sinit = "0.0d";
+                        break;
+                    case "boolean":
+                        sinit = "false";
+                        break;
+                    case "char":
+                        sinit = "'\\u0000'";
+                        break;
+                    default:
+                        sinit = "null";
+                        break;
+                }
+                winit = Wrap.simpleWrap(sinit);
                 subkind = SubKind.VAR_DECLARATION_SUBKIND;
             }
             int nameStart = compileSource.lastIndexOf(name, nameMax);
@@ -861,28 +888,6 @@
                     state.debug(ex, "termination");
                     state.closeDown();
                 }
-            } else if (si.subKind() == SubKind.VAR_DECLARATION_SUBKIND) {
-                switch (((VarSnippet) si).typeName()) {
-                    case "byte":
-                    case "short":
-                    case "int":
-                    case "long":
-                        value = "0";
-                        break;
-                    case "float":
-                    case "double":
-                        value = "0.0";
-                        break;
-                    case "boolean":
-                        value = "false";
-                        break;
-                    case "char":
-                        value = "''";
-                        break;
-                    default:
-                        value = "null";
-                        break;
-                }
             }
         }
         return events(c, outs, value, exception);
--- a/src/jdk.jshell/share/classes/jdk/jshell/Snippet.java	Thu Oct 25 11:05:24 2018 +0800
+++ b/src/jdk.jshell/share/classes/jdk/jshell/Snippet.java	Wed Oct 24 21:17:30 2018 -0700
@@ -250,7 +250,7 @@
          * @jls 8.3. VariableDeclarator without VariableInitializer in
          * FieldDeclaration.
          */
-        VAR_DECLARATION_SUBKIND(Kind.VAR),
+        VAR_DECLARATION_SUBKIND(Kind.VAR, true, true),
 
         /**
          * A variable declaration with an initializer expression. A
--- a/test/langtools/jdk/jshell/SnippetTest.java	Thu Oct 25 11:05:24 2018 +0800
+++ b/test/langtools/jdk/jshell/SnippetTest.java	Wed Oct 24 21:17:30 2018 -0700
@@ -73,7 +73,7 @@
     }
 
     public void testVarDeclarationKey() {
-        assertVarKeyMatch("int a;", false, "a", VAR_DECLARATION_SUBKIND, "int", added(VALID));
+        assertVarKeyMatch("int a;", true, "a", VAR_DECLARATION_SUBKIND, "int", added(VALID));
     }
 
     public void testVarDeclarationWithInitializerKey() {
--- a/test/langtools/jdk/jshell/ToolBasicTest.java	Thu Oct 25 11:05:24 2018 +0800
+++ b/test/langtools/jdk/jshell/ToolBasicTest.java	Wed Oct 24 21:17:30 2018 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133 8199912
+ * @bug 8143037 8142447 8144095 8140265 8144906 8146138 8147887 8147886 8148316 8148317 8143955 8157953 8080347 8154714 8166649 8167643 8170162 8172102 8165405 8174796 8174797 8175304 8167554 8180508 8166232 8196133 8199912 8211694
  * @summary Tests for Basic tests for REPL tool
  * @modules jdk.compiler/com.sun.tools.javac.api
  *          jdk.compiler/com.sun.tools.javac.main
@@ -833,8 +833,8 @@
                 a -> assertCommandOutputStartsWith(a, "import jdk.internal.misc.VM;", "|  Error:")
         );
         test(false, new String[]{"--no-startup",
-            "-R--add-exports", "-Rjava.base/jdk.internal.misc=ALL-UNNAMED",
-            "-C--add-exports", "-Cjava.base/jdk.internal.misc=ALL-UNNAMED"},
+                        "-R--add-exports", "-Rjava.base/jdk.internal.misc=ALL-UNNAMED",
+                        "-C--add-exports", "-Cjava.base/jdk.internal.misc=ALL-UNNAMED"},
                 a -> assertImport(a, "import jdk.internal.misc.VM;", "", "jdk.internal.misc.VM"),
                 a -> assertCommand(a, "System.err.println(VM.isBooted())", "", "", null, "", "true\n")
         );
@@ -844,4 +844,15 @@
         );
     }
 
+    public void testRedeclareVariableNoInit() {
+        test(
+                a -> assertCommand(a, "Integer a;", "a ==> null"),
+                a -> assertCommand(a, "a instanceof Integer;", "$2 ==> false"),
+                a -> assertCommand(a, "a = 1;", "a ==> 1"),
+                a -> assertCommand(a, "Integer a;", "a ==> null"),
+                a -> assertCommand(a, "a instanceof Integer;", "$5 ==> false"),
+                a -> assertCommand(a, "a", "a ==> null")
+        );
+     }
+
 }
--- a/test/langtools/jdk/jshell/VariablesTest.java	Thu Oct 25 11:05:24 2018 +0800
+++ b/test/langtools/jdk/jshell/VariablesTest.java	Wed Oct 24 21:17:30 2018 -0700
@@ -23,7 +23,7 @@
 
 /*
  * @test
- * @bug 8144903 8177466 8191842
+ * @bug 8144903 8177466 8191842 8211694
  * @summary Tests for EvaluationState.variables
  * @library /tools/lib
  * @modules jdk.compiler/com.sun.tools.javac.api
@@ -466,6 +466,54 @@
         assertVarDisplayName("var v6 = new Runnable() { public void run() { } };", "<anonymous class implementing Runnable>");
     }
 
+    public void varDeclNoInit() {
+        assertVarDeclNoInit("byte", "b",  "0");
+        assertVarDeclNoInit("short", "h",  "0");
+        assertVarDeclNoInit("int", "i",  "0");
+        assertVarDeclNoInit("long", "l",  "0");
+        assertVarDeclNoInit("float", "f",  "0.0");
+        assertVarDeclNoInit("double", "d",  "0.0");
+        assertVarDeclNoInit("boolean", "n",  "false");
+        assertVarDeclNoInit("char", "c",  "'\\000'");
+        assertVarDeclNoInit("Object", "o",  "null");
+        assertVarDeclNoInit("String", "s", "null");
+    }
+
+    public void varDeclRedefNoInit() {
+        assertVarDeclRedefNoInit("byte", "b", "1", "0");
+        assertVarDeclRedefNoInit("short", "h", "2", "0");
+        assertVarDeclRedefNoInit("int", "i", "3", "0");
+        assertVarDeclRedefNoInit("long", "l", "4L", IGNORE_VALUE, "0");
+        assertVarDeclRedefNoInit("float", "f", "3.14f", IGNORE_VALUE, "0.0");
+        assertVarDeclRedefNoInit("double", "d", "3.1415926", "0.0");
+        assertVarDeclRedefNoInit("boolean", "n", "true", "false");
+        assertVarDeclRedefNoInit("char", "c", "'x'", "'\\000'");
+        assertVarDeclRedefNoInit("Object", "o", "new Object()", IGNORE_VALUE, "null");
+        assertVarDeclRedefNoInit("String", "s", "\"hi\"", "null");
+    }
+
+    private void assertVarDeclRedefNoInit(String typeName, String name, String value, String dvalue) {
+        assertVarDeclRedefNoInit(typeName, name, value, value, dvalue);
+    }
+
+    private void assertVarDeclRedefNoInit(String typeName, String name, String value, String rvalue, String dvalue) {
+        VarSnippet vs = varKey(assertEval(typeName + " " + name + " = " + value + ";", rvalue));
+        assertVarDeclNoInit(typeName,  name, dvalue,
+                ste(vs, VALID, VALID, false, null),
+                ste(vs, VALID, OVERWRITTEN, false, MAIN_SNIPPET));
+    }
+
+    private VarSnippet assertVarDeclNoInit(String typeName, String name, String dvalue) {
+        return assertVarDeclNoInit(typeName, name, dvalue, added(VALID));
+    }
+
+    private VarSnippet assertVarDeclNoInit(String typeName, String name, String dvalue, STEInfo mainInfo, STEInfo... updates) {
+        VarSnippet vs = varKey(assertEval(typeName + " " + name + ";", dvalue, mainInfo, updates));
+        assertEquals(vs.typeName(), typeName);
+        assertEval(name, dvalue, added(VALID));
+        return vs;
+    }
+
     private void assertVarDisplayName(String var, String typeName) {
         assertEquals(varKey(assertEval(var)).typeName(), typeName);
     }