Bug 39248 - object += "string"; okay with javac but not JDT--JLS ambiguity?
Summary: object += "string"; okay with javac but not JDT--JLS ambiguity?
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 2.1   Edit
Hardware: PC Windows XP
: P3 minor (vote)
Target Milestone: 3.0 M2   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2003-06-23 21:32 EDT by derc CLA
Modified: 2003-07-01 05:47 EDT (History)
0 users

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description derc CLA 2003-06-23 21:32:11 EDT
javac and JBuilder's compiler allow the following statement to compile, but JDT
does not:

Object o = null;
o += "String";   // -> The operator += is undefined for the argument type(s)
Object, String
                 // Note that 'o = o + "String"' does not result in a compiler
error.

It's not clear (to me, at least) from the JLS whether this should be legal or not:

JLS 15.25.2 Compound Assignment Operators
All compound assignment operators require both operands to be of primitive type,
except for +=, which allows the right-hand operand to be of any type if the
left- hand operand is of type String.

A compound assignment expression of the form E1 op= E2 is equivalent to E1 =
(T)((E1) op (E2)), where T is the type of E1, except that E1 is evaluated only once.
------------JLS 15.17.1 String Concatenation Operator +
If only one operand expression is of type String, then string conversion is
performed on the other operand to produce a string at run time. The result is a
reference to a newly created String object that is the concatenation of the two
operand strings.
Comment 1 Philipe Mulet CLA 2003-06-24 04:53:09 EDT
On the surface, both behaviors should be consistent (o+=s and o=o+s), but 
interestingly enough Jikes does agree with us.

Need to investigate some more.
Comment 2 Philipe Mulet CLA 2003-06-24 04:58:07 EDT
I suspect javac got tweaked to accept this code, against the spec.
Comment 3 Philipe Mulet CLA 2003-06-24 05:20:14 EDT
Changing our behavior to accept object += string, on the basis that 
object=object+string is fine.

Fixed
Comment 4 derc CLA 2003-06-30 15:16:15 EDT
Sincere apologies, but I just noticed in the JDK 1.4.2 release notes 
(http://java.sun.com/j2se/1.4.2/changes.html#javac) that Sun has changed javac 
to no longer allow this behavior:

Bug 4642850 fixed: javac now correctly no longer allows "+=" string 
concatenation with an Object variable on the left-hand side. 

Please revert back to the original behavior.  Thanks!
Comment 5 Philipe Mulet CLA 2003-07-01 05:47:53 EDT
Good find. I had only commented the Object check, will resurrect it.
Thanks for reopening.

Closing