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

commit 9459feef501fa5f7b9c79afb823c9dd4ecc9485a
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 23d2fc418e..101950d859 100644
--- 
a/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
+++ 
b/src/main/java/org/codehaus/groovy/transform/stc/StaticTypeCheckingVisitor.java
@@ -2532,6 +2532,10 @@ public class StaticTypeCheckingVisitor extends 
ClassCodeVisitorSupport {
                 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);
                 }
                 return;
             }
diff --git a/src/test/groovy/transform/stc/MethodReferenceTest.groovy 
b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
index f189fb6b91..fdd61d69a9 100644
--- a/src/test/groovy/transform/stc/MethodReferenceTest.groovy
+++ b/src/test/groovy/transform/stc/MethodReferenceTest.groovy
@@ -1031,6 +1031,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