Type-variable references from local interface declaration

Tagir Valeev amaembo at gmail.com
Mon Jun 15 11:45:51 UTC 2020


Hello!

The same problem appears for local enums as well. And, I believe, it
causes type system unsoundness:

class LocalEnum {
  static <U> U getAndSet(U u) {
    enum X {
      A;
      U u;
    }
    U old = X.A.u;
    X.A.u = u;
    return old;
  }

  public static void main(String[] args) {
    String s = getAndSet("foo");
    Integer i = getAndSet(123);
  }
}

This code can be compiled without error or warning, yet it produces
ClassCastException when executed.

With best regards,
Tagir Valeev.

On Mon, Jun 15, 2020 at 6:38 PM Tagir Valeev <amaembo at gmail.com> wrote:
>
> Hello!
>
> The spec draft for local interfaces and enums [1] tells that it's
> illegal to refer to the enclosing method type parameter from the local
> interface. Here's a simplified sample from the spec:
>
> class Box<T> {
>     static <U> Box<U> make(U val) {
>         interface Checker {
>             void check(U val); // compile-time error
>         }
>         return null;
>     }
> }
>
> However, it's successfully compilable using javac 15-ea+26-1287. I
> believe, either spec or implementation should be adjusted.
>
> With best regards,
> Tagir Valeev.
>
> [1] http://cr.openjdk.java.net/~gbierman/jep384/jep384-20200506/specs/local-statics-jls.html#jls-6.5.5.1


More information about the amber-dev mailing list