JDK-5101303 : misleading "incompatible types" compiler error
  • Type: Enhancement
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 5.0
  • Priority: P3
  • Status: Resolved
  • Resolution: Fixed
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2004-09-13
  • Updated: 2013-05-31
  • Resolved: 2013-05-31
The Version table provides details related to the release that this issue/RFE will be addressed.

Unresolved : Release in which this issue/RFE will be addressed.
Resolved: Release in which this issue/RFE has been resolved.
Fixed : Release in which this issue/RFE has been fixed. The release containing this fix may be available for download as an Early Access Release or a General Availability Release.

To download the current JDK release, click here.
JDK 7
7-poolFixed
Related Reports
Relates :  
Relates :  
Relates :  
Description
Name: gm110360			Date: 09/13/2004


FULL PRODUCT VERSION :
java version "1.5.0-rc"
Java(TM) 2 Runtime Environment, Standard Edition (build 1.5.0-rc-b63)
Java HotSpot(TM) Client VM (build 1.5.0-rc-b63, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
Compiling the attached source emits the following error message:

21: incompatible types
found   : java.util.Iterator<R>
required: java.util.Iterator<R>
      return iterator;
             ^
1 error

I cannot see what might be wrong with the source, but in any case the error message is not helpful.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
compile the sample source


ERROR MESSAGES/STACK TRACES THAT OCCUR :
21: incompatible types
found   : java.util.Iterator<R>
required: java.util.Iterator<R>
      return iterator;
             ^
1 error


REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
import java.util.Iterator;

class MyIterator<R> implements Iterator<R> {
  public void remove() {
  }

  public R next() {
    return null;
  }

  public boolean hasNext() {
    return false;
  }
}

class MyClass<R> {
  final Iterator<R> iterator = new MyIterator<R>();

  class IterableImpl<R> implements Iterable<R> {
    public Iterator<R> iterator() {
      return iterator;
    }
  }
}

---------- END SOURCE ----------
(Incident Review ID: 305630) 
======================================================================
###@###.### 10/8/04 20:28 GMT

Comments
As of JDK 7, those messages have been improved by using extra information: Test.java:21: error: incompatible types: Iterator<R#1> cannot be converted to Iterator<R#2> return iterator; ^ where R#1,R#2 are type-variables: R#1 extends Object declared in class MyClass R#2 extends Object declared in class MyClass.IterableImpl 1 error Now it's clear that the two 'R' in the original message refer to different types.
31-05-2013

EVALUATION Don't put a type parameter on the inner class IterableImpl: class MyClass<R> { final Iterator<R> iterator = new MyIterator<R>(); class IterableImpl implements Iterable<R> { public Iterator<R> iterator() { return iterator; } } } ###@###.### 2004-09-13 gyrefalcon suggests: "I'd like an explicitly qualified type parameter in that message so I can tell MyClass.<R> apart from IterableImpl.<R>" Which is a good idea. ###@###.### 10/8/04 20:29 GMT
08-10-0004