Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Compiler error for function with static return type #308

Closed
eaaltonen opened this issue Jun 12, 2017 · 12 comments
Closed

Compiler error for function with static return type #308

eaaltonen opened this issue Jun 12, 2017 · 12 comments

Comments

@eaaltonen
Copy link

Environment:

  • Eclipse IDE for Java Developers 4.6.3.20170314-1500
  • Groovy-Eclipse Feature 2.9.2.xx-201706120159-e46
  • JDT Core patch for Groovy-Eclipse plugin on Eclipse 4.6 2.9.2.xx-201706120159-e46

I changed a function

def static myFunc(...)

to

static Map<String, MyObject> myFunc(...)

And now I'm looking at the following stack trace

Multiple markers at this line
	- General error during instruction selection: java.lang.IllegalStateException java.lang.IllegalStateException at 
	 org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.syntheticMethods(SourceTypeBinding.java:2468) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.initializeMembers(JDTClassNode.java:301) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.initialize(JDTClassNode.java:228) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.lazyClassInit(JDTClassNode.java:159) at 
	 org.codehaus.groovy.ast.ClassNode.getUnresolvedSuperClass(ClassNode.java:1124) at 
	 org.codehaus.groovy.ast.ClassNode.getUnresolvedSuperClass(ClassNode.java:1114) at 
	 org.codehaus.groovy.ast.ClassNode.getSuperClass(ClassNode.java:1108) at 
	 org.codehaus.groovy.ast.ClassNode.isDerivedFrom(ClassNode.java:1046) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isAssignableTo(StaticTypeCheckingSupport.java:382) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.allParametersAndArgumentsMatch(StaticTypeCheckingSupport.java:251) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.measureParametersAndArgumentsDistance(StaticTypeCheckingSupport.java:
	 1032) at org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod(StaticTypeCheckingSupport.java:1001) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments(StaticTypeCheckingSupport.java:
	 926) at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.findMethod(StaticTypeCheckingVisitor.java:3904) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.findMethodOrFail(StaticTypeCheckingVisitor.java:3662) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.findMethodOrFail(StaticCompilationVisitor.java:412) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorCallExpression(StaticTypeCheckingVisitor.java:1917) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitConstructorCallExpression(StaticCompilationVisitor.java:372) at 
	 org.codehaus.groovy.ast.expr.ConstructorCallExpression.visit(ConstructorCallExpression.java:46) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBinaryExpression(StaticTypeCheckingVisitor.java:571) at 
	 org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:51) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:74) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:281) at 
	 org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:40) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:251) at 
	 org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitIfElse(StaticTypeCheckingVisitor.java:3232) at 
	 org.codehaus.groovy.ast.stmt.IfStatement.visit(IfStatement.java:43) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:40) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:251) at 
	 org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitForLoop(CodeVisitorSupport.java:46) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitForLoop(ClassCodeVisitorSupport.java:286) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitForLoop(StaticTypeCheckingVisitor.java:1621) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitForLoop(StaticCompilationVisitor.java:398) at 
	 org.codehaus.groovy.ast.stmt.ForStatement.visit(ForStatement.java:49) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:40) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:251) at 
	 org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:148) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:159) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorOrMethod(StaticTypeCheckingVisitor.java:1816) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:170) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.startMethodInference(StaticTypeCheckingVisitor.java:2151) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitMethod(StaticTypeCheckingVisitor.java:2110) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitMethod(StaticCompilationVisitor.java:185) at 
	 org.codehaus.groovy.transform.sc.StaticCompileTransformation.visit(StaticCompileTransformation.java:76) at 
	 org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:147) at 
	 org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:214) at 
	 org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1131) at 
	 org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:620) at 
	 org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:598) at 
	 org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.processToPhase(GroovyCompilationUnitDeclaration.java:
	 218) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.generateCode(GroovyCompilationUnitDeclaration.java:322) 
	 at org.eclipse.jdt.internal.compiler.Compiler.resolve(Compiler.java:1052) at org.eclipse.jdt.internal.compiler.Compiler.resolve(Compiler.java:
	 1091) at org.eclipse.jdt.internal.core.CompilationUnitProblemFinder.process(CompilationUnitProblemFinder.java:219) at 
	 org.eclipse.jdt.internal.core.CompilationUnitProblemFinder.process(CompilationUnitProblemFinder.java:288) at 
	 org.codehaus.jdt.groovy.model.GroovyReconcileWorkingCopyOperation.makeConsistent(GroovyReconcileWorkingCopyOperation.java:80) at 
	 org.eclipse.jdt.internal.core.ReconcileWorkingCopyOperation.executeOperation(ReconcileWorkingCopyOperation.java:90) at 
	 org.eclipse.jdt.internal.core.JavaModelOperation.run(JavaModelOperation.java:724) at 
	 org.eclipse.jdt.internal.core.JavaModelOperation.runOperation(JavaModelOperation.java:790) at 
	 org.codehaus.jdt.groovy.model.GroovyCompilationUnit.reconcile(GroovyCompilationUnit.java:431) at 
	 org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:128) at 
	 org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.access$0(JavaReconcilingStrategy.java:110) at 
	 org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy$1.run(JavaReconcilingStrategy.java:90) at 
	 org.eclipse.core.runtime.SafeRunner.run(SafeRunner.java:42) at 
	 org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:87) at 
	 org.eclipse.jdt.internal.ui.text.java.JavaReconcilingStrategy.reconcile(JavaReconcilingStrategy.java:154) at 
	 org.eclipse.jdt.internal.ui.text.CompositeReconcilingStrategy.reconcile(CompositeReconcilingStrategy.java:89) at 
	 org.eclipse.jdt.internal.ui.text.JavaCompositeReconcilingStrategy.reconcile(JavaCompositeReconcilingStrategy.java:104) at 
	 org.eclipse.jface.text.reconciler.MonoReconciler.process(MonoReconciler.java:73) at org.eclipse.jface.text.reconciler.AbstractReconciler
	 $BackgroundThread.run(AbstractReconciler.java:207)
	- General error during instruction selection: java.lang.IllegalStateException java.lang.IllegalStateException at 
	 org.eclipse.jdt.internal.compiler.lookup.SourceTypeBinding.syntheticMethods(SourceTypeBinding.java:2468) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.initializeMembers(JDTClassNode.java:301) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.initialize(JDTClassNode.java:228) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.JDTClassNode.lazyClassInit(JDTClassNode.java:159) at 
	 org.codehaus.groovy.ast.ClassNode.getUnresolvedSuperClass(ClassNode.java:1124) at 
	 org.codehaus.groovy.ast.ClassNode.getUnresolvedSuperClass(ClassNode.java:1114) at 
	 org.codehaus.groovy.ast.ClassNode.getSuperClass(ClassNode.java:1108) at 
	 org.codehaus.groovy.ast.ClassNode.isDerivedFrom(ClassNode.java:1046) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.isAssignableTo(StaticTypeCheckingSupport.java:382) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.allParametersAndArgumentsMatch(StaticTypeCheckingSupport.java:251) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.measureParametersAndArgumentsDistance(StaticTypeCheckingSupport.java:
	 1032) at org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.chooseBestMethod(StaticTypeCheckingSupport.java:1001) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingSupport.findDGMMethodsByNameAndArguments(StaticTypeCheckingSupport.java:
	 926) at org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.findMethod(StaticTypeCheckingVisitor.java:3904) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.findMethodOrFail(StaticTypeCheckingVisitor.java:3662) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.findMethodOrFail(StaticCompilationVisitor.java:412) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorCallExpression(StaticTypeCheckingVisitor.java:1917) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitConstructorCallExpression(StaticCompilationVisitor.java:372) at 
	 org.codehaus.groovy.ast.expr.ConstructorCallExpression.visit(ConstructorCallExpression.java:46) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitBinaryExpression(StaticTypeCheckingVisitor.java:571) at 
	 org.codehaus.groovy.ast.expr.BinaryExpression.visit(BinaryExpression.java:51) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitExpressionStatement(CodeVisitorSupport.java:74) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitExpressionStatement(ClassCodeVisitorSupport.java:281) at 
	 org.codehaus.groovy.ast.stmt.ExpressionStatement.visit(ExpressionStatement.java:42) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:40) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:251) at 
	 org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitIfElse(StaticTypeCheckingVisitor.java:3232) at 
	 org.codehaus.groovy.ast.stmt.IfStatement.visit(IfStatement.java:43) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:40) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:251) at 
	 org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitForLoop(CodeVisitorSupport.java:46) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitForLoop(ClassCodeVisitorSupport.java:286) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitForLoop(StaticTypeCheckingVisitor.java:1621) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitForLoop(StaticCompilationVisitor.java:398) at 
	 org.codehaus.groovy.ast.stmt.ForStatement.visit(ForStatement.java:49) at 
	 org.codehaus.groovy.ast.CodeVisitorSupport.visitBlockStatement(CodeVisitorSupport.java:40) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitBlockStatement(ClassCodeVisitorSupport.java:251) at 
	 org.codehaus.groovy.ast.stmt.BlockStatement.visit(BlockStatement.java:71) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitClassCodeContainer(ClassCodeVisitorSupport.java:148) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitConstructorOrMethod(ClassCodeVisitorSupport.java:159) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitConstructorOrMethod(StaticTypeCheckingVisitor.java:1816) at 
	 org.codehaus.groovy.ast.ClassCodeVisitorSupport.visitMethod(ClassCodeVisitorSupport.java:170) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.startMethodInference(StaticTypeCheckingVisitor.java:2151) at 
	 org.codehaus.groovy.transform.stc.StaticTypeCheckingVisitor.visitMethod(StaticTypeCheckingVisitor.java:2110) at 
	 org.codehaus.groovy.transform.sc.StaticCompilationVisitor.visitMethod(StaticCompilationVisitor.java:185) at 
	 org.codehaus.groovy.transform.sc.StaticCompileTransformation.visit(StaticCompileTransformation.java:76) at 
	 org.codehaus.groovy.transform.ASTTransformationVisitor.visitClass(ASTTransformationVisitor.java:147) at 
	 org.codehaus.groovy.transform.ASTTransformationVisitor$2.call(ASTTransformationVisitor.java:214) at 
	 org.codehaus.groovy.control.CompilationUnit.applyToPrimaryClassNodes(CompilationUnit.java:1131) at 
	 org.codehaus.groovy.control.CompilationUnit.doPhaseOperation(CompilationUnit.java:620) at 
	 org.codehaus.groovy.control.CompilationUnit.processPhaseOperations(CompilationUnit.java:598) at 
	 org.codehaus.groovy.control.CompilationUnit.compile(CompilationUnit.java:575) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.processToPhase(GroovyCompilationUnitDeclaration.java:
	 218) at 
	 org.codehaus.jdt.groovy.internal.compiler.ast.GroovyCompilationUnitDeclaration.generateCode(GroovyCompilationUnitDeclaration.java:322) 
	 at org.eclipse.jdt.internal.compiler.Compiler.process(Compiler.java:891) at 
	 org.eclipse.jdt.internal.compiler.ProcessTaskManager.run(ProcessTaskManager.java:141) at java.lang.Thread.run(Thread.java:745)

