This is an automated email from the ASF dual-hosted git repository. emilles pushed a commit to branch GROOVY_4_0_X in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/GROOVY_4_0_X by this push: new 1b810fc5c4 GROOVY-11427: STC: `Class<T>` receiver only matches static method of `T` 1b810fc5c4 is described below commit 1b810fc5c4938b76c90904fb3327b21bb4fa818c Author: Eric Milles <eric.mil...@thomsonreuters.com> AuthorDate: Tue Jun 25 13:47:57 2024 -0500 GROOVY-11427: STC: `Class<T>` receiver only matches static method of `T` --- .../groovy/transform/stc/StaticTypeCheckingVisitor.java | 16 +++++++++++----- src/test/groovy/transform/stc/BugsSTCTest.groovy | 10 +++++++++- src/test/groovy/transform/stc/MethodCallsSTCTest.groovy | 4 ++-- src/test/groovy/transform/stc/MiscSTCTest.groovy | 3 ++- .../transform/traitx/TraitWithClosureOrLambda.groovy | 2 +- 5 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java index 36b082ffd1..d612cb2f01 100644 --- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java +++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java @@ -5157,6 +5157,7 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { if (isClassClassNodeWrappingConcreteType(receiver)) { // GROOVY-6802, GROOVY-6803, GROOVY-9415 List<MethodNode> result = findMethod(receiver.getGenericsTypes()[0].getType(), name, args); + result = allowStaticAccessToMember(result, true); // GROOVY-11427 if (!result.isEmpty()) return result; } @@ -5995,16 +5996,21 @@ public class StaticTypeCheckingVisitor extends ClassCodeVisitorSupport { } protected void addNoMatchingMethodError(final ClassNode receiver, final String name, ClassNode[] args, final ASTNode origin) { - String error; + String classifier; + String descriptor; if ("<init>".equals(name)) { + classifier = "constructor"; // remove implicit agruments [String, int] from enum constant construction if (receiver.isEnum() && args.length >= 2) args = Arrays.copyOfRange(args, 2, args.length); - error = "Cannot find matching constructor " + prettyPrintTypeName(receiver) + toMethodParametersString("", args); + descriptor = prettyPrintTypeName(receiver) + toMethodParametersString("", args); // type is name } else { - ClassNode type = isClassClassNodeWrappingConcreteType(receiver) ? receiver.getGenericsTypes()[0].getType() : wrapTypeIfNecessary(receiver); - error = "Cannot find matching method " + prettyPrintTypeName(type) + "#" + toMethodParametersString(name, args) + ". Please check if the declared type is correct and if the method exists."; + classifier = "method"; + descriptor = prettyPrintTypeName(wrapTypeIfNecessary(receiver)) + "#" + toMethodParametersString(name, args); + if (isClassClassNodeWrappingConcreteType(receiver)) { ClassNode t = receiver.getGenericsTypes()[0].getType(); + descriptor += " or static method " + prettyPrintTypeName(t) + "#" + toMethodParametersString(name, args); + } } - addStaticTypeError(error, origin); + addStaticTypeError("Cannot find matching " + classifier + " " + descriptor + ". Please check if the declared type is correct and if the method exists.", origin); } protected void addAmbiguousErrorMessage(final List<MethodNode> foundMethods, final String name, final ClassNode[] args, final Expression expr) { diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy index b0f479634a..d6f96d47fd 100644 --- a/src/test/groovy/transform/stc/BugsSTCTest.groovy +++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy @@ -1092,13 +1092,21 @@ class BugsSTCTest extends StaticTypeCheckingTestCase { ''' } + // GROOVY-11427 + void testClassSubscript() { + shouldFailWithMessages ''' + Class<String> c = String.class + def x = c[0] + ''', + 'Cannot find matching method java.lang.Class#getAt(int) or static method java.lang.String#getAt(int)' + } + // GROOVY-9999 void testMathSqrt() { assertScript ''' def test() { Math.sqrt(Math.PI*2) } - double result = (double) test() // 2.5066282746310002 assert result > 2.5066 ''' diff --git a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy index 7770040c26..93e61129d1 100644 --- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy +++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy @@ -1758,12 +1758,12 @@ class MethodCallsSTCTest extends StaticTypeCheckingTestCase { shouldFailWithMessages ''' Double.isFiniteMissing(2.0d) ''', - 'Cannot find matching method java.lang.Double#isFiniteMissing(double)' + 'Cannot find ',' static method java.lang.Double#isFiniteMissing(double)' shouldFailWithMessages ''' String.doSomething() ''', - 'Cannot find matching method java.lang.String#doSomething()' + 'Cannot find ',' static method java.lang.String#doSomething()' } // GROOVY-6776 diff --git a/src/test/groovy/transform/stc/MiscSTCTest.groovy b/src/test/groovy/transform/stc/MiscSTCTest.groovy index 9bde8b8185..6d111d674a 100644 --- a/src/test/groovy/transform/stc/MiscSTCTest.groovy +++ b/src/test/groovy/transform/stc/MiscSTCTest.groovy @@ -408,7 +408,8 @@ class MiscSTCTest extends StaticTypeCheckingTestCase { Foo.newInstance2(new CustomNumber()) } } - ''', 'Cannot find matching method Foo#newInstance2(CustomNumber)' + ''', + 'Cannot find ',' static method Foo#newInstance2(CustomNumber)' } // GROOVY-8380 diff --git a/src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy b/src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy index 9cf62a1737..624a9f603c 100644 --- a/src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy +++ b/src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy @@ -343,7 +343,7 @@ final class TraitWithClosureOrLambda { } } ''' - assert err =~ /Cannot find matching method [\w\$]+#config/ + assert err =~ /Cannot find matching method .+#config/ } // GROOVY-11265