scip-java icon indicating copy to clipboard operation
scip-java copied to clipboard

semanticdb-javac terminates in error "silently", due to a NullPointerException

Open spangaer opened this issue 11 months ago • 3 comments

Platform: VSCode/Metals/Bloop on Windows 10 with Java 8 GraalVM 20

This issue is a propagation of https://github.com/scalameta/metals/issues/6191

It looks like it went sour in:

  • https://github.com/sourcegraph/scip-java/compare/v0.9.3...v0.9.4
  • https://github.com/sourcegraph/scip-java/pull/618

Still broken against v0.9.9 against which I did my debugging below.

It seems to be triggered by an annotation on a local variable

  private <T> Function<InvocationHandler, T> getConstructor(Class<T> contract) {
    @SuppressWarnings("unchecked")
    Constructor<T> constructor = (Constructor<T>) 

Which throws a NullPointerException here

JavacTrees.getElement(TreePath) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\api\JavacTrees.java:319)
JavacTrees.getElement(TreePath) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\api\JavacTrees.java:119)
SemanticdbTrees.annotationBuilder(AnnotationTree) (Unknown Source:87)
SemanticdbTrees.annotations(Tree) (Unknown Source:71)
SemanticdbVisitor.emitSymbolInformation(Element,Tree) (Unknown Source:134)
SemanticdbVisitor.emitSymbolOccurrence(Element,Tree,Name,Semanticdb$SymbolOccurrence$Role,CompilerRange) (Unknown Source:114)
SemanticdbVisitor.resolveVariableTree(VariableTree,TreePath) (Unknown Source:282)
SemanticdbVisitor.resolveNodes() (Unknown Source:209)
SemanticdbVisitor.buildTextDocument(CompilationUnitTree) (Unknown Source:94)
SemanticdbTaskListener.onFinishedAnalyze(TaskEvent) (Unknown Source:96)
SemanticdbTaskListener.finished(TaskEvent) (Unknown Source:63)
ClientCodeWrapper$WrappedTaskListener.finished(TaskEvent) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\api\ClientCodeWrapper.java:681)
MultiTaskListener.finished(TaskEvent) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\api\MultiTaskListener.java:111)
JavaCompiler.flow(Env,Queue) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\JavaCompiler.java:1342)
JavaCompiler.flow(Env) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\JavaCompiler.java:1296)
JavaCompiler.compile2() (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\JavaCompiler.java:901)
JavaCompiler.compile(List,List,Iterable) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\JavaCompiler.java:860)
Main.compile(String[],String[],Context,List,Iterable) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\Main.java:523)
Main.compile(String[],Context,List,Iterable) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\Main.java:381)
Main.compile(String[],Context) (c:\Program Files\Java\graalvm-ce-java8-20.3.3\src\com\sun\tools\javac\main\Main.java:370)

JavacTrees


    public Symbol getElement(TreePath path) {
        JCTree tree = (JCTree) path.getLeaf();

Because here https://github.com/sourcegraph/scip-java/blob/e4c4ffb42e11cf8bab94e8d038c42f939e6b3d27/semanticdb-javac/src/main/java/com/sourcegraph/semanticdb_javac/SemanticdbTrees.java#L87

variableTreePath ends up being null.

For some reason the new construct cannot cope with annotations on local variables. If I move the annotation to the method level it seems to get past the null pointer problem.

Why it fails on that exact instance is unclear, I have different java codebases where I use it on locals, fields or classes where I didn't see the issue surface. Perhaps it has to do with the method being private (e.g. the variable was named local14 in the tree model).

More I wasn't able to determine whilst debugging on the release version of the Javac plugin. I think I collected al relevant info. So far I still have the break on the code line just before the NullPointerException occurs should I need to attempt to gather anything else.

spangaer avatar Mar 26 '24 12:03 spangaer

Reproducer: https://github.com/spangaer/semanticdb-nullpointer

spangaer avatar Mar 27 '24 14:03 spangaer

@spangaer does changing the annotation to include the parameter name fix this issue? e.g. @SuppressWarnings(value = "unchecked")

Arthurm1 avatar Apr 05 '24 17:04 Arthurm1

Yes:

  1. Putting it on the method level w.o. the a named param works: https://github.com/spangaer/semanticdb-nullpointer/commit/0278db4ade0a62ef0691b408b7eaf1544b815595
  2. Putting a named param works too it seems: https://github.com/spangaer/semanticdb-nullpointer/commit/ce068b0e2d139c1bc8611aa7a1d1633b8de2a110

spangaer avatar Apr 06 '24 16:04 spangaer