Bug 31467

Summary: spurious "Incompatible conditional operand types" on ?: when assigning to abstract
Product: [Eclipse Project] JDT Reporter: npitman <npitman>
Component: CoreAssignee: JDT-Core-Inbox <jdt-core-inbox>
Status: RESOLVED WONTFIX QA Contact:
Severity: normal    
Priority: P3    
Version: 2.1   
Target Milestone: 2.1 RC1   
Hardware: PC   
OS: Windows XP   
Whiteboard:

Description npitman CLA 2003-02-10 11:32:53 EST
When using a trinary ?: expression, the compiler gives a spurious error that 
the types are incompatible if they are different subtypes.  Workaround - cast 
either the true or false result to abstract class.

Version: 2.1
Build id: 200212181304
WinXP home
JDK 1.4.1_01

----test case---
note the last line generates an error as marked.

void test(int a){
	
	OutputStream os;
	FileOutputStream fs =  new FileOutputStream("a");
	ByteArrayOutputStream bs =new ByteArrayOutputStream();
	
	os = new FileOutputStream("a");
	os = new ByteArrayOutputStream();
	os = fs;
	os = bs;
	os = (a > 5) ? (OutputStream)fs: bs;  // OK
	os = (a > 5) ? fs: (OutputStream)bs;  // OK
	os = (a > 5) ? fs: bs;  //Incompatible conditional operand types 
FileOutputStream and ByteArrayOutputStream
	
}
Comment 1 Adam Kiezun CLA 2003-02-10 12:00:27 EST
compiler lives in jdt core
Comment 2 Olivier Thomann CLA 2003-02-10 12:18:19 EST
Maybe the message is not very clear, but this code is incorrect.
Look at the JLS
http://java.sun.com/docs/books/jls/second_edition/html/expressions.doc.html#290293
* If the second and third operands are of different reference types, then it
must be possible to convert one of the types to the other type (call this latter
type T) by assignment conversion (ยง5.2); the type of the conditional expression
is T. It is a compile-time error if neither type is assignment compatible with
the other type.

So like ByteArrayOutputStream cannot be assignment compatible with
FileOutputStream, the compile reports a compile-error. This is clearly a valid
error.
Javac 1.4.1 and jikes 1.18 report the same problem.
Comment 3 Philipe Mulet CLA 2003-02-10 12:26:16 EST
Closing, we implement the JLS spec strictly.