Skip to content

Commit

Permalink
GROOVY-10328: STC: List<? super Type> dot get returns Object, not Type
Browse files Browse the repository at this point in the history
3_0_X backport
  • Loading branch information
eric-milles committed Dec 11, 2023
1 parent e25fc2e commit a4e87e4
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -1918,11 +1918,11 @@ static ClassNode applyGenericsContext(final Map<GenericsTypeName, GenericsType>
}

static ClassNode getCombinedBoundType(final GenericsType genericsType) {
// TODO: this method should really return some kind of meta ClassNode
// representing the combination of all bounds. The code here, just picks
// something out to be able to proceed and is not actually correct
// TODO: This method should really return some kind of meta ClassNode
// representing the combination of all bounds. The code here just picks
// something out to be able to proceed and is not actually correct.
if (hasNonTrivialBounds(genericsType)) {
if (genericsType.getLowerBound() != null) return genericsType.getLowerBound();
if (genericsType.getLowerBound() != null) return OBJECT_TYPE; // GROOVY-10328
if (genericsType.getUpperBounds() != null) return genericsType.getUpperBounds()[0];
}
return genericsType.getType();
Expand Down
46 changes: 23 additions & 23 deletions src/test/groovy/bugs/Groovy9074.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class Groovy9074 extends GroovyTestCase {

void _FIXME_testWildcardCapture() {
def err = shouldFail '''
@groovy.transform.CompileStatic
@groovy.transform.TypeChecked
class Main {
private static Collection<?> c = new ArrayList<String>()
static main(args) {
Expand All @@ -46,20 +46,20 @@ final class Groovy9074 extends GroovyTestCase {
def err = shouldFail '''
import java.awt.Canvas
abstract class Shape {
abstract void draw(Canvas c)
abstract void draw(Canvas c)
}
class Circle extends Shape {
private int x, y, radius
@Override void draw(Canvas c) {}
private int x, y, radius
@Override void draw(Canvas c) {}
}
class Rectangle extends Shape {
private int x, y, width, height
@Override void draw(Canvas c) {}
private int x, y, width, height
@Override void draw(Canvas c) {}
}
@groovy.transform.CompileStatic
@groovy.transform.TypeChecked
void addRectangle(List<? extends Shape> shapes) {
shapes.add(0, new Rectangle()) // TODO: compile-time error!
shapes.add(0, new Rectangle()) // TODO: compile-time error!
}
'''

Expand All @@ -71,20 +71,20 @@ final class Groovy9074 extends GroovyTestCase {
assertScript '''
import java.awt.Canvas
abstract class Shape {
abstract void draw(Canvas c)
abstract void draw(Canvas c)
}
class Circle extends Shape {
private int x, y, radius
@Override void draw(Canvas c) {}
private int x, y, radius
@Override void draw(Canvas c) {}
}
class Rectangle extends Shape {
private int x, y, width, height
@Override void draw(Canvas c) {}
private int x, y, width, height
@Override void draw(Canvas c) {}
}
@groovy.transform.CompileStatic
@groovy.transform.TypeChecked
void addRectangle(List<? super Shape> shapes) {
shapes.add(0, new Rectangle())
shapes.add(0, new Rectangle())
}
List<Shape> list = []
Expand All @@ -99,12 +99,12 @@ final class Groovy9074 extends GroovyTestCase {
new CompilationUnit().with {
addSource 'Main.groovy', '''
class Factory {
def <T> T make(Class<T> type, ... args) {}
def <T> T make(Class<T> type, ... args) {}
}
@groovy.transform.CompileStatic
@groovy.transform.TypeChecked
void test(Factory fact, Rule rule) {
Type bean = fact.make(rule.type)
Type bean = fact.make(rule.type)
}
'''

Expand All @@ -124,16 +124,16 @@ final class Groovy9074 extends GroovyTestCase {
}
}

void _FIXME_testWildcardSuper2() {
void testWildcardSuper2() {
new CompilationUnit().with {
addSource 'Main.groovy', '''
class Factory {
def <T> T make(Class<T> type, ... args) {}
def <T> T make(Class<T> type, ... args) {}
}
@groovy.transform.CompileStatic
@groovy.transform.TypeChecked
void test(Factory fact, Rule rule) {
Type bean = fact.make(rule.type) // can't assign "? super Type" to "Type"
Type bean = fact.make(rule.type) // can't assign "? super Type" to "Type"
}
'''

Expand All @@ -152,7 +152,7 @@ final class Groovy9074 extends GroovyTestCase {
def err = shouldFail {
compile CLASS_GENERATION
}
assert err =~ "cannot convert from capture#1-of ? super Type to Type"
assert err =~ 'Cannot assign value of type java.lang.Object to variable of type Type'
}
}
}
23 changes: 22 additions & 1 deletion src/test/groovy/transform/stc/TypeInferenceSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -225,7 +225,8 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
'''
}

@NotYetImplemented // GROOVY-7971
// GROOVY-7971
@NotYetImplemented
void testInstanceOf9() {
assertScript '''
import groovy.json.JsonOutput
Expand Down Expand Up @@ -1418,4 +1419,24 @@ class TypeInferenceSTCTest extends StaticTypeCheckingTestCase {
test( [id:'x'] )
'''
}

// GROOVY-10328
void testInferredTypeForMapOrList() {
assertScript '''
@ASTTest(phase=INSTRUCTION_SELECTION, value={
for (decl in node.code.statements*.expression) {
assert decl.getNodeMetaData(INFERRED_TYPE) == OBJECT_TYPE
}
})
void test(List<? super String> list, Map<String, ? super String> map) {
def a = list.first()
def b = list.get(0)
def c = list[0]
def x = map.get('foo')
def y = map['foo']
def z = map.foo
}
'''
}
}

0 comments on commit a4e87e4

Please sign in to comment.