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

Tomas Hajny XHajT03 at hajny.biz
Wed Jul 3 16:32:21 CEST 2019


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


More information about the fpc-devel mailing list