This is the mail archive of the
gcc-bugs@gcc.gnu.org
mailing list for the GCC project.
[Bug java/42892] New: Incorrect code generated for enhanced for loop.
- From: "suckfish at ihug dot co dot nz" <gcc-bugzilla at gcc dot gnu dot org>
- To: gcc-bugs at gcc dot gnu dot org
- Date: 28 Jan 2010 06:51:06 -0000
- Subject: [Bug java/42892] New: Incorrect code generated for enhanced for loop.
- Reply-to: gcc-bugzilla at gcc dot gnu dot org
The following code (to be attached) contains two loops, one of which is
miscompiled.
The first is an enhanced for loop, the second is the same loop, but manually
desugared accorded to the JLS (JLS 3, section 14.14.2
http://java.sun.com/docs/books/jls/third_edition/html/statements.html#14.14.2).
The two loops should therefore behave identically. They do not; the first loop
[incorrectly] finishes normally, the second loop [correctly] throws a class
cast exception.
The issue is that according to the JLS,
for (String s:u) ...
gets desugarred to
for (Type i = u.iterator(); i.hasNext(); ) { String s = i.next(); ... }
where Type is the type of u.iterator().
However, after erasing generics, the initialisation of the loop variable i
potentially requires a cast (after erase, in this case, u.iterator() has
compile-time type Iterator, while the variable has type MyIterator).
It appears that gcj does not insert the required cast.
(Both Sun javac and eclipse get this wrong also, in an identical manner. It is
possible that Sun will decide to change the JLS to match the compiler
behaviour?)
--
Summary: Incorrect code generated for enhanced for loop.
Product: gcc
Version: 4.4.3
Status: UNCONFIRMED
Severity: normal
Priority: P3
Component: java
AssignedTo: unassigned at gcc dot gnu dot org
ReportedBy: suckfish at ihug dot co dot nz
GCC host triplet: x86_64-redhat-linux
http://gcc.gnu.org/bugzilla/show_bug.cgi?id=42892