Bug 25001 - dos equis
Summary: dos equis
Status: RESOLVED FIXED
Alias: None
Product: gcc
Classification: Unclassified
Component: java (show other bugs)
Version: 4.0.1
: P3 normal
Target Milestone: 4.3.0
Assignee: Not yet assigned to anyone
URL:
Keywords:
Depends on: 28067
Blocks:
  Show dependency treegraph
 
Reported: 2005-11-23 14:07 UTC by Vadim Nasardinov
Modified: 2007-01-09 20:48 UTC (History)
2 users (show)

See Also:
Host:
Target:
Build:
Known to work:
Known to fail:
Last reconfirmed: 2006-02-26 18:49:58


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Vadim Nasardinov 2005-11-23 14:07:35 UTC
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.
Comment 1 Vadim Nasardinov 2005-11-23 14:12:31 UTC
> 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.

See also

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

   | 15.28 Constant Expression
   | 
   | A compile-time constant expression is an expression denoting a
   | value of primitive type or a String that is composed using only
   | the following:
   | 
   | [...]
   | 
   | * Simple names that refer to final variables whose initializers
   |   are constant expressions
Comment 2 Ranjit Mathew 2005-11-24 09:33:36 UTC
Confirmed on mainline. Also confirmed that GCJX does not have this bug.
Comment 3 Tom Tromey 2007-01-09 20:48:17 UTC
All gcj front end bugs have been fixed by the gcj-eclipse branch merge.
I'm mass-closing the affected PRs.
If you believe one of these was closed in error, please reopen it
with a note explaining why.
Thanks.