Bug 516084 - Need implicit narrowing of type warning on plus-equals and minus-equals
Summary: Need implicit narrowing of type warning on plus-equals and minus-equals
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.8   Edit
Hardware: PC Linux
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords:
Depends on:
Blocks:
 
Reported: 2017-05-03 03:06 EDT by Luke Hutchison CLA
Modified: 2023-02-22 17:05 EST (History)
4 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Luke Hutchison CLA 2017-05-03 03:06:43 EDT
The following code compiles without warning on Eclipse:

        int x = 5;
        x -= 3.1f;
        System.out.println(x); // outputs 1

There should be an implicit narrowing of type warning on the second line.

(Yes, it is entirely valid in Java not to give a warning here, and I don't think that javac reports this warning either, but not showing a warning in this case is a major source of obscure bugs.)

Obviously other type combinations apply too, e.g. incrementing a char/short/int/float using a double value, etc.

Note that the following does report an error on the second line ("The operator -= is undefined for the argument type(s) Integer, float"):

        Integer x = 5;
        x -= 3.1f;
        System.out.println(x);
Comment 1 Jay Arthanareeswaran CLA 2017-05-03 04:41:25 EDT
This code is reported, though:

      x = x - 3.1f;
Comment 2 Luke Hutchison CLA 2017-05-03 06:36:14 EDT
Jay: yes, exactly, that is the point :-)  The += and -= operators should behave the same way. It is dangerous for them not to.
Comment 3 Jay Arthanareeswaran CLA 2017-05-03 07:18:24 EDT
At this point I am not sure if this is a bug or not. I very vaguely remember there was a similar discussion/bug long time back.

Sasi, do you remember?
Comment 4 Sasikanth Bharadwaj CLA 2017-05-04 04:56:05 EDT
(In reply to comment #3)
> At this point I am not sure if this is a bug or not. I very vaguely remember
> there was a similar discussion/bug long time back.
> 
> Sasi, do you remember?
I don't see a bug here

x = x - 3.1f is a type mismatch error and adding the required cast to int makes the error disappear and there is no warning either.
x -= 3.1f is equivalent to x = (int)(x - 3.1f) which is accepted without any warning

so indeed the behavior is consistent

As for warnings for narrowing of types, I can't recollect any related bugs in the recent past. That may be before I started
Comment 5 Luke Hutchison CLA 2017-05-04 05:17:00 EDT
The current behavior is indeed consistent with the spec for compound assignment operators, describing an automatic cast:

https://docs.oracle.com/javase/specs/jls/se7/html/jls-15.html#jls-15.26.2

*However*, these automatic casts are implicit, and they can be narrowing casts, which is a recipe for bugs.

In C, an implicit narrowing cast is a warning, whereas in Java, it is usually an error, precisely because if the programmer is not required to indicate that loss of precision is the expected and desired behavior of a piece of code, bugs frequently result.

Therefore, in spite of the fact that the Java spec says that compound assignment operators should automatically cast to the LHS type of the assignment, I strongly recommend that Eclipse support a warning here.

I have had to track bugs down arising from this behavior several times in the past, and it's almost always maddeningly hard to determine what went wrong and where, because the symptoms of silent loss of precision often manifest far downstream of the cause.

Adding a warning to these compound assignment operators will not break old code, but will save programmers (particularly those who do a lot of numerical work) a lot of time trying to figure out strange behavior downstream of accidental narrowing of precision.
Comment 6 Luke Hutchison CLA 2018-09-14 05:01:34 EDT
Pinging on this again. There really needs to be an implicit narrowing cast warning here. This issue has stung me a few times over the years.
Comment 7 Jay Arthanareeswaran CLA 2018-09-14 05:28:29 EDT
If you are asking us to deviate from the spec, that might not be possible. Having an option to enable/disable this might be an overkill too.
Comment 8 Luke Hutchison CLA 2018-09-14 05:51:45 EDT
I don't see how reporting a warning that OpenJDK may not report is a deviation from the spec? My understanding is that to conform to the spec, there must be a 1:1 match between error (non-compilable) and non-error (compilable) code, and anything that javac reports a warning for, other Java compiler implementations should report a warning for. But I think that gives the freedom for other compilers such as ecj to report additional warnings if they see fit, in order to be more safe or more conservative? That's what I'm asking for.
Comment 9 Eclipse Genie CLA 2020-10-09 07:29:06 EDT
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.
Comment 10 Luke Hutchison CLA 2020-10-09 12:21:12 EDT
This is still important (pinging so that the bug doesn't get closed)
Comment 11 Luke Hutchison CLA 2020-11-10 04:56:13 EST
Just today I spent a couple of hours debugging an issue that yet again turned out to be caused by exactly this problem. I don't know how many times I have run into this problem over the years. Please add a compiler warning option for ecj, whether or not javac will do the same.
Comment 12 Jay Arthanareeswaran CLA 2020-11-10 05:46:50 EST
Luke, I understand. But right now the JDT team is terribly under-staffed. Couple of our experts in this area are not active these days. So, unless someone from the community takes this up, I can't really help.
Comment 13 Eclipse Genie CLA 2023-02-22 17:05:44 EST
This bug hasn't had any activity in quite some time. Maybe the problem got resolved, was a duplicate of something else, or became less pressing for some reason - or maybe it's still relevant but just hasn't been looked at yet.

If you have further information on the current state of the bug, please add it. The information can be, for example, that the problem still occurs, that you still want the feature, that more information is needed, or that the bug is (for whatever reason) no longer relevant.

--
The automated Eclipse Genie.