JDK-4154563 : javac accepts division by zero constant expressions in case expressions.
  • Type: Bug
  • Component: tools
  • Sub-Component: javac
  • Affected Version: 1.2.0,1.2.1
  • Priority: P4
  • Status: Closed
  • Resolution: Won't Fix
  • OS: solaris_2.5.1,windows_95
  • CPU: x86,sparc
  • Submitted: 1998-07-03
  • Updated: 2000-03-02
  • Resolved: 2000-03-02
Description

Name: ngC57085			Date: 07/03/98



Compiler version "1.2beta4" (build JDK-1.2beta4-K, green threads, sunwjit)

Java compiler crashes while trying to compile next test.
This test also crashes all 1.2beta4 compiler versions,
but bug is absent in 12.beta3.
An example and compiler diagnostics follow:
-------------------------------------------------------
public class B {

   public static void main(String argv[]) {
      switch( 0 ){
       case 0/0:
      }
   }
}

> uname -a
SunOS novo35 5.5.1 Generic_103640-12 sun4m sparc SUNW,SPARCstation-20
> javac B.java
java.lang.ClassCastException: sun.tools.tree.DivideExpression
        at sun.tools.tree.SwitchStatement.code(Compiled Code)
        at sun.tools.javac.SourceMember.code(Compiled Code)
        at sun.tools.javac.SourceMember.code(Compiled Code)
        at sun.tools.javac.SourceClass.compileClass(Compiled Code)
        at sun.tools.javac.SourceClass.compile(Compiled Code)
        at sun.tools.javac.Main.compile(Compiled Code)
        at sun.tools.javac.Main.main(Compiled Code)
error: An exception has occurred in the compiler; please file a bug report (http://java.sun.com/cgi-bin/bugreport.cgi).
1 error
> 
-------------------------------------------------------

======================================================================

Name: krT82822			Date: 08/04/99


8/4/99 kevin.ryan@eng -- bugs 4154563 and 4230296 apply.

//dummy.java
package droopy;
public class dummy {
        private class subdummy {
                void test()
                {
                        m_x = 3;
                }
        }
        final int y = 270;
        static final int doo = Dippy.dad;
        static final int dum = Dippy.did;
        int m_x;
        Dippy m_dippy;
        public void main(String argv[])
	{
                final int a = y;
                m_x = a;
		for (int i=0; i<10; i++)
		{
			switch(i)
			{
				case 2: 
				case 4: 
					m_x++; 
					break; 
				case 6: 
					m_x--; 
					break;
                                case dum:
                                        break;
				default:
					break;
			}
		}
	}
}
//Dippy.java
package droopy;
import droopy.dummy;
public class Dippy
{
        dummy m_dummy;
        public static final int did=dummy.doo;
        public static final int dad = 7;
}
//go.bat (make it)
\jdk1.2.1\bin\javac Dippy.java dummy.java
//result:
java.lang.ClassCastException
        at sun.tools.tree.SwitchStatement.code(Compiled Code)
        at sun.tools.tree.ForStatement.code(ForStatement.java:156)
        at sun.tools.tree.CompoundStatement.code(Compiled Code)
        at sun.tools.javac.SourceMember.code(Compiled Code)
        at sun.tools.javac.SourceMember.code(Compiled Code)
        at sun.tools.javac.SourceClass.compileClass(Compiled Code)
        at sun.tools.javac.SourceClass.compile(Compiled Code)
        at sun.tools.javac.Main.compile(Compiled Code)
        at sun.tools.javac.Main.main(Main.java:733)
error: An exception has occurred in the compiler; please file a bug report (http
://java.sun.com/cgi-bin/bugreport.cgi).
1 error
(Review ID: 93472)
======================================================================

Comments
EVALUATION The compiler used to report all attempts to divide by the constant zero as compile-time errors. This was fixed in beta3 so that code would be generated for division by constant zero. Unfortunately this bug was introduced. The compiler should handle a division by zero in a case expression gracefully. Note that there are other problems in the compiler related to case expressions: specifically, certain syntactically non-constant constructs are permitted when they should not be. todd.turnidge@Eng 1998-07-06 Kestrel-L reports the 0/0 as a non-constant expression. This is better than evaluating it at compile time and taking an exception, but does not conform to JLS 15.27. The expression should be considered constant, but code generated to throw the exception at runtime. william.maddox@Eng 1999-08-06 A close reading of JLS 15.27 indicates that an expression dividing by zero should not be be considered a constant expression: "A compile-time *constant expression* is an expression denoting a value of primitive type or a String that is composed using only the following ...". An expression dividing by a constant zero clearly denotes nothing at all. This is the interpretation currently taken by the new compiler, whether deliberate or not. It has been determined, however, that such an expression should be considered a compile-time constant, but cause the program to be rejected at compile-time. This constitutes a small specification change. See bug number 4178182. william.maddox@Eng 1999-10-26 Name: ab88733 Date: 03/02/2000 This bug is being closed because the new javac compiler does not exhibit this behavior. No work on the old javac compiler is planned. The new javac compiler is shipping with the 1.3 release of J2SE. The old compiler is provided in 1.3 for backwards compatibility; it will be withdrawn in a future release of J2SE. ======================================================================
11-06-2004