8211694: JShell: Redeclared variable should be reset

Reviewed-by: sundar
This commit is contained in:
Robert Field 2018-10-24 21:17:30 -07:00
parent f095a9ee49
commit 0d815c2677
5 changed files with 92 additions and 28 deletions

View File

@ -361,6 +361,33 @@ class Eval {
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 @@ class Eval {
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);

View File

@ -250,7 +250,7 @@ public abstract class Snippet {
* @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

View File

@ -73,7 +73,7 @@ public class SnippetTest extends KullaTesting {
}
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() {

View File

@ -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 @@ public class ToolBasicTest extends ReplToolTesting {
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 class ToolBasicTest extends ReplToolTesting {
);
}
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")
);
}
}

View File

@ -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 @@ public class VariablesTest extends KullaTesting {
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);
}