Bug 527347 - Subtyping error between wildcard and genericty
Summary: Subtyping error between wildcard and genericty
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.6   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-11-16 08:58 EST by Daniele Varacca CLA
Modified: 2023-01-04 03:02 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 Daniele Varacca CLA 2017-11-16 08:58:55 EST
The following class compiles with the eclipse compiler, but it should not. Indeed when run, it produces a ClassCastException. The call g(ll) should not be accepted

import java.util.*;

public class Wild {
		static <T> void g (List<? extends List<T>> ca) {
		     ca.get(0).add(ca.get(1).get(0));
		}

		public static void main (String[] args) {
			List<List<?>> l = new ArrayList<>();
			List<String> lstr = new ArrayList<>();
			List<Integer> lint = new ArrayList<>();
			lint.add(new Integer(5));
			l.add(lstr);
			l.add(lint);
			List<? extends List<?>> ll=l; 
			g(ll);
			String s = lstr.get(0); 
		}
		
}

Indeed javac does not accept it.
Comment 1 Stephan Herrmann CLA 2017-11-16 09:16:14 EST
Thanks for the report. I can reproduce in all versions of ecj.

Unification of captures and wildcards has been subject to many bugs & discussions in the past. This will not be an easy fix without breaking things in other places.
Comment 2 Daniele Varacca CLA 2017-11-16 09:38:30 EST
Well, the example is a bit contrived, and I'm not sure many people would write this in real applications - we came up with this example during a advance java programming course for Master students...
Comment 3 Manoj N Palat CLA 2018-05-17 03:24:28 EDT
bulk move out of 4.8
Comment 4 Manoj N Palat CLA 2018-08-16 00:08:26 EDT
Bulk move out of 4.9
Comment 5 Eclipse Genie CLA 2023-01-03 16:19:31 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.
Comment 6 Daniele Varacca CLA 2023-01-04 02:27:28 EST
The Genie asked me to verify the status of this bug, which is still present. I suppose nobody came up with natural code that produces it, so perhaps you should just leave a footnote somewhere saying that there is a minor discrepancy between ecj and javac and that is it.
Comment 7 Daniele Varacca CLA 2023-01-04 03:02:42 EST
By the way, for the record, the same problem arises even if you call

g(l);

You don't need the extra variable ll.