RFR: 8246778: Compiler implementation for Sealed Classes (Second Preview)

Maurizio Cimadamore mcimadamore at openjdk.java.net
Mon Nov 16 14:01:04 UTC 2020


On Mon, 16 Nov 2020 13:30:06 GMT, Vicente Romero <vromero at openjdk.org> wrote:

> Please review the code for the second iteration of sealed classes. In this iteration we are:
> 
> - Enhancing narrowing reference conversion to allow for stricter checking of cast conversions with respect to sealed type hierarchies
> - Also local classes are not considered when determining implicitly declared permitted direct subclasses of a sealed class or sealed interface
> - renaming Class::permittedSubclasses to Class::getPermittedSubclasses, still in the same method, the return type has been changed to Class<?>[] instead of the previous ClassDesc[]
> - adding code to make sure that annotations can't be sealed
> - improving some tests
> 
> TIA
> 
> Related specs:
> [Sealed Classes JSL](http://cr.openjdk.java.net/~gbierman/jep397/jep397-20201104/specs/sealed-classes-jls.html)
> [Sealed Classes JVMS](http://cr.openjdk.java.net/~gbierman/jep397/jep397-20201104/specs/sealed-classes-jvms.html)
> [Additional: Contextual Keywords](http://cr.openjdk.java.net/~gbierman/jep397/jep397-20201104/specs/contextual-keywords-jls.html)

I left some comments re. the changes in cast conversion routine

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java line 1659:

> 1657:             result = isCastable.visit(t,s);
> 1658:         }
> 1659:         if ((t.tsym.isSealed() || s.tsym.isSealed())) {

It would probably be better to only run this extra check if `result == true`, to minimize compatibility impact.

src/jdk.compiler/share/classes/com/sun/tools/javac/code/Types.java line 1672:

> 1670:             }
> 1671:             // if both are classes or both are interfaces, shortcut
> 1672:             if (ts.isInterface() == ss.isInterface()) {

What happens with code like this?

interface A permits B { }
non-sealed interface B extends A { }
interface C { }

class D implements C, B { } // this is a valid witness for both A and C, but A and C are unrelated with subtyping

class Test {
  void m(A a, C c) {
     a = (A)c;
  }
}```
Note that, w/o sealed types, the above snippet compiles ok - e.g. casting C to A is not going to give problems (as there could be a common subtype D <: A, C).

-------------

PR: https://git.openjdk.java.net/jdk/pull/1227


More information about the compiler-dev mailing list