Search code examples
javacovariancestack-overflowcontravariancetypechecking

Java snippet that causes stack overflow in the compiler or typechecker (javac)?


Yesterday at a seminar the presenter (Peter Sestoft) showed a small java program, with 3 classes, featuring both co-variance and contra-variance. When attempting to compile using javac, the type checker will throw a StackOverflowException.

The snippet is developed by some guys that work at Microsoft (think one was called Kennedy).

Can't find it using Google. Does anyone know the code snippet, and could you paste it here (it's max. 10 lines of code), for everyone to see? :)

It was quite fun...


Solution

  • Found it (asked the presenter)! It's a StackOverflowException in both 6.0 and 7.0:

    class T { }
    class N<Z> { }
    class C<X> extends N<N<? super C<C<X>>>> {
      N<? super C<T>> cast(C<T> c) { return c; }
    }
    

    It's from Andrew Kennedy and Benjamin Pierce: On Decidability of Nominal Subtyping with Variance. International Workshop on Foundations and Developments of Object-Oriented Languages g(FOOL/WOOD'07), Nice, France 2007.