GCJ produces incorrect byte- and native code for a variation of
  2-expressive-puzzlers/puzzle-8/DosEquis.java
from
  http://www.javapuzzlers.com/java-puzzlers.zip

Specifically,

| $ cat DosEquis.java
| public class DosEquis {
|     public static void main(String[] _) {
|         char x = 'X';
|         final int i = 0;
|         System.out.print  (true  ? x : 0);
|         System.out.println(false ? i : x);
|     }
| }

This should print out XX, as it does under Sun's JVM:

| $ java -version
| java version "1.4.2_08"
| Java(TM) 2 Runtime Environment, Standard Edition (build 1.4.2_08-b03)
| Java HotSpot(TM) Client VM (build 1.4.2_08-b03, mixed mode)
| $ javac DosEquis.java
| $ java -cp . DosEquis
| XX

Under GCJ, I get X88 instead:

| $ gcj --version
| gcj (GCC) 4.0.1 20050727 (Red Hat 4.0.1-5)
| Copyright (C) 2005 Free Software Foundation, Inc.
| This is free software; see the source for copying conditions.  There is NO
| warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
| 
| $ gcj -C DosEquis.java
| $ gij DosEquis
| X88
| $ gcj -o dos-equis --main=DosEquis DosEquis.java
| $ ./dos-equis 
| X88

Eclipse compiler gets it right:

| $ rm DosEquis.class 
| $ ecj -v
| Eclipse Java Compiler v_579_R31x, 3.1.1 release, Copyright IBM Corp \
| 2000, 2005. All rights reserved.
| $ ecj DosEquis.java
| $ gij DosEquis
| XX

GCJ violates the following JLS clause:

http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293

  | 15.25 Conditional Operator ? :
  | 
  | The type of a conditional expression is determined as follows:
  | 
  | * If one of the operands is of type T where T is byte, short, or
  |   char, and the other operand is a constant expression of type int
  |   whose value is representable in type T, then the type of the
  |   conditional expression is T.

Since i is declared final in the above example, it is a constant
expression of type int.  Therefore, the above clause applies in this
case.


-- 
           Summary: dos equis
           Product: gcc
           Version: 4.0.1
            Status: UNCONFIRMED
          Severity: normal
          Priority: P3
         Component: java
        AssignedTo: unassigned at gcc dot gnu dot org
        ReportedBy: vadimn at redhat dot com


http://gcc.gnu.org/bugzilla/show_bug.cgi?id=25001

Reply via email to