-
Bug
-
Resolution: Won't Fix
-
P4
-
None
-
1.2.0
-
sparc
-
solaris_2.5.1
Name: laC46010 Date: 05/19/98
JLS 14.9 (p. 275) requires:
Every case constant expression associated with a switch
statement must be assignable (5.2) to the type of the
Expression.
However javac permits the following illegal combinations:
-------------------------
switch | case
expression | expression
type | type
===========+=============
byte | char
byte | short
char | short
-------------------------
and doesn't detect a compile-time error in the JCK test case:
lang/STMT/stmt035/stmt03502/stmt03502.java
It should report about illegal types of expressions
in the following lines:
case b2:
case b4:
because char type of b2 and short type of b4 are not assignable to byte type of b.
b2 and b4 should be explicitely converted to int or byte in these cases.
It looks like javac treats all integral constant expressions after "case"
to be of type int (in this cases JLS 5.2 (p.61) allows special case
of narrowing primitive conversion to type of switch expressions).
But JLS 15.13.1 (p.344) explicitely says that "If the Identifier occurs
within the scope of a parameter or local variable named by that same
Identifier, then the type of the ExpressionName is the declared type of the
parameter or local variable"
---------------------------------------------------------------
// Ident: @(#)stmt03502.java 1.6 96/11/18
// Copyright 11/18/96 Sun Microsystems, Inc. All Rights Reserved
package javasoft.sqe.tests.lang.stmt035.stmt03502;
import java.io.PrintStream;
public class stmt03502 {
final static int b1 = 1;
final static short b2 = 2;
final static byte b3 = 3;
final static char b4 = 4;
public static void main(String argv[]) {
System.exit(run(argv, System.out) + 95/*STATUS_TEMP*/);
}
public static int run(String argv[],PrintStream out) {
int a1 = 0;
int a2 = 0;
int a3 = 0;
int a4 = 0;
int a5 = 0;
int a6 = 0;
int a7 = 0;
int a8 = 0;
byte b = 2;
for ( b = 1; b < 6; b++ ) {
a1 += 1;
switch ( b ) {
case b1:
a2 += 1;
break;
case b2:
a3 += 1;
break;
case b3:
a4 += 1;
break;
case b4:
a5 += 1;
break;
default:
a6 += 1;
}
a7 += 1;
}
a8 += 1;
if ( a1 == 5 && a2 == 1 && a3 == 1 && a4 == 1 &&
a5 == 1 && a6 == 1 && a7 == 5 && a8 == 1 )
return 0/*STATUS_PASSED*/;
out.println ("failed");
out.println( a1+" "+a2+" "+a3+" "+a4+" "+a5+" "+a6+" "+a7+" "+a8);
return 2/*STATUS_FAILED*/;
}
}
---------------------------------------------------------------
Hook 5(hook5): test
======================================================================