Bug 106946 - [1.5][compiler] suspicious compiler errors on mix of for each and generics
Summary: [1.5][compiler] suspicious compiler errors on mix of for each and generics
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.2 M2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-08-13 13:15 EDT by jho CLA
Modified: 2005-09-05 05:41 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 jho CLA 2005-08-13 13:15:11 EDT
//suspicious compiler errors on mix of for each and generics
class Node{}
interface Set1<N extends Node> extends Iterable<N>{}
interface Set2 extends Iterable<Node>{}
class SetIterator<N extends Node> implements Iterator<N>{
	public N next(){return null;}
	public boolean hasNext(){return true;}
	public void remove(){}
}
interface Set3<N extends Node> extends Iterable<N>{
	SetIterator<N> iterator();
}
class X{
	void f1(Set1 s){
		Node n_ = s.iterator().next();
		//   ^Type mismatch: cannot convert from Object to Node
		// this was unexpected (s can only contain types derivered 
from Node)
		for (Node n : s){			
			//    ^Type mismatch: cannot convert from Object to 
Node
			// this was unexpected 
		}
	}
	void f2(Set2 s){
		Node n_ = s.iterator().next();
		for (Node n : s){			
		}
	}
	void f3(Set3 s){
		Node n_ = s.iterator().next();
		//  (^ no error here)
		for (Node n : s){			
			//    ^Type mismatch: cannot convert from Object to 
Node
			// this is even stranger as we already know that 
s.iterator().next() have the right type
		}
	}
}
Comment 1 Philipe Mulet CLA 2005-09-05 05:25:12 EDT
It indeed looks surprising, now javac agrees with us.
Comment 2 Philipe Mulet CLA 2005-09-05 05:40:20 EDT
Got it, problem comes from the fact Set1 is a raw type, where Set2 isn't.
When Set1 (raw) is used, its supertype is also erased. Thus the supertype of raw
Set1 is raw Iterable as opposed to Iterable<N>; hence it is inheriting
#iterator() which returns an instance of raw Iterator.

Instead of using Set1, use Set1<Node> instead.

Added GenericTypeTest#test809
Comment 3 Philipe Mulet CLA 2005-09-05 05:41:50 EDT
also read http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=5006039