groovy-notifications mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul King (JIRA)" <j...@apache.org>
Subject [jira] [Updated] (GROOVY-8336) Static compilation requires casting inside instanceof check (additional cases)
Date Sun, 01 Oct 2017 05:57:02 GMT

     [ https://issues.apache.org/jira/browse/GROOVY-8336?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
]

Paul King updated GROOVY-8336:
------------------------------
    Description: 
Cloned from GROOVY-8260 to handle these additional cases:
{code}
import groovy.transform.CompileStatic

@CompileStatic
Date ternaryParam(Object input) {
    input instanceof Date ? input : null
}

@CompileStatic
Date ternaryLocalVar(Object input) {
    Object copy = input
    copy instanceof Date ? copy : null
}

@CompileStatic
Date ifThenElseParam(Object ifThenElseInput) {
    if (ifThenElseInput instanceof Date) {
        ifThenElseInput
    } else {
        null
    }
}

@CompileStatic
Date ifThenElseLocalVar(Object input) {
    Date result
    if (input instanceof Date) {
        result = input
    } else {
        result = null
    }
    result
}

class FooBase {}
class FooChild extends FooBase{}
@CompileStatic
FooChild ifThenElseLocalVar2(FooBase input) {
    FooChild result
    if (input instanceof FooChild) {
        result = input
    } else {
        result = null
    }
    result
}

def d = new Date()
assert ternaryParam(42) == null
assert ternaryParam('foo') == null
assert ternaryParam(d) == d
assert ternaryLocalVar(42) == null
assert ternaryLocalVar('foo') == null
assert ternaryLocalVar(d) == d
assert ifThenElseParam(42) == null
assert ifThenElseParam('foo') == null
assert ifThenElseParam(d) == d
assert ifThenElseLocalVar(42) == null
assert ifThenElseLocalVar('foo') == null
assert ifThenElseLocalVar(d) == d
def fc = new FooChild()
assert ifThenElseLocalVar2(fc) == fc
assert ifThenElseLocalVar2(new FooBase()) == null
{code}

  was:
There are many times I find myself having to cast variables directly inside an {{instanceof
Class}} check.

In addition, I have found a specific problem with a generic {{<T extends SomeClass>}}
where a method in the class that returns {{T}}, Groovy believes a collection is being returned.
This is the line in question: https://github.com/grails/gorm-graphql/blob/ea6887bef0ced75d9cc0d01dc7b94d4fc0520266/core/src/main/groovy/org/grails/gorm/graphql/fetcher/impl/EntityDataFetcher.groovy#L45

The goal of this issue is to have the following codebase compile without changes.

{{git clone -b broken_compilation https://github.com/grails/gorm-graphql}}

Attempt to compile with {{./gradlew clean classes}}


> Static compilation requires casting inside instanceof check (additional cases)
> ------------------------------------------------------------------------------
>
>                 Key: GROOVY-8336
>                 URL: https://issues.apache.org/jira/browse/GROOVY-8336
>             Project: Groovy
>          Issue Type: Bug
>          Components: Compiler
>    Affects Versions: 2.4.12
>         Environment: macOS JDK 1.8
>            Reporter: James Kleeh
>            Assignee: Paul King
>             Fix For: 2.4.13
>
>
> Cloned from GROOVY-8260 to handle these additional cases:
> {code}
> import groovy.transform.CompileStatic
> @CompileStatic
> Date ternaryParam(Object input) {
>     input instanceof Date ? input : null
> }
> @CompileStatic
> Date ternaryLocalVar(Object input) {
>     Object copy = input
>     copy instanceof Date ? copy : null
> }
> @CompileStatic
> Date ifThenElseParam(Object ifThenElseInput) {
>     if (ifThenElseInput instanceof Date) {
>         ifThenElseInput
>     } else {
>         null
>     }
> }
> @CompileStatic
> Date ifThenElseLocalVar(Object input) {
>     Date result
>     if (input instanceof Date) {
>         result = input
>     } else {
>         result = null
>     }
>     result
> }
> class FooBase {}
> class FooChild extends FooBase{}
> @CompileStatic
> FooChild ifThenElseLocalVar2(FooBase input) {
>     FooChild result
>     if (input instanceof FooChild) {
>         result = input
>     } else {
>         result = null
>     }
>     result
> }
> def d = new Date()
> assert ternaryParam(42) == null
> assert ternaryParam('foo') == null
> assert ternaryParam(d) == d
> assert ternaryLocalVar(42) == null
> assert ternaryLocalVar('foo') == null
> assert ternaryLocalVar(d) == d
> assert ifThenElseParam(42) == null
> assert ifThenElseParam('foo') == null
> assert ifThenElseParam(d) == d
> assert ifThenElseLocalVar(42) == null
> assert ifThenElseLocalVar('foo') == null
> assert ifThenElseLocalVar(d) == d
> def fc = new FooChild()
> assert ifThenElseLocalVar2(fc) == fc
> assert ifThenElseLocalVar2(new FooBase()) == null
> {code}



--
This message was sent by Atlassian JIRA
(v6.4.14#64029)

Mime
View raw message