Watch, Follow, &
Connect with Us
Public Report
Report From: Delphi-BCB/Compiler/Delphi/Anonymous Methods    [ Add a report in this area ]  
Report #:  68881   Status: Closed
Incorrect codegen for anonymous method / generics
Project:  Delphi Build #:  12.0.3210.17555
Version:    12.0 Submitted By:   Eric Grange
Report Type:  Crash / Data loss / Total failure Date Reported:  11/13/2008 5:32:45 AM
Severity:    Critical / Show Stopper Last Updated: 3/20/2012 2:24:39 AM
Platform:    All platforms Internal Tracking #:   266623
Resolution: Fixed (Resolution Comments) Resolved in Build: : 12.0.3266.18473
Duplicate of:  None
Voting and Rating
Overall Rating: (1 Total Rating)
5.00 out of 5
Total Votes: 10
Description
The following code demonstrates an incorrect codegen on the "FDataObject.FieldSet:=True;" assignment, which written over the "Field" field as if its length was 1 byte (it's a 4 bytes integer).
The issue seems to happen between generics and anonymous functions, if the assignment code is copied to the StoreData body, its codegen will be correct, only the version in the anonymous method body seems affected.
Steps to Reproduce:
Use the following code.
Displayed value will be 466 instead of 1234.
Breakpoint on the "FDataObject.FieldSet:=True;" line and go to asm view, the offset is incorrect.


type
    TAnonymousMethod = reference to procedure;
    TAnonymousFunction<T> = reference to function : T;

    TDataObject<T> = class
       Field : T;
       FieldSet : Boolean;
    end;

    TSomeObject<T> = class
       FDataObject : TDataObject<T>;
       function StoreData(func : TAnonymousFunction<T>) : TAnonymousMethod;
    end;

function TSomeObject<T>.StoreData(func : TAnonymousFunction<T>) :
TAnonymousMethod;
begin
    FDataObject:=TDataObject<T>.Create;
    result:=procedure
            begin
               FDataObject.Field:=func();
               FDataObject.FieldSet:=True; // comment out to get 1234
            end;
end;

procedure TForm1.Button2Click(Sender: TObject);
var
    someObject : TSomeObject<Integer>;
    myAnonMethod : TAnonymousMethod;
begin
    someObject:=TSomeObject<Integer>.Create;
    myAnonMethod:=someObject.StoreData(function : Integer
                                       begin
                                          Result:=1234;
                                       end);
    myAnonMethod(); // execute it

    Caption:=IntToStr(someObject.FDataObject.Field); // shows 466
    someObject.Free;
end;
Workarounds
None
Attachment
None
Comments

Eric Grange at 11/13/2008 6:30:49 AM -
Apparently the TAnonymousFunction<T> parameter and assignment in the sample code are not needed to reproduce the issue.

Further details may be found in
https://forums.codegear.com/thread.jspa?threadID=6935

Server Response from: ETNACODE01