[fpc-devel] [Suggestion] Enumeration range-check intrinsic

J. Gareth Moreton gareth at moreton-family.com
Wed Jul 3 18:26:18 CEST 2019


Sometimes I can be too passionate for my own good, and I want FPC to 
continue being a great tool, without problems that prove to be a 
game-breaker.

I would like to continue working on this particular issue and hopefully 
find an answer that everyone is happy with.  There are ways that an 
enumeration can take on an invalid value, but let's at least give the 
programmer an option of detecting the validity and not be forced to let 
their program enter an undefined state, because sometimes that is simply 
not an acceptable path.

And I have been saying this to myself, but it might be a bit too 
radical... "It's time FPC left the shadow of Delphi and stepped into the 
light!" - let it become its own thing (but with Delphi compatibility 
when it's needed with {$MODE DELPHI}).

Gareth aka. Kit


On 03/07/2019 15:32, Tomas Hajny wrote:
> On 2019-07-03 08:58, Ondrej Pokorny wrote:
>
>
> Hi,
>
>> If you want to extend -CR to perform "integer as enum" on
>> enum(integer), please do it consequently and perform "integer as
>> ShortInt" also on ShortInt(integer) - do range checking on explicit
>> typecasts for all simple types, not only enums. People will love you
>> for this:
>>
>> var
>>   S: ShortInt;
>>   I: Integer;
>> begin
>>   I := 130;
>>   S := ShortInt(I);
>> end;
>>
>> shall now raise a range check error with -CR. You can even optimize
>> the compiler to verify constants on compile-time:
>>
>> var
>>   S: ShortInt;
>> begin
>>   S := ShortInt($FF);
>> end;
>>
>> should not compile any more with -CR. Good idea.
>
> Well, I'm not sure to which extent the comment was meant seriously (no 
> smiles around), but I'm not convinced that explicit typecasts should 
> ever result in errors (even a warning would be very questionable, 
> IMHO), because explicit typecasts belong to the category "strong tools 
> in responsibility of the user" (similarly to working with pointers, 
> embedded assembler, etc.).
>
>
>  .
>  .
>> /If, at run-time, an index or enumeration type is specified that is
>> out of the declared range of the compiler, then a run-time error is
>> generated, and the program exits with exit code 201. This can happen
>> when doing a typecast (implicit or explicit) on an enumeration type or
>> subrange type./
>
> Obviously, this doesn't happen with explicit typecasts and I believe 
> that it's correct.
>
> Tomas
> _______________________________________________
> fpc-devel maillist  -  fpc-devel at lists.freepascal.org
> https://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel
>
>

---
This email has been checked for viruses by Avast antivirus software.
https://www.avast.com/antivirus



More information about the fpc-devel mailing list