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

前端 未结 1 1005
无人及你
无人及你 2021-01-03 21:38

I\'m trying to figure out whether it is possible to initialise a record containing a dynamic array using the \"implicit\" class operator in Delphi (Berlin 10.1 upd 1)

<
相关标签:
1条回答
  • 2021-01-03 21:50

    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],eax
    0040B1FA 837DE800         cmp dword ptr [ebp-$18],$00
    0040B1FE 740B             jz $0040b20b
    0040B200 8B45E8           mov eax,[ebp-$18]
    0040B203 83E804           sub eax,$04
    0040B206 8B00             mov eax,[eax]
    0040B208 8945E8           mov [ebp-$18],eax
    0040B20B 8D4DD8           lea ecx,[ebp-$28]
    0040B20E 8B55E8           mov edx,[ebp-$18]
    0040B211 4A               dec edx
    0040B212 B8FC044100       mov eax,$004104fc // <-- wrong one
    0040B217 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],eax
    0040B237 837DE400         cmp dword ptr [ebp-$1c],$00
    0040B23B 740B             jz $0040b248
    0040B23D 8B45E4           mov eax,[ebp-$1c]
    0040B240 83E804           sub eax,$04
    0040B243 8B00             mov eax,[eax]
    0040B245 8945E4           mov [ebp-$1c],eax
    0040B248 8D4DD4           lea ecx,[ebp-$2c]
    0040B24B 8B55E4           mov edx,[ebp-$1c]
    0040B24E 4A               dec edx
    0040B24F A1FC044100       mov eax,[$004104fc] // <-- correct one
    0040B254 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.

    0 讨论(0)
提交回复
热议问题