[fpc-pascal] New Warnings with fpc >= 3.2.0

Sven Barth pascaldragon at googlemail.com
Wed Mar 11 14:22:27 CET 2020


fredvs via fpc-pascal <fpc-pascal at lists.freepascal.org> schrieb am Mi., 11.
März 2020, 11:16:

> Hello.
>
> Compiling that code with fpc 3.2.0 gives the warning that follows:
>
> --->  if (kind <> tabulatorkindty(-1)) --> gives now the warning:
>
> "msedrawtext.pas(1115,48) Warning: range check error while evaluating
> constants (-1 must be between 0 and 3)"
>
> tabulatorkindty is declared as:
>
> ---> tabulatorkindty = (tak_left,tak_right,tak_centered,tak_decimal);
>
> So, if I understand ok, with new feature of fpc, "-1" is no more permitted
>

By definition an enum value can only contain the values that are listed in
its declaration, anything else is an invalid value. If it contains another
value then you have already made a mistake when converting some other type
to the enum.

To highlight the problem: if the variable "kind" would contain -2 instead
of -1 then your check would still fail.

To safely convert a non-enum value (e.g. an Integer) to an enum you should
use something like this:

if (Ord(Low(TMyEnum)) <= MyInt) and (MyInt <= Ord(High(TMyEnum))) then
  MyEnum := TMyEnum(MyInt)
else
  raise Exception.Create('Curse you!');

Regards,
Sven

>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-pascal/attachments/20200311/bafc496f/attachment.html>


More information about the fpc-pascal mailing list