Some Eclipse Foundation services are deprecated, or will be soon. Please ensure you've read this important communication.
Bug 343704 - [1.5][compiler] Wrong reporting of unchecked downcasts
Summary: [1.5][compiler] Wrong reporting of unchecked downcasts
Status: CLOSED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.6.1   Edit
Hardware: All All
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: Srikanth Sankaran CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2011-04-24 11:21 EDT by Matt McCutchen CLA
Modified: 2019-11-17 19:37 EST (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Matt McCutchen CLA 2011-04-24 11:21:12 EDT
The determination of whether a downcast from S to T is unchecked appears to be seriously flawed:

- Eclipse assumes that it is unchecked if S has fewer type arguments than T (http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java?revision=1.138&view=markup#l326).  This is faulty because all of the type arguments of T may be piled into fewer type arguments of S.

- Eclipse seems to test whether those type arguments of T that are wildcards or type variables are determined by S (http://dev.eclipse.org/viewcvs/viewvc.cgi/org.eclipse.jdt.core/compiler/org/eclipse/jdt/internal/compiler/ast/CastExpression.java?revision=1.138&view=markup#l331).  This is somewhat backwards.  Type arguments that are unbounded wildcards do not need the check (nothing can go wrong), while type arguments that are specific reference types definitely do to make sure that S determines the claimed value of the type argument.

Examples:

class BogusDowncastChecking {
    static interface Tag<A> {}
    static class Cell<T> implements Tag<Void> {
        final T obj;
        Cell(T obj) { this.obj = obj; }
    }

    static Cell<String> unsound(Tag<Void> c) {
        /* Expected: Unchecked cast warning.  Tag<Void> gives us no information
         *     about T.
         * Actual: OK */
        return (Cell<String>) c;
    }

    public static void main(String[] args) {
        Cell<String> oops = unsound(new Cell<Integer>(42));
        String s = oops.obj; // ClassCastException at synthetic cast
        System.out.println(s);
    }

    static interface Group<U, V> {}
    static interface TwoParams<A, B> extends Tag<Group<A, B>> {}

    static TwoParams<Integer, String> ok(Tag<Group<Integer, String>> x) {
        /* Expected: OK.  Group<Integer, String> forces A = Integer and
         *     B = String.
         * Actual: Unchecked cast warning. */
        return (TwoParams<Integer, String>) x;
    }

    static TwoParams<?, ?> weird(Tag<Group<Integer, String>> x) {
        /* Expected: OK.  All the type arguments of the target type are
         *     unbounded wildcards, so this cannot go wrong.
         * Actual: "Cannot cast" error?! */
        return (TwoParams<?, ?>) x;
    }
}

The noted incorrect behaviors are the same in Fedora's eclipse-jdt-3.6.1-6.2.fc14.x86_64 (M20100909-0800) and Eclipse 4.0.0 (I20100727-1520).  javac gets all of these cases right.
Comment 1 Eclipse Genie CLA 2019-11-17 19:37:22 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.