Skip to content

Fix incorrect type resolution around array property accesses #323

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

Merged
merged 1 commit into from
Dec 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Various intrinsic routines had incorrect signatures around dynamic and open arrays.
- False positives around platform-dependent binary expressions in `PlatformDependentTruncation`.
- Incorrect type resolution around array property accesses.

## [1.12.0] - 2024-12-02

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -782,26 +782,31 @@ private void handleParenthesizedExpression(ParenthesizedExpressionNode parenthes
}
}

private boolean handleDefaultArrayProperties(ArrayAccessorNode accessor) {
if (!declarations.isEmpty()
&& declarations.stream().allMatch(NameResolver::isDefaultArrayProperty)) {
Type type = currentType;
if (type.isClassReference()) {
type = ((ClassReferenceType) type).classType();
} else if (type.isProcedural()) {
type = ((ProceduralType) type).returnType();
}
private void addExplicitDefaultArrayPropertyDeclarations() {
if (!declarations.stream().allMatch(NameResolver::isDefaultArrayProperty)) {
return;
}

if (type.isStruct()) {
StructType structType = (StructType) TypeUtils.findBaseType(type);
NameDeclaration declaration = Iterables.getLast(declarations);
((StructTypeImpl) structType)
.findDefaultArrayProperties().stream()
.filter(property -> property.getName().equalsIgnoreCase(declaration.getName()))
.forEach(declarations::add);
}
Type type = currentType;
if (type.isClassReference()) {
type = ((ClassReferenceType) type).classType();
} else if (type.isProcedural()) {
type = ((ProceduralType) type).returnType();
}

// An explicit array property access can be handled by argument disambiguation.
if (type.isStruct()) {
StructType structType = (StructType) TypeUtils.findBaseType(type);
NameDeclaration declaration = Iterables.getLast(declarations);
((StructTypeImpl) structType)
.findDefaultArrayProperties().stream()
.filter(property -> property.getName().equalsIgnoreCase(declaration.getName()))
.forEach(declarations::add);
}
}

private boolean handleDefaultArrayProperties(ArrayAccessorNode accessor) {
if (!declarations.isEmpty() && isArrayProperty(Iterables.getLast(declarations))) {
addExplicitDefaultArrayPropertyDeclarations();
return false;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -870,6 +870,20 @@ void testHiddenDefaultProperties() {
verifyUsages(11, 14, reference(27, 25));
}

@Test
void testArrayProperties() {
execute("properties/ArrayProperties.pas");
verifyUsages(9, 13, reference(29, 23));
verifyUsages(16, 10, reference(28, 2));
}

@Test
void testClassArrayProperties() {
execute("properties/ClassArrayProperties.pas");
verifyUsages(9, 13, reference(29, 24));
verifyUsages(16, 10, reference(28, 2));
}

@Test
void testDefaultArrayProperties() {
execute("properties/DefaultArrayProperties.pas");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
unit ArrayProperties;

interface

implementation

type
TStringHelper = record helper for string
function IsEmpty: Boolean;
end;

TFoo = class
property Baz[I: Integer]: string;
end;

procedure Consume(S: string);
begin
// do nothing
end;

procedure Consume(C: Char);
begin
// do nothing
end;

function Test(Foo: TFoo): Boolean;
begin
Consume(Foo.Baz[0]);
Result := Foo.Baz[0].IsEmpty;
end;

end.
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
unit ClassArrayProperties;

interface

implementation

type
TStringHelper = record helper for string
function IsEmpty: Boolean;
end;

TFoo = class
class property Baz[I: Integer]: string;
end;

procedure Consume(S: string);
begin
// do nothing
end;

procedure Consume(C: Char);
begin
// do nothing
end;

function Test(Foo: TFoo): Boolean;
begin
Consume(TFoo.Baz[0]);
Result := TFoo.Baz[0].IsEmpty;
end;

end.
Loading