INRIA / INRIA/spoon

Spoon hangs when building a model when nested conditional expressions containing a pattern matching with variable creation

Open
#5,511 0 comments 0 reactions 0 assignees View on GitHub
bug
Dominant language
Java
Stars
2k
Forks
392
Avg merge
11h 24m
Merged PRs (30d)
36

Description

### Describe the bug

I'm from [PVS-Studio LLC](https://pvs-studio.com/en/). We develop a static analyzer for Java code that uses [the Spoon library](https://github.com/INRIA/spoon) (we use the 10.3.0 version). Spoon uses org.eclipse.jdt.core 3.32.0 library.

When such code is evaluated via Spoon (a compliance level equal to 17 is used) the org.eclipse.jdt.internal.compiler.ast.Statement class hangs on a method Statement.addPatternVariables(LocalVariableBinding[] current, LocalVariableBinding add).

We have already created a bug [Issue ](https://github.com/eclipse-jdt/eclipse.jdt.core/issues/1485) in the [Eclipse Core Github repository](https://github.com/eclipse-jdt/eclipse.jdt.core).

Here is a StackTrace of the hang:
```
at org.eclipse.jdt.internal.compiler.ast.Statement.addPatternVariables(Statement.java:522)
at org.eclipse.jdt.internal.compiler.ast.Statement.addPatternVariables(Statement.java:507)
at org.eclipse.jdt.internal.compiler.ast.Statement.addPatternVariablesWhenTrue(Statement.java:495)
at org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression.collectPatternVariablesToScope(AND_AND_Expression.java:284)
at org.eclipse.jdt.internal.compiler.ast.AND_AND_Expression.collectPatternVariablesToScope(AND_AND_Expression.java:281)
at org.eclipse.jdt.internal.compiler.ast.ConditionalExpression.collectPatternVariablesToScope(ConditionalExpression.java:460)
at org.eclipse.jdt.internal.compiler.ast.ConditionalExpression.resolveType(ConditionalExpression.java:535)
at org.eclipse.jdt.internal.compiler.ast.ConditionalExpression.resolveType(ConditionalExpression.java:546)
at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.resolve(LocalDeclaration.java:390)
at org.eclipse.jdt.internal.compiler.ast.LocalDeclaration.resolve(LocalDeclaration.java:259)
at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolveStatements(AbstractMethodDeclaration.java:662)
at org.eclipse.jdt.internal.compiler.ast.MethodDeclaration.resolveStatements(MethodDeclaration.java:388)
at org.eclipse.jdt.internal.compiler.ast.AbstractMethodDeclaration.resolve(AbstractMethodDeclaration.java:571)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1503)
at org.eclipse.jdt.internal.compiler.ast.TypeDeclaration.resolve(TypeDeclaration.java:1628)
at org.eclipse.jdt.internal.compiler.ast.CompilationUnitDeclaration.resolve(CompilationUnitDeclaration.java:667)
at spoon.support.compiler.jdt.TreeBuilderCompiler.buildUnits(TreeBuilderCompiler.java:105)
at spoon.support.compiler.jdt.JDTBatchCompiler.getUnits(JDTBatchCompiler.java:282)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnits(JDTBasedSpoonCompiler.java:418)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildUnitsAndModel(JDTBasedSpoonCompiler.java:370)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.buildSources(JDTBasedSpoonCompiler.java:336)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:117)
at spoon.support.compiler.jdt.JDTBasedSpoonCompiler.build(JDTBasedSpoonCompiler.java:100)
at spoon.Launcher.buildModel(Launcher.java:782)
at com.pvsstudio.projects.Module.build(Module.java:428)
at com.pvsstudio.runner.ModuleModelBuilder.createModuleWorkerWithBuiltModelAndAnalyzeTasksForModule(ModuleModelBuilder.java:136)
at com.pvsstudio.runner.AnalyzerRunner.run(AnalyzerRunner.java:93)
at com.pvsstudio.Main.main(Main.java:575)
```

### Source code you are trying to analyze/transform

```Java
public class Main {
static class PvsVariable {
}

static class PvsVariableNext_1 extends PvsVariable {
}

static class PvsVariableNext_2 extends PvsVariableNext_1 {
}

static class PvsVariableNext_3 extends PvsVariableNext_2 {
}

static class PvsVariableNext_4 extends PvsVariableNext_3 {
}

public static void main(String[] args) {
final var pvsVariable = new PvsVariable();
final Object origVar =
pvsVariable instanceof PvsVariableNext_1 var_first
? null
: (pvsVariable instanceof PvsVariableNext_2 var_second &&
pvsVariable instanceof PvsVariableNext_3 var_three &&
true) ? null
: null;
}
}
```

### Source code for your Spoon processing

_No response_

### Actual output

_No response_

### Expected output

_No response_

### Spoon Version

10.3.0

### JVM Version

JDK 17

### What operating system are you using?

Windows, Linux, macOS

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.