[fpc-devel] Wrong docs: not initialized global variables
Florian Klämpfl
florian at freepascal.org
Tue Jul 3 22:08:52 CEST 2018
Am 03.07.2018 um 21:30 schrieb Ondrej Pokorny:
> On 03.07.2018 20:54, Florian Klämpfl wrote:
>> The warning happens also for any other call which takes an uninitialized variable as var parameter (see e.g.
>> fillchar). So the warning increases only orthogonality of the language. It was an oversight that it not was thrown
>> before.
>
> This is correct in the oversimplified world only.
I changed it because some people thought they might make their point with it. Thank them.
>
> The not-oversimplified world is if the compiler was able to detect if the data in the VAR parameter is being read/used
> (and not initialized first) and emit a warning only for the methods that do read the data. This would both increase
> orthogonality of the warning (warning is not really the language, isn't it?) and it would be logical. E.g.:
>
> program Project1;
> type
> TMyArray = array[0..10] of Integer;
> procedure Init2(var A: TMyArray);
> begin
> A[2] := 1;
> end;
> var
> A: TMyArray;
> begin
> Init2(A);
> end.
>
> should not emit the warning because A is not being read in Init2.
(please note it throws only a hint in this case as A *is* initialized as it is a global variable).
FPC has no global data flow analysis (and I do not plan to do so yet), so this is warning is currently far out of reach.
>
> IMPORTANT: the compiler knows that SetLength and FillChar don't read/use the data so no warning is appropriate there.
SetLength does somehow :), as the examples showed. FillChar is a different thing, it has the problem due of its TP
inheritance (and the compiler does not know that fillchar is special).
> Neither in FillChar, nor in SetLength.
>
> ---
>
> What you achieved with SetLength and FillChar emitting a warning is that you made the compiler more stupid instead of
> making it more clever.
The compiler has not to be clever, it has to follow rules. And the rules which need to be followed is the meaning of the
var parameter. It means: data in/data out.
More information about the fpc-devel
mailing list