This is the mail archive of the gcc-patches@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Patch: Java compound assignment -vs- non-String LHS


This is invalid Java code:

    public class fp
    {
	void foo() {
	  Object o = null;
	  o += "";
	}
    }

However, it is currently accepted by gcj because String is assignable
to Object.  (A similar example using Comparable instead of Object can
also be constructed.)

This patch fixes the problem.  It changes gcj to reject any compound
assignment whose LHS is a non-String reference type.  This is correct
because the only valid compound assignment with a reference type is a
String `+='.

I've rebuilt libgcj with this patch with no problems.  This fixes two
Jacks failures.

Ok to commit?

(FWIW I'm not certain that this is the best possible error message to
give.  I'm happy to change it if you have a different preference.)

2000-11-20  Tom Tromey  <tromey@cygnus.com>

	* parse.y (java_complete_lhs): Only allow compound assignment of
	reference type if type is String.

Tom

Index: parse.y
===================================================================
RCS file: /cvs/gcc/egcs/gcc/java/parse.y,v
retrieving revision 1.224
diff -u -r1.224 parse.y
--- parse.y	2000/11/13 13:23:37	1.224
+++ parse.y	2000/11/20 21:37:17
@@ -11482,6 +11503,12 @@
 	     E1 = (T)(E1 op E2), with T being the type of E1. */
 	  nn = java_complete_tree (build_cast (EXPR_WFL_LINECOL (wfl_op2), 
 					       TREE_TYPE (lvalue), nn));
+
+	  /* If the assignment is compound and has reference type,
+	     then ensure the LHS has type String and nothing else.  */
+	  if (JREFERENCE_TYPE_P (TREE_TYPE (lvalue))
+	      && ! JSTRING_TYPE_P (TREE_TYPE (lvalue)))
+	    parse_error_context (wfl_op2, "Incompatible type for assignment");
 
 	  /* 15.25.2.b: Left hand is an array access. FIXME */
 	}

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]