Skip to content

Commit 65fae3a

Browse files
committed
Improve type resolution for multidimensional constant array expressions
1 parent c130158 commit 65fae3a

File tree

2 files changed

+28
-3
lines changed

2 files changed

+28
-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: 27 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -26,9 +26,12 @@
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;
3031
import org.sonar.plugins.communitydelphi.api.type.Type;
32+
import org.sonar.plugins.communitydelphi.api.type.Type.CollectionType;
3133
import org.sonar.plugins.communitydelphi.api.type.TypeFactory;
34+
import org.sonar.plugins.communitydelphi.api.type.Typed;
3235

3336
public final class ArrayExpressionNodeImpl extends ExpressionNodeImpl
3437
implements ArrayExpressionNode {
@@ -65,11 +68,32 @@ public String getImage() {
6568
@Override
6669
@Nonnull
6770
protected Type createType() {
71+
int nestingLevel = 0;
72+
DelphiNode parent = this;
73+
do {
74+
++nestingLevel;
75+
parent = parent.getParent();
76+
} while (parent instanceof ArrayExpressionNode);
77+
6878
Type elementType = TypeFactory.unknownType();
69-
List<ExpressionNode> elements = getElements();
70-
if (!elements.isEmpty()) {
71-
elementType = elements.get(0).getType();
79+
if (parent instanceof Typed) {
80+
elementType = ((Typed) parent).getType();
81+
for (int i = 0; i < nestingLevel; ++i) {
82+
if (!(elementType instanceof CollectionType)) {
83+
elementType = TypeFactory.unknownType();
84+
break;
85+
}
86+
elementType = ((CollectionType) elementType).elementType();
87+
}
88+
}
89+
90+
if (elementType.isUnknown()) {
91+
List<ExpressionNode> elements = getElements();
92+
if (!elements.isEmpty()) {
93+
elementType = elements.get(0).getType();
94+
}
7295
}
96+
7397
return ((TypeFactoryImpl) getTypeFactory()).array(null, elementType, Set.of(ArrayOption.FIXED));
7498
}
7599
}

0 commit comments

Comments
 (0)