views:

74

answers:

2

Yesterday at a seminar the presenter 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...

+2  A: 

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;
    }
}
Tom Hawtin - tackline
Nice examples, especially the second one. But the one I'm looking for fails in both 6.0 and 7.0. I can't quite remember it, but I'd recognize it if I saw it again :)
Skipperkongen
+1  A: 

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.

Skipperkongen
ofc it may not cause a StackOverflowException on a machine with infinite storage, but that was implied. Don't know if that is the case or not.
Skipperkongen