I tried to create a small example to reproduce the problem, but the small example was too small to trigger the problem.

@eric-milles eric-milles changed the title Compiler error for function with static return type. Compiler error for function with static return type Jun 13, 2017
@eric-milles
Copy link
Member

eric-milles commented Jun 13, 2017

Are you using @TypeChecked or @CompileStatic?

@eaaltonen
Copy link
Author

Yes, all but one method was annotated with @CompileStatic, and removing the annotations did also remove the error.

I would prefer to keep the annotation thought, if it improves performance.

@eric-milles
Copy link
Member

I'm just asking because it helps in creating a smaller example that recreates the problem. Can you show the class structure? That is take out all the method bodies? Sometimes something like an inner class or an overloaded method or a varargs param triggers the issue.

@eaaltonen
Copy link
Author

I started reducing the code until I noticed that an annotation in a constructor was also required to trigger the issue. Following that, I managed to produce this example that recreates the problem
https://github.com/eaaltonen/eclipse-groovy-compiler-problem/blob/master/src/main/groovy/Library.groovy

@eric-milles
Copy link
Member

eric-milles commented Jun 16, 2017 via email

@eaaltonen
Copy link
Author

@eric-milles The @NonNull comes from checker-framework defined in the build.gradle, which I've now added.

I can reproduce the problem both with compile and compileOnly scopes.

