2

I have three types, A, B, and C, which are defined as follows:

public sealed interface A extends Comparable<A> permits B<?>, C { ...
    
public non-sealed interface B<T> extends A { ...
    
public record C(String s, int i) implements A { ...

Everything compiles and works fine in Eclipse. Now when I run a gradle build, I get an error error: '{' expected at the place of the opening bracket at permits B<?>. When I remove <?>, so that the type definition is as follows (raw type):

public sealed interface A extends Comparable<A> permits B, C { ...

...then gradle doesn't complain and the build is successful. Is this a gradle bug or is the type definition I am using not allowed?

user7291698
  • 1,972
  • 2
  • 15
  • 30
  • Sounds like when you use Gradle, you are compiling it with an older Java version that does not support sealed classes and interfaces. – Jesper Aug 22 '22 at 09:54

1 Answers1

4

According to the Java Language Specification §8.1.6 the items of the permits type list must be without their type parameters.

So, permits B<?> is incorrect and has to be corrected to permits B.

Please make sure that permits B<?> does not give a compile error and permits B<?> does give a B is a raw type. References to generic type B<T> should be parameterized warning instead, has been reported to Eclipse JDT.

howlger
  • 31,050
  • 11
  • 59
  • 99
  • Small addition: in fact, due to type erasure, it doesn't make sense to write `permits B>`, since you anyway can't permit B but not B for example. Hence they logically forbid it in the spec to avoid confusion. – QuentinC Aug 22 '22 at 10:04
  • 1
    @QuentinC Yes, it doesn't make sense. It's a bug of Eclipse JDT 4.24 showing a raw type warning for `permits B` and not showing a compile error for `permits B>`. – howlger Aug 22 '22 at 10:08
  • Thank you, howlger and QuentinC! Your reasoning makes sense, however, I've read JLS §8.1.6 now and can't find an explicit statement that the `permits` type list must be without their type parameters, how do you infer this from §8.1.6? – user7291698 Aug 22 '22 at 11:05
  • 2
    @user7291698 The grammar is given as `ClassPermits: permits TypeName {, TypeName}` not as `ClassPermits: permits TypeName [TypeParameters] {, TypeName [TypeParameters] }`. – howlger Aug 22 '22 at 11:17
  • There is an open bug for the issue here: https://bugs.eclipse.org/bugs/show_bug.cgi?id=574573 – KasperNielsen Jan 14 '23 at 22:57