You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@groovy.apache.org by em...@apache.org on 2024/06/25 19:25:50 UTC
(groovy) branch master updated: GROOVY-11427: STC: `Class` receiver only matches static method of `T`
This is an automated email from the ASF dual-hosted git repository.
emilles pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/groovy.git
The following commit(s) were added to refs/heads/master by this push:
new 7aff28a27b GROOVY-11427: STC: `Class<T>` receiver only matches static method of `T`
7aff28a27b is described below
commit 7aff28a27b406a2ed80167f12f943aa64d12a784
Author: Eric Milles <er...@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 +++++++++++-----
.../transform/stc/ArraysAndCollectionsSTCTest.groovy | 13 -------------
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 +-
6 files changed, 25 insertions(+), 23 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 3d4fd070c7..113490a4f3 100644
--- a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -5074,6 +5074,7 @@ trying: for (ClassNode[] signature : signatures) {
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;
}
@@ -5923,16 +5924,21 @@ trying: for (ClassNode[] signature : signatures) {
}
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/ArraysAndCollectionsSTCTest.groovy b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
index f08d050d60..18fb44a22b 100644
--- a/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/ArraysAndCollectionsSTCTest.groovy
@@ -782,19 +782,6 @@ class ArraysAndCollectionsSTCTest extends StaticTypeCheckingTestCase {
'''
}
- // GROOVY-8074
- void testMapSubclassPropertyStyleAccess() {
- assertScript '''
- class MyMap extends LinkedHashMap {
- def foo = 1
- }
-
- def map = new MyMap()
- map.put('foo', 42)
- assert map.foo == 1
- '''
- }
-
// GROOVY-6311
void testSetSpread() {
assertScript '''
diff --git a/src/test/groovy/transform/stc/BugsSTCTest.groovy b/src/test/groovy/transform/stc/BugsSTCTest.groovy
index 6d24223915..d248cd4af0 100644
--- a/src/test/groovy/transform/stc/BugsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/BugsSTCTest.groovy
@@ -1139,13 +1139,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 544bbbfe7d..2836e9c4b8 100644
--- a/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MethodCallsSTCTest.groovy
@@ -1876,12 +1876,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 b497ec2239..0960606a81 100644
--- a/src/test/groovy/transform/stc/MiscSTCTest.groovy
+++ b/src/test/groovy/transform/stc/MiscSTCTest.groovy
@@ -460,7 +460,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 50a98f7450..1a7f844b5a 100644
--- a/src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy
+++ b/src/test/org/codehaus/groovy/transform/traitx/TraitWithClosureOrLambda.groovy
@@ -339,7 +339,7 @@ final class TraitWithClosureOrLambda {
}
}
'''
- assert err.message.contains('Cannot find matching method T$Trait$Helper#config')
+ assert err.message.contains('Cannot find matching method ')
}
// GROOVY-11265