Bug 521352 - [compiler] Code involving wildcards and cast compiles with ECJ but fails to compile with Oracle JDK
Summary: [compiler] Code involving wildcards and cast compiles with ECJ but fails to c...
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.7   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2017-08-24 04:51 EDT by Tobias Gierke CLA
Modified: 2022-12-23 10:27 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 Tobias Gierke CLA 2017-08-24 04:51:30 EDT
Eclipse Version: Oxygen Release (4.7.0)
Build id: 20170620-1800

java version "1.8.0_121"
Java(TM) SE Runtime Environment (build 1.8.0_121-b13)
Java HotSpot(TM) 64-Bit Server VM (build 25.121-b13, mixed mode)


The following code compiles in Eclipse but fails to compile on JDK 

--------------------
import java.io.Serializable;
import java.util.Set;

public class BugTest
{
    public interface IModel<T> {
    }
    
    public static Object someMethod(IModel<? extends Serializable> model)
    {
        return createStuff( (IModel<Set<String>>) model );
    }
    
    private static Object createStuff(IModel<Set<String>> model) {
        return null; 
    }
}
--------------------

Error message given by the Oracle compiler is:

---------------------
BugTest.java:11: error: incompatible types: IModel<CAP#1> cannot be converted to IModel<Set<String>>
        return createStuff( (IModel<Set<String>>) model );
                                                  ^
  where CAP#1 is a fresh type-variable:
    CAP#1 extends Serializable from capture of ? extends Serializable
---------------------
Comment 1 Stephan Herrmann CLA 2017-08-24 09:30:31 EDT
Thanks for the report.

The question at hand is:
  "Are CAP#1 (with upper bound Serializable) and Set<String> provably distinct type arguments?"

Looking at Bug 158870 it seems that ecj implemented TypeBinding.isProvablyDistinctTypeArgument while the spec was still being clarified (and this method never significantly changed during the last 10 years).

E.g. in JLS 3 we find:
 "Two type arguments are provably distinct if neither of the arguments is a type variable or wildcard, and the two arguments are not the same type."
This is not sufficient for reporting an error (and ecj does implement more specific checks than that).

Things have changes since, and here's a bullet from 4.5.1 (since Java 7) which we fail to correctly implement:

• One type argument is a type variable or wildcard, with an upper bound (from
capture conversion (§5.1.10), if necessary) of S; and the other type argument T
is not a type variable or wildcard; and neither |S| <: |T| nor |T| <: |S| (§4.8, §4.10).

In ecj, TypeBinding.isProvableDistinctSubType(TypeBinding) is satisfied, when if finds two interface types, at which point those types are not *provably distinct types*, but that's not enough for *type arguments*.


OTOH, if you have an example, where the cast succeeds at runtime, then you have provided a counter example to the proof to be made, and hence the intuition of "provably distinct" is falsified. However, since JLS provides its own specification for "provably distinct", intuitive reasoning doesn't seem to apply.
Comment 2 Manoj N Palat CLA 2018-05-17 03:23:22 EDT
bulk move out of 4.8
Comment 3 Manoj N Palat CLA 2018-08-16 00:07:36 EDT
Bulk move out of 4.9
Comment 4 Eclipse Genie CLA 2022-12-23 10:27:02 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.