[ 
https://issues.apache.org/jira/browse/GROOVY-7631?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=14962602#comment-14962602
 ] 

Shil Sinha edited comment on GROOVY-7631 at 10/18/15 9:53 PM:
--------------------------------------------------------------

I tested this on 2.0.0, 2.0.8, 2.1.9, 2.2.2 and 2.3.11; the behavior is the 
same across those versions. 

Edit:
This seems to be an issue for all primitive values, not just 0. Example:

{code}
@groovy.transform.CompileStatic
void test() {
    // these all fail
    assert true != null
    assert (byte) 1 != null
    assert (short) 1 != null
    assert 1 != null
    assert 1L != null
    assert 1f != null
    assert 1d != null
    assert (char) 1 != null    
}
test()
{code}

If the types above are replaced with their wrapper types, the test passes. 
The relevant code appears to be in CompareToNullExpression.visit method, where 
the primitive operand (which is being compared to null) is popped from the 
stack and a 0 is pushed, regardless of whether the comparison is an equals (==) 
or not equals (!=). 

This could be a breaking change if people were relying on {{p != null}} to 
return false for falsy primitives, but it's probably a change that should be 
made given the behavior in the dynamic case.


was (Author: shils):
I tested this on 2.0.0, 2.0.8, 2.1.9, 2.2.2 and 2.3.11; the behavior is the 
same across those versions. 

Edit:
This seems to be an issue for all primitive values, not just 0. Example:

{code}
@groovy.transform.CompileStatic
void test() {
    // these all fail
    assert true != null
    assert (byte) 1 != null
    assert (short) 1 != null
    assert 1 != null
    assert 1L != null
    assert 1f != null
    assert 1d != null
    assert (char) 1 != null    
}
test()
{code}

If the types above are replaced with their wrapper types, the test passes. The 
relevant code appears to be in CompareToNullExpression.visit method, where the 
primitive operand is popped from the stack and a 0 is pushed.


 

> @CompileStatic compiles 0 != null to false
> ------------------------------------------
>
>                 Key: GROOVY-7631
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7631
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static compilation
>    Affects Versions: 2.4.5
>            Reporter: Kevin Gu
>            Assignee: Cédric Champeau
>
> import groovy.transform.*;
> // @CompileStatic
> class Bug {
>     void exec() {
>         println 0 != null
>     }
> }
> new Bug().exec()
> ​The above code prints "true". But when @CompileStatic is umcommented, it 
> prints "false".



--
This message was sent by Atlassian JIRA
(v6.3.4#6332)

Reply via email to