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

Jonas Maebe jonas at freepascal.org
Sat Jul 15 12:40:21 CEST 2017


On 14/07/17 02:40, Martok wrote:
> There is a fundamental difference in the type system between a somewhat sensible
> (if unexpected) assumption in FPC and a more practical documented definition in
> every other Pascal compiler. An assumption that even FPC follows only in this
> one single spot.

Several times in this thread I've already given examples in this thread 
that this is not true. In several places FPC generates code based on the 
assumption that data locations of a particular type (including enums) 
will only contain any values other than the ones that are valid for 
them. For enums manifests itself a.o. in the absence of generated range 
checks in various places (array indexing, assignments), and in 
comparisons that get optimised away at compile time because they will 
always have the same result at run time according to the type information.

If a data location has a particular type but does not contain a value 
that is valid for that type (e.g. because it has not been initialised 
with one, or because an invalid value was put there via an explicit type 
cast or assembler code), then the result is undefined. Note that 
"undefined" does not mean "the code will crash". It is one possibility, 
but in the general sense it means "anything could happen".


Jonas



More information about the fpc-devel mailing list