Bug 366277 - [1.7] Incorrect dead code/null value analysis in try-with-resources
Summary: [1.7] Incorrect dead code/null value analysis in try-with-resources
Status: NEW
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.8   Edit
Hardware: PC Windows 7
: P3 normal (vote)
Target Milestone: ---   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard: stalebug
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2011-12-09 18:51 EST by Trevor Robinson CLA
Modified: 2023-01-16 15:46 EST (History)
5 users (show)

See Also:


Attachments
Test case to demonstrate issue (728 bytes, text/plain)
2011-12-09 18:52 EST, Trevor Robinson CLA
no flags Details
Another complete test case, showing difference between try-with-resources and expanded try form (1.97 KB, text/plain)
2016-09-20 12:18 EDT, Joshua Taylor CLA
no flags Details

Note You need to log in before you can comment on or make changes to this bug.
Description Trevor Robinson CLA 2011-12-09 18:51:06 EST
Build Identifier: I20111027-1800

The println statement below is incorrectly marked as dead code, presumably because the static analysis of whether 'data' is null does not consider that the IOException could come from stream.close() after stream.read() completes successfully.

    public static void main(String[] args)
    {
        String data = null;
        try (FakeStream stream = new FakeStream())
        {
            data = stream.read();
        }
        catch (IOException e)
        {
            if (data != null)
                System.out.println("got " + data); // marked incorrectly as dead code
        }
    }


Reproducible: Always

Steps to Reproduce:
Compile and run the attached test case. The println statement will be marked with a dead code warning, but running the program shows that the println is executed.
Comment 1 Trevor Robinson CLA 2011-12-09 18:52:23 EST
Created attachment 208211 [details]
Test case to demonstrate issue
Comment 2 Srikanth Sankaran CLA 2011-12-09 19:34:44 EST
Ayush, please take a look, if fix is clean & safe, we will consider
backport to 3.7.2.
Comment 3 Stephan Herrmann CLA 2011-12-09 20:01:34 EST
(In reply to comment #0)
> Compile and run the attached test case. The println statement will be marked
> with a dead code warning, but running the program shows that the println is
> executed.

At least the println is not "optimized" out, which I believe is a merit of
bug 338234.
Comment 4 Joshua Taylor CLA 2016-09-20 12:18:37 EDT
Created attachment 264292 [details]
Another complete test case, showing difference between try-with-resources and expanded try form

This code demonstrates the issue as of Neon, and shows the difference between the try-with-resources form and the expanded form.

Eclipse Java EE IDE for Web Developers.
Version: Neon Release (4.6.0)
Build id: 20160613-1800
Comment 5 Joshua Taylor CLA 2016-09-20 12:21:17 EDT
I'm able to reproduce this as of 2016-09-20 in Eclipse Neon, 4.6.0.  I've added another attachment that demonstrates the issue more fully, and contrasts the try-with-resources form with the expanded try form.  I ran into this and also asked a Stack Overflow question about it (http://stackoverflow.com/q/39598715/1281433).

Eclipse Java EE IDE for Web Developers.
Version: Neon Release (4.6.0)
Build id: 20160613-1800
Comment 6 Holger Voormann CLA 2018-05-15 10:55:57 EDT
See also https://stackoverflow.com/q/50351907/6505250
Comment 7 Manoj N Palat CLA 2018-05-17 03:22:50 EDT
bulk move out of 4.8
Comment 8 Manoj N Palat CLA 2018-08-16 00:08:20 EDT
Bulk move out of 4.9
Comment 9 Eclipse Genie CLA 2023-01-16 15:46:42 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.