eclipse-jdt / eclipse.jdt.core

Eclipse Public License 2.0
159 stars 129 forks source link

Occasional NPE dereferencing variable bindings/element inside a lambda #2269

Open mickaelistria opened 6 months ago

mickaelistria commented 6 months ago

Add this to ASTConverter18Tests, and watch it fail with a NPE

// Inspired from ResolveTests18.test429934
public void test429934_DOM() throws CoreException {
    this.workingCopy = getWorkingCopy("/Converter18/src/X.java", true/*resolve*/);
    String contents = """
            interface Function<T, R> {
               R apply(T t);
            }
            public class X {
                public static void main(String[] args) {
                    Function<String, String> f= (String s) -> s;
                }
            }
            """;

    String selection = "s";
    int start = contents.lastIndexOf(selection);
    int length = selection.length();

    Name name = (Name)NodeFinder.perform(buildAST(contents, this.workingCopy, false), start, length);
    IVariableBinding variableBinding = (IVariableBinding)name.resolveBinding();
    assertTrue(variableBinding.getJavaElement() instanceof ILocalVariable);
}

throws

java.lang.NullPointerException: Cannot invoke "org.eclipse.jdt.internal.compiler.lookup.TypeBinding.original()" because "lambdaExpression.resolvedType" is null
    at org.eclipse.jdt.internal.core.LambdaExpression.findLambdaSuperType(LambdaExpression.java:66)
    at org.eclipse.jdt.internal.core.LambdaExpression.<init>(LambdaExpression.java:57)
    at org.eclipse.jdt.internal.core.LambdaFactory.createLambdaExpression(LambdaFactory.java:30)
    at org.eclipse.jdt.core.dom.MethodBinding$LambdaMethod.getUnresolvedJavaElement(MethodBinding.java:629)
    at org.eclipse.jdt.core.dom.MethodBinding.getJavaElement(MethodBinding.java:304)
    at org.eclipse.jdt.core.dom.VariableBinding.getUnresolvedJavaElement(VariableBinding.java:296)
    at org.eclipse.jdt.core.dom.VariableBinding.getJavaElement(VariableBinding.java:161)
    at org.eclipse.jdt.core.tests.dom.ASTConverter18Test.test429934_DOM(ASTConverter18Test.java:5506)
jukzi commented 6 months ago

i can reproduce the NPE but note, that the java snippet does not compile anyway image

did you mean (String s) -> s;?