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

At the seminar yesterday, the facilitator (Peter Sestoft) demonstrated a small java program consisting of 3 classes, with covariance and contradiction. If you try to compile using javac, the type checker will throw a StackOverflowException.

The fragment was developed by some guys who work at Microsoft (I think it was called Kennedy).

Cannot find it using Google. Does anyone know a code snippet, and you can paste it here (this is no more than 10 lines of code) so that everyone can see? :)

It was pretty fun ...

+5
source share
2 answers

Found it (the leader asked)! This is a StackOverflowExceptionin 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; }
}

This is from Andrew Kennedy and Benjamin Pierce: on the solvability of the nominee. Subtyping with a difference. International Workshop on the Basics and Development of Object Oriented Languages ​​g (FOOL / WOOD'07), Nice, France 2007

+3
source

Have you tried bugs.sun.com? Here a is StackOverflowErroronly in 5.0:

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 one (only 5.0):

class F<T> {}
class C<X extends F<F<? super X>>> {
    C(X x) {
        F<? super X> f = x;
    }
}
+3
source

All Articles