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

fix: fixed identifier correctness check when simplenamePart == "" #3550

Merged
merged 4 commits into from
Sep 11, 2020

Conversation

Strum355
Copy link
Contributor

@Strum355 Strum355 commented Aug 25, 2020

Exception when indexing https://github.com/google/gson library in module mode (with module-info.java and package-info.javas) with https://github.com/sourcegraph/lsif-java with #3549 applied.

Exception does not occur when the library is not a module (with module-info.java and package-info.javas removed) (Note, need to double check the behaviour in each scenario: gson-module+3549, gson-not-module+3549, gson-module, gson-not-module)

Occurs with TypeToken<List<String>[]> @ https://github.com/google/gson/blob/master/gson/src/test/java/com/google/gson/GenericArrayTypeTest.java#L45

Debug state screenshots for context:

Stacktrace
Exception in thread "main" java.lang.StringIndexOutOfBoundsException: String index out of range: 0
        at java.base/java.lang.StringLatin1.charAt(StringLatin1.java:48)
        at java.base/java.lang.String.charAt(String.java:711)
        at spoon.support.reflect.reference.CtReferenceImpl.checkIdentifierChars(CtReferenceImpl.java:129)
        at spoon.support.reflect.reference.CtReferenceImpl.checkAllParts(CtReferenceImpl.java:119)
        at spoon.support.reflect.reference.CtReferenceImpl.checkIdentiferForJLSCorrectness(CtReferenceImpl.java:107)
        at spoon.support.reflect.reference.CtReferenceImpl.setSimpleName(CtReferenceImpl.java:52)
        at spoon.support.compiler.jdt.ReferenceBuilder.getTypeParameterReference(ReferenceBuilder.java:738)
        at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:691)
        at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:637)
        at spoon.support.compiler.jdt.ReferenceBuilder.getTypeReference(ReferenceBuilder.java:519)
        at spoon.support.compiler.jdt.ReferenceBuilder.getExecutableReference(ReferenceBuilder.java:457)
        at spoon.support.compiler.jdt.JDTTreeBuilder.visit(JDTTreeBuilder.java:907)
        at org.eclipse.jdt.internal.compiler.ast.QualifiedAllocationExpression.traverse(QualifiedAllocationExpression.java:673)
        at org.eclipse.jdt.internal.compiler.ast.MessageSend.traverse(MessageSend.java:1110)
        at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.traverse(LocalDeclaration.java:480)
        at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.traverse(MethodDeclaration.java:365)
        at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.traverse(TypeDeclaration.java:1479)
        at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:826)
        at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.traverse(CompilationUnitDeclaration.java:787)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.lambda$buildModel$0(JDTBasedSpoonCompiler.java:437)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.forEachCompilationUnit(JDTBasedSpoonCompiler.java:466)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildModel(JDTBasedSpoonCompiler.java:435)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:372)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:337)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:114)
        at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:97)
        at spoon.Launcher.buildModel(Launcher.java:755)
        at ProjectIndexer.index(ProjectIndexer.java:44)
        at Main.main(Main.java:16)
Screenshots

image

image

@Strum355 Strum355 changed the title Fixed JSLCorrectness fix when simplenamePart == "" Fixed JSLCorrectness check when simplenamePart == "" Aug 25, 2020
@MartinWitt
Copy link
Collaborator

Good catch, haven't thought about this case while writing the code. Would you mind adding a new testcase in CorrectIdentifierTest

@Strum355
Copy link
Contributor Author

Annoyingly, the issue doesnt seem to occur when gson is not a module, with simplename never being == List<String>[], which gives me the impression that there is a more deep seated issue. I lack the knowledge of the codebase to speculate so I'm wondering if you've any ideas?

@Strum355
Copy link
Contributor Author

Heres the scenario matrix. Patched = with #3549 applied, zero check = with this PR applied, module = if gson has module-info.java and package-info.java

  • unpatched, zero check, module: Module should be known | expected, should be a module
  • unpatched, no check, module: Module should be known | expected, should be a module
  • patched, zero check, module: PartialSourcePosition only contains a CompilationUnit | probably expected
  • patched, no check, module: String index out of range: 0 | not expected
  • unpatched, zero check, not module: PartialSourcePosition only contains a CompilationUnit | probably expected
  • unpatched, no check, not module: PartialSourcePosition only contains a CompilationUnit | probably expected
  • patched, zero check, not module: PartialSourcePosition only contains a CompilationUnit | probably expected
  • patched, no check, not module: PartialSourcePosition only contains a CompilationUnit | probably expected

PartialSourcePosition only contains a CompilationUnit is an exception I havent come across before this PR and must investigate further (potential future PR), so I'll consider that as "progress". What I dont get is why the out of range only occurs when gson is a java9 module 🤔

@Strum355
Copy link
Contributor Author

Have discovered why I got PartialSourcePosition only contains a CompilationUnit, PR coming for that. Why index out of range happens only in module mode is still a mystery

@monperrus
Copy link
Collaborator

Thanks a lot for the analysis, this is something we've seen in the past.

OK, let's merge the other ones and come back to this one later.

@monperrus
Copy link
Collaborator

Thanks @Strum355. Could you add the corresponding test case?

@Strum355 Strum355 force-pushed the jlscorrect-fix branch 2 times, most recently from bc483e2 to 80d2ad6 Compare September 9, 2020 11:00
@Strum355
Copy link
Contributor Author

Strum355 commented Sep 9, 2020

@monperrus test case added 👍

@Test
public void correctSquareBrackets() {
CtLocalVariableReference<Object> localVariableRef = new Launcher().getFactory().createLocalVariableReference();
assertDoesNotThrow(() -> localVariableRef.setSimpleName("List<String>[]"));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

is that a valid name for a local variable reference? Maybe you mean a type reference instead?

Copy link
Collaborator

@MartinWitt MartinWitt Sep 9, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It isn't a allowed identifier for a local variable, must be more or less alphanumeric with some exceptions.
We have 1 check for all setSimpleName methods. Looking at the decision today i would prefer something like a visitor. Allowing different handling for the cases., like type or local variable.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

OK, @Strum355 could you create a type reference instead?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

My attempts have been unsuccessful unfortunately, I consistently get NPE as getPackage always returns null and Ive been unable to run the Launcher in classpath mode. Im afraid my knowledge of Spoon only goes sofar 😞

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed by CtTypeReference localVariableRef = new Launcher().getFactory().createTypeReference();

LGTM, OK to merge

@monperrus monperrus changed the title Fixed JSLCorrectness check when simplenamePart == "" fix: fixed identifier correctness check when simplenamePart == "" Sep 11, 2020
@monperrus monperrus merged commit a14923c into INRIA:master Sep 11, 2020
@monperrus
Copy link
Collaborator

Thanks @Strum355

@monperrus monperrus mentioned this pull request Oct 14, 2020
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

Successfully merging this pull request may close these issues.

3 participants