You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@groovy.apache.org by "Edinson E. Padrón Urdaneta" <ed...@gmail.com> on 2015/09/27 22:07:05 UTC

Unexpected behavior with Arrays.asList and an array of primitives even under CompileStatic

The code:

1 @groovy.transform.CompileStatic
2 public class ToyingAround {
3     public static void main(String[] args) {
4         int[] myIntArray = [0, 1, 2, 3, 4];  // Change to braces for java
5         List<Integer> myIntegerList = Arrays.asList(myIntArray);
6
7         myIntArray[0] = 4;
8         myIntegerList.set(4, 0);
9
10        assert Arrays.toString(myIntArray) == "[4, 1, 2, 3, 4]";
11        assert myIntegerList.toString() == "[0, 1, 2, 3, 0]";
12        assert Arrays.toString(myIntArray) != myIntegerList.toString();
13    }
14}

As you can see, the CompileStatic transformation is not getting the error
in line 5. IntelliJ shows a warning and the Java compiler with the java
version of this code throws the following error:

Error:(5, 52) java: incompatible types: inference variable T has
incompatible bounds
    equality constraints: java.lang.Integer
    lower bounds: int[]

My question: is this the desire behavior? Is this an error that
CompileStatic is not catching by mistake? Or... ?

Thanks in advance and cheers.

Re: Unexpected behavior with Arrays.asList and an array of primitives even under CompileStatic

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
​​​​I trust in you when you say that there is a big chance that it isn't a
bug, but It's at very least an unexpected behavior, isn't it?​ I mean, it's
great that an IDE like IntelliJ gives you a warning but we shouldn't rely
on that to avoid a potential bug that could be hard to find.​

I'm sorry if I look like a stubborn person, I just want to share my concern
about the case, and maybe bring light to a potential wrong/unexpected
behavior of the language I like so much.

Cheers.

Re: Unexpected behavior with Arrays.asList and an array of primitives even under CompileStatic

Posted by Jochen Theodorou <bl...@gmx.org>.
frankly I am not sure this is a bug. Since vargs are involved things are 
more difficult. What I can say is, that the int[] is cast to an 
Object[]. Either dynamically by the runtime in normal Groovy, or through 
static code using ScriptBytecodeAdapter.castToType with Object[]. Since 
@CompileStatic only mimics the dynamic code I am unsure as of if to 
classify this as a static compilation bug.

bye blackdrag

Am 01.10.2015 03:08, schrieb Edinson E. Padrón Urdaneta:
> Should I open a jira issue? I'm not familiar with the procedure.
>
> On Mon, Sep 28, 2015 at 3:06 AM, Edinson E. Padrón Urdaneta
> <edinson.padron.urdaneta@gmail.com
> <ma...@gmail.com>> wrote:
>
>     I think (and I can be very wrong) what happens is that
>     `Arrays.toString(myIntArray)`is transformed
>     to`Arrays.toString(myIntArray as Integer[])`explaining why the
>     original array and the list that should be backed by it are not
>     bound at all. It's just an hypotheses, of course.
>
>


-- 
Jochen "blackdrag" Theodorou
blog: http://blackdragsview.blogspot.com/


Re: Unexpected behavior with Arrays.asList and an array of primitives even under CompileStatic

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
Should I open a jira issue? I'm not familiar with the procedure.

On Mon, Sep 28, 2015 at 3:06 AM, Edinson E. Padrón Urdaneta <
edinson.padron.urdaneta@gmail.com> wrote:

> I think (and I can be very wrong) what happens is that `
> Arrays.toString(myIntArray)` is transformed to `Arrays.toString(myIntArray
> as Integer[])` explaining why the original array and the list that should
> be backed by it are not bound at all. It's just an hypotheses, of course.
>

Re: Unexpected behavior with Arrays.asList and an array of primitives even under CompileStatic

Posted by "Edinson E. Padrón Urdaneta" <ed...@gmail.com>.
I think (and I can be very wrong) what happens is that `
Arrays.toString(myIntArray)` is transformed to `Arrays.toString(myIntArray
as Integer[])` explaining why the original array and the list that should
be backed by it are not bound at all. It's just an hypotheses, of course.

Re: Unexpected behavior with Arrays.asList and an array of primitives even under CompileStatic

Posted by Jochen Theodorou <bl...@gmx.org>.
Am 27.09.2015 22:07, schrieb Edinson E. Padrón Urdaneta:
> The code:
>
> 1 @groovy.transform.CompileStatic
> 2 public class ToyingAround {
> 3     public static void main(String[] args) {
> 4         int[] myIntArray = [0, 1, 2, 3, 4];  // Change to braces for java
> 5         List<Integer> myIntegerList = Arrays.asList(myIntArray);
> 6
> 7         myIntArray[0] = 4;
> 8         myIntegerList.set(4, 0);
> 9
> 10        assert Arrays.toString(myIntArray) == "[4, 1, 2, 3, 4]";
> 11        assert myIntegerList.toString() == "[0, 1, 2, 3, 0]";
> 12        assert Arrays.toString(myIntArray) != myIntegerList.toString();
> 13    }
> 14}
>
> As you can see, the CompileStatic transformation is not getting the
> error in line 5. IntelliJ shows a warning and the Java compiler with the
> java version of this code throws the following error:
>
> Error:(5, 52) java: incompatible types: inference variable T has
> incompatible bounds
>      equality constraints: java.lang.Integer
>      lower bounds: int[]
>
> My question: is this the desire behavior? Is this an error that
> CompileStatic is not catching by mistake? Or... ?

Doesn't that produce a list with an int[] as single element? But yes, 
this looks like a bug in static compilation. Unless, there is an 
alternative asList method, which takes int[]... but I have not seenthat. 
So this should not compile there. In normal Groovy this does not make 
sense, from the API, but it is not really wrong.

bye blackdrag

-- 
Jochen "blackdrag" Theodorou
blog: http://blackdragsview.blogspot.com/