Skip to content
Permalink
Browse files
8259905: Compiler treats 'sealed' keyword as 'var' keyword
Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Jan 29, 2021
1 parent 6415d3b commit b2e582a8826ad994c886e8f7c30787c69be82820
@@ -3338,13 +3338,15 @@ JCVariableDecl variableDeclaratorRest(int pos, JCModifiers mods, JCExpression ty
if (elemType.hasTag(IDENT)) {
Name typeName = ((JCIdent)elemType).name;
if (restrictedTypeNameStartingAtSource(typeName, pos, !compound && localDecl) != null) {
if (type.hasTag(TYPEARRAY) && !compound) {
if (typeName != names.var) {
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedHere(typeName));
} else if (type.hasTag(TYPEARRAY) && !compound) {
//error - 'var' and arrays
reportSyntaxError(pos, Errors.RestrictedTypeNotAllowedArray(typeName));
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedArray(typeName));
} else {
if(compound)
//error - 'var' in compound local var decl
reportSyntaxError(pos, Errors.RestrictedTypeNotAllowedCompound(typeName));
reportSyntaxError(elemType.pos, Errors.RestrictedTypeNotAllowedCompound(typeName));
startPos = TreeInfo.getStartPos(mods);
if (startPos == Position.NOPOS)
startPos = TreeInfo.getStartPos(type);
@@ -5,13 +5,13 @@ ParserTest.java:24:14: compiler.err.restricted.type.not.allowed: var, 10
ParserTest.java:28:20: compiler.err.restricted.type.not.allowed: var, 10
ParserTest.java:36:27: compiler.err.restricted.type.not.allowed: var, 10
ParserTest.java:38:5: compiler.err.restricted.type.not.allowed.here: var
ParserTest.java:41:15: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:42:13: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:43:17: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:44:13: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:45:15: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:46:13: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:49:24: compiler.err.restricted.type.not.allowed.compound: var
ParserTest.java:41:9: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:42:9: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:43:9: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:44:9: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:45:9: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:46:9: compiler.err.restricted.type.not.allowed.array: var
ParserTest.java:49:9: compiler.err.restricted.type.not.allowed.compound: var
ParserTest.java:54:5: compiler.err.restricted.type.not.allowed.here: var
ParserTest.java:58:16: compiler.err.restricted.type.not.allowed.here: var
ParserTest.java:59:14: compiler.err.restricted.type.not.allowed.here: var
@@ -258,30 +258,8 @@ public void testRestrictedKeyword() {
"class SealedTest { int sealed = 0; int non = 0; int ns = non-sealed; }",
"class SealedTest { void test(String sealed) { } }",
"class SealedTest { void sealed(String sealed) { } }",
"class SealedTest { void test() { String sealed = null; } }"
)) {
assertOK(s);
}

for (String s : List.of(
"class sealed {}",
"enum sealed {}",
"record sealed() {}",
"interface sealed {}",
"@interface sealed {}"
)) {
assertFail("compiler.err.restricted.type.not.allowed", s);
}

for (String s : List.of(
"class Foo { sealed m() {} }",
"class Foo { sealed i; }",
"class Foo { void m(sealed i) {} }"
)) {
assertFail("compiler.err.restricted.type.not.allowed.here", s);
}
"class SealedTest { void test() { String sealed = null; } }",

for (String s : List.of(
"class SealedTest { String permits; }",
"class SealedTest { int permits = 0; }",
"class SealedTest { void test(String permits) { } }",
@@ -292,6 +270,12 @@ public void testRestrictedKeyword() {
}

for (String s : List.of(
"class sealed {}",
"enum sealed {}",
"record sealed() {}",
"interface sealed {}",
"@interface sealed {}",

"class permits {}",
"enum permits {}",
"record permits() {}",
@@ -302,10 +286,16 @@ public void testRestrictedKeyword() {
}

for (String s : List.of(
"class Foo { sealed m() {} }",
"class Foo { sealed i; }",
"class Foo { void m() { sealed i; } }",
"class Foo { void m(sealed i) {} }",

"class Foo { permits m() {} }",
"class Foo { permits i; }",
"class Foo { void m() { permits i; } }",
"class Foo { void m(permits i) {} }"
)) {
)) {
assertFail("compiler.err.restricted.type.not.allowed.here", s);
}

0 comments on commit b2e582a

Please sign in to comment.