Bug 148041

Summary: [compiler][1.5] should issue unchecked warning on cast to Set<X> of Iterator#next () returning X
Product: [Eclipse Project] JDT Reporter: Maxime Daniel <maxime_daniel>
Component: CoreAssignee: Philipe Mulet <philippe_mulet>
Status: VERIFIED FIXED QA Contact:
Severity: normal    
Priority: P3    
Version: 3.2   
Target Milestone: 3.3 M3   
Hardware: PC   
OS: All   
Whiteboard:
Attachments:
Description Flags
Proposed patch none

Description Maxime Daniel CLA 2006-06-21 10:22:34 EDT
Version: 3.2.0
Build id: I20060614-0843

The following source code:

import java.util.HashSet;
import java.util.Iterator;
import java.util.Set;
public class X {
	public static void main(String[] args) {
		Set<X> set = new HashSet<X>();
		for (Iterator<X> iterator = set.iterator(); iterator.hasNext();) {
			Set<X> element = (Set<X>) iterator.next(); // warning
		}
	}
}

gets an unchecked warning on the cast with javac 1.5.0_08 and 6.0 b86. Eclipse does not complain.
Arguably, the return type of iterator.next() is X, which cannot be cast to Set<X>.
Note that (boolean) iterator.next() or (Integer) iterator.next() both raise plain errors with javac and eclipse.
All other things being equal, getting X to implement Set<X> silences the unchecked warning raised by javac.
Comment 1 Maxime Daniel CLA 2006-06-21 10:28:18 EDT
See GenericTypeTest#252, which trigerred the bug. (Checked that removing the error on channels does not improve the situation.)
Comment 2 Philipe Mulet CLA 2006-10-09 10:38:18 EDT
Reproduced with HEAD.
Comment 3 Philipe Mulet CLA 2006-10-30 12:18:31 EST
Added GenericTypeTest#test1062-1064
Comment 4 Philipe Mulet CLA 2006-10-30 12:20:38 EST
Created attachment 52944 [details]
Proposed patch
Comment 5 Philipe Mulet CLA 2006-10-30 12:22:20 EST
Released for 3.3M3.
Fixed
Comment 6 David Audel CLA 2006-10-31 04:00:27 EST
Verified for 3.3 M3 using build I20061030-0010
Comment 7 David Audel CLA 2006-10-31 04:10:22 EST
Verified using I20061030-1704 and not I20061030-0010