Bug 106284

Summary: [1.5][compiler] Type inference succeeds, but should fail
Product: [Eclipse Project] JDT Reporter: Krzysztof Sobolewski <jezuch>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: RESOLVED DUPLICATE QA Contact:
Severity: normal    
Priority: P3    
Version: 3.1   
Target Milestone: 3.1.1   
Hardware: PC   
OS: All   
Whiteboard:

Description Krzysztof Sobolewski CLA 2005-08-06 14:08:38 EDT
Sample code:

class Infer
{
    private static <T extends Comparable<? super T>> T max(T... elems)
    {
        T max=null;
        for (T elem : elems)
            if (max == null || max.compareTo(elem) < 0)
                max=elem;
        return max;
    }

    public static void main(String[] args)
    {
        System.out.println(max(1, 2.0, new BigDecimal(Math.PI)));
    }
}

This code compiles fine in Eclipse 3.1 and throws ClassCastException in runtime.
The whole point of generics is to prevent "unexpected" ClassCastExceptions such
as this (when there are no warnings or errors) and it is true for this sample as
well: this should not compile. The type inferred for <T> in max is Number but
Number is *not* Comparable<Number>.
javac says:
src/jezuch/utils/starmapper3/test/Infer.java:18: <T>max(T...) in
jezuch.utils.starmapper3.test.Infer cannot be applied to
(int,double,java.math.BigDecimal); inferred type argument(s)
java.lang.Number&java.lang.Comparable<? extends
java.lang.Number&java.lang.Comparable<?>> do not conform to bounds of type
variable(s) T
        System.out.println(max(1, 2.0, new BigDecimal(Math.PI)));
Comment 1 Philipe Mulet CLA 2005-08-08 10:19:29 EDT
Added GenericTypeTest#test798

*** This bug has been marked as a duplicate of 103485 ***