Uploaded image for project: 'JDK'
  1. JDK
  2. JDK-4302616

use of boolean assignmt (vs. comparison) in if statement not caught

XMLWordPrintable

    • x86
    • windows_95



      Name: krT82822 Date: 01/04/2000


      orig synopsos: "Incorrect if statment causes internal compiler error."

      java version "1.1.8" (kestrel 1.3.0 build "R" compiler does not catch this, either)

      1/2/2000 eval1127@eng -- this may be more of an RFE than a bug. A compiler warning would be useful to catch use of this idiom, in case it's unintentional.

      public class Blah {

              public static void main(String[] args) {
                      boolean flag;

                      if( flag = true){ // comparison was intended; omission of second "=" leads to valid assignment, and results in "true" value...
                      }
                      else{
                              System.out.println("Hello?");
                      }
              }
      }


      user's summary comments (from follow-up email). See full, original test case below.

      It's a bug. Let's refer back to the original test case:
      ------
      public class PushButton extends JButton {
          private boolean flag = false;
          public PushButton() {
          } // end constructor

          private class ButtonActionListener implements ActionListener{
              public void actionPerformed(ActionEvent e){
                  if( flag = true){
                  }
                  else{
                      System.out.println("Hello?");
                  }
              } // end actionPerformed
          } // end addActionListener

      } // end class
      ------
      I called the original test case the 'minimum' needed, because when I was
      trimming down my original code to make the original test case, the error
      would not show up unless I left certain parts of code in the test case.
      Hence, the original test case is what I believe is the 'minimum' set of code
      parts needed to recreate the error. For example: The 'else' part must be
      doing something (i.e., print 'Hello?') for the error to show up, there must
      also be a 'else' part and a constructor as well.
      I also consider it a bug because I really don't want assignments in Boolean
      conditionals.


      ----------- original test case: --------------------------

      Example code:
      -----
      import javax.swing.border.*;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.event.*;
      import java.util.*;

      public class PushButton extends JButton {
          private boolean flag = false;
          public PushButton() {
          } // end constructor

          private class ButtonActionListener implements ActionListener{
              public void actionPerformed(ActionEvent e){
                  if( flag = true){
                  }
                  else{
                      System.out.println("Hello?");
                  }
              } // end actionPerformed
          } // end addActionListener

      } // end class
      -----
      Notice that there is only one equal sign, this is an assignment, not an
      equality test. The compiler should complain about this, instead it does this:
      -----
      cd f:/src/
      javac -g PushButton.java
      sun.tools.java.CompilerError: codeBranch method
      at sun.tools.tree.Expression.codeBranch(Expression.java:310)
      at sun.tools.tree.NotExpression.codeBranch(NotExpression.java:117)
      at sun.tools.tree.IfStatement.code(IfStatement.java:124)
      at sun.tools.javac.SourceField.code(Compiled Code)
      at sun.tools.javac.SourceField.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:465)
      PushButton.java:0: inner class PushButton. ButtonActionListener:void
      actionPerformed(java.awt.event.ActionEvent)@sun.tools.java.CompilerError:
      codeBranch method
      import javax.swing.*;
      ^
      1 error

      Compilation exited abnormally with code 1 at Fri Dec 31 13:22:06
      ----
      This bug was submitted before, this current submission is a response to the
      reply I received (Review ID: 99456) in which the reviewer said that they could
      not reproduce it.
      The example code above is a copy of my orginal code trimmed down to the minimum
      needed to cause the error. The error messages is what appears when I try to
      compile the example code.

      --------------------

      1/1/2000 eval1127@eng -- under 1.2.2, it produces:

      java.lang.NullPointerException
              at sun.tools.tree.AssignExpression.inlineValue(AssignExpression.java, Compiled Code)
              at sun.tools.tree.UnaryExpression.inlineValue(UnaryExpression.java, Compiled Code)
              at sun.tools.tree.IfStatement.inline(IfStatement.java, Compiled Code)
              at sun.tools.tree.CompoundStatement.inline(CompoundStatement.java, Compiled Code)
              at sun.tools.javac.SourceMember.inline(SourceMember.java, Compiled Code)
              at sun.tools.javac.SourceMember.code(SourceMember.java, Compiled Code)
              at sun.tools.javac.SourceClass.compileClass(SourceClass.java, Compiled Code)
              at sun.tools.javac.SourceClass.compile(SourceClass.java, Compiled Code)
              at sun.tools.javac.Main.compile(Main.java, Compiled Code)
              at sun.tools.javac.Main.main(Main.java, 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

      -----------------

      under kestrel (1.3.0 build "R"), it compiles "OK". Will now check what results/interpretation it produced...

      It does the assignment, and since the LHS evaluates to "true", executes the block right after it, as in:

      ( 35 )% java ButtonFrame
      value of flag before incorrect if: false
      I'm right after the incorrect assignment
      value of flag after incorrect if sequence: true

      -----------------

      revised test case:

      import javax.swing.*;
      import java.awt.*;
      import java.awt.event.*;
      import javax.swing.event.*;
      import java.util.*;

      class PushButton extends JButton {
          private boolean flag = false;

          public PushButton() {
                      super("pressing matter");
                      addActionListener((ActionListener) new ButtonActionListener());
          } // end constructor

          private class ButtonActionListener implements ActionListener{
              public void actionPerformed(ActionEvent e){
                              System.out.println("value of flag before incorrect if: " + flag);
                  if( flag = true){
                                      System.out.println("I'm right after the incorrect assignment");
                  }
                              else if( flag == true ) {
                                      System.out.println("I'm right after the test for equality");
                              }
                  else{
                      System.out.println("Hello?");
                  }
                              System.out.println("value of flag after incorrect if sequence: " + flag);
              } // end actionPerformed
          } // end addActionListener

      } // end class

      public class ButtonFrame extends JFrame {
              public ButtonFrame() {
                      super("frame title");
                      PushButton b = new PushButton();
                      b.setBackground(Color.yellow);
                      JPanel p = new JPanel();
                      p.setLayout(new BorderLayout());
                      p.add(b, BorderLayout.CENTER);
                      setContentPane(p);
                      pack();
              }

              public static void main(String[] args) {
                      ButtonFrame f = new ButtonFrame();
                      f.setLocation(300,300);
                      f.setVisible(true);
              }
      }
      (Review ID: 99476)
      ======================================================================

            wmaddoxsunw William Maddox (Inactive)
            kryansunw Kevin Ryan (Inactive)
            Votes:
            0 Vote for this issue
            Watchers:
            0 Start watching this issue

              Created:
              Updated:
              Resolved:
              Imported:
              Indexed: