[fpc-devel] Wrong docs: not initialized global variables

Ondrej Pokorny lazarus at kluug.net
Wed Apr 4 19:32:09 CEST 2018


On 04.04.2018 18:53, Jonas Maebe wrote:
> On 04/04/18 18:44, Ondrej Pokorny wrote:
>> I want to stress that the compiler emits a warning on code that does 
>> not have (and also cannot have) an error
>
> An error is wrong code behaviour. If you do not initialise a variable 
> with the correct value, then you can have an error.

No, no, no and again no. Do you try to convince me that I cannot rely on 
well documented compiler behavior? Even Sven stated earlier in this 
thread that the FPC docs are correct in case of managed variables: 
"Managed types are an exception to this rule: Managed types are always 
initialized: in general this means setting the reference count to zero, 
or setting the pointer value of the type to Nil." and "It should be 
stressed that initialized variables are initialized when they come into 
scope" https://www.freepascal.org/docs-html/ref/refse24.html

Again, the code
procedure Test;
var S: string; I: Integer;
begin
   for I := 0 to 1 do
     S := S + '1';
   Writeln(S);
end;

does not have an error, cannot have an error and the behavior is 
guaranteed and well documented => no warning should be here.

+ Do you try to convince me that the code below can have an error and 
should emit a warning as well because it does not initialize the FS and 
FI fields?

program Project1;
type
   TMyObject = class
   private
     FS: string;
     FI: Integer;
   public
     property S: string read FS write FS;
     property I: Integer read FI write FI;
   end;

begin
   with TMyObject.Create do
   begin
     Writeln(S);
     Writeln(I);
     Free;
   end;
end.

It's the same case - object fields are documented to be initialized. You 
mean I should not rely on this feature and it is an error that I did not 
initialize them with the correct values?

> This correct value can be different from "empty string" or "nil". 

If I want to have a different value from "empty string" or "nil" I know 
I have to initialize it by myself. I don't need a warning for it.

> For the same reason, we also warn about uninitialised global variables 
> (if this can be detected without interprocedural analysis, i.e., if 
> they are only used in the main program code; but that is merely due to 
> a limitation of the analysis).

Uninitialised global variables are the same case: the compiler should 
not emit a warning here because again, it is well documented that they 
are implicitely initialized (at least in the Object Pascal Language 
Guide from Borland from 2002 it is documented).

Ondrej
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://lists.freepascal.org/pipermail/fpc-devel/attachments/20180404/2a2681d5/attachment.html>


More information about the fpc-devel mailing list