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 d67c1d6c37 GROOVY-11385: STC: `Type::new` for abstract class is 
impossible
d67c1d6c37 is described below

commit d67c1d6c37d1b180b8a29ef1f960be0e8e6cb6cb
Author: Eric Milles <eric.mil...@thomsonreuters.com>
AuthorDate: Thu Jun 20 14:52:20 2024 -0500

    GROOVY-11385: STC: `Type::new` for abstract class is impossible
---
 .../transform/stc/StaticTypeCheckingVisitor.java   |  4 +++
 .../transform/stc/MethodReferenceTest.groovy       | 29 ++++++++++++++++++++++
 2 files changed, 33 insertions(+)

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 f3b93de708..d309551fdc 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2583,6 +2583,10 @@ out:    if ((samParameterTypes.length == 1 && 
isOrImplements(samParameterTypes[0
                 if (isClassClassNodeWrappingConcreteType(type)){
                     type = type.getGenericsTypes()[0].getType();
                     storeType(expression,wrapClosureType(type));
+                    // GROOVY-11385: check if create possible
+                    if (type.isAbstract() && !type.isArray())
+                        addStaticTypeError("Cannot instantiate the type " +
+                                    prettyPrintTypeName(type), expression);
                     // GROOVY-10930: check constructor reference
                     ClassNode[] signature = 
expression.getNodeMetaData(CLOSURE_ARGUMENTS);
                     if (signature != null) { Expression[] mocks = 
Arrays.stream(signature)
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy 
b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index 3aac78c553..3995542c67 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -1020,6 +1020,35 @@ final class MethodReferenceTest {
         '''
     }
 
+    @Test // class::new -- GROOVY-11385
+    void testFunctionCN8() {
+        def err = shouldFail shell, '''
+            abstract class A {
+                A(String s) {}
+            }
+            @CompileStatic
+            void test() {
+                Function<String, A> f = A::new
+                f.apply("") // InstantiationException
+            }
+
+            test()
+        '''
+        assert err =~ /Cannot instantiate the type A/
+
+        for (op in ['::','.&']) {
+            err = shouldFail shell, """
+                @CompileStatic
+                void test() {
+                    Supplier<Number> s = Number${op}new
+                }
+
+                test()
+            """
+            assert err =~ /Cannot instantiate the type java.lang.Number/
+        }
+    }
+
     @Test // arrayClass::new
     void testIntFunctionCN() {
         assertScript shell, '''

Reply via email to