[sealed][15] javac rejects type parameters extending intersection sealed types

Tagir Valeev amaembo at gmail.com
Mon Oct 5 05:06:57 UTC 2020


Hello!

>  //Intellij 2020.2: "<'T' exte": sealed non-sealed or final modifiers expected

Should be fixed in 2020.3 EAP, please check.

With best regards,
Tagir Valeev.

On Sat, Oct 3, 2020 at 6:04 PM Zi Xin <hive7003 at outlook.com> wrote:
>
> Hello!
>
> The following code does not compile on openjdk 15 GA:
>
>
> sealed interface Up permits DownImpl {
>     <T extends Aux0 & Sti1> T foo();
> }
>
> sealed interface Aux0 permits Ver1 { }
>
> sealed interface Sti1 permits Ver1 { }
>
> sealed abstract class Asc3 permits Ver1 { }
>
> interface Open { }
>
> final class Ver1 extends Asc3 implements Aux0, Sti1 { }
>
> final class Fin2 {}
>
> final class DownImpl implements Up {
>     @Override
>     //sealed1 & sealed2
>     public <T extends Aux0 & Sti1> T foo() {
>         return null;
>     }
>
>     //sealed2 & sealed1
>     public static <T extends Sti1 & Aux0> void bar() {}
>     //Intellij 2020.2: "<'T' exte": sealed non-sealed or final modifiers expected
>
>     //sealed2 & non-sealed
>     public static <T extends Sti1 & Open> void baz() {}
>
>     //non-sealed & sealed1
>     public static <T extends Open & Aux0> void thud() {}
>
>     //sealed abstract & non-sealed
>     public static <T extends Asc3 & Open> void qux() {}
>
>     //sealed abstract & sealed1
>     public static <T extends Asc3 & Aux0> void corge() {}
>
>     //sealed abstract & non-sealed
>     public static <T extends Asc3 & Open> void waldo() {}
>
> //  //non-sealed & sealed abstract
> //  public static <T extends Open & Asc3> void grault() {} //should not pass
>
>     //sealed2
>     public static <T extends Sti1> T passed_2() {
>         return null;
>     }
> }
>
> //sealed1
> class Parameterized<E extends Aux0> { } // passed!
> //sealed1 & sealed2
> class ParameterizedIntersect<E extends Aux0 & Sti1> { }
>
> --------------------------------------------------------------
>
> javac displays following errors:
>
> IssueErr.java:4: error: class is not allowed to extend sealed class: Aux0
>     <T extends Aux0 & Sti1> T foo();
>      ^
> IssueErr.java:22: error: class is not allowed to extend sealed class: Aux0
>     public <T extends Aux0 & Sti1> T foo() {
>             ^
> IssueErr.java:27: error: class is not allowed to extend sealed class: Sti1
>     public static <T extends Sti1 & Aux0> void bar() {}
>                    ^
> IssueErr.java:31: error: class is not allowed to extend sealed class: Sti1
>     public static <T extends Sti1 & Open> void baz() {}
>                    ^
> IssueErr.java:34: error: class is not allowed to extend sealed class: Aux0
>     public static <T extends Open & Aux0> void thud() {}
>                    ^
> IssueErr.java:37: error: class is not allowed to extend sealed class: Asc3
>     public static <T extends Asc3 & Open> void qux() {}
>                    ^
> IssueErr.java:40: error: class is not allowed to extend sealed class: Asc3
>     public static <T extends Asc3 & Aux0> void corge() {}
>                    ^
> IssueErr.java:43: error: class is not allowed to extend sealed class: Asc3
>     public static <T extends Asc3 & Open> void waldo() {}
>                    ^
> IssueErr.java:57: error: class is not allowed to extend sealed class: Aux0
> class ParameterizedIntersect<E extends Aux0 & Sti1> { }
>                              ^
> Note: IssueErr.java uses preview language features.
> Note: Recompile with -Xlint:preview for details.
> 9 errors
>
> Process finished with exit code 1
>
> ---------------------------------
>
> I would expect that the code compiles since a final class name may be used in type bound declarations [1]. IMO the same goes for sealed classes.
> And I couldn't find any topic on it in the spec[2][3] or JEP[4].
> Is it an implementation oversight or a spec issue?
>
> Best regards,
> Zhou Zi-yan
>
>
> [1]
> final class FinalV1 {}
> class Pa1<T extends FinalV1> {}
> [2] https://docs.oracle.com/javase/specs/jls/se15/jls15.pdf
> [3] https://docs.oracle.com/javase/specs/jls/se15/preview/specs/sealed-classes-jls.html
> [4] http://openjdk.java.net/jeps/360


More information about the compiler-dev mailing list