开发者

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

开发者 https://www.devze.com 2022-12-12 17:15 出处:网络
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 check

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 sni开发者_如何学运维ppet, and could you paste it here (it's max. 10 lines of code), for everyone to see? :)

It was quite fun...


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;
    }
}


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消