[fpc-devel] Dangerous optimization in CASE..OF

Ondrej Pokorny lazarus at kluug.net
Sun Jul 16 23:17:56 CEST 2017


On 16.07.2017 23:11, Florian Klämpfl wrote:
> Am 16.07.2017 um 22:39 schrieb Florian Klämpfl:
>> Am 16.07.2017 um 22:15 schrieb Martok:
>>> However:
>>> ---------------------------
>>> {$mode objfpc}
>>> type
>>>    TExplEnum = (a=1, b=3, c=5, d=7);
>>>    TSubEnum = a..d;
>>>    TEnArr = array[TSubEnum] of Byte;
>>>
>>> begin
>>>    WriteLn('SizeOf(TEnArr) = ', SizeOf(TEnArr));
>>>    WriteLn('Low(TEnArr) = ', Low(TEnArr), ', ', Ord(Low(TEnArr)));
>>>    WriteLn('High(TEnArr) = ', High(TEnArr), ', ', Ord(High(TEnArr)));
>>> end.
>>> ---------------------------
>>> SizeOf(TEnArr) = 7
>>> Low(TEnArr) = a, 1
>>> High(TEnArr) = d, 7
>>> ---------------------------
>>>
>>> That difference was unexpected. At least for me.
>> Indeed, this is a bug. IMO the declaration of TSubEnum should not be allowed.
> I made a patch and tested it, however, this causes regressions in our tests, so I am not sure if it
> should be changed.

It must not be changed. Delphi documentation is clear about this case: 
http://docwiki.embarcadero.com/RADStudio/XE5/en/Simple_Types#Enumerated_Types_with_Explicitly_Assigned_Ordinality

/type Size = (Small = 5, Medium = 10, Large = Small + Medium);//
//defines a type called Size whose possible values include Small, 
Medium, and Large, where Ord(Small) returns 5, Ord(Medium) returns 10, 
and Ord(Large) returns 15.//
//*An enumerated type is, in effect, a subrange whose lowest and highest 
values correspond to the lowest and highest ordinalities of the 
constants in the declaration. **In the previous example, the Size type 
has 11 possible values whose ordinalities range from 5 to 15. (Hence the 
type array[Size] of Char represents an array of 11 characters.) Only 
three of these values have names, but the others are accessible through 
typecasts and through routines such as Pred, Succ, Inc, and Dec.*/

Ondrej
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20170716/f98e8e7a/attachment.html>


More information about the fpc-devel mailing list