Skip to content

Commit

Permalink
8259905: Compiler treats 'sealed' keyword as 'var' keyword
Browse files Browse the repository at this point in the history
Reviewed-by: jlahoda
  • Loading branch information
Vicente Romero committed Jan 29, 2021
1 parent 6415d3b commit b2e582a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 34 deletions.
Expand Up @@ -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);
Expand Down
14 changes: 7 additions & 7 deletions test/langtools/tools/javac/lvti/ParserTest.out
Expand Up @@ -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
Expand Down
38 changes: 14 additions & 24 deletions test/langtools/tools/javac/sealed/SealedCompilationTests.java
Expand Up @@ -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) { } }",
Expand All @@ -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() {}",
Expand All @@ -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);
}

Expand Down

1 comment on commit b2e582a

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.