@eric-milles
Copy link
Member

eric-milles commented Jun 21, 2017 via email

@eaaltonen
Copy link
Author

No. gradle build runs without issues and the code also works as intended at runtime.

@eric-milles
Copy link
Member

@eaaltonen Sorry I lost track of this issue. Is this still reproducible with the current snapshot.

@eric-milles
Copy link
Member

@eaaltonen In this particular case, I think @NonNull is being used incorrectly. I discovered this when I changed my test projects to Java 7 and I got an error of a type annotation. Your application of NonNull in JavaClass is not protecting the parameter a. This is because NonNull is targeted at types, so it is actually applied to the Object type.

package mypack;
import org.checkerframework.checker.nullness.qual.NonNull;
public class JavaClass {
    public JavaClass(@NonNull OtherClass a, int b) {
    }
}
@Target({ElementType.TYPE_USE, ElementType.TYPE_PARAMETER})
public @interface NonNull {}

A typical nullness annotation that I have seen before looks like this:

package edu.umd.cs.findbugs.annotations;
...
@Target({ElementType.FIELD, ElementType.METHOD, ElementType.PARAMETER, ElementType.LOCAL_VARIABLE})
public @interface NonNull {}

Ultimately, I think this issue is a case of annotation with runtime retention but has not been supplied on the classpath. The type system of JDT is trying to resolve all referenced types and cannot find NonNull.

@eaaltonen
Copy link
Author

@eric-milles

Your application of NonNull in JavaClass is not protecting the parameter a. This is because NonNull is targeted at types, so it is actually applied to the Object type.

To my understanding, the annotation is saying that "the value of parameter a must never be null".

This is supported by the example at https://checkerframework.org/manual/#example-use
Further, an article on JSR 308
http://www.oracle.com/technetwork/articles/java/ma14-architect-annotations-2177655.html
says the following:

If the type annotation is to target a type parameter, the annotation must contain the following meta-annotation:

@target
(ElementType.TYPE_PARAMETER)

which the @Nonnull annotation has. So I believe the usage is correct for Java 8.

@eaaltonen
Copy link
Author

As for the error message, it is no longer present in either the actual code or the example problem.

Current installed plugin versions are:

  • Groovy-Eclipse Feature 2.9.2.xx-201707291445-e46
  • JDT Core patch for Groovy-Eclipse plugin on Eclipse 4.6 2.9.2.xx-201707291445-e46

so, all good now.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants