@@ -27,10 +27,11 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
27
27
assertScript '''
28
28
String[] strings = ['a','b','c']
29
29
String str = strings[0]
30
+ assert str == 'a'
30
31
'''
31
32
}
32
33
33
- void testArrayElementReturnType () {
34
+ void testArrayElementTypeInference () {
34
35
shouldFailWithMessages '''
35
36
String[] strings = ['a','b','c']
36
37
int str = strings[0]
@@ -43,10 +44,43 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
43
44
''' , ' Cannot assign value of type java.lang.String into array of type int[]'
44
45
}
45
46
47
+ // GROOVY-9985, GROOVY-9994
46
48
void testWrongComponentTypeInArrayInitializer () {
47
49
shouldFailWithMessages '''
48
- int[] intArray = new int[]{'a'}
49
- ''' , ' Cannot assign value of type java.lang.String into array of type int[]'
50
+ new int['a']
51
+ ''' , ' Cannot convert from java.lang.String to int'
52
+ shouldFailWithMessages '''
53
+ new int[]{'a'}
54
+ ''' , ' Cannot convert from java.lang.String to int'
55
+ shouldFailWithMessages '''
56
+ new Integer[]{new Object(),1}
57
+ ''' , ' Cannot convert from java.lang.Object to java.lang.Integer'
58
+ }
59
+
60
+ // GROOVY-10111
61
+ void testBoundedComponentTypeInArrayInitializer () {
62
+ assertScript '''
63
+ class C<X, Y> {
64
+ }
65
+ def <X extends C<Number, String>> X[] m() {
66
+ new X[]{new C<Number, String>()}
67
+ }
68
+ '''
69
+ shouldFailWithMessages '''
70
+ class C<X, Y> {
71
+ }
72
+ def <X extends C<Number, String>> X[] m() {
73
+ new X[]{new C<Object, String>()}
74
+ }
75
+ ''' , ' Cannot convert from C<java.lang.Object, java.lang.String> to X'
76
+ }
77
+
78
+ void testConvertibleTypesInArrayInitializer () {
79
+ assertScript '''
80
+ def strings = new String[]{1,(long)2,(short)3}
81
+ assert strings.every { it.class == String }
82
+ assert strings.toString() == '[1, 2, 3]'
83
+ '''
50
84
}
51
85
52
86
void testAssignValueInArrayWithCorrectType () {
0 commit comments