Author Topic: Confusion with SETS while Translating Delphi code  (Read 765 times)

jamie

  • Hero Member
  • *****
  • Posts: 8071
Confusion with SETS while Translating Delphi code
« on: October 12, 2026, 08:21:46 pm »
Code: Pascal  [Select][+][-]
  1. unit Unit1;
  2.  
  3. {$mode Delphi}{$H+}
  4.  
  5. interface
  6.  
  7. uses
  8.   Classes, SysUtils, Forms, Controls, Graphics, Dialogs, StdCtrls;
  9.  
  10. type
  11.  TMasterSet = (one,two,three,four,five,six, seven, eight, nine, ten);
  12.  TFirstSet = Set of three..seven;
  13.  TSecondSet = Set of Eight..Ten;
  14.  
  15.   { TForm1 }
  16.  
  17.   TForm1 = class(TForm)
  18.     Button1: TButton;
  19.     procedure Button1Click(Sender: TObject);
  20.   private
  21.  
  22.   public
  23.  
  24.   end;
  25.  
  26. var
  27.   Form1: TForm1;
  28.  
  29. implementation
  30.  
  31. {$R *.lfm}
  32.  
  33. { TForm1 }
  34.  
  35. procedure TForm1.Button1Click(Sender: TObject);
  36. Var
  37.   S1:TFirstSet=[];
  38.   S2:TSecondSet = [Eight];
  39. begin
  40.   S1 := S1+[Eight];//This works.
  41.   S1 := S1+S2; //This works. Should it?
  42.   If S1 <> [] Then Beep; //Varifys the set isn't empty.
  43.   Include(S1, S2);// Generates internal error in project but type error in test?
  44.   Include(S1,Three); //Works.
  45. end;
  46.  
  47. end.
  48.  
  49.  
  50.  

I tried to create a test app to show the complete results of attempting to port an old Delphi program over, something small I found on the net and there are some strange usages of SETS.

I then translated the app, (Delphi mode), the include, will generate an internal compiler error but here for a simple test I could not get that part to fault however in the Delphi app the basic S1+S1+S2 works without any errors, as it does here, TOO!

 I can load the Delphi app up and get the exact internal error if needed, but it's based on the use of "include" and if you notice, the two sets are not the same, as I attempted to duplicate the situation in the Delphi app.

 Here, the compiler will catch that mismatch at the include but it won't with the S1+S1+S2;

FPC 3.2.4

Jamie

The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Re: Confusion with SETS while Translating Delphi code
« Reply #1 on: October 12, 2026, 09:02:23 pm »
Afaik in both FPC and Delphi this depends on range checking aka $R+ ?

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #2 on: October 12, 2026, 09:24:44 pm »
I didn't turn on Range check in the test app but I did have it on with the actual app and it does not matter.

Code: Pascal  [Select][+][-]
  1.     attrs := attrs+[t];
  2.     include(attrs,t); //internal compiler error;
  3.     case t of
  4.     Kconst:     spec := '#' + spec;
  5.     KRestrict:  spec := 'R' + spec;
  6.     Kvolatile:  spec := 'V' + spec;
  7.     end;                                              
  8.  

That is a chuck of the code.

Quote
uTablesC.pas(2299,5) Error: Internal error 200306031
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #3 on: October 12, 2026, 09:27:03 pm »
I could give you a published copy of what I get at the point where it does this, but the file size is ~439k

I think that is too large for here.
The only true wisdom is knowing you know nothing

Martin_fr

  • Administrator
  • Hero Member
  • *
  • Posts: 12820
  • Debugger - SynEdit - and more
    • wiki
Re: Confusion with SETS while Translating Delphi code
« Reply #4 on: October 12, 2026, 10:32:39 pm »
Quote
uTablesC.pas(2299,5) Error: Internal error 200306031

That is thrown in procedure tcgx86.check_register_size

So basically to reproduce those enum/sets probably need to have specific sizes => I guess the compiler picks a register to hold the data, and it turns out to be to small.

It may also be, that this then relies on -O2 or -Or ? But again, just guessing.

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #5 on: October 12, 2026, 11:14:20 pm »
Interesting.

I just did this.

Code: Pascal  [Select][+][-]
  1. {$MINEMUMSIZE 4}
  2.  

No longer a compiler fault. Hmm..

This is from the Help file, it states in Delphi mode it defaults to 1 byte.

Maybe that should be brought up to current times? :o

In any case, I think the compiler should be able to generate a better message ?  :D

Jamie
The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Re: Confusion with SETS while Translating Delphi code
« Reply #6 on: October 12, 2026, 11:22:26 pm »
Yes, FPC originally was much more "word" oriented, while Delphi had a lot of x86 subtypes (8,16-bit).

Also sets were originally allocated wordwise, while delphi did sets.

But please, share a compilable example that differs in Delphi and mode delphi ?

The IE of course is always reportable.

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #7 on: October 13, 2026, 12:02:35 am »
Hmm
The only true wisdom is knowing you know nothing

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #8 on: October 13, 2026, 12:03:38 am »
Thats the best I can do.

its only a partially converted old DElphi app. but I commented out the section I put in at the time so you can see what happens when you build it.

