You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@groovy.apache.org by "Eric Milles (Jira)" <ji...@apache.org> on 2023/04/28 20:01:00 UTC

[jira] [Comment Edited] (GROOVY-11020) NullPointerException in instruction selection when calling parameterized function whose type parameter has an upper bound corresponding to a SAM type

    [ https://issues.apache.org/jira/browse/GROOVY-11020?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17717799#comment-17717799 ] 

Eric Milles edited comment on GROOVY-11020 at 4/28/23 8:00 PM:
---------------------------------------------------------------

I have a fix for the NPE.  The resolution of "y::addFirst" to the second type variable is a bit more complicated.  {{forAllButLast}} is deprecated and uses an odd style for the covariance of a parameter:
{code:java}
static <T,C extends Closure<? super T>> T forAllButLast(Iterable<T> iterable, C closure)
{code}
Its replacement uses something that supports closures/lambdas/references more directly:
{code:java}
static <E> E forEachButLast(Iterable<E> iterable, Closure<? super E> closure)
{code}

You should be able to use the latter without issue.


was (Author: emilles):
I have a fix for the NPE.  The resolution of "y::addFirst" to the second type variable is a bit more complicated.  {{forAllButLast}} is deprecated and uses an odd style for the covariance of a parameter:
{code:java}
static <T,C extends Closure<? super T>> T forAllButLast(Iterable<T> iterable, C closure)
{code}
Its replacement uses something that supports lambdas/closures more directly:
{code:java}
static <E> E forEachButLast(Iterable<E> iterable, Closure<? super E> closure)
{code}

You should be able to use the latter without issue.

> NullPointerException in instruction selection when calling parameterized function whose type parameter has an upper bound corresponding to a SAM type
> -----------------------------------------------------------------------------------------------------------------------------------------------------
>
>                 Key: GROOVY-11020
>                 URL: https://issues.apache.org/jira/browse/GROOVY-11020
>             Project: Groovy
>          Issue Type: Bug
>          Components: Static Type Checker
>            Reporter: Thodoris Sotiropoulos
>            Assignee: Eric Milles
>            Priority: Minor
>             Fix For: 4.0.12
>
>
> I have the following code
> {code}
> import java.util.*;
> import java.util.function.*;
> class Main {
>   static final void test(ArrayDeque<String> y) {
>     
>     m(y::addFirst);
>   }
>   static <C extends Consumer<String>> void m(C cl) {}
> }
> {code}
> h3. Actual behavior
> The compiler crashes with the following stacktrace:
> {code}
> >>> a serious error occurred: BUG! exception in phase 'instruction selection' in source unit 'Main.groovy' unexpected NullPointerException
> >>> stacktrace:
> BUG! exception in phase 'instruction selection' in source unit 'Main.groovy' unexpected NullPointerException
>         at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:953)
>         at org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:694)
>         at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:668)
>         at org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:649)
>         at org.codehaus.groovy.tools.FileSystemCompiler.compile(FileSystemCompiler.java:311)
>         at org.codehaus.groovy.tools.FileSystemCompiler.doCompilation(FileSystemCompiler.java:240)
>         at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompile(FileSystemCompiler.java:165)
>         at org.codehaus.groovy.tools.FileSystemCompiler.commandLineCompileWithErrorHandling(FileSystemCompiler.java:205)
>         at org.codehaus.groovy.tools.FileSystemCompiler.main(FileSystemCompiler.java:189)
> Caused by: java.lang.NullPointerException
>         at org.codehaus.groovy.ast.tools.GenericsUtils.parameterizeSAM(GenericsUtils.java:974)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.inferClosureParameterTypes(StaticTypeCheckingVisitor.java:3110)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitMethodCallArguments(StaticTypeCheckingVisitor.java:2904)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitStaticMethodCallExpression(StaticTypeCheckingVisitor.java:2819)
>         at org.codehaus.groovy.ast.expr.StaticMethodCallExpression.visit(StaticMethodCallExpression.java:44)
>         at org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:117)
>         at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:212)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitExpressionStatement(StaticTypeCheckingVisitor.java:2217)
>         at org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:41)
>         at org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:86)
>         at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:175)
>         at org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:72)
>         at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:139)
>         at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:118)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.lambda$visitConstructorOrMethod$28(StaticTypeCheckingVisitor.java:2680)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.doWithTypeCheckingExtensions(StaticTypeCheckingVisitor.java:463)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorOrMethod(StaticTypeCheckingVisitor.java:2680)
>         at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:110)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.startMethodInference(StaticTypeCheckingVisitor.java:2663)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitMethod(StaticTypeCheckingVisitor.java:2642)
>         at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitConstructorOrMethod(StaticCompilationVisitor.java:189)
>         at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitMethod(StaticCompilationVisitor.java:204)
>         at org.codehaus.groovy.ast.ClassNode.visitMethods(ClassNode.java:1144)
>         at org.codehaus.groovy.ast.ClassNode.visitContents(ClassNode.java:1137)
>         at org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClass(ClassCodeVisitorSupport.java:52)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.lambda$visitClass$3(StaticTypeCheckingVisitor.java:499)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.doWithTypeCheckingExtensions(StaticTypeCheckingVisitor.java:463)
>         at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitClass(StaticTypeCheckingVisitor.java:499)
>         at org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitClass(StaticCompilationVisitor.java:151)
>         at org.codehaus.groovy.transform.sc.StaticCompileTransformation.visit(StaticCompileTransformation.java:68)
>         at org.codehaus.groovy.control.customizers.ASTTransformationCustomizer.call(ASTTransformationCustomizer.groovy:303)
>         at org.codehaus.groovy.control.CompilationUnit$IPrimaryClassNodeOperation.doPhaseOperation(CompilationUnit.java:948)
>         ... 8 more
> {code}
> h3. Expected behavior 
> Compile successfully
> h3. Notes
> Tested against master (commit: 61f445cf4b1099eb25f770cc0685efb10783b8f1)
> Test case adapted from a program that uses the apache-commons-collections lib:
> {code}
> import java.util.*;
> import java.util.function.*;
> class Main {
>   static final void test(Iterator<String> x,
>                          ArrayDeque<String> y) {
>     
>     org.apache.commons.collections4.CollectionUtils.forAllButLastDo(x, y::addFirst);
>   }
> }
> {code}



--
This message was sent by Atlassian Jira
(v8.20.10#820010)