8273257: jshell doesn't compile a sealed hierarchy with a sealed interface and a non-sealed leaf

Reviewed-by: vromero
This commit is contained in:
Jan Lahoda 2021-09-03 09:30:56 +00:00
parent f17ee0c5c7
commit 7b023a3f60
3 changed files with 45 additions and 5 deletions

View File

@ -5300,9 +5300,16 @@ public class Attr extends JCTree.Visitor {
if (sealedSupers.isEmpty()) {
if ((c.flags_field & Flags.NON_SEALED) != 0) {
boolean hasErrorSuper = types.directSupertypes(c.type)
.stream()
.anyMatch(s -> s.tsym.kind == Kind.ERR);
boolean hasErrorSuper = false;
hasErrorSuper |= types.directSupertypes(c.type)
.stream()
.anyMatch(s -> s.tsym.kind == Kind.ERR);
ClassType ct = (ClassType) c.type;
hasErrorSuper |= !ct.isCompound() && ct.interfaces_field != ct.all_interfaces_field;
if (!hasErrorSuper) {
log.error(TreeInfo.diagnosticPositionFor(c, env.tree), Errors.NonSealedWithNoSealedSupertype(c));
}

View File

@ -23,7 +23,7 @@
/*
* @test
* @bug 8246353
* @bug 8246353 8273257
* @summary Test sealed class in jshell
* @modules jdk.jshell
* @build KullaTesting TestingInputStream ExpectedDiagnostic
@ -53,6 +53,16 @@ public class SealedClassesTest extends KullaTesting {
assertEval("new I()");
}
public void testInterface() {
TypeDeclSnippet base = classKey(
assertEval("sealed interface I permits C {}",
ste(MAIN_SNIPPET, Status.NONEXISTENT, Status.RECOVERABLE_NOT_DEFINED, false, null)));
assertEval("final class C implements I {}",
added(VALID),
ste(base, Status.RECOVERABLE_NOT_DEFINED, Status.VALID, true, null));
assertEval("new C()");
}
public void testNonSealed() {
TypeDeclSnippet base = classKey(
assertEval("sealed class B permits I {}",
@ -63,4 +73,15 @@ public class SealedClassesTest extends KullaTesting {
assertEval("class I2 extends I {}");
assertEval("new I2()");
}
public void testNonSealedInterface() {
TypeDeclSnippet base = classKey(
assertEval("sealed interface B permits C {}",
ste(MAIN_SNIPPET, Status.NONEXISTENT, Status.RECOVERABLE_NOT_DEFINED, false, null)));
assertEval("non-sealed class C implements B {}",
added(VALID),
ste(base, Status.RECOVERABLE_NOT_DEFINED, Status.VALID, true, null));
assertEval("class C2 extends C {}");
assertEval("new C2()");
}
}

View File

@ -25,7 +25,7 @@
* SealedCompilationTests
*
* @test
* @bug 8246353
* @bug 8246353 8273257
* @summary Negative compilation tests, and positive compilation (smoke) tests for sealed classes
* @library /lib/combo /tools/lib
* @modules
@ -727,6 +727,18 @@ public class SealedCompilationTests extends CompilationTestCase {
""");
}
public void testNonSealedErroneousSuperInterface() {
assertFail("compiler.err.cant.resolve",
d -> {
if (diags.keys().size() != 1) {
fail("Unexpected errors: " + diags.toString());
}
},
"""
non-sealed class C implements Undefined {}
""");
}
public void testIllFormedNonSealed() {
for (String s : List.of(
"""