Jamie
The only true wisdom is knowing you know nothing

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Confusion with SETS while Translating Delphi code
« Reply #9 on: October 13, 2026, 07:30:42 am »
I am sorry, but you still did not get a correct answer....
Below is how to solve it with explanation why.
Code: Pascal  [Select][+][-]
  1. program sets;
  2. {$mode delphi}
  3. type
  4.  TMasterSet = (one,two,three,four,five,six, seven, eight, nine, ten);
  5. {$if 0}
  6.  // here's where it goes wrong:
  7.  // you define * distictive sets *
  8.  // what happens is that TMasterset.three has index two
  9.  // and TFirstset.three has index 0
  10.  // They are not compatible
  11.  TFirstSet = Set of three..seven
  12.  TSecondSet = Set of Eight..Ten;
  13. {$endif}
  14. // solve it like so:
  15. var
  16.   S1:set of TMasterset = [three,four,five,six,seven];
  17.   S2:set of TMasterset = [eight,nine,ten];
  18. begin
  19. end.
What you were doing has never, ever worked in Delphi, btw.

In trunk I have a - quite exotic - way to solve it in a more elegant way. If you use trunk and are curious I can put it here.
« Last Edit: October 13, 2026, 09:05:08 am by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #10 on: October 13, 2026, 03:26:58 pm »
Please look at the download.

The only true wisdom is knowing you know nothing

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Re: Confusion with SETS while Translating Delphi code
« Reply #11 on: October 13, 2026, 03:32:17 pm »
Please look at the download.

I tried to compile it, but it complains about missing config.pas

Hobbit00

  • Jr. Member
  • **
  • Posts: 70
Re: Confusion with SETS while Translating Delphi code
« Reply #12 on: October 13, 2026, 04:44:23 pm »
@jamie
If you like, I can explain how sets work in Delphi using the example from your first post (see the attached image).
When you create "set of" types using an enumeration like "one", "two", etc., the types aren't actually functionally different, because they all derive their values ​​from that same enumeration.
Therefore, all "Sets" derived from the same values type (like the proposed `TMasterSet` enumeration) are perfectly compatible with one another.

So, what changes when you declare them?

The only thing that changes is the definition of their storage capacity:
- `TFirstSet` can contain only the first eight values ​​enumerated in `TMasterSet`, due to the range specified during initialization (`Set of three..seven`).
- `TSecondSet` can contain all the values ​​enumerated in `TMasterSet`, based on its own enumeration definition.

In Delphi, when you declare a "set of", you are defining the storage size, not the actual limit for value assignment:
`TFirstSet = Set of three..seven;` specifies that the set must hold values ​​from the third to the seventh position of the enumeration. Consequently, a set with a capacity of 8 is created (set capacities are always multiples of eight, as 8 values ​​correspond to one byte of physical memory), specifically spanning from the first value of `TMasterSet` to the eight ("one" through "eight"); this perfectly accommodates the five values ​​specified in the initial definition.
`TSecondSet`, on the other hand, will accommodate all 10 values ​​(requiring 2 bytes of storage) because the initial values—"eight" and "nine"—extend beyond the single-byte limit.

The result is that `TSecondSet` can hold all values ​​enumerated in `TMasterSet'. `TFirstSet` can only hold the first eight: any subsequent values, even if assigned, will be discarded (like you see with "S1+S2" or "Include(S1, Nine);"

Hope this help you.

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: Confusion with SETS while Translating Delphi code
« Reply #13 on: October 13, 2026, 05:08:15 pm »
I looked at the downloads. All it needs in{$ifdef fpc}{$mode delphi}{$endif} in  EVERY unit.
BY HAND... not that stupid converter.

But your original question was really wrong: nothing to do with sets at all. Proper translation.
The way you handle sets is like it wrote... :D

THIS IS STUPID:
Code: Pascal  [Select][+][-]
  1. type
  2.  TMasterSet = (one,two,three,four,five,six, seven, eight, nine, ten);// index 0,1,2,3,4,5,6,7,8,9
  3.  TFirstSet = Set of three..seven;//  0.1.2.3.4.5.6
  4.  TSecondSet = Set of Eight..Ten; //Oh well... trying to help...

@Jamie
You are a wonder in coming up with really silly questions and answers, but I still will help to solve them.
« Last Edit: October 13, 2026, 05:18:00 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

jamie

  • Hero Member
  • *****
  • Posts: 8071
Re: Confusion with SETS while Translating Delphi code
« Reply #14 on: October 13, 2026, 11:11:21 pm »
I tried to kill or stop the drivel of what I posted because it was only an example to attempt a fault to show the internal error of the actual code I downloaded.

 for whatever reason the zip util in Lazarus or the published stuff does not compress it like it does coming from the net, so I attached the original.

 Run it through Delphi converter.

 you will needthe TListBox that has the virtual mode in it working with the events or, just mod the code a bit to use the standard TListBox.

 The whole point of this practice was to demonstrate what I found in the project, I just couldn't come up with a working example that failed.

attached is the original code (not mine)


The only true wisdom is knowing you know nothing

 

TinyPortal © 2005-2018