Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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...

like image 794
Pimin Konstantin Kefaloukos Avatar asked Nov 25 '09 10:11

Pimin Konstantin Kefaloukos


2 Answers

Have you tried bugs.sun.com? Here's a StackOverflowError in 5.0 only:

import java.util.*;

class Test<T extends Comparable<? super T>> {

    abstract class Group<E extends Comparable<? super E>> 
    extends ArrayList<E> 
    implements Comparable<Group<? extends E>> {}

    abstract class Sequence<E extends Comparable<? super E>>
    extends TreeSet<E>
    implements Comparable<Sequence<? extends E>> {}

    public void containsCombination(SortedSet<Group<T>> groups,
                    SortedSet<Sequence<T>> sequences) {
        foo(groups, sequences);
    }

    <C extends Collection<T>> void foo(SortedSet<? extends C> setToCheck,
                       SortedSet<? extends C> validSet) {}

}

Here's another (again 5.0 only):

class F<T> {}
class C<X extends F<F<? super X>>> {
    C(X x) {
        F<? super X> f = x;
    }
}
like image 100
Tom Hawtin - tackline Avatar answered Nov 14 '22 23:11

Tom Hawtin - tackline


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.

like image 42
Pimin Konstantin Kefaloukos Avatar answered Nov 15 '22 01:11

Pimin Konstantin Kefaloukos