Skip to content

Commit

Permalink
GROOVY-10341: STC: error for super.abstractMethod()
Browse files Browse the repository at this point in the history
3_0_X backport
  • Loading branch information
eric-milles committed Dec 8, 2023
1 parent 3c96c6f commit e32dfaa
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -3570,6 +3570,13 @@ public void visitMethodCallExpression(final MethodCallExpression call) {
if (!directMethodCallCandidate.isStatic() && !(declaringClass.equals(CLASS_Type) || declaringClass.equals(OBJECT_TYPE)) // GROOVY-10939
&& receiver.equals(CLASS_Type) && chosenReceiver.getData() == null && !Boolean.TRUE.equals(call.getNodeMetaData(DYNAMIC_RESOLUTION))) {
addStaticTypeError("Non-static method " + prettyPrintTypeName(declaringClass) + "#" + directMethodCallCandidate.getName() + " cannot be called from static context", call);
} else if (directMethodCallCandidate.isAbstract() && isSuperExpression(objectExpression)) { // GROOVY-10341
String target = toMethodParametersString(directMethodCallCandidate.getName(), extractTypesFromParameters(directMethodCallCandidate.getParameters()));
if (Traits.hasDefaultImplementation(directMethodCallCandidate)) { // GROOVY-10494
addStaticTypeError("Default method " + target + " requires qualified super", call);
} else {
addStaticTypeError("Abstract method " + target + " cannot be called directly", call);
}
}

ClassNode returnType = getType(directMethodCallCandidate);
Expand Down
36 changes: 35 additions & 1 deletion src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
*/
package groovy.transform.stc

import groovy.test.NotYetImplemented
import org.codehaus.groovy.control.MultipleCompilationErrorsException

import static org.codehaus.groovy.control.customizers.builder.CompilerCustomizationBuilder.withConfig
Expand Down Expand Up @@ -331,7 +332,7 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
'''
}

void testCallToSuperDefault() {
void testCallToSuperDefault1() {
assertScript '''
interface I<T> {
default m(T t) {
Expand All @@ -348,6 +349,23 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-10494
@NotYetImplemented
void testCallToSuperDefault2() {
shouldFailWithMessages '''
interface I<T> {
default void m(T t) {
}
}
class C implements I<String> {
@Override void m(String s) {
super.m(s)
}
}
''',
'Default method m(T) requires qualified super'
}

// GROOVY-10922
void testCallToSuperGenerated() {
assertScript '''
Expand Down Expand Up @@ -1636,6 +1654,22 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase {
'''
}

// GROOVY-10341
void testCallAbstractSuperMethod() {
shouldFailWithMessages '''
abstract class Foo {
abstract def m()
}
class Bar extends Foo {
@Override
def m() {
super.m()
}
}
''',
'Abstract method m() cannot be called directly'
}

// GROOVY-5810
void testCallStaticSuperMethod() {
assertScript '''
Expand Down

0 comments on commit e32dfaa

Please sign in to comment.