integrated-application-development / sonar-delphi

Delphi language plugin for SonarQube
GNU Lesser General Public License v3.0
104 stars 17 forks source link

Ambiguous declarations erroneously found for `string` and `array of Char` parameter types #285

Open zaneduffield opened 3 months ago

zaneduffield commented 3 months ago

Prerequisites

SonarDelphi version

1.6.0

SonarQube version

No response

Issue description

sonar-delphi fails to resolve the correct overload when a single-character string is passed to a function with overloads for both string and array of Char as the relevant parameter.

I noticed this because a warning is logged when scanning JclStrings, and I reduced it to the minimal reproducible example.

Steps to reproduce

procedure Foo(Value: array of Char); overload; begin end;
procedure Foo(Value: string); overload; begin end;

begin
  Foo('a');  // [WARN] Ambiguous declarations could not be resolved
  Foo('aa'); // no warning logged
end.

Minimal Delphi code exhibiting the issue

No response

zaneduffield commented 3 months ago

I should note that the 'a' value isn't even compatible with the array of Char overload.

zaneduffield commented 3 months ago

It seems to be related specifically to the open array parameter; if you replace array of Char with a type equivalent to array[0..1] of Char then sonar-delphi doesn't warn about the ambiguity (and the 'a' literal is compatible with both, but prefers the string overload).

Cirras commented 3 months ago

Constant values (like text literals) cannot be used as arguments to open array parameters.

The compiler error is: E2192 Constants cannot be used as open array arguments

SonarDelphi isn't currently emulating this restriction correctly. This would be very easy to fix if it was limited to literals (and we might hack an incomplete fix targeting literals), but it applies to all constant expressions. The correct fix is blocked by #116.