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

Shil Sinha commented on GROOVY-7639:
------------------------------------

I mean things like foo?.doBar() where doBar() returns a primitive type. Example:

{code}
class Foo {
    int bar

    int doBar() {
        bar
    }
}

@groovy.transform.CompileStatic
void test() {
    Foo foo = null
    assert !(foo?.doBar() == 7) // passes
    assert !(foo?.bar == 7) // NPE
}
test()
{code}

When storing the type for safe method call expressions (via 
StaticTypeCheckingVisitor.storeType), wrapper types are stored in place of 
primitives, resulting in ScriptBytecodeAdapter.compareEqual comparisons for 
things like foo?.doBar() == 7. There's no such guard for safe property 
expressions, so the inferred type of foo?.x is int and a primitive comparison 
is performed instead.

Though it would be possible to handle this during class generation, it seems 
logical to me that the inferred return type for a safe expression, method call 
or property, should not be a primitive type. However, the tests in GROOVY-5613 
explicitly test for the opposite in the case of property expressions. 


> NPE when using safe traversal operator with CompileStatic
> ---------------------------------------------------------
>
>                 Key: GROOVY-7639
>                 URL: https://issues.apache.org/jira/browse/GROOVY-7639
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.5
>            Reporter: Ben Podoll
>
> In the code below, the "if" condition throws a NPE when it reaches the "p3" 
> object. If I remove the @CompileStatic from the "SomeOther" class it works.
> {code:java}
> import groovy.transform.CompileStatic
> @CompileStatic
> class Person {
>   String name
>   OtherInfo otherInfo
> }
> @CompileStatic
> class OtherInfo {
>   int favoriteNumber
> }
> @CompileStatic
> class SomeOther {
>   SomeOther() {
>     Person p1 = new Person(name: 'Ben', otherInfo: new 
> OtherInfo(favoriteNumber: 1))
>     Person p2 = new Person(name: 'Aaron', otherInfo: new OtherInfo())
>     Person p3 = new Person(name: 'Fred')
>     [p1,p2,p3].each { Person p ->
>       println "checking: ${p.name}"
>       println p?.otherInfo?.favoriteNumber
>       if(p?.otherInfo?.favoriteNumber == 7){
>         println "luckiest number!"
>       }
>     }
>   }
> }
> new SomeOther()
> {code}



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

Reply via email to