Bug 422657 - [compiler][null][correlation] Null inference fails when intermediate variable between null check and object usage is used
Summary: [compiler][null][correlation] Null inference fails when intermediate variable...
Status: CLOSED DUPLICATE of bug 538421
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 4.3.1   Edit
Hardware: PC Windows 7
: P3 enhancement (vote)
Target Milestone: 4.7 M1   Edit
Assignee: JDT-Core-Inbox CLA
QA Contact:
URL:
Whiteboard:
Keywords: helpwanted
Depends on:
Blocks:
 
Reported: 2013-11-27 06:10 EST by Michael Becker CLA
Modified: 2018-08-30 10:32 EDT (History)
1 user (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description Michael Becker CLA 2013-11-27 06:10:22 EST
JDT's null inference fails in the simple case of an intermediate variable between the null check of the object and the actual object usage. This is a problem expecially when using Xtend together with @NonNull/@Nullable annotations and strict checks, as Xtend generally creates Java code like:

Object input = InputSource.getInput(); // return value annotated as @Nullable
boolean _tripleNotEquals = (input != null);
if (_tripleNotEquals) {
    Object convert = Converter.convert(input); // parameter annotated with @NonNull
}

In this example, it is clearly apparent that input can never be null in line 4. However, the JDT infers the input as potentially null / @Nullable and creates an error if the "Conflict between null annotations and null inference" preference is set to "Error".

Steps to Reproduce:

1) Preparation:
- go to Preferences | Java | Compiler | Errors/Warnings | Null Analysis
- check "Enable annotation-based null analysis"
- set "Conflict between null annotations and null inference" to "Error"

2) create Java class:

package test;

import java.util.Random;
import org.eclipse.jdt.annotation.NonNull;
import org.eclipse.jdt.annotation.Nullable;

public class Example
{
    @Nullable
    public Object getInput()
    {
        return new Random().nextBoolean() ? new Object() : null;
    }

    @NonNull
    public Object convert(@NonNull final Object input)
    {
        return input;
    }

    public void perform()
    {
        final Object input = getInput();
        final boolean notNull = (input != null);
        if (notNull)
        {
            // leads to error "Null type mismatch: required '@NonNull Object' but the provided value is inferred as @Nullable"
            final Object converted = convert(input);
        }
    }
}
Comment 1 Stephan Herrmann CLA 2013-11-28 07:58:24 EST
This RFE is a member of a group of existing requests marked as [correlation]: https://bugs.eclipse.org/bugs/buglist.cgi?short_desc=[correlation]&classification=Eclipse&query_format=advanced&short_desc_type=allwordssubstr&component=Core&product=JDT&list_id=7605356

It is a known limitation of the existing analysis that it cannot track the correlation between local variables, like: "I know that when (notNull==true) then also (input!=null)".

Adding correlation analysis would be a complex task, with significant impact on compiler performance. I don't have a strategy how to make both ends meet.

Maybe you should ask the Xtend guys to stop generating that intermediate boolean variable. Alternatively: could you configure the source folder holding the generated Java files to ignore optional errors/warnings, or would that break warnings in Xtend, too?
Comment 2 Stephan Herrmann CLA 2016-06-28 17:17:29 EDT
Bulk closing all compiler bugs tagged [null][correlation], because we have no plans to add such a feature: it would be a tremendous implementation effort, beyond our current man power, and may be impossible to achieve within the desired performance bounds.

If s.o. has a viable strategy or even implementation for such a feature, I'm all ears.
Comment 3 Jay Arthanareeswaran CLA 2016-08-03 07:58:12 EDT
Verified for 4.7 M1.
Comment 4 Stephan Herrmann CLA 2018-08-30 10:32:49 EDT
I created a new umbrella RFE outlining what would be needed to address this issue.

*** This bug has been marked as a duplicate of bug 538421 ***