Bug 106284 - [1.5][compiler] Type inference succeeds, but should fail
Summary: [1.5][compiler] Type inference succeeds, but should fail
Status: RESOLVED DUPLICATE of bug 103485
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC All
: P3 normal (vote)
Target Milestone: 3.1.1   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-08-06 14:08 EDT by Krzysztof Sobolewski CLA
Modified: 2005-08-08 10:19 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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 ***