Bug 117495 - [compiler] ternary ops return wrong type when condition is boolean literal
Summary: [compiler] ternary ops return wrong type when condition is boolean literal
Status: VERIFIED FIXED
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.1.2   Edit
Assignee: Olivier Thomann CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2005-11-22 10:03 EST by Kjetil Ødegaard CLA
Modified: 2006-01-10 05:48 EST (History)
0 users

See Also:


Attachments
Proposed fix (2.29 KB, patch)
2005-11-22 19:54 EST, Olivier Thomann CLA
no flags Details | Diff

Note You need to log in before you can comment on or make changes to this bug.
Description Kjetil Ødegaard CLA 2005-11-22 10:03:40 EST
These are not equivalent in the Eclipse compiler:
		int x = 2;
		System.out.println("n: "+(x > 1  ? 2 : 1.0));

		=> n: 2.0
and
		
		System.out.println("n: "+(true ? 2 : 1.0));

		=> n: 21

This is incorrect according to

http://java.sun.com/docs/books/jls/third_edition/html/expressions.html#15.25
Comment 1 Kjetil Ødegaard CLA 2005-11-22 10:08:33 EST
The compiler also allows this code, which the Sun compiler rejects:

	System.out.println("n: "+(x > 1 ? "x" : 1));
Comment 2 Olivier Thomann CLA 2005-11-22 10:40:21 EST
Reproduced case from comment 0 with HEAD.
Case from comment 1 doesn't fail with java 1.5 (-source 1.5), but fails with javac 1.5 (-source 1.4).
With compliance 1.5 and source 1.4, we also report:
----------
1. ERROR in D:\tests_sources\X.java
 (at line 4)
	System.out.println("n: "+(x > 1 ? "x" : 1));
	                         ^^^^^^^^^^^^^^^^^
Incompatible conditional operand types String and int
----------
1 problem (1 error)

So only the first case is an issue.
Comment 3 Olivier Thomann CLA 2005-11-22 19:54:09 EST
Created attachment 30422 [details]
Proposed fix

The problem in the first case comes from the constant computation. We have the right type (double) for the constant of the conditional expression, but we simply get the stringValue() of the int constant (2).
Philippe, could you please review the proposed fix?
Comment 4 Olivier Thomann CLA 2005-11-22 20:34:14 EST
All existing tests passed with this patch.
Comment 5 Olivier Thomann CLA 2005-11-23 12:14:00 EST
Fixed and released in HEAD.
Added regression tests org.eclipse.jdt.core.tests.compiler.regression.ConstantTest.test017/018.
Comment 6 Philipe Mulet CLA 2005-11-24 03:24:13 EST
Pls backport to 3.1 stream.
Comment 7 Olivier Thomann CLA 2005-11-28 10:30:32 EST
Backporter to 3.1 maintenance.
Regression tests added to org.eclipse.jdt.core.tests.compiler.regression.ConstantTest.test014/015.
Comment 8 Olivier Thomann CLA 2005-11-28 10:41:51 EST
Backported to 3.0 maintenance.
Regression tests added to
org.eclipse.jdt.core.tests.compiler.regression.ConstantTest.test012/013.
Comment 9 Frederic Fusier CLA 2005-12-12 11:46:30 EST
Verified for 3.2 M4 using build I20051212-0010
Comment 10 Frederic Fusier CLA 2006-01-09 06:54:41 EST
Reopen for 3.1.2 verification
Comment 11 Frederic Fusier CLA 2006-01-09 06:55:33 EST
We also need to verify that this bug is fixed using R3_1_maintenance build as
well before 3.1.2 delivery...
Comment 12 Maxime Daniel CLA 2006-01-10 05:43:39 EST
Verified for 3.1.2 using build M20060109-1200.
Comment 13 Frederic Fusier CLA 2006-01-10 05:48:34 EST
Verified for 3.1.2 using build M20060109-1200 (maxime)