Skip to content

Commit 41a1799

Browse files
cirrasfourls
authored andcommitted
Improve type comparisons between classes and interfaces
1 parent 74e970b commit 41a1799

File tree

3 files changed

+26
-1
lines changed

3 files changed

+26
-1
lines changed

CHANGELOG.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,11 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
77

88
## [Unreleased]
99

10+
### Fixed
11+
12+
- Inaccurate type comparisons between class and interface types, where class-to-class upcasts were
13+
not always preferred over class-to-interface upcasts.
14+
1015
## [1.18.0] - 2025-08-05
1116

1217
### Added

delphi-frontend/src/main/java/au/com/integradev/delphi/symbol/resolve/TypeComparer.java

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@
4848
import org.sonar.plugins.communitydelphi.api.type.Type.PointerType;
4949
import org.sonar.plugins.communitydelphi.api.type.Type.ProceduralType;
5050
import org.sonar.plugins.communitydelphi.api.type.Type.StringType;
51+
import org.sonar.plugins.communitydelphi.api.type.Type.StructType;
5152
import org.sonar.plugins.communitydelphi.api.type.Type.SubrangeType;
5253

5354
final class TypeComparer {
@@ -780,7 +781,11 @@ private static EqualityType compareParameters(List<Parameter> from, List<Paramet
780781

781782
private static EqualityType compareObject(Type from, Type to) {
782783
if (from.isStruct() && from.isDescendantOf(to)) {
783-
return CONVERT_LEVEL_1;
784+
if (((StructType) from).kind() == ((StructType) to).kind()) {
785+
return CONVERT_LEVEL_1;
786+
} else {
787+
return CONVERT_LEVEL_2;
788+
}
784789
} else if (from.isPointer() && !to.isRecord()) {
785790
PointerType fromPointer = (PointerType) from;
786791
if (fromPointer.isUntypedPointer()) {

delphi-frontend/src/test/java/au/com/integradev/delphi/symbol/resolve/InvocationResolverTest.java

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -439,6 +439,21 @@ void testInheritedTypes() {
439439
assertResolved(parent, grandparent, child);
440440
}
441441

442+
@Test
443+
void testClassToInterfaceTypes() {
444+
Type grandparent = TypeMocker.struct("Grandparent", CLASS);
445+
Type parent = TypeMocker.struct("Parent", CLASS, grandparent);
446+
Type child = TypeMocker.struct("Child", CLASS, parent);
447+
Type intf = TypeMocker.struct("Intf", INTERFACE);
448+
449+
when(child.ancestorList()).thenReturn(Set.of(parent, intf));
450+
when(child.isDescendantOf(grandparent)).thenReturn(true);
451+
when(child.isDescendantOf(parent)).thenReturn(true);
452+
when(child.isDescendantOf(intf)).thenReturn(true);
453+
454+
assertResolved(child, grandparent, intf);
455+
}
456+
442457
@Test
443458
void testVarParameters() {
444459
Type openArray =

0 commit comments

Comments
 (0)