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

Ondrej Pokorny lazarus at kluug.net
Fri Apr 20 21:35:49 CEST 2018


On 13.04.2018 23:16, Sven Barth via fpc-devel wrote:
> Ondrej Pokorny <lazarus at kluug.net <mailto:lazarus at kluug.net>> schrieb 
> am Fr., 13. Apr. 2018, 21:16:
>
>     On 13.04.2018 14:08, Sven Barth via fpc-devel wrote:
>>     Ondrej Pokorny <lazarus at kluug.net <mailto:lazarus at kluug.net>>
>>     schrieb am Fr., 13. Apr. 2018, 12:52:
>>
>>         I introduced the AS operator for enumerations in
>>         https://bugs.freepascal.org/view.php?id=33603
>>
>>
>>     What about enums with holes?
>
>     No problem because Low() and High() work with these enums as well.
>     See the test case project I attached to the bug report - it has a
>     test with such an enum.
>
>
> I wasn't talking about the boundaries. I meant undefined values inside 
> the enum. If we want such a cast operator to work with such enums as 
> well it should check for invalid values inside the enum, too. 
> Otherwise the operator isn't worth it and should be forbidden for such 
> enums.

Sven (or anybody else), could you please comment on 
https://bugs.freepascal.org/view.php?id=33603 ? I feel I am getting crazy.

 From what I read, there seems to be a difference between FPC and Delphi 
understanding of "enums with holes":

1.) FPC understands the holes as undefined values - docs: 
https://www.freepascal.org/docs-html/ref/refsu4.html#x26-280003.1.1 :

Type
   EnumType = (one, two, three, forty := 40, thirty := 30);

It is necessary to keepfortyandthirtyin the correct order. When using 
enumeration types it is important to keep the following points in mind:

 1. ThePredandSuccfunctions cannot be used on this kind of enumeration
    types. Trying to do this anyhow will result in a compiler error.

2.) Delphi understands the holes as well-defined values that only lack a 
name - docs: 
http://docwiki.embarcadero.com/RADStudio/Tokyo/en/Simple_Types_(Delphi)#Enumerated_Types_with_Explicitly_Assigned_Ordinality

type Size = (Small = 5, Medium = 10, Large = Small + Medium);

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.

----

Also, the difference is demonstrated in the fact that Delphi and FPC 
delphi mode allow to define an array of Size. OBJFPC mode doesn't allow it.

----

Therefore I enabled the IS/AS operators on enums with holes only in 
Delphi mode and disabled them in all other modes.

----

Am I right in my understanding?

Ondrej

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180420/3413318d/attachment.html>


More information about the fpc-devel mailing list