Bug 236019 - [compiler] Compiler doesn't complain for a function which returns an int, returns indeed null
Summary: [compiler] Compiler doesn't complain for a function which returns an int, ret...
Status: RESOLVED INVALID
Alias: None
Product: JDT
Classification: Eclipse Project
Component: Core (show other bugs)
Version: 3.3.2   Edit
Hardware: PC Windows XP
: P3 normal (vote)
Target Milestone: 3.4   Edit
Assignee: Philipe Mulet CLA
QA Contact:
URL:
Whiteboard:
Keywords:
Depends on:
Blocks:
 
Reported: 2008-06-06 07:32 EDT by David Pérez CLA
Modified: 2008-06-11 04:36 EDT (History)
2 users (show)

See Also:


Attachments

Note You need to log in before you can comment on or make changes to this bug.
Description David Pérez CLA 2008-06-06 07:32:15 EDT
Build ID:  M20080221-1800

Steps To Reproduce:
1.Create a Java project
2.Create a .java source file
3.Enter a function like this:

class Test {
    ArrayList params;

    public int getSqlParamCount() {
        return params == null ? null:params.size();
    }
}


More information:
If we remove the conditional, then the compiler complains:

class Test {
    ArrayList params;

    public int getSqlParamCount() {
        return null;
    }
}
Comment 1 Olivier Thomann CLA 2008-06-06 09:35:44 EDT
Are you compiling in 1.5 mode ?
What are your compiler settings?
In 1.5, I think autoboxing is used in this case.
It leads to a NPE at runtime.
Comment 2 Olivier Thomann CLA 2008-06-06 09:36:15 EDT
javac 1.6 and 1.5.0 also compile this code.
Comment 3 David Pérez CLA 2008-06-06 10:28:34 EDT
That's true, I'm using 1.6 target.

Even though, javac behaves so, I would consider this behavior as a bug.
Comment 4 Philipe Mulet CLA 2008-06-10 18:34:13 EDT
The conditional operator offers special type checking rules, which allow for a boxing conversion to occur for free. The plain 'return null' is not as sophisticated.

This is a situation where we stick to the language spec, and you should blame the spec (and then we would happily tune our semantics accordingly if you win this fight).

Comment 5 Philipe Mulet CLA 2008-06-10 18:35:14 EDT
Added AutoboxingTest#test159
Comment 6 David Pérez CLA 2008-06-11 04:36:34 EDT
Thanks for the explanation.
Luckily this behaviour isn't so important, so I'm not going to fight against anyone for this.