Skip to content

Commit

Permalink
GROOVY-10845
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-milles committed Nov 17, 2022
1 parent 7e12c28 commit 8b35a8d
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -872,6 +872,29 @@ public void testEnum10823() {
runConformTest(sources, "42null");
}

@Test
public void testEnum10845() {
//@formatter:off
String[] sources = {
"Script.groovy",
"@groovy.transform.TupleConstructor(defaults=false)\n" +
"@groovy.transform.TypeChecked\n" +
"enum E {\n" +
" X\n" +
" final Number number\n" +
"}\n",
};
//@formatter:on

runNegativeTest(sources,
"----------\n" +
"1. ERROR in Script.groovy (at line 4)\n" +
"\tX\n" +
"\t^\n" +
"Groovy:[Static type checking] - Cannot find matching constructor E()\n" +
"----------\n");
}

@Test
public void testEnumValues_GRE1071() {
//@formatter:off
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3115,7 +3115,19 @@ public void visitStaticMethodCallExpression(final StaticMethodCallExpression cal
addStaticTypeError("cannot resolve dynamic method name at compile time.", call);
return;
}

// GRECLIPSE add -- GROOVY-10845: $INIT(Object[]) delegates
if (name.equals("$INIT") && call.getOwnerType().isEnum()) {
FieldExpression target = (FieldExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
ConstructorCallExpression cce = new ConstructorCallExpression(call.getOwnerType(), call.getArguments());
cce.setSourcePosition(target.getField());
visitConstructorCallExpression(cce);
//
MethodNode init = call.getOwnerType().getDeclaredMethods("$INIT").get(0);
call.putNodeMetaData(StaticTypesMarker.INFERRED_TYPE, init.getReturnType());
call.putNodeMetaData(StaticTypesMarker.DIRECT_METHOD_CALL_TARGET, init);
return;
}
// GRECLIPSE end
if (extension.beforeMethodCall(call)) {
extension.afterMethodCall(call);
return;
Expand Down Expand Up @@ -6612,6 +6624,14 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) {
}

protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) {
// GRECLIPSE add -- GROOVY-10845
if ("<init>".equals(name)) {
// remove implicit agruments [String, int] from enum constant construction
ClassNode[] actual = (receiver.isEnum() && args.length >= 2) ? Arrays.copyOfRange(args, 2, args.length) : args;
addStaticTypeError("Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", actual), call);
return;
}
// GRECLIPSE end
if (isClassClassNodeWrappingConcreteType(receiver)) {
receiver = receiver.getGenericsTypes()[0].getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2957,7 +2957,19 @@ public void visitStaticMethodCallExpression(final StaticMethodCallExpression cal
addStaticTypeError("cannot resolve dynamic method name at compile time.", call);
return;
}

// GRECLIPSE add -- GROOVY-10845: $INIT(Object[]) delegates
if (name.equals("$INIT") && call.getOwnerType().isEnum()) {
FieldExpression target = (FieldExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
ConstructorCallExpression cce = new ConstructorCallExpression(call.getOwnerType(), call.getArguments());
cce.setSourcePosition(target.getField());
visitConstructorCallExpression(cce);
//
MethodNode init = call.getOwnerType().getDeclaredMethods("$INIT").get(0);
call.putNodeMetaData(INFERRED_TYPE, init.getReturnType());
call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, init);
return;
}
// GRECLIPSE end
if (extension.beforeMethodCall(call)) {
extension.afterMethodCall(call);
return;
Expand Down Expand Up @@ -6384,6 +6396,14 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) {
}

protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) {
// GRECLIPSE add -- GROOVY-10845
if ("<init>".equals(name)) {
// remove implicit agruments [String, int] from enum constant construction
ClassNode[] actual = (receiver.isEnum() && args.length >= 2) ? Arrays.copyOfRange(args, 2, args.length) : args;
addStaticTypeError("Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", actual), call);
return;
}
// GRECLIPSE end
if (isClassClassNodeWrappingConcreteType(receiver)) {
receiver = receiver.getGenericsTypes()[0].getType();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2706,7 +2706,19 @@ public void visitStaticMethodCallExpression(final StaticMethodCallExpression cal
addStaticTypeError("cannot resolve dynamic method name at compile time.", call);
return;
}

// GRECLIPSE add -- GROOVY-10845: $INIT(Object[]) delegates
if (name.equals("$INIT") && call.getOwnerType().isEnum()) {
FieldExpression target = (FieldExpression) typeCheckingContext.getEnclosingBinaryExpression().getLeftExpression();
ConstructorCallExpression cce = new ConstructorCallExpression(call.getOwnerType(), call.getArguments());
cce.setSourcePosition(target.getField());
visitConstructorCallExpression(cce);
//
MethodNode init = call.getOwnerType().getDeclaredMethods("$INIT").get(0);
call.putNodeMetaData(INFERRED_TYPE, init.getReturnType());
call.putNodeMetaData(DIRECT_METHOD_CALL_TARGET, init);
return;
}
// GRECLIPSE end
if (extension.beforeMethodCall(call)) {
extension.afterMethodCall(call);
return;
Expand Down Expand Up @@ -5912,6 +5924,14 @@ protected void addStaticTypeError(final String msg, final ASTNode expr) {
}

protected void addNoMatchingMethodError(ClassNode receiver, final String name, final ClassNode[] args, final Expression call) {
// GRECLIPSE add -- GROOVY-10845
if ("<init>".equals(name)) {
// remove implicit agruments [String, int] from enum constant construction
ClassNode[] actual = (receiver.isEnum() && args.length >= 2) ? Arrays.copyOfRange(args, 2, args.length) : args;
addStaticTypeError("Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", actual), call);
return;
}
// GRECLIPSE end
if (isClassClassNodeWrappingConcreteType(receiver)) {
receiver = receiver.getGenericsTypes()[0].getType();
}
Expand Down

0 comments on commit 8b35a8d

Please sign in to comment.