Bug 31467 - spurious "Incompatible conditional operand types" on ?: when assigning to abstract
Summary: spurious "Incompatible conditional operand types" on ?: when assigning to abs...
Status: RESOLVED WONTFIX
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 2.1 RC1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-02-10 11:32 EST by npitman CLA
Modified: 2003-02-10 12:26 EST (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
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.