Skip to content

Commit a4e87e4

Browse files
committed
GROOVY-10328: STC: List<? super Type> dot get returns Object, not Type
3_0_X backport
1 parent e25fc2e commit a4e87e4

File tree

3 files changed

+49
-28
lines changed

3 files changed

+49
-28
lines changed

src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingSupport.java

+4-4
Original file line numberDiff line numberDiff line change
@@ -1918,11 +1918,11 @@ static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType>
19181918
}
19191919

19201920
static ClassNode getCombinedBoundType(final GenericsType genericsType) {
1921-
// TODO: this method should really return some kind of meta ClassNode
1922-
// representing the combination of all bounds. The code here, just picks
1923-
// something out to be able to proceed and is not actually correct
1921+
// TODO: This method should really return some kind of meta ClassNode
1922+
// representing the combination of all bounds. The code here just picks
1923+
// something out to be able to proceed and is not actually correct.
19241924
if (hasNonTrivialBounds(genericsType)) {
1925-
if (genericsType.getLowerBound() != null) return genericsType.getLowerBound();
1925+
if (genericsType.getLowerBound() != null) return OBJECT_TYPE; // GROOVY-10328
19261926
if (genericsType.getUpperBounds() != null) return genericsType.getUpperBounds()[0];
19271927
}
19281928
return genericsType.getType();

src/test/groovy/bugs/Groovy9074.groovy

+23-23
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ final class Groovy9074 extends GroovyTestCase {
2929

3030
void _FIXME_testWildcardCapture() {
3131
def err = shouldFail '''
32-
@groovy.transform.CompileStatic
32+
@groovy.transform.TypeChecked
3333
class Main {
3434
private static Collection<?> c = new ArrayList<String>()
3535
static main(args) {
@@ -46,20 +46,20 @@ final class Groovy9074 extends GroovyTestCase {
4646
def err = shouldFail '''
4747
import java.awt.Canvas
4848
abstract class Shape {
49-
abstract void draw(Canvas c)
49+
abstract void draw(Canvas c)
5050
}
5151
class Circle extends Shape {
52-
private int x, y, radius
53-
@Override void draw(Canvas c) {}
52+
private int x, y, radius
53+
@Override void draw(Canvas c) {}
5454
}
5555
class Rectangle extends Shape {
56-
private int x, y, width, height
57-
@Override void draw(Canvas c) {}
56+
private int x, y, width, height
57+
@Override void draw(Canvas c) {}
5858
}
5959
60-
@groovy.transform.CompileStatic
60+
@groovy.transform.TypeChecked
6161
void addRectangle(List<? extends Shape> shapes) {
62-
shapes.add(0, new Rectangle()) // TODO: compile-time error!
62+
shapes.add(0, new Rectangle()) // TODO: compile-time error!
6363
}
6464
'''
6565

@@ -71,20 +71,20 @@ final class Groovy9074 extends GroovyTestCase {
7171
assertScript '''
7272
import java.awt.Canvas
7373
abstract class Shape {
74-
abstract void draw(Canvas c)
74+
abstract void draw(Canvas c)
7575
}
7676
class Circle extends Shape {
77-
private int x, y, radius
78-
@Override void draw(Canvas c) {}
77+
private int x, y, radius
78+
@Override void draw(Canvas c) {}
7979
}
8080
class Rectangle extends Shape {
81-
private int x, y, width, height
82-
@Override void draw(Canvas c) {}
81+
private int x, y, width, height
82+
@Override void draw(Canvas c) {}
8383
}
8484
85-
@groovy.transform.CompileStatic
85+
@groovy.transform.TypeChecked
8686
void addRectangle(List<? super Shape> shapes) {
87-
shapes.add(0, new Rectangle())
87+
shapes.add(0, new Rectangle())
8888
}
8989
9090
List<Shape> list = []
@@ -99,12 +99,12 @@ final class Groovy9074 extends GroovyTestCase {
9999
new CompilationUnit().with {
100100
addSource 'Main.groovy', '''
101101
class Factory {
102-
def <T> T make(Class<T> type, ... args) {}
102+
def <T> T make(Class<T> type, ... args) {}
103103
}
104104
105-
@groovy.transform.CompileStatic
105+
@groovy.transform.TypeChecked
106106
void test(Factory fact, Rule rule) {
107-
Type bean = fact.make(rule.type)
107+
Type bean = fact.make(rule.type)
108108
}
109109
'''
110110

@@ -124,16 +124,16 @@ final class Groovy9074 extends GroovyTestCase {
124124
}
125125
}
126126

127-
void _FIXME_testWildcardSuper2() {
127+
void testWildcardSuper2() {
128128
new CompilationUnit().with {
129129
addSource 'Main.groovy', '''
130130
class Factory {
131-
def <T> T make(Class<T> type, ... args) {}
131+
def <T> T make(Class<T> type, ... args) {}
132132
}
133133
134-
@groovy.transform.CompileStatic
134+
@groovy.transform.TypeChecked
135135
void test(Factory fact, Rule rule) {
136-
Type bean = fact.make(rule.type) // can't assign "? super Type" to "Type"
136+
Type bean = fact.make(rule.type) // can't assign "? super Type" to "Type"
137137
}
138138
'''
139139

@@ -152,7 +152,7 @@ final class Groovy9074 extends GroovyTestCase {
152152
def err = shouldFail {
153153
compile CLASS_GENERATION
154154
}
155-
assert err =~ "cannot convert from capture#1-of ? super Type to Type"
155+
assert err =~ 'Cannot assign value of type java.lang.Object to variable of type Type'
156156
}
157157
}
158158
}

src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy

+22-1
Original file line numberDiff line numberDiff line change
@@ -225,7 +225,8 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
225225
'''
226226
}
227227

228-
@NotYetImplemented // GROOVY-7971
228+
// GROOVY-7971
229+
@NotYetImplemented
229230
void testInstanceOf9() {
230231
assertScript '''
231232
import groovy.json.JsonOutput
@@ -1418,4 +1419,24 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
14181419
test( [id:'x'] )
14191420
'''
14201421
}
1422+
1423+
// GROOVY-10328
1424+
void testInferredTypeForMapOrList() {
1425+
assertScript '''
1426+
@ASTTest(phase=INSTRUCTION_SELECTION, value={
1427+
for (decl in node.code.statements*.expression) {
1428+
assert decl.getNodeMetaData(INFERRED_TYPE) == OBJECT_TYPE
1429+
}
1430+
})
1431+
void test(List<? super String> list, Map<String, ? super String> map) {
1432+
def a = list.first()
1433+
def b = list.get(0)
1434+
def c = list[0]
1435+
1436+
def x = map.get('foo')
1437+
def y = map['foo']
1438+
def z = map.foo
1439+
}
1440+
'''
1441+
}
14211442
}

0 commit comments

Comments
 (0)