Skip to content

Commit

Permalink
GROOVY-5441
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed May 28, 2021
1 parent 359b06c commit 0060908
Show file tree
Hide file tree
Showing 4 changed files with 70 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -367,6 +367,49 @@ public void testGenericTypecast2() {
"----------\n");
}

@Test // GROOVY-5441
public void testGenericsArityErrors() {
//@formatter:off
String[] sources = {
"Script.groovy",
"groovy.lang.Tuple2<Object> tooFew\n" +
"java.util.List<Object,Object> tooMany\n" +
"def (\n" +
" java.util.Map<Object> tooFew2,\n" +
" java.lang.Object<Object> tooMany2\n" +
") = [null,null]\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Script.groovy (at line 1)\n" +
"\tgroovy.lang.Tuple2<Object> tooFew\n" +
"\t^^^^^^^^^^^^^^^^^^\n" +
"Groovy:The class groovy.lang.Tuple2<Object> (supplied with 1 type parameter) refers to the class groovy.lang.Tuple2<T1,T2> which takes 2 parameters\n" +
"----------\n" +
"2. ERROR in Script.groovy (at line 2)\n" +
"\tjava.util.List<Object,Object> tooMany\n" +
"\t^^^^^^^^^^^^^^\n" +
"Groovy:The class java.util.List<Object,Object> (supplied with 2 type parameters) refers to the class java.util.List<E> which takes 1 parameter\n" +
"----------\n" +
"3. ERROR in Script.groovy (at line 4)\n" +
"\tjava.util.Map<Object> tooFew2,\n" +
"\t^^^^^^^^^^^^^\n" +
"Groovy:The class java.util.Map<Object> (supplied with 1 type parameter) refers to the class java.util.Map<K,V> which takes 2 parameters\n" +
"----------\n" +
"4. ERROR in Script.groovy (at line 5)\n" +
"\tjava.lang.Object<Object> tooMany2\n" +
"\t^^^^^^^^^^^^^^^^\n" +
"The type Object is not generic; it cannot be parameterized with arguments <Object>\n" +
"----------\n" +
"5. ERROR in Script.groovy (at line 5)\n" +
"\tjava.lang.Object<Object> tooMany2\n" +
"\t^^^^^^^^^^^^^^^^\n" +
"Groovy:The class java.lang.Object<Object> (supplied with 1 type parameter) refers to the class java.lang.Object which takes no parameters\n" +
"----------\n");
}

@Test
public void testCallingGenericConstructors() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.transform.trait.Traits;

import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isUnboundedWildcard;
Expand Down Expand Up @@ -119,11 +120,19 @@ public void visitDeclarationExpression(DeclarationExpression expression) {
if (expression.isMultipleAssignmentDeclaration()) {
TupleExpression tExpr = expression.getTupleExpression();
for (Expression nextExpr : tExpr.getExpressions()) {
/* GRECLIPSE edit -- GROOVY-5441
ClassNode declType = nextExpr.getType();
*/
ClassNode declType = ((VariableExpression)nextExpr).getOriginType();
// GRECLIPSE end
checkGenericsUsage(declType, declType.redirect());
}
} else {
/* GRECLIPSE edit -- GROOVY-5441
ClassNode declType = expression.getVariableExpression().getType();
*/
ClassNode declType = expression.getVariableExpression().getOriginType();
// GRECLIPSE end
checkGenericsUsage(declType, declType.redirect());
}
super.visitDeclarationExpression(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.transform.trait.Traits;

import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isUnboundedWildcard;
Expand Down Expand Up @@ -120,11 +121,19 @@ public void visitDeclarationExpression(DeclarationExpression expression) {
if (expression.isMultipleAssignmentDeclaration()) {
TupleExpression tExpr = expression.getTupleExpression();
for (Expression nextExpr : tExpr.getExpressions()) {
/* GRECLIPSE edit -- GROOVY-5441
ClassNode declType = nextExpr.getType();
*/
ClassNode declType = ((VariableExpression)nextExpr).getOriginType();
// GRECLIPSE end
checkGenericsUsage(declType, declType.redirect());
}
} else {
/* GRECLIPSE edit -- GROOVY-5441
ClassNode declType = expression.getVariableExpression().getType();
*/
ClassNode declType = expression.getVariableExpression().getOriginType();
// GRECLIPSE end
checkGenericsUsage(declType, declType.redirect());
}
super.visitDeclarationExpression(expression);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
import org.codehaus.groovy.ast.expr.DeclarationExpression;
import org.codehaus.groovy.ast.expr.Expression;
import org.codehaus.groovy.ast.expr.TupleExpression;
import org.codehaus.groovy.ast.expr.VariableExpression;
import org.codehaus.groovy.transform.trait.Traits;

import static org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isUnboundedWildcard;
Expand Down Expand Up @@ -120,11 +121,19 @@ public void visitDeclarationExpression(DeclarationExpression expression) {
if (expression.isMultipleAssignmentDeclaration()) {
TupleExpression tExpr = expression.getTupleExpression();
for (Expression nextExpr : tExpr.getExpressions()) {
/* GRECLIPSE edit -- GROOVY-5441
ClassNode declType = nextExpr.getType();
*/
ClassNode declType = ((VariableExpression)nextExpr).getOriginType();
// GRECLIPSE end
checkGenericsUsage(declType, declType.redirect());
}
} else {
/* GRECLIPSE edit -- GROOVY-5441
ClassNode declType = expression.getVariableExpression().getType();
*/
ClassNode declType = expression.getVariableExpression().getOriginType();
// GRECLIPSE end
checkGenericsUsage(declType, declType.redirect());
}
super.visitDeclarationExpression(expression);
Expand Down

0 comments on commit 0060908

Please sign in to comment.