JDK-6809101 : Compiler error for conditional operator used with bad types is misleading.
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 6u12
  • Priority: P4
  • Status: Resolved
  • Resolution: Cannot Reproduce
  • OS: windows_xp
  • CPU: x86
  • Submitted: 2009-02-24
  • Updated: 2013-05-30
  • Resolved: 2013-05-30
Related Reports
Relates :  
Description
FULL PRODUCT VERSION :
java version "1.6.0_12"
Java(TM) SE Runtime Environment (build 1.6.0_12-b04)
Java HotSpot(TM) Client VM (build 11.2-b01, mixed mode, sharing)

ADDITIONAL OS VERSION INFORMATION :
Microsoft Windows XP [Version 5.1.2600]

A DESCRIPTION OF THE PROBLEM :
When the types of a conditional operator are incompatible, the error message reads "neither is a subtype of the other". While that is true, the message seems to indicate that it is required that one be a subtype of the other. This is probably left over from the time when that was the case. My reading of the spec is that a conditional operator will always have a valid result type unless one or both of the operand types is void. Perhaps the error message should indicate "void operand not allowed", or something like that.

STEPS TO FOLLOW TO REPRODUCE THE PROBLEM :
Compile program.

EXPECTED VERSUS ACTUAL BEHAVIOR :
EXPECTED -
Error message indicating that void types are not allowed as operands to the conditional operator.
ACTUAL -
Error message that seems to imply that one operand to the conditional operator must be a subtype of the other.

ERROR MESSAGES/STACK TRACES THAT OCCUR :
CondTest.java:3: incompatible types for ?: neither is a subtype of the other

REPRODUCIBILITY :
This bug can be reproduced always.

---------- BEGIN SOURCE ----------
public class CondTest {
   public static void main(String[] args) {
      Object o = ((3 > 2)? "x" : "x".wait());
   }
}
---------- END SOURCE ----------

Comments
Message has changed in JDK 8: error: incompatible types: bad type in conditional expression Object o = ((3 > 2)? "x" : "x".wait()); ^ void cannot be converted to Object 1 error
30-05-2013

EVALUATION The error message should be slightly adjusted in order to reflect the JSL 3 contents. Something like: unapplicable type for ? operator: type #1: Integer type #2: void could be less misleading - I agree that the message should not talk about subtyping, since subtyping is not the only rule considered when applying the ? operator.
20-07-2009