Jump to content
Sign in to follow this  
dummzeuch

[DCC Warning] W1013 Constant 0 converted to NIL

Recommended Posts

I just for the first time ever came across the warning "[DCC Warning]: W1013 Constant 0 converted to NIL".

I got it for the following piece of code:

var
  SomeList: TList;
//...
  SomeObj := SomeList.Extract(0); // <== here

WTF? I would have expected a compile error, not a warning because TList.Extract is declared as:

    function Extract(Item: Pointer): Pointer; inline;

without any overload.

I overlooked that warning a first, expecting an overload with an integer parameter (the index into the list like with Delete), which would make a lot of sense.

 

(This is with Delphi XE2.)

So apparently the compiler automatically converts the integer 0 to a nil pointer.

Who (tf) considered this a feature? This is Pascal, after all, not C and is supposed to be type safe!

Share this post


Link to post
Posted (edited)

The exact same is with Delphi 11. Tested with:

 

procedure TForm1.FormCreate(Sender: TObject);
var
  f: pointer;
begin
  f:=0;
end;
 

 

[dcc32 Warning] Unit1.pas(29): W1013 Constant 0 converted to NIL

 

 

Here is what helps says about this warning:

 

 

The Delphi compiler now allows the constant 0 to be used in pointer expressions in place of NIL. This change was made to allow older code to still compile with changes which were made in the low-level RTL.

 

program Produce;

  procedure p0(p : Pointer);
  begin
  end;

begin
  p0(0);
end.

In this example, the procedure p0 is declared to take a Pointer parameter yet the constant 0 is passed. The compiler will perform the necessary conversions internally, changing 0 into NIL, so that the code will function properly.

 

program Solve;

  procedure p0(p : Pointer);
  begin
  end;

begin
  p0(NIL);
end.

There are two approaches to solving this problem. In the case above the constant 0 has been replaced with NIL. Alternatively the procedure definition could be changed so that the parameter type is of Integer type.

Edited by Lajos Juhász

Share this post


Link to post

I noticed this a few days ago, when by mistake assigned 0 to array:

 

var arr: array of integer;
begin
  arr := 0;
end.

It gives the same W1013 warning.

 

 

Share this post


Link to post
Posted (edited)
29 minutes ago, dummzeuch said:

is supposed to be type safe!

Well, it is. Explicit.

Edited by Attila Kovacs

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
Sign in to follow this  

×