Skip to content

Commit 4017843

Browse files
cirrasfourls
authored andcommitted
Improve type resolution for multidimensional constant array expressions
1 parent c130158 commit 4017843

File tree

2 files changed

+29
-3
lines changed

2 files changed

+29
-3
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
6363
- Name resolution failures on invocations of methods with generic open array parameters.
6464
- Name resolution failures around `Create` calls on types with `constructor` constraints.
6565
- Name resolution failures on `read`, `write`, and `stored` specifiers of indexed properties.
66+
- Type resolution failures on array expressions nested within multidimensional constant arrays.
6667
- Incorrect file position calculation for multiline string tokens.
6768
- Analysis errors around `type of` type declarations.
6869

delphi-frontend/src/main/java/au/com/integradev/delphi/antlr/ast/node/ArrayExpressionNodeImpl.java

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,13 @@
2626
import javax.annotation.Nonnull;
2727
import org.antlr.runtime.Token;
2828
import org.sonar.plugins.communitydelphi.api.ast.ArrayExpressionNode;
29+
import org.sonar.plugins.communitydelphi.api.ast.DelphiNode;
2930
import org.sonar.plugins.communitydelphi.api.ast.ExpressionNode;
31+
import org.sonar.plugins.communitydelphi.api.ast.TypeDeclarationNode;
3032
import org.sonar.plugins.communitydelphi.api.type.Type;
33+
import org.sonar.plugins.communitydelphi.api.type.Type.CollectionType;
3134
import org.sonar.plugins.communitydelphi.api.type.TypeFactory;
35+
import org.sonar.plugins.communitydelphi.api.type.Typed;
3236

3337
public final class ArrayExpressionNodeImpl extends ExpressionNodeImpl
3438
implements ArrayExpressionNode {
@@ -65,11 +69,32 @@ public String getImage() {
6569
@Override
6670
@Nonnull
6771
protected Type createType() {
72+
int nestingLevel = 0;
73+
DelphiNode parent = this;
74+
do {
75+
++nestingLevel;
76+
parent = parent.getParent();
77+
} while (parent instanceof ArrayExpressionNode);
78+
6879
Type elementType = TypeFactory.unknownType();
69-
List<ExpressionNode> elements = getElements();
70-
if (!elements.isEmpty()) {
71-
elementType = elements.get(0).getType();
80+
if (parent instanceof Typed && !(parent instanceof TypeDeclarationNode)) {
81+
elementType = ((Typed) parent).getType();
82+
for (int i = 0; i < nestingLevel; ++i) {
83+
if (!(elementType instanceof CollectionType)) {
84+
elementType = TypeFactory.unknownType();
85+
break;
86+
}
87+
elementType = ((CollectionType) elementType).elementType();
88+
}
89+
}
90+
91+
if (elementType.isUnknown()) {
92+
List<ExpressionNode> elements = getElements();
93+
if (!elements.isEmpty()) {
94+
elementType = elements.get(0).getType();
95+
}
7296
}
97+
7398
return ((TypeFactoryImpl) getTypeFactory()).array(null, elementType, Set.of(ArrayOption.FIXED));
7499
}
75100
}

0 commit comments

Comments
 (0)