Skip to content

Commit 07adab7

Browse files
committed
GROOVY-9997
1 parent a513d63 commit 07adab7

File tree

4 files changed

+60
-1
lines changed

4 files changed

+60
-1
lines changed

base-test/org.eclipse.jdt.groovy.core.tests.compiler/src/org/eclipse/jdt/groovy/core/tests/xform/TypeCheckedTests.java

+17
Original file line numberDiff line numberDiff line change
@@ -1359,4 +1359,21 @@ public void testTypeChecked9995() {
13591359

13601360
runConformTest(sources, "1");
13611361
}
1362+
1363+
@Test
1364+
public void testTypeChecked9997() {
1365+
//@formatter:off
1366+
String[] sources = {
1367+
"Main.groovy",
1368+
"@groovy.transform.TypeChecked\n" +
1369+
"void test() {\n" +
1370+
" def cast = (Comparator<Integer>) { a, b -> Integer.compare(a, b) }\n" +
1371+
" def coerce = { a, b -> Integer.compare(a, b) } as Comparator<Integer>\n" +
1372+
"}\n" +
1373+
"test()\n",
1374+
};
1375+
//@formatter:on
1376+
1377+
runConformTest(sources, "");
1378+
}
13621379
}

base/org.codehaus.groovy25/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+14
Original file line numberDiff line numberDiff line change
@@ -4427,6 +4427,7 @@ protected Map<VariableExpression, List<ClassNode>> pushAssignmentTracking() {
44274427

44284428
@Override
44294429
public void visitCastExpression(final CastExpression expression) {
4430+
/* GRECLIPSE edit -- GROOVY-9997
44304431
super.visitCastExpression(expression);
44314432
if (!expression.isCoerce()) {
44324433
ClassNode targetType = expression.getType();
@@ -4437,6 +4438,19 @@ public void visitCastExpression(final CastExpression expression) {
44374438
}
44384439
}
44394440
storeType(expression, expression.getType());
4441+
*/
4442+
ClassNode type = expression.getType();
4443+
Expression source = expression.getExpression();
4444+
if (isFunctionalInterface(type)) {
4445+
processFunctionalInterfaceAssignment(type, source);
4446+
}
4447+
4448+
source.visit(this);
4449+
4450+
if (!expression.isCoerce() && !checkCast(type, source) && !isDelegateOrOwnerInClosure(source)) {
4451+
addStaticTypeError("Inconvertible types: cannot cast " + prettyPrintType(getType(source)) + " to " + prettyPrintType(type), expression);
4452+
}
4453+
// GRECLIPSE end
44404454
}
44414455

44424456
private boolean isDelegateOrOwnerInClosure(Expression exp) {

base/org.codehaus.groovy30/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+14
Original file line numberDiff line numberDiff line change
@@ -4247,6 +4247,7 @@ protected Map<VariableExpression, List<ClassNode>> pushAssignmentTracking() {
42474247

42484248
@Override
42494249
public void visitCastExpression(final CastExpression expression) {
4250+
/* GRECLIPSE edit -- GROOVY-9997
42504251
super.visitCastExpression(expression);
42514252
if (!expression.isCoerce()) {
42524253
ClassNode targetType = expression.getType();
@@ -4257,6 +4258,19 @@ public void visitCastExpression(final CastExpression expression) {
42574258
}
42584259
}
42594260
storeType(expression, expression.getType());
4261+
*/
4262+
ClassNode type = expression.getType();
4263+
Expression source = expression.getExpression();
4264+
if (isFunctionalInterface(type)) {
4265+
processFunctionalInterfaceAssignment(type, source);
4266+
}
4267+
4268+
source.visit(this);
4269+
4270+
if (!expression.isCoerce() && !checkCast(type, source) && !isDelegateOrOwnerInClosure(source)) {
4271+
addStaticTypeError("Inconvertible types: cannot cast " + prettyPrintType(getType(source)) + " to " + prettyPrintType(type), expression);
4272+
}
4273+
// GRECLIPSE end
42604274
}
42614275

42624276
private boolean isDelegateOrOwnerInClosure(final Expression exp) {

base/org.codehaus.groovy40/src/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java

+15-1
Original file line numberDiff line numberDiff line change
@@ -1961,7 +1961,7 @@ public void visitField(final FieldNode node) {
19611961
if (init instanceof ConstructorCallExpression) {
19621962
inferDiamondType((ConstructorCallExpression) init, node.getOriginType());
19631963
}
1964-
*/
1964+
*/
19651965
typeCheckAssignment(bexp, left, lType, init, getResultType(lType, ASSIGN, rType, bexp));
19661966
// GRECLIPSE end
19671967
}
@@ -4213,6 +4213,7 @@ protected Map<VariableExpression, List<ClassNode>> pushAssignmentTracking() {
42134213

42144214
@Override
42154215
public void visitCastExpression(final CastExpression expression) {
4216+
/* GRECLIPSE edit -- GROOVY-9997
42164217
super.visitCastExpression(expression);
42174218
if (!expression.isCoerce()) {
42184219
ClassNode targetType = expression.getType();
@@ -4223,6 +4224,19 @@ public void visitCastExpression(final CastExpression expression) {
42234224
}
42244225
}
42254226
storeType(expression, expression.getType());
4227+
*/
4228+
ClassNode type = expression.getType();
4229+
Expression source = expression.getExpression();
4230+
if (isFunctionalInterface(type)) {
4231+
processFunctionalInterfaceAssignment(type, source);
4232+
}
4233+
4234+
source.visit(this);
4235+
4236+
if (!expression.isCoerce() && !checkCast(type, source) && !isDelegateOrOwnerInClosure(source)) {
4237+
addStaticTypeError("Inconvertible types: cannot cast " + prettyPrintType(getType(source)) + " to " + prettyPrintType(type), expression);
4238+
}
4239+
// GRECLIPSE end
42264240
}
42274241

42284242
private boolean isDelegateOrOwnerInClosure(final Expression exp) {

0 commit comments

Comments
 (0)