Initialisation of Delphi Record containing a dynamic array with 'Implicit' class operator Initialisation of Delphi Record containing a dynamic array with 'Implicit' class operator arrays arrays

Initialisation of Delphi Record containing a dynamic array with 'Implicit' class operator


Looks like you found a bug with the codegen there (and it also exists in the Win64 Compiler). I looked through the generated asm and it seems the compiler produces a wrong instruction for the operator overload instead. That is why wrong values end up in the array inside the operator overload. Please report this in Quality Portal.

Code generated for the bad result:

Project109.dpr.46: r3 := iArray;0040B1F2 A1FC044100       mov eax,[$004104fc]0040B1F7 8945E8           mov [ebp-$18],eax0040B1FA 837DE800         cmp dword ptr [ebp-$18],$000040B1FE 740B             jz $0040b20b0040B200 8B45E8           mov eax,[ebp-$18]0040B203 83E804           sub eax,$040040B206 8B00             mov eax,[eax]0040B208 8945E8           mov [ebp-$18],eax0040B20B 8D4DD8           lea ecx,[ebp-$28]0040B20E 8B55E8           mov edx,[ebp-$18]0040B211 4A               dec edx0040B212 B8FC044100       mov eax,$004104fc // <-- wrong one0040B217 E87CF5FFFF       call TRec.&op_Implicit

Code for an equal method:

Project109.dpr.47: r3 := TRec.Implicit(iArray);0040B22F A1FC044100       mov eax,[$004104fc]0040B234 8945E4           mov [ebp-$1c],eax0040B237 837DE400         cmp dword ptr [ebp-$1c],$000040B23B 740B             jz $0040b2480040B23D 8B45E4           mov eax,[ebp-$1c]0040B240 83E804           sub eax,$040040B243 8B00             mov eax,[eax]0040B245 8945E4           mov [ebp-$1c],eax0040B248 8D4DD4           lea ecx,[ebp-$2c]0040B24B 8B55E4           mov edx,[ebp-$1c]0040B24E 4A               dec edx0040B24F A1FC044100       mov eax,[$004104fc] // <-- correct one0040B254 E8CFF5FFFF       call TRec.Implicit

However you can avoid this by adding another overload for the Implicit operator with the parameter type TArray<UInt64> and then also declare your local variable as that type so the compiler choses the correct overload (the one it does not generate wrong code for in this case).

But be aware that this will only work when you pass variables of the type TArray<UInt64> and call the wrong one when you have any other dynamic array of UInt64 because of Delphis strict type rules.

Update: This defect was reported in RSP-16084 and fixed in Delphi 10.2 Tokyo.